-
Notifications
You must be signed in to change notification settings - Fork 4
/
derive_sha.go
61 lines (55 loc) · 1.5 KB
/
derive_sha.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
package types
import (
"github.com/LemoFoundationLtd/lemochain-go/chain/deputynode"
"github.com/LemoFoundationLtd/lemochain-go/common"
"github.com/LemoFoundationLtd/lemochain-go/common/crypto"
"github.com/LemoFoundationLtd/lemochain-go/common/merkle"
)
var emptyHash = crypto.Keccak256Hash(nil)
// DeriveTxsSha 计算交易的根HASH
func DeriveTxsSha(txs []*Transaction) common.Hash {
if txs == nil || len(txs) == 0 {
return emptyHash
}
leaves := make([]common.Hash, 0, len(txs))
for _, tx := range txs {
leaves = append(leaves, tx.Hash())
}
m := merkle.New(leaves)
return m.Root()
}
// DeriveEventsSha 计算event的根HASH
func DeriveEventsSha(events []*Event) common.Hash {
if events == nil || len(events) == 0 {
return emptyHash
}
leaves := make([]common.Hash, 0, len(events))
for _, event := range events {
leaves = append(leaves, event.Hash())
}
m := merkle.New(leaves)
return m.Root()
}
// DeriveChangeLogsSha 计算changelog的根HASH
func DeriveChangeLogsSha(logs []*ChangeLog) common.Hash {
if logs == nil || len(logs) == 0 {
return emptyHash
}
leaves := make([]common.Hash, 0, len(logs))
for _, log := range logs {
leaves = append(leaves, log.Hash())
}
m := merkle.New(leaves)
return m.Root()
}
func DeriveDeputyRootSha(nodes deputynode.DeputyNodes) common.Hash {
if nodes == nil || len(nodes) == 0 {
return emptyHash
}
leaves := make([]common.Hash, 0, len(nodes))
for _, n := range nodes {
leaves = append(leaves, n.Hash())
}
m := merkle.New(leaves)
return m.Root()
}