Commit f09b5da2 authored by manish's avatar manish
Browse files

This commits replace peer.Block2 by common.Block

https://jira.hyperledger.org/browse/FAB-1201



Change-Id: I34f7f56887c25dae712188a50e8bdcd13ae619d2
Signed-off-by: default avatarmanish <manish.sethi@gmail.com>
parent 2570f8f7
......@@ -40,6 +40,7 @@ import (
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/core/crypto/primitives"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/protos/common"
"github.com/spf13/viper"
"golang.org/x/net/context"
"google.golang.org/grpc"
......@@ -182,7 +183,8 @@ func endTxSimulation(txsim ledger.TxSimulator, payload []byte, commit bool, prop
}
//create the block with 1 transaction
block := &pb.Block2{Transactions: [][]byte{envBytes}}
block := common.NewBlock(1, []byte{})
block.Data.Data = [][]byte{envBytes}
if _, _, err = lgr.RemoveInvalidTransactionsAndPrepare(block); err != nil {
return err
}
......
......@@ -369,31 +369,31 @@ func init() {
func init() { proto.RegisterFile("table.proto", fileDescriptor0) }
var fileDescriptor0 = []byte{
// 413 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x6c, 0x52, 0xd1, 0x6a, 0xdb, 0x30,
0x14, 0x8d, 0x62, 0xd9, 0x69, 0x6f, 0xbb, 0xa1, 0x89, 0x11, 0x04, 0x7b, 0x31, 0x7e, 0x18, 0x7e,
0x99, 0xcd, 0x12, 0xe3, 0x0f, 0xf0, 0x3a, 0x96, 0xc2, 0x68, 0x87, 0xea, 0x32, 0xb6, 0x37, 0xdb,
0x55, 0x13, 0x31, 0xc7, 0x0a, 0xb6, 0xb2, 0xe1, 0x8f, 0x1b, 0xec, 0xd3, 0x86, 0xa4, 0x04, 0x4a,
0x9b, 0xb7, 0x7b, 0xee, 0x3d, 0xe7, 0xe8, 0xde, 0x83, 0xe0, 0x42, 0x57, 0x75, 0x2b, 0x92, 0x5d,
0xaf, 0xb4, 0xa2, 0x78, 0xd8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0xde, 0x09, 0x70, 0x88,
0xe2, 0x57, 0xc6, 0xc9, 0xe1, 0xe3, 0x24, 0xcf, 0x98, 0x1f, 0xa2, 0x18, 0x1f, 0x27, 0x79, 0x66,
0xbc, 0xea, 0x51, 0x8b, 0x81, 0x05, 0x21, 0x8a, 0x2f, 0x8d, 0x97, 0x85, 0xf4, 0x2d, 0xe0, 0x5a,
0xa9, 0x96, 0xcd, 0x4c, 0xda, 0xab, 0x09, 0xb7, 0xa8, 0x98, 0x81, 0xff, 0xbb, 0x6a, 0xf7, 0x22,
0xfa, 0x00, 0x1e, 0x57, 0x7f, 0xe8, 0x7b, 0x98, 0xb9, 0xdb, 0x06, 0x86, 0x6c, 0x04, 0x97, 0x4f,
0x23, 0xe0, 0xc7, 0x61, 0xf1, 0x1d, 0xe6, 0xaa, 0x5f, 0x27, 0x9b, 0x71, 0x27, 0xfa, 0x56, 0x3c,
0xac, 0x45, 0xef, 0xfe, 0xcc, 0x50, 0x80, 0x4d, 0xfa, 0x9b, 0x01, 0x3f, 0x3f, 0xae, 0xa5, 0xde,
0xec, 0xeb, 0xa4, 0x51, 0xdb, 0xf4, 0x09, 0x35, 0x7d, 0xac, 0xea, 0x5e, 0x36, 0x69, 0xa3, 0x7a,
0x91, 0x36, 0x9b, 0x4a, 0x76, 0x8d, 0x7a, 0x10, 0xa9, 0x79, 0xa8, 0x0e, 0xac, 0xcd, 0xf2, 0x7f,
0x00, 0x00, 0x00, 0xff, 0xff, 0x35, 0x1b, 0xaf, 0x40, 0x89, 0x02, 0x00, 0x00,
// 404 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x6c, 0x52, 0xdd, 0x8a, 0x9b, 0x40,
0x14, 0x5e, 0xd7, 0xd1, 0xec, 0x9e, 0xdd, 0x16, 0x3b, 0x94, 0x20, 0xf4, 0xa6, 0x78, 0x51, 0xf6,
0xa6, 0x4a, 0x4d, 0xc8, 0x03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, 0x00,
}
......@@ -16,9 +16,7 @@ limitations under the License.
package committer
import (
pb "github.com/hyperledger/fabric/protos/peer"
)
import "github.com/hyperledger/fabric/protos/common"
// Committer is the interface supported by committers
// The only committer is noopssinglechain committer.
......@@ -30,13 +28,13 @@ import (
type Committer interface {
// Commit block to the ledger
CommitBlock(block *pb.Block2) error
CommitBlock(block *common.Block) error
// Get recent block sequence number
LedgerHeight() (uint64, error)
// Gets blocks with sequence numbers provided in the slice
GetBlocks(blockSeqs []uint64) []*pb.Block2
GetBlocks(blockSeqs []uint64) []*common.Block
// Closes committing service
Close()
......
......@@ -18,6 +18,7 @@ package committer
import (
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/op/go-logging"
)
......@@ -46,7 +47,7 @@ func NewLedgerCommitter(ledger ledger.ValidatedLedger) *LedgerCommitter {
}
// CommitBlock commits block to into the ledger
func (lc *LedgerCommitter) CommitBlock(block *pb.Block2) error {
func (lc *LedgerCommitter) CommitBlock(block *common.Block) error {
if _, _, err := lc.ledger.RemoveInvalidTransactionsAndPrepare(block); err != nil {
return err
}
......@@ -69,8 +70,8 @@ func (lc *LedgerCommitter) LedgerHeight() (uint64, error) {
}
// GetBlocks used to retrieve blocks with sequence numbers provided in the slice
func (lc *LedgerCommitter) GetBlocks(blockSeqs []uint64) []*pb.Block2 {
var blocks []*pb.Block2
func (lc *LedgerCommitter) GetBlocks(blockSeqs []uint64) []*common.Block {
var blocks []*common.Block
for _, seqNum := range blockSeqs {
if blck, err := lc.ledger.GetBlockByNumber(seqNum); err != nil {
......
......@@ -51,7 +51,7 @@ func TestKVLedgerBlockStorage(t *testing.T) {
simulator.Done()
simRes, _ := simulator.GetTxSimulationResults()
block1 := testutil.ConstructBlockForSimulationResults(t, [][]byte{simRes}, true)
block1 := testutil.ConstructBlock(t, [][]byte{simRes}, true)
err = committer.CommitBlock(block1)
assert.NoError(t, err)
......@@ -65,8 +65,7 @@ func TestKVLedgerBlockStorage(t *testing.T) {
assert.NoError(t, err)
bcInfo, _ = ledger.GetBlockchainInfo()
serBlock1, _ := pb.ConstructSerBlock2(block1)
block1Hash := serBlock1.ComputeHash()
block1Hash := block1.Header.Hash()
testutil.AssertEquals(t, bcInfo, &pb.BlockchainInfo{
Height: 1, CurrentBlockHash: block1Hash, PreviousBlockHash: []byte{}})
}
......@@ -40,7 +40,6 @@ import (
"github.com/hyperledger/fabric/gossip/integration"
gossip_proto "github.com/hyperledger/fabric/gossip/proto"
"github.com/hyperledger/fabric/gossip/state"
pb "github.com/hyperledger/fabric/protos/peer"
)
var logger *logging.Logger // package-level logger
......@@ -243,8 +242,11 @@ func (d *DeliverService) readUntilClose() {
}
logger.Warning("Got error ", t)
case *orderer.DeliverResponse_Block:
block := &pb.Block2{}
seqNum := t.Block.Header.Number
block := &common.Block{}
block.Header = t.Block.Header
block.Metadata = t.Block.Metadata
block.Data = &common.BlockData{}
for _, d := range t.Block.Data.Data {
if d != nil {
if env, err := putils.GetEnvelopeFromBlock(d); err != nil {
......@@ -264,7 +266,7 @@ func (d *DeliverService) readUntilClose() {
} else {
// TODO: call VSCC now
if t, err := proto.Marshal(env); err == nil {
block.Transactions = append(block.Transactions, t)
block.Data.Data = append(block.Data.Data, t)
} else {
fmt.Printf("Cannot marshal transactoins %s\n", err)
}
......@@ -321,8 +323,8 @@ func createGossipMsg(payload *gossip_proto.Payload) *gossip_proto.GossipMessage
return gossipMsg
}
func createPayload(seqNum uint64, block2 *pb.Block2) *gossip_proto.Payload {
marshaledBlock, _ := proto.Marshal(block2)
func createPayload(seqNum uint64, block *common.Block) *gossip_proto.Payload {
marshaledBlock, _ := proto.Marshal(block)
return &gossip_proto.Payload{
Data: marshaledBlock,
SeqNum: seqNum,
......
......@@ -68,18 +68,18 @@ func init() {
func init() { proto1.RegisterFile("attributes.proto", fileDescriptor0) }
var fileDescriptor0 = []byte{
// 204 bytes of a gzipped FileDescriptorProto
// 202 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x12, 0x48, 0x2c, 0x29, 0x29,
0xca, 0x4c, 0x2a, 0x2d, 0x49, 0x2d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x03, 0x53,
0xc5, 0x4a, 0xc9, 0x5c, 0xe2, 0x8e, 0x70, 0x39, 0xdf, 0xd4, 0x92, 0xc4, 0x94, 0xc4, 0x92, 0x44,
0xd7, 0xbc, 0x92, 0xa2, 0x4a, 0x21, 0x15, 0x2e, 0x5e, 0xb8, 0x94, 0x5f, 0x62, 0x6e, 0xaa, 0x04,
0xa3, 0x02, 0xa3, 0x06, 0x67, 0x10, 0xaa, 0xa0, 0x90, 0x12, 0x17, 0x0f, 0x5c, 0xc0, 0x3b, 0xb5,
0x52, 0x82, 0x49, 0x81, 0x51, 0x83, 0x27, 0x08, 0x45, 0x4c, 0x29, 0x9b, 0x4b, 0x08, 0xd3, 0x12,
0x21, 0x29, 0x2e, 0x0e, 0x18, 0x1b, 0x6c, 0x34, 0x4f, 0x10, 0x9c, 0x2f, 0x64, 0xc9, 0xc5, 0x0e,
0x72, 0x44, 0x66, 0x6a, 0xb1, 0x04, 0x93, 0x02, 0xb3, 0x06, 0xb7, 0x91, 0x3c, 0xc4, 0xdd, 0xc5,
0x7a, 0x38, 0x5c, 0x1b, 0x04, 0x53, 0xef, 0x64, 0x13, 0x65, 0x95, 0x9e, 0x59, 0x92, 0x51, 0x9a,
0xa4, 0x97, 0x9c, 0x9f, 0xab, 0x9f, 0x51, 0x59, 0x90, 0x5a, 0x94, 0x93, 0x9a, 0x92, 0x9e, 0x5a,
0xa4, 0x9f, 0x96, 0x98, 0x54, 0x94, 0x99, 0xac, 0x9f, 0x9c, 0x5f, 0x94, 0xaa, 0x9f, 0x5c, 0x54,
0x59, 0x50, 0x92, 0xaf, 0x8f, 0x08, 0x17, 0x7d, 0xb0, 0xf9, 0x49, 0x90, 0x70, 0x31, 0x06, 0x04,
0x00, 0x00, 0xff, 0xff, 0xc7, 0x30, 0xd3, 0xb6, 0x32, 0x01, 0x00, 0x00,
0x52, 0x82, 0x09, 0xa8, 0x88, 0x27, 0x08, 0x45, 0x4c, 0x29, 0x9b, 0x4b, 0x08, 0xd3, 0x12, 0x21,
0x29, 0x2e, 0x0e, 0x18, 0x1b, 0x6c, 0x34, 0x4f, 0x10, 0x9c, 0x2f, 0x64, 0xc9, 0xc5, 0x0e, 0x72,
0x44, 0x66, 0x6a, 0x31, 0xd0, 0x40, 0x66, 0x0d, 0x6e, 0x23, 0x79, 0x88, 0xbb, 0x8b, 0xf5, 0x70,
0xb8, 0x36, 0x08, 0xa6, 0xde, 0xc9, 0x26, 0xca, 0x2a, 0x3d, 0xb3, 0x24, 0xa3, 0x34, 0x49, 0x2f,
0x39, 0x3f, 0x57, 0x3f, 0xa3, 0xb2, 0x20, 0xb5, 0x28, 0x27, 0x35, 0x25, 0x3d, 0xb5, 0x48, 0x3f,
0x2d, 0x31, 0xa9, 0x28, 0x33, 0x59, 0x3f, 0x39, 0xbf, 0x28, 0x55, 0x3f, 0xb9, 0xa8, 0xb2, 0xa0,
0x24, 0x5f, 0x1f, 0x11, 0x2e, 0xfa, 0x60, 0xf3, 0x93, 0x20, 0xe1, 0x62, 0x0c, 0x08, 0x00, 0x00,
0xff, 0xff, 0xc7, 0x30, 0xd3, 0xb6, 0x32, 0x01, 0x00, 0x00,
}
......@@ -28,6 +28,7 @@ import (
"github.com/hyperledger/fabric/core/ledger/kvledger"
"github.com/hyperledger/fabric/core/peer"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
putils "github.com/hyperledger/fabric/protos/utils"
)
......@@ -310,7 +311,9 @@ func (e *Endorser) commitTxSimulation(proposal *pb.Proposal, signer msp.SigningI
if err != nil {
return err
}
block := &pb.Block2{Transactions: [][]byte{txBytes}}
block := common.NewBlock(1, []byte{})
block.Data.Data = [][]byte{txBytes}
block.Header.DataHash = block.Data.Hash()
if _, _, err = lgr.RemoveInvalidTransactionsAndPrepare(block); err != nil {
return err
}
......
......@@ -21,6 +21,7 @@ import (
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
)
......@@ -50,11 +51,11 @@ var (
// An implementation of this interface is expected to take an argument
// of type `IndexConfig` which configures the block store on what items should be indexed
type BlockStore interface {
AddBlock(block *pb.Block2) error
AddBlock(block *common.Block) error
GetBlockchainInfo() (*pb.BlockchainInfo, error)
RetrieveBlocks(startNum uint64) (ledger.ResultsIterator, error)
RetrieveBlockByHash(blockHash []byte) (*pb.Block2, error)
RetrieveBlockByNumber(blockNum uint64) (*pb.Block2, error)
RetrieveBlockByHash(blockHash []byte) (*common.Block, error)
RetrieveBlockByNumber(blockNum uint64) (*common.Block, error)
RetrieveTxByID(txID string) (*pb.Transaction, error)
Shutdown()
}
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package fsblkstorage
import (
"fmt"
"github.com/golang/protobuf/proto"
ledgerutil "github.com/hyperledger/fabric/core/ledger/util"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"
)
type serializedBlockInfo struct {
blockHeader *common.BlockHeader
txOffsets map[string]*locPointer
}
func serializeBlock(block *common.Block) ([]byte, *serializedBlockInfo, error) {
buf := proto.NewBuffer(nil)
var err error
info := &serializedBlockInfo{}
info.blockHeader = block.Header
if err = addHeaderBytes(block.Header, buf); err != nil {
return nil, nil, err
}
if info.txOffsets, err = addDataBytes(block.Data, buf); err != nil {
return nil, nil, err
}
if err = addMetadataBytes(block.Metadata, buf); err != nil {
return nil, nil, err
}
return buf.Bytes(), info, nil
}
func deserializeBlock(serializedBlockBytes []byte) (*common.Block, error) {
block := &common.Block{}
var err error
b := ledgerutil.NewBuffer(serializedBlockBytes)
if block.Header, err = extractHeader(b); err != nil {
fmt.Printf("h:%s\n", err)
return nil, err
}
if block.Data, _, err = extractData(b); err != nil {
fmt.Printf("d:%s\n", err)
return nil, err
}
if block.Metadata, err = extractMetadata(b); err != nil {
fmt.Printf("m:%s\n", err)
return nil, err
}
return block, nil
}
func extractSerializedBlockInfo(serializedBlockBytes []byte) (*serializedBlockInfo, error) {
info := &serializedBlockInfo{}
var err error
b := ledgerutil.NewBuffer(serializedBlockBytes)
info.blockHeader, err = extractHeader(b)
if err != nil {
return nil, err
}
_, info.txOffsets, err = extractData(b)
if err != nil {
return nil, err
}
return info, nil
}
func addHeaderBytes(blockHeader *common.BlockHeader, buf *proto.Buffer) error {
if err := buf.EncodeVarint(blockHeader.Number); err != nil {
return err
}
if err := buf.EncodeRawBytes(blockHeader.DataHash); err != nil {
return err
}
if err := buf.EncodeRawBytes(blockHeader.PreviousHash); err != nil {
return err
}
return nil
}
func addDataBytes(blockData *common.BlockData, buf *proto.Buffer) (map[string]*locPointer, error) {
txOffsets := make(map[string]*locPointer)
if err := buf.EncodeVarint(uint64(len(blockData.Data))); err != nil {
return nil, err
}
for _, txEnvelopeBytes := range blockData.Data {
offset := len(buf.Bytes())
txid, err := extractTxID(txEnvelopeBytes)
if err != nil {
return nil, err
}
if err := buf.EncodeRawBytes(txEnvelopeBytes); err != nil {
return nil, err
}
txOffsets[txid] = &locPointer{offset, len(buf.Bytes()) - offset}
}
return txOffsets, nil
}
func addMetadataBytes(blockMetadata *common.BlockMetadata, buf *proto.Buffer) error {
numItems := uint64(0)
if blockMetadata != nil {
numItems = uint64(len(blockMetadata.Metadata))
}
if err := buf.EncodeVarint(numItems); err != nil {
return err
}
if numItems == 0 {
return nil
}
for _, b := range blockMetadata.Metadata {
if err := buf.EncodeRawBytes(b); err != nil {
return err
}
}
return nil
}
func extractHeader(buf *ledgerutil.Buffer) (*common.BlockHeader, error) {
header := &common.BlockHeader{}
var err error
if header.Number, err = buf.DecodeVarint(); err != nil {
return nil, err
}
if header.DataHash, err = buf.DecodeRawBytes(false); err != nil {
return nil, err
}
if header.PreviousHash, err = buf.DecodeRawBytes(false); err != nil {
return nil, err
}
return header, nil
}
func extractData(buf *ledgerutil.Buffer) (*common.BlockData, map[string]*locPointer, error) {
data := &common.BlockData{}
txOffsets := make(map[string]*locPointer)
var numItems uint64
var err error
if numItems, err = buf.DecodeVarint(); err != nil {
return nil, nil, err
}
for i := uint64(0); i < numItems; i++ {
var txEnvBytes []byte
var txid string
txOffset := buf.GetBytesConsumed()
if txEnvBytes, err = buf.DecodeRawBytes(false); err != nil {
return nil, nil, err
}
if txid, err = extractTxID(txEnvBytes); err != nil {
return nil, nil, err
}
data.Data = append(data.Data, txEnvBytes)
txOffsets[txid] = &locPointer{txOffset, buf.GetBytesConsumed() - txOffset}
}
return data, txOffsets, nil
}
func extractMetadata(buf *ledgerutil.Buffer) (*common.BlockMetadata, error) {
metadata := &common.BlockMetadata{}
var numItems uint64
var metadataEntry []byte
var err error
if numItems, err = buf.DecodeVarint(); err != nil {
return nil, err
}
for i := uint64(0); i < numItems; i++ {
if metadataEntry, err = buf.DecodeRawBytes(false); err != nil {
return nil, err
}
metadata.Metadata = append(metadata.Metadata, metadataEntry)
}
return metadata, nil
}
func extractTxID(txEnvelopBytes []byte) (string, error) {
txEnvelope, err := utils.GetEnvelope(txEnvelopBytes)
if err != nil {
return "", err
}
txPayload, err := utils.GetPayload(txEnvelope)
if err != nil {
return "", nil
}
return txPayload.Header.ChainHeader.TxID, nil
}
......@@ -14,52 +14,50 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package peer
package fsblkstorage
import (
"reflect"
"testing"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/core/ledger/testutil"
putils "github.com/hyperledger/fabric/protos/utils"
)
func TestSerBlock2(t *testing.T) {
tx1 := &Transaction{}
tx1.Actions = []*TransactionAction{
&TransactionAction{Header: []byte("action1"), Payload: []byte("payload1")},
&TransactionAction{Header: []byte("action2"), Payload: []byte("payload2")}}
tx1Bytes, err := proto.Marshal(tx1)
if err != nil {
t.Fatalf("Error:%s", err)
}
tx2 := &Transaction{}
tx2.Actions = []*TransactionAction{
&TransactionAction{Header: []byte("action1"), Payload: []byte("payload1")},
&TransactionAction{Header: []byte("action2"), Payload: []byte("payload2")}}
tx2Bytes, err := proto.Marshal(tx2)
if err != nil {
t.Fatalf("Error:%s", err)
}
func TestBlockSerialization(t *testing.T) {
block := testutil.ConstructTestBlock(t, 10, 100)
bb, _, err := serializeBlock(block)
testutil.AssertNoError(t, err, "")
deserializedBlock, err := deserializeBlock(bb)
testutil.AssertNoError(t, err, "")
testutil.AssertEquals(t, deserializedBlock, block)
}
block := &Block2{}
block.PreviousBlockHash = []byte("PreviousBlockHash")
block.Transactions = [][]byte{tx1Bytes, tx2Bytes}
testSerBlock2(t, block)
func TestExtractTxid(t *testing.T) {
txid := "txID"
txEnv, txid, _ := testutil.ConstructTransaction(t, testutil.ConstructRandomBytes(t, 50), false)
txEnvBytes, _ := putils.GetBytesEnvelope(txEnv)
extractedTxid, err := extractTxID(txEnvBytes)
testutil.AssertNoError(t, err, "")
testutil.AssertEquals(t, extractedTxid, txid)
}
func testSerBlock2(t *testing.T, block *Block2) {
serBlock, err := ConstructSerBlock2(block)
if err != nil {
t.Fatalf("Error:%s", err)
}
serDeBlock, err := serBlock.ToBlock2()
if err != nil {
t.Fatalf("Error:%s", err)
}
if !reflect.DeepEqual(block, serDeBlock) {
t.Fatalf("Block is not same after serialization-deserialization. \n\t Expected=%#v, \n\t Actual=%#v", block, serDeBlock)