-
Notifications
You must be signed in to change notification settings - Fork 643
/
vm.go
322 lines (270 loc) · 8.1 KB
/
vm.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package spchainvm
import (
"errors"
"time"
"github.com/gorilla/rpc/v2"
"github.com/ava-labs/gecko/cache"
"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"
"github.com/ava-labs/gecko/snow/choices"
"github.com/ava-labs/gecko/snow/consensus/snowman"
"github.com/ava-labs/gecko/snow/engine/common"
"github.com/ava-labs/gecko/utils/crypto"
"github.com/ava-labs/gecko/utils/formatting"
"github.com/ava-labs/gecko/utils/timer"
"github.com/ava-labs/gecko/utils/wrappers"
jsoncodec "github.com/ava-labs/gecko/utils/json"
)
const (
batchTimeout = time.Second
idCacheSize = 10000
sigCache = 10000
)
var (
maxBatchSize = 30
)
var (
errNoTxs = errors.New("no transactions")
errUnsupportedFXs = errors.New("unsupported feature extensions")
)
// VM implements the snowman.ChainVM interface
type VM struct {
ctx *snow.Context
// State management
state *prefixedState
baseDB database.Database
factory crypto.FactorySECP256K1R
// The ID of the last accepted block
lastAccepted ids.ID
// Transaction issuing
preferred ids.ID
toEngine chan<- common.Message
timer *timer.Timer
txs []*Tx
currentBlocks map[[32]byte]*LiveBlock
onAccept func(ids.ID)
}
/*
******************************************************************************
********************************* Snowman API ********************************
******************************************************************************
*/
// Initialize implements the snowman.ChainVM interface
func (vm *VM) Initialize(
ctx *snow.Context,
db database.Database,
genesisBytes []byte,
toEngine chan<- common.Message,
fxs []*common.Fx,
) error {
if len(fxs) != 0 {
return errUnsupportedFXs
}
vm.ctx = ctx
vm.state = &prefixedState{
block: &cache.LRU{Size: idCacheSize},
account: &cache.LRU{Size: idCacheSize},
status: &cache.LRU{Size: idCacheSize},
}
vm.baseDB = db
vm.factory.Cache.Size = sigCache
if dbStatus, err := vm.state.DBInitialized(db); err != nil || dbStatus == choices.Unknown {
if err := vm.initState(genesisBytes); err != nil {
return err
}
}
vm.timer = timer.NewTimer(func() {
ctx.Lock.Lock()
defer ctx.Lock.Unlock()
vm.FlushTxs()
})
go ctx.Log.RecoverAndPanic(vm.timer.Dispatch)
vm.toEngine = toEngine
lastAccepted, err := vm.state.LastAccepted(db)
if err != nil {
return err
}
vm.lastAccepted = lastAccepted
vm.currentBlocks = make(map[[32]byte]*LiveBlock)
return nil
}
// Bootstrapping marks this VM as bootstrapping
func (vm *VM) Bootstrapping() error { return nil }
// Bootstrapped marks this VM as bootstrapped
func (vm *VM) Bootstrapped() error { return nil }
// Shutdown implements the snowman.ChainVM interface
func (vm *VM) Shutdown() error {
if vm.timer == nil {
return nil
}
// There is a potential deadlock if the timer is about to execute a timeout.
// So, the lock must be released before stopping the timer.
vm.ctx.Lock.Unlock()
vm.timer.Stop()
vm.ctx.Lock.Lock()
return vm.baseDB.Close()
}
// BuildBlock implements the snowman.ChainVM interface
func (vm *VM) BuildBlock() (snowman.Block, error) {
vm.timer.Cancel()
if len(vm.txs) == 0 {
return nil, errNoTxs
}
defer vm.FlushTxs()
txs := vm.txs
if len(txs) > maxBatchSize {
txs = txs[:maxBatchSize]
}
vm.txs = vm.txs[len(txs):]
builder := Builder{
NetworkID: 0,
ChainID: vm.ctx.ChainID,
}
rawBlock, err := builder.NewBlock(vm.preferred, txs)
if err != nil {
vm.ctx.Log.Warn("Dropping transactions due to %s", err)
return nil, err
}
rawBlock.startVerify(vm.ctx, &vm.factory)
block := &LiveBlock{
vm: vm,
block: rawBlock,
}
return block, vm.state.SetBlock(vm.baseDB, rawBlock.ID(), rawBlock)
}
// ParseBlock implements the snowman.ChainVM interface
func (vm *VM) ParseBlock(b []byte) (snowman.Block, error) {
c := Codec{}
rawBlock, err := c.UnmarshalBlock(b)
if err != nil {
return nil, err
}
rawBlock.startVerify(vm.ctx, &vm.factory)
block := &LiveBlock{
vm: vm,
block: rawBlock,
}
return block, vm.state.SetBlock(vm.baseDB, rawBlock.ID(), rawBlock)
}
// GetBlock implements the snowman.ChainVM interface
func (vm *VM) GetBlock(id ids.ID) (snowman.Block, error) {
blk, err := vm.state.Block(vm.baseDB, id)
if err != nil {
return nil, err
}
blk.startVerify(vm.ctx, &vm.factory)
return &LiveBlock{
vm: vm,
block: blk,
}, nil
}
// SetPreference sets what the current tail of the chain is
func (vm *VM) SetPreference(preferred ids.ID) { vm.preferred = preferred }
// LastAccepted returns the last accepted block ID
func (vm *VM) LastAccepted() ids.ID { return vm.lastAccepted }
// CreateHandlers makes new service objects with references to the vm
func (vm *VM) CreateHandlers() map[string]*common.HTTPHandler {
newServer := rpc.NewServer()
codec := jsoncodec.NewCodec()
newServer.RegisterCodec(codec, "application/json")
newServer.RegisterCodec(codec, "application/json;charset=UTF-8")
// Name the API service "spchain"
vm.ctx.Log.AssertNoError(newServer.RegisterService(&Service{vm: vm}, "spchain"))
return map[string]*common.HTTPHandler{
"": &common.HTTPHandler{LockOptions: common.WriteLock, Handler: newServer},
}
}
// CreateStaticHandlers makes new service objects without references to the vm
func (vm *VM) CreateStaticHandlers() map[string]*common.HTTPHandler {
newServer := rpc.NewServer()
codec := jsoncodec.NewCodec()
newServer.RegisterCodec(codec, "application/json")
newServer.RegisterCodec(codec, "application/json;charset=UTF-8")
// Name the API service "spchain"
_ = newServer.RegisterService(&StaticService{}, "spchain")
return map[string]*common.HTTPHandler{
"": &common.HTTPHandler{LockOptions: common.NoLock, Handler: newServer},
}
}
// IssueTx ...
// TODO: Remove this
func (vm *VM) IssueTx(b []byte, onDecide func(choices.Status)) (ids.ID, error) {
codec := Codec{}
tx, err := codec.UnmarshalTx(b)
if err != nil {
return ids.ID{}, err
}
tx.startVerify(vm.ctx, &vm.factory)
tx.onDecide = onDecide
vm.issueTx(tx)
return tx.id, nil
}
// GetAccount returns the account with the specified address
func (vm *VM) GetAccount(db database.Database, address ids.ShortID) Account {
if account, err := vm.state.Account(db, address.LongID()); err == nil {
return account
}
builder := Builder{
NetworkID: 0,
ChainID: vm.ctx.ChainID,
}
return builder.NewAccount(address, 0, 0)
}
/*
******************************************************************************
********************************** Timer API *********************************
******************************************************************************
*/
// FlushTxs into consensus
func (vm *VM) FlushTxs() {
vm.timer.Cancel()
if len(vm.txs) > 0 {
select {
case vm.toEngine <- common.PendingTxs:
default:
vm.ctx.Log.Warn("Dropping block due to too frequent issuance")
vm.timer.SetTimeoutIn(batchTimeout)
}
}
}
/*
******************************************************************************
******************************* Implementation *******************************
******************************************************************************
*/
// Consensus:
func (vm *VM) initState(genesisBytes []byte) error {
errs := wrappers.Errs{}
vdb := versiondb.New(vm.baseDB)
b := Builder{}
block, err := b.NewBlock(ids.Empty, nil)
errs.Add(err)
c := Codec{}
accounts, err := c.UnmarshalGenesis(genesisBytes)
errs.Add(err)
errs.Add(vm.state.SetBlock(vdb, block.ID(), block))
errs.Add(vm.state.SetStatus(vdb, block.ID(), choices.Accepted))
errs.Add(vm.state.SetLastAccepted(vdb, block.ID()))
for _, account := range accounts {
errs.Add(vm.state.SetAccount(vdb, account.ID().LongID(), account))
}
errs.Add(vm.state.SetDBInitialized(vdb, choices.Processing))
if errs.Errored() {
return errs.Err
}
return vdb.Commit()
}
func (vm *VM) issueTx(tx *Tx) {
vm.ctx.Log.Verbo("Issuing tx:\n%s", formatting.DumpBytes{Bytes: tx.Bytes()})
vm.txs = append(vm.txs, tx)
switch {
case len(vm.txs) == maxBatchSize:
vm.FlushTxs()
case len(vm.txs) == 1:
vm.timer.SetTimeoutIn(batchTimeout)
}
}