forked from kaspanet/kaspad
/
block.go
84 lines (71 loc) · 2.05 KB
/
block.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package externalapi
import "math/big"
// DomainBlock represents a Kash block
type DomainBlock struct {
Header BlockHeader
Transactions []*DomainTransaction
}
// Clone returns a clone of DomainBlock
func (block *DomainBlock) Clone() *DomainBlock {
transactionClone := make([]*DomainTransaction, len(block.Transactions))
for i, tx := range block.Transactions {
transactionClone[i] = tx.Clone()
}
return &DomainBlock{
Header: block.Header,
Transactions: transactionClone,
}
}
// If this doesn't compile, it means the type definition has been changed, so it's
// an indication to update Equal and Clone accordingly.
var _ = DomainBlock{nil, []*DomainTransaction{}}
// Equal returns whether block equals to other
func (block *DomainBlock) Equal(other *DomainBlock) bool {
if block == nil || other == nil {
return block == other
}
if len(block.Transactions) != len(other.Transactions) {
return false
}
if !block.Header.Equal(other.Header) {
return false
}
for i, tx := range block.Transactions {
if !tx.Equal(other.Transactions[i]) {
return false
}
}
return true
}
// BlockHeader represents an immutable block header.
type BlockHeader interface {
BaseBlockHeader
ToMutable() MutableBlockHeader
}
// BaseBlockHeader represents the header part of a Kash block
type BaseBlockHeader interface {
Version() uint16
Parents() []BlockLevelParents
DirectParents() BlockLevelParents
HashMerkleRoot() *DomainHash
AcceptedIDMerkleRoot() *DomainHash
UTXOCommitment() *DomainHash
TimeInMilliseconds() int64
Bits() uint32
Nonce() uint64
DAAScore() uint64
BlueScore() uint64
BlueWork() *big.Int
PruningPoint() *DomainHash
BlockLevel(maxBlockLevel int) int
Equal(other BaseBlockHeader) bool
}
// MutableBlockHeader represents a block header that can be mutated, but only
// the fields that are relevant to mining (Nonce and TimeInMilliseconds).
type MutableBlockHeader interface {
BaseBlockHeader
ToImmutable() BlockHeader
SetNonce(nonce uint64)
SetTimeInMilliseconds(timeInMilliseconds int64)
SetHashMerkleRoot(hashMerkleRoot *DomainHash)
}