-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.go
164 lines (133 loc) · 4 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// block is just a tx mass, but can be treat as a block
// it means that after the vault summary, the data in previous block chain can be throw
// we just need to keep the latest some block and treasuries to make the chain safe
package ngtype
import (
"bytes"
"errors"
"math/big"
"time"
"github.com/gogo/protobuf/proto"
"github.com/mr-tron/base58"
"github.com/ngin-network/cryptonight-go"
"github.com/whyrusleeping/go-logging"
)
var (
ErrBlockIsBare = errors.New("the block is bare")
ErrBlockIsUnsealing = errors.New("the block is unsealing")
ErrBlockHeightInvalid = errors.New("the block's height is invalid")
ErrBlockMTreeInvalid = errors.New("the merkle tree in block is invalid")
ErrBlockPrevBlockHash = errors.New("the block's previous block hash is invalid")
ErrBlockPrevTreasuryHash = errors.New("the block's backend vault is invalid")
ErrBlockDiffInvalid = errors.New("the block's difficulty is invalid")
ErrBlockHashInvalid = errors.New("the block's hash is invalid")
ErrBlockNonceInvalid = errors.New("the block's Nonce is invalid")
ErrMalformedBlock = errors.New("the block structure is malformed")
)
var log = logging.MustGetLogger("block")
// IsCheckpoint will check whether the Block is the
func (m *Block) IsCheckpoint() bool {
return m.Height%8 == 0
}
func (m *Block) IsUnsealing() bool {
return m.TrieHash == nil
}
func (m *Block) IsSealed() bool {
return m.Nonce != nil
}
func (m *Block) IsGenesisBlock() bool {
return m.Height == 0
}
// ToUnsealing converts a bare block to an unsealing block
func (m *Block) ToUnsealing(ops []*Operation) *Block {
b := m.Copy()
ops = append(ops, b.Operations...)
mTreeHash := NewOpTrie(ops).TrieRoot()
copy(b.TrieHash, mTreeHash)
return b
}
// ToUnsealing converts an unsealing block to a sealed block
func (m *Block) ToSealed(nonce []byte, hash []byte) *Block {
b := m.Copy()
if b.IsUnsealing() == false {
log.Error(ErrBlockIsBare)
return nil
}
b.Nonce = nonce
return b
}
// GetBlob will return a complete blob for block hash
// = PoWBlob + Nonce
func (m *Block) GetBlob() ([]byte, error) {
b := m.Copy()
if b.Nonce == nil {
return nil, errors.New("the block need to get Nonce (mined) before getting blob")
}
b.Hash = nil // empty the hash prop
return proto.Marshal(b)
}
// GetHash will help you get the hash of block
func (m *Block) CalculateHash() ([]byte, error) {
blob, err := m.GetBlob()
if err != nil {
return nil, err
}
if m.Nonce == nil {
return nil, errors.New("missing the Nonce")
}
m.Hash = cryptonight.Sum(blob, 0)
return m.Hash, nil
}
// GetHash will help you get the hash of block
func (m *Block) VerifyHash() bool {
blob, _ := m.GetBlob()
return bytes.Compare(m.Hash, cryptonight.Sum(blob, 0)) == 0
}
// NewBareBlock will return an unsealing block and
// then you need to add ops and seal with the correct Nonce
func NewBareBlock(height uint64, prevBlockHash, prevVaultHash []byte, PublicKey []byte, diff *big.Int) *Block {
block := &Block{
Height: height,
Beneficiary: PublicKey[:],
Timestamp: time.Now().Unix(),
PrevBlockHash: prevBlockHash[:],
PrevVaultHash: prevVaultHash[:],
//Empty params
//MTreeHash: nil,
//Ops: nil,
//Nonce: nil,
//Hash: nil,
Difficulty: diff.Bytes(),
}
return block
}
// GetGenesisBlock will return a complete sealed GenesisBlock
func GetGenesisBlock() *Block {
bPK, _ := base58.FastBase58Decoding(GenesisPK)
b := &Block{
Height: 0,
Timestamp: 1024,
TrieHash: NewOpTrie(nil).TrieRoot(),
PrevBlockHash: nil,
PrevVaultHash: nil,
Beneficiary: bPK,
Nonce: GenesisNonce.Bytes(),
Difficulty: GenesisDifficulty.Bytes(),
}
return b
}
// CheckError will check the errors in block inner fields
func (m *Block) CheckError() error {
if m.Nonce == nil {
return ErrBlockNonceInvalid
}
mTreeHash := NewOpTrie(m.Operations).TrieRoot()
if bytes.Compare(mTreeHash[:], m.TrieHash) != 0 {
return ErrBlockMTreeInvalid
}
return nil
}
func (m *Block) Copy() *Block {
b := *m
return &b
}