Skip to content

Commit

Permalink
[block] deprecate some code in block.go and body.go
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Jun 14, 2022
1 parent 4be5188 commit e2b4bbc
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 168 deletions.
29 changes: 0 additions & 29 deletions blockchain/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,35 +52,6 @@ func (b *Block) Serialize() ([]byte, error) {
return proto.Marshal(b.ConvertToBlockPb())
}

// ConvertFromBlockPb converts Block to Block
func (b *Block) ConvertFromBlockPb(pbBlock *iotextypes.Block, id uint32) error {
b.Header = Header{}
if err := b.Header.LoadFromBlockHeaderProto(pbBlock.GetHeader()); err != nil {
return err
}
b.Body = Body{}
if err := b.Body.LoadProto(pbBlock.GetBody(), id); err != nil {
return err
}

return b.ConvertFromBlockFooterPb(pbBlock.GetFooter())
}

// Deserialize parses the byte stream into a Block
func (b *Block) Deserialize(buf []byte, id uint32) error {
pbBlock := iotextypes.Block{}
if err := proto.Unmarshal(buf, &pbBlock); err != nil {
return err
}
if err := b.ConvertFromBlockPb(&pbBlock, id); err != nil {
return err
}
b.Receipts = nil

// verify merkle root can match after deserialize
return b.VerifyTxRoot()
}

// VerifyTxRoot verifies the transaction root hash
func (b *Block) VerifyTxRoot() error {
root, err := b.CalculateTxRoot()
Expand Down
28 changes: 19 additions & 9 deletions blockchain/block/block_deserializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"

"github.com/iotexproject/iotex-core/action"
)

