github.com/iotexproject/iotex-core@v1.14.1-rc1/blockchain/block/block_deserializer_test.go (about)

     1  // Copyright (c) 2022 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package block
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/iotexproject/go-pkgs/hash"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestBlockDeserializer(t *testing.T) {
    16  	r := require.New(t)
    17  	bd := Deserializer{}
    18  	blk, err := bd.FromBlockProto(&_pbBlock)
    19  	r.NoError(err)
    20  	body, err := bd.fromBodyProto(_pbBlock.Body)
    21  	r.NoError(err)
    22  	r.Equal(body, blk.Body)
    23  
    24  	txHash, err := blk.CalculateTxRoot()
    25  	r.NoError(err)
    26  	blk.Header.txRoot = txHash
    27  	blk.Header.receiptRoot = hash.Hash256b(([]byte)("test"))
    28  	raw, err := blk.Serialize()
    29  	r.NoError(err)
    30  
    31  	newblk, err := (&Deserializer{}).DeserializeBlock(raw)
    32  	r.NoError(err)
    33  	r.Equal(blk, newblk)
    34  	r.Equal(_pbBlock.Body.Actions[0].Core.ChainID, blk.Actions[0].ChainID())
    35  	r.Equal(_pbBlock.Body.Actions[1].Core.ChainID, blk.Actions[1].ChainID())
    36  }
    37  
    38  func TestBlockStoreDeserializer(t *testing.T) {
    39  	require := require.New(t)
    40  	store, err := makeStore()
    41  	require.NoError(err)
    42  
    43  	storeProto := store.ToProto()
    44  
    45  	require.NotNil(storeProto)
    46  
    47  	bd := Deserializer{}
    48  	store1, err := bd.BlockFromBlockStoreProto(storeProto)
    49  	require.NoError(err)
    50  
    51  	require.Equal(store1.height, store.Block.height)
    52  	require.Equal(store1.Header.prevBlockHash, store.Block.Header.prevBlockHash)
    53  	require.Equal(store1.Header.blockSig, store.Block.Header.blockSig)
    54  }