/
utils.go
46 lines (40 loc) · 1.35 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright (c) 2019 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
package block
import (
"math/big"
"github.com/iotexproject/go-pkgs/hash"
"go.uber.org/zap"
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/crypto"
"github.com/iotexproject/iotex-core/pkg/log"
)
func calculateTxRoot(acts []*action.SealedEnvelope) (hash.Hash256, error) {
h := make([]hash.Hash256, 0, len(acts))
for _, act := range acts {
actHash, err := act.Hash()
if err != nil {
log.L().Debug("Error in getting hash", zap.Error(err))
return hash.ZeroHash256, err
}
h = append(h, actHash)
}
if len(h) == 0 {
return hash.ZeroHash256, nil
}
return crypto.NewMerkleTree(h).HashTree(), nil
}
// calculateTransferAmount returns the calculated transfer amount
func calculateTransferAmount(acts []*action.SealedEnvelope) *big.Int {
transferAmount := big.NewInt(0)
for _, act := range acts {
transfer, ok := act.Action().(*action.Transfer)
if !ok {
continue
}
transferAmount.Add(transferAmount, transfer.Amount())
}
return transferAmount
}