// Deserializer de-serializes a block
Expand All @@ -32,11 +34,14 @@ func (bd *Deserializer) SetEvmNetworkID(id uint32) *Deserializer {

// FromBlockProto converts protobuf to block
func (bd *Deserializer) FromBlockProto(pbBlock *iotextypes.Block) (*Block, error) {
b := Block{}
if err := b.Header.LoadFromBlockHeaderProto(pbBlock.GetHeader()); err != nil {
var (
b = Block{}
err error
)
if err = b.Header.LoadFromBlockHeaderProto(pbBlock.GetHeader()); err != nil {
return nil, errors.Wrap(err, "failed to deserialize block header")
}
if err := b.Body.LoadProto(pbBlock.GetBody(), bd.evmNetworkID); err != nil {
if b.Body, err = bd.fromBodyProto(pbBlock.GetBody()); err != nil {
return nil, errors.Wrap(err, "failed to deserialize block body")
}
if err := b.ConvertFromBlockFooterPb(pbBlock.GetFooter()); err != nil {
Expand All @@ -62,13 +67,17 @@ func (bd *Deserializer) DeserializeBlock(buf []byte) (*Block, error) {
return b, nil
}

// FromBodyProto converts protobuf to body
func (bd *Deserializer) FromBodyProto(pbBody *iotextypes.BlockBody) (*Body, error) {
// fromBodyProto converts protobuf to body
func (bd *Deserializer) fromBodyProto(pbBody *iotextypes.BlockBody) (Body, error) {
b := Body{}
if err := b.LoadProto(pbBody, bd.evmNetworkID); err != nil {
return nil, errors.Wrap(err, "failed to deserialize block body")
for _, actPb := range pbBody.Actions {
act, err := (&action.Deserializer{}).SetEvmNetworkID(bd.evmNetworkID).ActionToSealedEnvelope(actPb)
if err != nil {
return b, errors.Wrap(err, "failed to deserialize block body")
}
b.Actions = append(b.Actions, act)
}
return &b, nil
return b, nil
}

// DeserializeBody de-serializes a block body
Expand All @@ -77,5 +86,6 @@ func (bd *Deserializer) DeserializeBody(buf []byte) (*Body, error) {
if err := proto.Unmarshal(buf, &pb); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal block body")
}
return bd.FromBodyProto(&pb)
b, err := bd.fromBodyProto(&pb)
return &b, err
}
8 changes: 3 additions & 5 deletions blockchain/block/block_deserializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package block
import (
"testing"

"github.com/golang/protobuf/proto"
"github.com/iotexproject/go-pkgs/hash"
"github.com/stretchr/testify/require"
)
Expand All @@ -19,16 +18,15 @@ func TestBlockDeserializer(t *testing.T) {
bd := Deserializer{}
blk, err := bd.FromBlockProto(&_pbBlock)
r.NoError(err)
body, err := bd.FromBodyProto(_pbBlock.Body)
body, err := bd.fromBodyProto(_pbBlock.Body)
r.NoError(err)
r.Equal(body, &blk.Body)
r.Equal(body, blk.Body)

txHash, err := blk.CalculateTxRoot()
r.NoError(err)
blk.Header.txRoot = txHash
blk.Header.receiptRoot = hash.Hash256b(([]byte)("test"))
pb := blk.ConvertToBlockPb()
raw, err := proto.Marshal(pb)
raw, err := blk.Serialize()
r.NoError(err)

newblk, err := (&Deserializer{}).DeserializeBlock(raw)
Expand Down
19 changes: 0 additions & 19 deletions blockchain/block/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,6 @@ var (
}
)

func TestConvertFromBlockPb(t *testing.T) {
blk := Block{}
require.NoError(t, blk.ConvertFromBlockPb(&_pbBlock, 0))

txHash, err := blk.CalculateTxRoot()
require.NoError(t, err)

blk.Header.txRoot = txHash
blk.Header.receiptRoot = hash.Hash256b(([]byte)("test"))

raw, err := blk.Serialize()
require.NoError(t, err)

var newblk Block
err = newblk.Deserialize(raw, 0)
require.NoError(t, err)
require.Equal(t, blk, newblk)
}

func TestBlockCompressionSize(t *testing.T) {
for _, n := range []int{1, 10, 100, 1000, 10000} {
blk := makeBlock(t, n)
Expand Down
24 changes: 0 additions & 24 deletions blockchain/block/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,6 @@ func (b *Body) Serialize() ([]byte, error) {
return proto.Marshal(b.Proto())
}

// LoadProto loads body from proto
func (b *Body) LoadProto(pbBlock *iotextypes.BlockBody, id uint32) error {
b.Actions = []action.SealedEnvelope{}
for _, actPb := range pbBlock.Actions {
act, err := (&action.Deserializer{}).SetEvmNetworkID(id).ActionToSealedEnvelope(actPb)
if err != nil {
return err
}
b.Actions = append(b.Actions, act)
}

return nil
}

// Deserialize parses the byte stream into a Block
func (b *Body) Deserialize(buf []byte, id uint32) error {
pb := iotextypes.BlockBody{}
if err := proto.Unmarshal(buf, &pb); err != nil {
return err
}

return b.LoadProto(&pb, id)
}

// CalculateTxRoot returns the Merkle root of all txs and actions in this block.
func (b *Body) CalculateTxRoot() (hash.Hash256, error) {
return calculateTxRoot(b.Actions)
Expand Down
24 changes: 15 additions & 9 deletions blockchain/block/body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,37 @@ func TestSerDer(t *testing.T) {
body := Body{}
ser, err := body.Serialize()
require.NoError(err)
require.NoError(body.Deserialize(ser, 0))
require.Equal(0, len(body.Actions))
body2, err := (&Deserializer{}).DeserializeBody(ser)
require.NoError(err)
require.Equal(0, len(body2.Actions))

body, err = makeBody()
require.NoError(err)
ser, err = body.Serialize()
require.NoError(err)
require.NoError(body.Deserialize(ser, 0))
require.Equal(1, len(body.Actions))
body2, err = (&Deserializer{}).DeserializeBody(ser)
require.NoError(err)
require.Equal(1, len(body2.Actions))
require.Equal(&body, body2)
}

func TestLoadProto(t *testing.T) {
require := require.New(t)
body := Body{}
blockBody := body.Proto()
require.NotNil(blockBody)
require.NoError(body.LoadProto(blockBody, 0))
require.Equal(0, len(body.Actions))
body2, err := (&Deserializer{}).fromBodyProto(blockBody)
require.NoError(err)
require.Equal(0, len(body2.Actions))

body, err := makeBody()
body, err = makeBody()
require.NoError(err)
blockBody = body.Proto()
require.NotNil(blockBody)
require.NoError(body.LoadProto(blockBody, 0))
require.Equal(1, len(body.Actions))
body2, err = (&Deserializer{}).fromBodyProto(blockBody)
require.NoError(err)
require.Equal(1, len(body2.Actions))
require.Equal(body, body2)
}

func TestCalculateTxRoot(t *testing.T) {
Expand Down
73 changes: 0 additions & 73 deletions consensus/scheme/rolldpos/subchain_test.go

This file was deleted.

0 comments on commit e2b4bbc

Please sign in to comment.