/
utils.go
64 lines (56 loc) · 1.73 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. 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 (
"bytes"
"math/big"
"github.com/iotexproject/go-pkgs/hash"
"github.com/pkg/errors"
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/crypto"
)
func calculateTxRoot(acts []action.SealedEnvelope) hash.Hash256 {
h := make([]hash.Hash256, 0, len(acts))
for _, act := range acts {
h = append(h, act.Hash())
}
if len(h) == 0 {
return hash.ZeroHash256
}
return crypto.NewMerkleTree(h).HashTree()
}
// 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
}
// VerifyBlock verifies the block signature and tx root
func VerifyBlock(blk *Block) error {
// verify new block's signature is correct
if !blk.VerifySignature() {
return errors.Errorf(
"failed to verify block's signature with public key: %x",
blk.PublicKey(),
)
}
hashExpect := blk.TxRoot()
hashActual := blk.CalculateTxRoot()
if !bytes.Equal(hashExpect[:], hashActual[:]) {
return errors.Errorf(
"wrong tx hash %x, expecting %x",
hashActual,
hashExpect,
)
}
return nil
}