Hash computes the unique Chain protocol hash of the BlockHeader.
()
| 11 | |
| 12 | // Hash computes the unique Chain protocol hash of the BlockHeader. |
| 13 | func (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. |
| 63 | func (bh *BlockHeader) Scan(val interface{}) error { |