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

     1  // Copyright (c) 2020 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  	"google.golang.org/protobuf/proto"
    10  
    11  	"github.com/iotexproject/iotex-proto/golang/iotextypes"
    12  
    13  	"github.com/iotexproject/iotex-core/action"
    14  )
    15  
    16  type (
    17  	// Store defines block storage schema
    18  	Store struct {
    19  		Block    *Block
    20  		Receipts []*action.Receipt
    21  	}
    22  )
    23  
    24  // Serialize returns the serialized byte stream of Store
    25  func (in *Store) Serialize() ([]byte, error) {
    26  	return proto.Marshal(in.ToProto())
    27  }
    28  
    29  // ToProto converts to proto message
    30  func (in *Store) ToProto() *iotextypes.BlockStore {
    31  	receipts := []*iotextypes.Receipt{}
    32  	for _, r := range in.Receipts {
    33  		receipts = append(receipts, r.ConvertToReceiptPb())
    34  	}
    35  	return &iotextypes.BlockStore{
    36  		Block:    in.Block.ConvertToBlockPb(),
    37  		Receipts: receipts,
    38  	}
    39  }
    40  
    41  // DeserializeBlockStoresPb decode byte stream into BlockStores pb message
    42  func DeserializeBlockStoresPb(buf []byte) (*iotextypes.BlockStores, error) {
    43  	pbStores := &iotextypes.BlockStores{}
    44  	if err := proto.Unmarshal(buf, pbStores); err != nil {
    45  		return nil, err
    46  	}
    47  	return pbStores, nil
    48  }