forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
live_block.go
247 lines (203 loc) · 5.81 KB
/
live_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package spchainvm
import (
"errors"
"github.com/ava-labs/gecko/database"
"github.com/ava-labs/gecko/database/versiondb"
"github.com/ava-labs/gecko/ids"
"github.com/ava-labs/gecko/snow/choices"
"github.com/ava-labs/gecko/snow/consensus/snowman"
"github.com/ava-labs/gecko/vms/components/missing"
)
var (
errRejected = errors.New("block is rejected")
errMissingBlock = errors.New("missing block")
)
// LiveBlock implements snow/snowman.Block
type LiveBlock struct {
// The VM this block exists within
vm *VM
verifiedBlock, verifiedState bool
validity error
// This block's parent
parent *LiveBlock
// This block's children
children []*LiveBlock
// The status of this block
status choices.Status
db *versiondb.Database
// Contains the actual transactions
block *Block
}
// ID returns the blkID
func (lb *LiveBlock) ID() ids.ID { return lb.block.id }
// Accept is called when this block is finalized as accepted by consensus
func (lb *LiveBlock) Accept() {
bID := lb.ID()
lb.vm.ctx.Log.Debug("Accepted block %s", bID)
lb.status = choices.Accepted
lb.vm.lastAccepted = bID
if err := lb.db.Commit(); err != nil {
lb.vm.ctx.Log.Debug("Failed to accept block %s due to %s", bID, err)
return
}
for _, child := range lb.children {
child.setBaseDatabase(lb.vm.baseDB)
}
delete(lb.vm.currentBlocks, bID.Key())
lb.parent = nil
lb.children = nil
for _, tx := range lb.block.txs {
if tx.onDecide != nil {
tx.onDecide(choices.Accepted)
}
}
if lb.vm.onAccept != nil {
lb.vm.onAccept(bID)
}
}
// Reject is called when this block is finalized as rejected by consensus
func (lb *LiveBlock) Reject() {
lb.vm.ctx.Log.Debug("Rejected block %s", lb.ID())
if err := lb.vm.state.SetStatus(lb.vm.baseDB, lb.ID(), choices.Rejected); err != nil {
lb.vm.ctx.Log.Debug("Failed to reject block %s due to %s", lb.ID(), err)
return
}
lb.status = choices.Rejected
delete(lb.vm.currentBlocks, lb.ID().Key())
lb.parent = nil
lb.children = nil
for _, tx := range lb.block.txs {
if tx.onDecide != nil {
tx.onDecide(choices.Rejected)
}
}
}
// Status returns the current status of this block
func (lb *LiveBlock) Status() choices.Status {
if lb.status == choices.Unknown {
lb.status = choices.Processing
if status, err := lb.vm.state.Status(lb.vm.baseDB, lb.block.ID()); err == nil {
lb.status = status
}
}
return lb.status
}
// Parent returns the parent of this block
func (lb *LiveBlock) Parent() snowman.Block {
parent := lb.parentBlock()
if parent != nil {
return parent
}
return &missing.Block{BlkID: lb.block.ParentID()}
}
func (lb *LiveBlock) parentBlock() *LiveBlock {
// If [lb]'s parent field is already non-nil, return the value in that field
if lb.parent != nil {
return lb.parent
}
// Check to see if [lb]'s parent is in currentBlocks
if parent, exists := lb.vm.currentBlocks[lb.block.ParentID().Key()]; exists {
lb.parent = parent
return parent
}
// Check to see if [lb]'s parent is in the vm database
if parent, err := lb.vm.state.Block(lb.vm.baseDB, lb.block.ParentID()); err == nil {
return &LiveBlock{
vm: lb.vm,
block: parent,
}
}
// Parent could not be found
return nil
}
// Bytes returns the binary representation of this transaction
func (lb *LiveBlock) Bytes() []byte { return lb.block.Bytes() }
// Verify the validity of this block
func (lb *LiveBlock) Verify() error {
switch status := lb.Status(); status {
case choices.Accepted:
return nil
case choices.Rejected:
return errRejected
default:
return lb.VerifyState()
}
}
// VerifyBlock the validity of this block
func (lb *LiveBlock) VerifyBlock() error {
if lb.verifiedBlock {
return lb.validity
}
lb.verifiedBlock = true
lb.validity = lb.block.verify(lb.vm.ctx, &lb.vm.factory)
return lb.validity
}
// VerifyState the validity of this block
func (lb *LiveBlock) VerifyState() error {
if err := lb.VerifyBlock(); err != nil {
return err
}
parent := lb.parentBlock()
if parent == nil {
return errMissingBlock
}
if err := parent.Verify(); err != nil {
return err
}
if lb.verifiedState {
return lb.validity
}
lb.verifiedState = true
// The database if this block were to be accepted
lb.db = versiondb.New(parent.database())
// Go through each transaction in this block.
// Validate each and apply its state transitions to [lb.db].
// Verify that taken together, these transactions are valid
// (e.g. they don't result in a negative account balance, etc.)
for _, tx := range lb.block.txs {
from := tx.key(lb.vm.ctx, &lb.vm.factory).Address()
fromAccount := lb.vm.GetAccount(lb.db, from)
newFromAccount, err := fromAccount.send(tx, lb.vm.ctx, &lb.vm.factory)
if err != nil {
lb.validity = err
break
}
if err := lb.vm.state.SetAccount(lb.db, from.LongID(), newFromAccount); err != nil {
lb.validity = err
break
}
to := tx.To()
toAccount := lb.vm.GetAccount(lb.db, to)
newToAccount, err := toAccount.receive(tx, lb.vm.ctx, &lb.vm.factory)
if err != nil {
lb.validity = err
break
}
if err := lb.vm.state.SetAccount(lb.db, to.LongID(), newToAccount); err != nil {
lb.validity = err
break
}
}
if err := lb.vm.state.SetStatus(lb.db, lb.ID(), choices.Accepted); err != nil {
lb.validity = err
}
if err := lb.vm.state.SetLastAccepted(lb.db, lb.ID()); err != nil {
lb.validity = err
}
// If this block is valid, add it as a child of its parent
// and add this block to currentBlocks
if lb.validity == nil {
parent.children = append(parent.children, lb)
lb.vm.currentBlocks[lb.block.ID().Key()] = lb
}
return lb.validity
}
func (lb *LiveBlock) database() database.Database {
if lb.Status().Decided() {
return lb.vm.baseDB
}
return lb.db
}
func (lb *LiveBlock) setBaseDatabase(db database.Database) { lb.db.SetDatabase(db) }