Skip to content

Commit

Permalink
fix Vbft Block serdes (#989)
Browse files Browse the repository at this point in the history
* fix Vbft Block serdes

* enforce merkleRoot data in Block msg

* fix error handlings
  • Loading branch information
Honglei-Cong authored and laizy committed Jun 28, 2019
1 parent cada27a commit 6475ec6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
28 changes: 28 additions & 0 deletions consensus/vbft/msg_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,31 @@ func TestBlockFetchRespMsgDeserialize(t *testing.T) {
}
t.Logf("BlockFetchRespMsg Serialize succ: %v\n", respmsg.BlockNumber)
}

func TestBlockSerialization(t *testing.T) {
blk, err := constructBlock()
if err != nil {
t.Errorf("constructBlock failed: %v", err)
return
}

data, err := blk.Serialize()
if err != nil {
t.Fatalf("serialize blk: %s", err)
}

blk2 := &Block{}
if err := blk2.Deserialize(data); err != nil {
t.Fatalf("deserialize blk: %s", err)
}

blk.EmptyBlock = nil
data2, err := blk.Serialize()
if err != nil {
t.Fatalf("serialize blk2: %s", err)
}
blk3 := &Block{}
if err := blk3.Deserialize(data2); err != nil {
t.Fatalf("deserialize blk2: %s", err)
}
}
33 changes: 20 additions & 13 deletions consensus/vbft/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"io"

"github.com/ontio/ontology/common"
"github.com/ontio/ontology/common/log"
"github.com/ontio/ontology/consensus/vbft/config"
"github.com/ontio/ontology/core/types"
)
Expand Down Expand Up @@ -78,6 +77,7 @@ func (blk *Block) Serialize() ([]byte, error) {
payload := common.NewZeroCopySink(nil)
payload.WriteVarBytes(sink.Bytes())

payload.WriteBool(blk.EmptyBlock != nil)
if blk.EmptyBlock != nil {
sink2 := common.NewZeroCopySink(nil)
blk.EmptyBlock.Serialization(sink2)
Expand Down Expand Up @@ -109,22 +109,29 @@ func (blk *Block) Deserialize(data []byte) error {
}

var emptyBlock *types.Block
if source.Len() > 0 {
hasEmptyBlock, irr, eof := source.NextBool()
if irr {
return fmt.Errorf("read empty-block-bool: %s", common.ErrIrregularData)
}
if eof {
return fmt.Errorf("read empty-block-bool: %s", io.ErrUnexpectedEOF)
}
if hasEmptyBlock {
buf2, _, irregular, eof := source.NextVarBytes()
if irregular == false && eof == false {
block2, err := types.BlockFromRawBytes(buf2)
if err == nil {
emptyBlock = block2
}
if irregular || eof {
return fmt.Errorf("read empty block failed: %v, %v", irregular, eof)
}
block2, err := types.BlockFromRawBytes(buf2)
if err != nil {
return fmt.Errorf("deserialize empty blk failed: %s", err)
}
emptyBlock = block2
}

var merkleRoot common.Uint256
if source.Len() > 0 {
merkleRoot, eof = source.NextHash()
if eof {
log.Errorf("Block Deserialize merkleRoot")
return io.ErrUnexpectedEOF
}
merkleRoot, eof = source.NextHash()
if eof {
return fmt.Errorf("block deserialize merkleRoot: %s", io.ErrUnexpectedEOF)
}
blk.Block = block
blk.EmptyBlock = emptyBlock
Expand Down

0 comments on commit 6475ec6

Please sign in to comment.