github.com/iotexproject/iotex-core@v1.14.1-rc1/blockchain/block/utils.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  	"go.uber.org/zap"
    13  
    14  	"github.com/iotexproject/iotex-core/action"
    15  	"github.com/iotexproject/iotex-core/crypto"
    16  	"github.com/iotexproject/iotex-core/pkg/log"
    17  )
    18  
    19  func calculateTxRoot(acts []*action.SealedEnvelope) (hash.Hash256, error) {
    20  	h := make([]hash.Hash256, 0, len(acts))
    21  	for _, act := range acts {
    22  		actHash, err := act.Hash()
    23  		if err != nil {
    24  			log.L().Debug("Error in getting hash", zap.Error(err))
    25  			return hash.ZeroHash256, err
    26  		}
    27  		h = append(h, actHash)
    28  	}
    29  	if len(h) == 0 {
    30  		return hash.ZeroHash256, nil
    31  	}
    32  	return crypto.NewMerkleTree(h).HashTree(), nil
    33  }
    34  
    35  // calculateTransferAmount returns the calculated transfer amount
    36  func calculateTransferAmount(acts []*action.SealedEnvelope) *big.Int {
    37  	transferAmount := big.NewInt(0)
    38  	for _, act := range acts {
    39  		transfer, ok := act.Action().(*action.Transfer)
    40  		if !ok {
    41  			continue
    42  		}
    43  		transferAmount.Add(transferAmount, transfer.Amount())
    44  	}
    45  	return transferAmount
    46  }