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

     1  // Copyright (c) 2019 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  	"math/big"
    10  
    11  	"github.com/iotexproject/go-pkgs/hash"
    12  	"github.com/iotexproject/iotex-proto/golang/iotextypes"
    13  	"google.golang.org/protobuf/proto"
    14  
    15  	"github.com/iotexproject/iotex-core/action"
    16  )
    17  
    18  // Body defines the struct of body
    19  type Body struct {
    20  	Actions []*action.SealedEnvelope
    21  }
    22  
    23  // Proto converts Body to Protobuf
    24  func (b *Body) Proto() *iotextypes.BlockBody {
    25  	actions := []*iotextypes.Action{}
    26  	for _, act := range b.Actions {
    27  		actions = append(actions, act.Proto())
    28  	}
    29  	return &iotextypes.BlockBody{
    30  		Actions: actions,
    31  	}
    32  }
    33  
    34  // Serialize returns the serialized byte stream of the block
    35  func (b *Body) Serialize() ([]byte, error) {
    36  	return proto.Marshal(b.Proto())
    37  }
    38  
    39  // CalculateTxRoot returns the Merkle root of all txs and actions in this block.
    40  func (b *Body) CalculateTxRoot() (hash.Hash256, error) {
    41  	return calculateTxRoot(b.Actions)
    42  }
    43  
    44  // CalculateTransferAmount returns the calculated transfer amount in this block.
    45  func (b *Body) CalculateTransferAmount() *big.Int {
    46  	return calculateTransferAmount(b.Actions)
    47  }