MCPcopy Create free account
hub / github.com/chain/txvm / Hash

Method Hash

protocol/bc/blockheader.go:13–60  ·  view source on GitHub ↗

Hash computes the unique Chain protocol hash of the BlockHeader.

()

Source from the content-addressed store, hash-verified

11
12// Hash computes the unique Chain protocol hash of the BlockHeader.
13func (bh *BlockHeader) Hash() (hash Hash) {
14 emptyHash := make([]byte, 32)
15 prevID, txRoot, contractRoot, nonceRoot := emptyHash, emptyHash, emptyHash, emptyHash
16 if bh.PreviousBlockId != nil {
17 prevID = bh.PreviousBlockId.Bytes()
18 }
19 if bh.TransactionsRoot != nil {
20 txRoot = bh.TransactionsRoot.Bytes()
21 }
22 if bh.ContractsRoot != nil {
23 contractRoot = bh.ContractsRoot.Bytes()
24 }
25 if bh.NoncesRoot != nil {
26 nonceRoot = bh.NoncesRoot.Bytes()
27 }
28
29 predicateTuple := txvm.Tuple{
30 txvm.Int(bh.NextPredicate.Version),
31 }
32 if bh.NextPredicate.Version == 1 {
33 predicateTuple = append(predicateTuple, txvm.Int(bh.NextPredicate.Quorum))
34 for _, pk := range bh.NextPredicate.Pubkeys {
35 predicateTuple = append(predicateTuple, txvm.Bytes(pk))
36 }
37 } else {
38 for _, item := range bh.NextPredicate.OtherFields {
39 predicateTuple = append(predicateTuple, item.asTxvm())
40 }
41 }
42
43 tupleHeader := txvm.Tuple{
44 txvm.Int(bh.Version),
45 txvm.Int(bh.Height),
46 txvm.Bytes(prevID),
47 txvm.Int(bh.TimestampMs),
48 txvm.Int(bh.RefsCount),
49 txvm.Int(bh.Runlimit),
50 txvm.Bytes(txRoot),
51 txvm.Bytes(contractRoot),
52 txvm.Bytes(nonceRoot),
53 predicateTuple,
54 }
55 for _, item := range bh.ExtraFields {
56 tupleHeader = append(tupleHeader, item.asTxvm())
57 }
58
59 return NewHash(txvm.VMHash("BlockID", txvm.Encode(tupleHeader)))
60}
61
62// Scan satisfies the database.sql.Scanner interface.
63func (bh *BlockHeader) Scan(val interface{}) error {

Callers 3

TestBlockPrevFunction · 0.95
TestBlockHeaderHashFunction · 0.45
TestSignBlockFunction · 0.45

Calls 7

IntTypeAlias · 0.92
BytesTypeAlias · 0.92
VMHashFunction · 0.92
EncodeFunction · 0.92
NewHashFunction · 0.85
asTxvmMethod · 0.80
BytesMethod · 0.45

Tested by 3

TestBlockPrevFunction · 0.76
TestBlockHeaderHashFunction · 0.36
TestSignBlockFunction · 0.36