-
Notifications
You must be signed in to change notification settings - Fork 670
/
manager.go
162 lines (136 loc) · 4.01 KB
/
manager.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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package executor
import (
"errors"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/vms/platformvm/block"
"github.com/ava-labs/avalanchego/vms/platformvm/metrics"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/executor"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool"
"github.com/ava-labs/avalanchego/vms/platformvm/validators"
)
var (
_ Manager = (*manager)(nil)
ErrChainNotSynced = errors.New("chain not synced")
)
type Manager interface {
state.Versions
// Returns the ID of the most recently accepted block.
LastAccepted() ids.ID
SetPreference(blkID ids.ID) (updated bool)
Preferred() ids.ID
GetBlock(blkID ids.ID) (snowman.Block, error)
GetStatelessBlock(blkID ids.ID) (block.Block, error)
NewBlock(block.Block) snowman.Block
// VerifyTx verifies that the transaction can be issued based on the currently
// preferred state. This should *not* be used to verify transactions in a block.
VerifyTx(tx *txs.Tx) error
// VerifyUniqueInputs verifies that the inputs are not duplicated in the
// provided blk or any of its ancestors pinned in memory.
VerifyUniqueInputs(blkID ids.ID, inputs set.Set[ids.ID]) error
}
func NewManager(
mempool mempool.Mempool,
metrics metrics.Metrics,
s state.State,
txExecutorBackend *executor.Backend,
validatorManager validators.Manager,
) Manager {
lastAccepted := s.GetLastAccepted()
backend := &backend{
Mempool: mempool,
lastAccepted: lastAccepted,
state: s,
ctx: txExecutorBackend.Ctx,
blkIDToState: map[ids.ID]*blockState{},
}
return &manager{
backend: backend,
verifier: &verifier{
backend: backend,
txExecutorBackend: txExecutorBackend,
},
acceptor: &acceptor{
backend: backend,
metrics: metrics,
validators: validatorManager,
bootstrapped: txExecutorBackend.Bootstrapped,
},
rejector: &rejector{
backend: backend,
addTxsToMempool: !txExecutorBackend.Config.PartialSyncPrimaryNetwork,
},
preferred: lastAccepted,
txExecutorBackend: txExecutorBackend,
}
}
type manager struct {
*backend
verifier block.Visitor
acceptor block.Visitor
rejector block.Visitor
preferred ids.ID
txExecutorBackend *executor.Backend
}
func (m *manager) GetBlock(blkID ids.ID) (snowman.Block, error) {
blk, err := m.backend.GetBlock(blkID)
if err != nil {
return nil, err
}
return m.NewBlock(blk), nil
}
func (m *manager) GetStatelessBlock(blkID ids.ID) (block.Block, error) {
return m.backend.GetBlock(blkID)
}
func (m *manager) NewBlock(blk block.Block) snowman.Block {
return &Block{
manager: m,
Block: blk,
}
}
func (m *manager) SetPreference(blkID ids.ID) bool {
updated := m.preferred != blkID
m.preferred = blkID
return updated
}
func (m *manager) Preferred() ids.ID {
return m.preferred
}
func (m *manager) VerifyTx(tx *txs.Tx) error {
if !m.txExecutorBackend.Bootstrapped.Get() {
return ErrChainNotSynced
}
stateDiff, err := state.NewDiff(m.preferred, m)
if err != nil {
return err
}
nextBlkTime, _, err := executor.NextBlockTime(stateDiff, m.txExecutorBackend.Clk)
if err != nil {
return err
}
_, err = executor.AdvanceTimeTo(m.txExecutorBackend, stateDiff, nextBlkTime)
if err != nil {
return err
}
err = tx.Unsigned.Visit(&executor.StandardTxExecutor{
Backend: m.txExecutorBackend,
State: stateDiff,
Tx: tx,
})
// We ignore [errFutureStakeTime] here because the time will be advanced
// when this transaction is issued.
//
// TODO: Remove this check post-Durango.
if errors.Is(err, executor.ErrFutureStakeTime) {
return nil
}
return err
}
func (m *manager) VerifyUniqueInputs(blkID ids.ID, inputs set.Set[ids.ID]) error {
return m.backend.verifyUniqueInputs(blkID, inputs)
}