-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[block] add block deserializer (#3186)
- Loading branch information
Showing
8 changed files
with
144 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) 2022 IoTeX Foundation | ||
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no | ||
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent | ||
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache | ||
// License 2.0 that can be found in the LICENSE file. | ||
|
||
package block | ||
|
||
import ( | ||
"github.com/iotexproject/iotex-proto/golang/iotextypes" | ||
"github.com/pkg/errors" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
// Deserializer de-serializes a block | ||
type Deserializer struct { | ||
} | ||
|
||
// 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 { | ||
return nil, errors.Wrap(err, "failed to deserialize block header") | ||
} | ||
if err := b.Body.LoadProto(pbBlock.GetBody()); err != nil { | ||
return nil, errors.Wrap(err, "failed to deserialize block body") | ||
} | ||
if err := b.ConvertFromBlockFooterPb(pbBlock.GetFooter()); err != nil { | ||
return nil, errors.Wrap(err, "failed to deserialize block footer") | ||
} | ||
return &b, nil | ||
} | ||
|
||
// DeserializeBlock de-serializes a block | ||
func (bd *Deserializer) DeserializeBlock(buf []byte) (*Block, error) { | ||
pbBlock := iotextypes.Block{} | ||
if err := proto.Unmarshal(buf, &pbBlock); err != nil { | ||
return nil, errors.Wrap(err, "failed to unmarshal block") | ||
} | ||
b, err := bd.FromBlockProto(&pbBlock) | ||
if err != nil { | ||
return nil, err | ||
} | ||
b.Receipts = nil | ||
if err = b.VerifyTxRoot(); err != nil { | ||
return nil, err | ||
} | ||
return b, nil | ||
} | ||
|
||
// FromBodyProto converts protobuf to body | ||
func (bd *Deserializer) FromBodyProto(pbBody *iotextypes.BlockBody) (*Body, error) { | ||
b := Body{} | ||
if err := b.LoadProto(pbBody); err != nil { | ||
return nil, errors.Wrap(err, "failed to deserialize block body") | ||
} | ||
return &b, nil | ||
} | ||
|
||
// DeserializeBody de-serializes a block body | ||
func (bd *Deserializer) DeserializeBody(buf []byte) (*Body, error) { | ||
pb := iotextypes.BlockBody{} | ||
if err := proto.Unmarshal(buf, &pb); err != nil { | ||
return nil, errors.Wrap(err, "failed to unmarshal block body") | ||
} | ||
return bd.FromBodyProto(&pb) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) 2022 IoTeX Foundation | ||
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no | ||
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent | ||
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache | ||
// License 2.0 that can be found in the LICENSE file. | ||
|
||
package block | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/golang/protobuf/proto" | ||
"github.com/iotexproject/go-pkgs/hash" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBlockDeserializer(t *testing.T) { | ||
r := require.New(t) | ||
bd := Deserializer{} | ||
blk, err := bd.FromBlockProto(&pbBlock) | ||
r.NoError(err) | ||
body, err := bd.FromBodyProto(pbBlock.Body) | ||
r.NoError(err) | ||
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) | ||
r.NoError(err) | ||
|
||
newblk, err := (&Deserializer{}).DeserializeBlock(raw) | ||
r.NoError(err) | ||
r.Equal(blk, newblk) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters