8
8
"errors"
9
9
"fmt"
10
10
"reflect"
11
- "time"
12
11
13
12
stdjson "encoding/json"
14
13
@@ -33,7 +32,6 @@ import (
33
32
"github.com/ava-labs/avalanchego/utils/json"
34
33
"github.com/ava-labs/avalanchego/utils/linkedhashmap"
35
34
"github.com/ava-labs/avalanchego/utils/set"
36
- "github.com/ava-labs/avalanchego/utils/timer"
37
35
"github.com/ava-labs/avalanchego/utils/timer/mockable"
38
36
"github.com/ava-labs/avalanchego/utils/wrappers"
39
37
"github.com/ava-labs/avalanchego/version"
@@ -57,8 +55,6 @@ import (
57
55
)
58
56
59
57
const (
60
- batchTimeout = time .Second
61
- batchSize = 30
62
58
assetToFxCacheSize = 1024
63
59
txDeduplicatorSize = 8192
64
60
)
@@ -110,12 +106,6 @@ type VM struct {
110
106
// Asset ID --> Bit set with fx IDs the asset supports
111
107
assetToFxCache * cache.LRU [ids.ID , set.Bits64 ]
112
108
113
- // Transaction issuing
114
- timer * timer.Timer
115
- batchTimeout time.Duration
116
- txs []snowstorm.Tx
117
- toEngine chan <- common.Message
118
-
119
109
baseDB database.Database
120
110
db * versiondb.Database
121
111
@@ -162,7 +152,7 @@ func (vm *VM) Initialize(
162
152
genesisBytes []byte ,
163
153
_ []byte ,
164
154
configBytes []byte ,
165
- toEngine chan <- common.Message ,
155
+ _ chan <- common.Message ,
166
156
fxs []* common.Fx ,
167
157
appSender common.AppSender ,
168
158
) error {
@@ -197,7 +187,6 @@ func (vm *VM) Initialize(
197
187
198
188
db := dbManager .Current ().Database
199
189
vm .ctx = ctx
200
- vm .toEngine = toEngine
201
190
vm .appSender = appSender
202
191
vm .baseDB = db
203
192
vm .db = versiondb .New (db )
@@ -248,15 +237,6 @@ func (vm *VM) Initialize(
248
237
return err
249
238
}
250
239
251
- vm .timer = timer .NewTimer (func () {
252
- ctx .Lock .Lock ()
253
- defer ctx .Lock .Unlock ()
254
-
255
- vm .FlushTxs ()
256
- })
257
- go ctx .Log .RecoverAndPanic (vm .timer .Dispatch )
258
- vm .batchTimeout = batchTimeout
259
-
260
240
vm .uniqueTxs = & cache.EvictableLRU [ids.ID , * UniqueTx ]{
261
241
Size : txDeduplicatorSize ,
262
242
}
@@ -339,16 +319,10 @@ func (vm *VM) SetState(_ context.Context, state snow.State) error {
339
319
}
340
320
341
321
func (vm * VM ) Shutdown (context.Context ) error {
342
- if vm .timer == nil {
322
+ if vm .state == nil {
343
323
return nil
344
324
}
345
325
346
- // There is a potential deadlock if the timer is about to execute a timeout.
347
- // So, the lock must be released before stopping the timer.
348
- vm .ctx .Lock .Unlock ()
349
- vm .timer .Stop ()
350
- vm .ctx .Lock .Lock ()
351
-
352
326
errs := wrappers.Errs {}
353
327
errs .Add (
354
328
vm .state .Close (),
@@ -480,16 +454,30 @@ func (vm *VM) Linearize(_ context.Context, stopVertexID ids.ID, toEngine chan<-
480
454
return nil
481
455
}
482
456
483
- func (vm * VM ) PendingTxs (context.Context ) []snowstorm.Tx {
484
- vm .timer .Cancel ()
457
+ func (vm * VM ) ParseTx (_ context.Context , bytes []byte ) (snowstorm.Tx , error ) {
458
+ rawTx , err := vm .parser .ParseTx (bytes )
459
+ if err != nil {
460
+ return nil , err
461
+ }
485
462
486
- txs := vm .txs
487
- vm .txs = nil
488
- return txs
489
- }
463
+ tx := & UniqueTx {
464
+ TxCachedState : & TxCachedState {
465
+ Tx : rawTx ,
466
+ },
467
+ vm : vm ,
468
+ txID : rawTx .ID (),
469
+ }
470
+ if err := tx .SyntacticVerify (); err != nil {
471
+ return nil , err
472
+ }
490
473
491
- func (vm * VM ) ParseTx (_ context.Context , b []byte ) (snowstorm.Tx , error ) {
492
- return vm .parseTx (b )
474
+ if tx .Status () == choices .Unknown {
475
+ vm .state .AddTx (tx .Tx )
476
+ tx .setStatus (choices .Processing )
477
+ return tx , vm .state .Commit ()
478
+ }
479
+
480
+ return tx , nil
493
481
}
494
482
495
483
func (vm * VM ) GetTx (_ context.Context , txID ids.ID ) (snowstorm.Tx , error ) {
@@ -513,60 +501,27 @@ func (vm *VM) GetTx(_ context.Context, txID ids.ID) (snowstorm.Tx, error) {
513
501
// either accepted or rejected with the appropriate status. This function will
514
502
// go out of scope when the transaction is removed from memory.
515
503
func (vm * VM ) IssueTx (b []byte ) (ids.ID , error ) {
516
- if ! vm .bootstrapped {
504
+ if ! vm .bootstrapped || vm . Builder == nil {
517
505
return ids.ID {}, errBootstrapping
518
506
}
519
507
520
- // If the chain has been linearized, issue the tx to the network.
521
- if vm .Builder != nil {
522
- tx , err := vm .parser .ParseTx (b )
523
- if err != nil {
524
- vm .ctx .Log .Debug ("failed to parse tx" ,
525
- zap .Error (err ),
526
- )
527
- return ids.ID {}, err
528
- }
529
-
530
- err = vm .network .IssueTx (context .TODO (), tx )
531
- if err != nil {
532
- vm .ctx .Log .Debug ("failed to add tx to mempool" ,
533
- zap .Error (err ),
534
- )
535
- return ids.ID {}, err
536
- }
537
-
538
- return tx .ID (), nil
539
- }
540
-
541
- // TODO: After the chain is linearized, remove the following code.
542
- tx , err := vm .parseTx (b )
508
+ tx , err := vm .parser .ParseTx (b )
543
509
if err != nil {
510
+ vm .ctx .Log .Debug ("failed to parse tx" ,
511
+ zap .Error (err ),
512
+ )
544
513
return ids.ID {}, err
545
514
}
546
- if err := tx .verifyWithoutCacheWrites (); err != nil {
515
+
516
+ err = vm .network .IssueTx (context .TODO (), tx )
517
+ if err != nil {
518
+ vm .ctx .Log .Debug ("failed to add tx to mempool" ,
519
+ zap .Error (err ),
520
+ )
547
521
return ids.ID {}, err
548
522
}
549
- vm .issueTx (tx )
550
- return tx .ID (), nil
551
- }
552
-
553
- /*
554
- ******************************************************************************
555
- ********************************** Timer API *********************************
556
- ******************************************************************************
557
- */
558
523
559
- // FlushTxs into consensus
560
- func (vm * VM ) FlushTxs () {
561
- vm .timer .Cancel ()
562
- if len (vm .txs ) != 0 {
563
- select {
564
- case vm .toEngine <- common .PendingTxs :
565
- default :
566
- vm .ctx .Log .Debug ("dropping message to engine due to contention" )
567
- vm .timer .SetTimeoutIn (vm .batchTimeout )
568
- }
569
- }
524
+ return tx .ID (), nil
570
525
}
571
526
572
527
/*
@@ -638,42 +593,6 @@ func (vm *VM) initState(tx *txs.Tx) {
638
593
}
639
594
}
640
595
641
- func (vm * VM ) parseTx (bytes []byte ) (* UniqueTx , error ) {
642
- rawTx , err := vm .parser .ParseTx (bytes )
643
- if err != nil {
644
- return nil , err
645
- }
646
-
647
- tx := & UniqueTx {
648
- TxCachedState : & TxCachedState {
649
- Tx : rawTx ,
650
- },
651
- vm : vm ,
652
- txID : rawTx .ID (),
653
- }
654
- if err := tx .SyntacticVerify (); err != nil {
655
- return nil , err
656
- }
657
-
658
- if tx .Status () == choices .Unknown {
659
- vm .state .AddTx (tx .Tx )
660
- tx .setStatus (choices .Processing )
661
- return tx , vm .state .Commit ()
662
- }
663
-
664
- return tx , nil
665
- }
666
-
667
- func (vm * VM ) issueTx (tx snowstorm.Tx ) {
668
- vm .txs = append (vm .txs , tx )
669
- switch {
670
- case len (vm .txs ) == batchSize :
671
- vm .FlushTxs ()
672
- case len (vm .txs ) == 1 :
673
- vm .timer .SetTimeoutIn (vm .batchTimeout )
674
- }
675
- }
676
-
677
596
// LoadUser returns:
678
597
// 1) The UTXOs that reference one or more addresses controlled by the given user
679
598
// 2) A keychain that contains this user's keys
0 commit comments