-
Notifications
You must be signed in to change notification settings - Fork 210
/
executor.go
241 lines (223 loc) · 6.55 KB
/
executor.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
package mesh
import (
"bytes"
"context"
"errors"
"fmt"
"sort"
"sync"
"time"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/datastore"
vm "github.com/spacemeshos/go-spacemesh/genvm"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/sql/layers"
"github.com/spacemeshos/go-spacemesh/sql/transactions"
"github.com/spacemeshos/go-spacemesh/txs"
)
var (
ErrLayerNotInOrder = errors.New("layers not applied in order")
ErrLayerApplied = errors.New("layer already applied")
)
type Executor struct {
logger log.Log
cdb *datastore.CachedDB
vm vmState
cs conservativeState
mu sync.Mutex
}
func NewExecutor(cdb *datastore.CachedDB, vm vmState, cs conservativeState, lg log.Log) *Executor {
return &Executor{
logger: lg,
cdb: cdb,
vm: vm,
cs: cs,
}
}
// Revert reverts the VM state and conservative cache to the given layer.
func (e *Executor) Revert(ctx context.Context, revertTo types.LayerID) error {
e.mu.Lock()
defer e.mu.Unlock()
logger := e.logger.WithContext(ctx).WithFields(log.Stringer("revert_to", revertTo))
if err := e.vm.Revert(revertTo); err != nil {
return fmt.Errorf("revert state: %w", err)
}
if err := e.cs.RevertCache(revertTo); err != nil {
return fmt.Errorf("revert cache: %w", err)
}
root, err := e.vm.GetStateRoot()
if err != nil {
return fmt.Errorf("get state hash: %w", err)
}
logger.Event().Info("reverted state", log.Stringer("state_hash", root))
return nil
}
// ExecuteOptimistic executes the specified transactions and returns a block that contains
// only successfully executed transactions.
func (e *Executor) ExecuteOptimistic(
ctx context.Context,
lid types.LayerID,
tickHeight uint64,
rewards []types.AnyReward,
tids []types.TransactionID,
) (*types.Block, error) {
e.mu.Lock()
defer e.mu.Unlock()
start := time.Now()
logger := e.logger.WithContext(ctx).WithFields(lid)
if err := e.checkOrder(lid); err != nil {
return nil, err
}
executable, err := e.getExecutableTxs(tids)
if err != nil {
return nil, err
}
crewards, err := e.convertRewards(rewards)
if err != nil {
return nil, err
}
ineffective, executed, err := e.vm.Apply(vm.ApplyContext{Layer: lid}, executable, crewards)
if err != nil {
return nil, fmt.Errorf("apply txs optimistically: %w", err)
}
b := &types.Block{
InnerBlock: types.InnerBlock{
LayerIndex: lid,
TickHeight: tickHeight,
Rewards: rewards,
},
}
for _, tx := range executed {
b.TxIDs = append(b.TxIDs, tx.ID)
}
b.Initialize()
updateResults(b.ID(), executed)
if err = e.cs.UpdateCache(ctx, lid, b.ID(), executed, ineffective); err != nil {
return nil, fmt.Errorf("update cache: %w", err)
}
state, err := e.vm.GetStateRoot()
if err != nil {
return nil, fmt.Errorf("get state hash: %w", err)
}
logger.Event().Info("optimistically executed block",
log.Stringer("block", b.ID()),
log.Stringer("state_hash", state),
log.Duration("duration", time.Since(start)),
log.Int("count", len(executed)),
log.Int("skipped", len(ineffective)),
log.Int("rewards", len(b.Rewards)),
)
return b, nil
}
// Execute transactions in the specified block and update the conservative cache.
func (e *Executor) Execute(ctx context.Context, lid types.LayerID, block *types.Block) error {
e.mu.Lock()
defer e.mu.Unlock()
start := time.Now()
if err := e.checkOrder(lid); err != nil {
return err
}
if block == nil {
return e.executeEmpty(ctx, lid)
}
logger := e.logger.WithContext(ctx).WithFields(lid, block.ID())
executable, err := e.getExecutableTxs(block.TxIDs)
if err != nil {
return err
}
rewards, err := e.convertRewards(block.Rewards)
if err != nil {
return err
}
ineffective, executed, err := e.vm.Apply(vm.ApplyContext{Layer: block.LayerIndex}, executable, rewards)
if err != nil {
return fmt.Errorf("apply block: %w", err)
}
updateResults(block.ID(), executed)
if err = e.cs.UpdateCache(ctx, block.LayerIndex, block.ID(), executed, ineffective); err != nil {
return fmt.Errorf("update cache: %w", err)
}
state, err := e.vm.GetStateRoot()
if err != nil {
return fmt.Errorf("get state hash: %w", err)
}
logger.Event().Info("executed block",
log.Stringer("block", block.ID()),
log.Stringer("state_hash", state),
log.Duration("duration", time.Since(start)),
log.Int("count", len(executed)),
)
return nil
}
func (e *Executor) convertRewards(rewards []types.AnyReward) ([]types.CoinbaseReward, error) {
res := make([]types.CoinbaseReward, 0, len(rewards))
for _, r := range rewards {
atx, err := e.cdb.GetAtxHeader(r.AtxID)
if err != nil {
return nil, fmt.Errorf("exec convert rewards: %w", err)
}
res = append(res, types.CoinbaseReward{
Coinbase: atx.Coinbase,
Weight: r.Weight,
})
}
sort.Slice(res, func(i, j int) bool {
return bytes.Compare(res[i].Coinbase.Bytes(), res[j].Coinbase.Bytes()) < 0
})
return res, nil
}
func (e *Executor) executeEmpty(ctx context.Context, lid types.LayerID) error {
start := time.Now()
logger := e.logger.WithContext(ctx).WithFields(lid)
if _, _, err := e.vm.Apply(vm.ApplyContext{Layer: lid}, nil, nil); err != nil {
return fmt.Errorf("apply empty layer: %w", err)
}
if err := e.cs.UpdateCache(ctx, lid, types.EmptyBlockID, nil, nil); err != nil {
return fmt.Errorf("update cache: %w", err)
}
state, err := e.vm.GetStateRoot()
if err != nil {
return fmt.Errorf("get state hash: %w", err)
}
logger.Event().Info("executed empty layer",
log.Stringer("state_hash", state),
log.Duration("duration", time.Since(start)),
)
return nil
}
func (e *Executor) checkOrder(lid types.LayerID) error {
inState, err := layers.GetLastApplied(e.cdb)
if err != nil {
return fmt.Errorf("executor get last applied: %w", err)
}
if !lid.After(inState) {
return fmt.Errorf("%w: %v", ErrLayerApplied, lid)
}
if lid != inState.Add(1) {
return fmt.Errorf("%w: %v, instate %v", ErrLayerNotInOrder, lid, inState)
}
return nil
}
func updateResults(bid types.BlockID, executed []types.TransactionWithResult) {
for _, tx := range executed {
tx.Block = bid
}
}
// getExecutableTxs retrieves a list of txs filtering transaction that were previously executed.
func (e *Executor) getExecutableTxs(ids []types.TransactionID) ([]types.Transaction, error) {
etxs := make([]types.Transaction, 0, len(ids))
for _, tid := range ids {
mtx, err := transactions.Get(e.cdb, tid)
if err != nil {
return nil, fmt.Errorf("executor get tx: %w", err)
}
if mtx.State == types.APPLIED {
continue
}
if mtx.TxHeader == nil {
txs.RawTxCount.WithLabelValues(txs.RawFromDB).Inc()
}
etxs = append(etxs, mtx.Transaction)
}
return etxs, nil
}