forked from evmos/ethermint
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathexecutor.go
260 lines (220 loc) · 6.49 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package app
import (
"context"
"io"
"sync"
"sync/atomic"
"cosmossdk.io/collections"
"cosmossdk.io/log"
"cosmossdk.io/store/cachemulti"
storetypes "cosmossdk.io/store/types"
abci "github.com/cometbft/cometbft/abci/types"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
blockstm "github.com/crypto-org-chain/go-block-stm"
)
const MinimalParallelPreEstimate = 16
func DefaultTxExecutor(_ context.Context,
txs [][]byte,
ms storetypes.MultiStore,
deliverTxWithMultiStore func(int, sdk.Tx, storetypes.MultiStore, map[string]any) *abci.ExecTxResult,
) ([]*abci.ExecTxResult, error) {
blockSize := len(txs)
results := make([]*abci.ExecTxResult, blockSize)
for i := 0; i < blockSize; i++ {
results[i] = deliverTxWithMultiStore(i, nil, ms, nil)
}
return evmtypes.PatchTxResponses(results), nil
}
type evmKeeper interface {
GetParams(ctx sdk.Context) evmtypes.Params
}
func STMTxExecutor(
stores []storetypes.StoreKey,
workers int,
estimate bool,
evmKeeper evmKeeper,
txDecoder sdk.TxDecoder,
) baseapp.TxExecutor {
var authStore, bankStore int
index := make(map[storetypes.StoreKey]int, len(stores))
for i, k := range stores {
switch k.Name() {
case authtypes.StoreKey:
authStore = i
case banktypes.StoreKey:
bankStore = i
}
index[k] = i
}
return func(
ctx context.Context,
txs [][]byte,
ms storetypes.MultiStore,
deliverTxWithMultiStore func(int, sdk.Tx, storetypes.MultiStore, map[string]any) *abci.ExecTxResult,
) ([]*abci.ExecTxResult, error) {
blockSize := len(txs)
if blockSize == 0 {
return nil, nil
}
results := make([]*abci.ExecTxResult, blockSize)
incarnationCache := make([]atomic.Pointer[map[string]any], blockSize)
for i := 0; i < blockSize; i++ {
m := make(map[string]any)
incarnationCache[i].Store(&m)
}
var (
estimates []blockstm.MultiLocations
memTxs []sdk.Tx
)
if estimate {
// pre-estimation
evmDenom := evmKeeper.GetParams(sdk.NewContext(ms, cmtproto.Header{}, false, log.NewNopLogger())).EvmDenom
memTxs, estimates = preEstimates(txs, workers, authStore, bankStore, evmDenom, txDecoder)
}
if err := blockstm.ExecuteBlockWithEstimates(
ctx,
blockSize,
index,
stmMultiStoreWrapper{ms},
workers,
estimates,
func(txn blockstm.TxnIndex, ms blockstm.MultiStore) {
var cache map[string]any
// only one of the concurrent incarnations gets the cache if there are any, otherwise execute without
// cache, concurrent incarnations should be rare.
v := incarnationCache[txn].Swap(nil)
if v != nil {
cache = *v
}
var memTx sdk.Tx
if memTxs != nil {
memTx = memTxs[txn]
}
results[txn] = deliverTxWithMultiStore(int(txn), memTx, msWrapper{ms}, cache)
if v != nil {
incarnationCache[txn].Store(v)
}
},
); err != nil {
return nil, err
}
return evmtypes.PatchTxResponses(results), nil
}
}
type msWrapper struct {
blockstm.MultiStore
}
var _ storetypes.MultiStore = msWrapper{}
func (ms msWrapper) getCacheWrapper(key storetypes.StoreKey) storetypes.CacheWrapper {
return ms.GetStore(key)
}
func (ms msWrapper) GetStore(key storetypes.StoreKey) storetypes.Store {
return ms.MultiStore.GetStore(key)
}
func (ms msWrapper) GetKVStore(key storetypes.StoreKey) storetypes.KVStore {
return ms.MultiStore.GetKVStore(key)
}
func (ms msWrapper) GetObjKVStore(key storetypes.StoreKey) storetypes.ObjKVStore {
return ms.MultiStore.GetObjKVStore(key)
}
func (ms msWrapper) CacheMultiStore() storetypes.CacheMultiStore {
return cachemulti.NewFromParent(ms.getCacheWrapper, nil, nil)
}
// Implements CacheWrapper.
func (ms msWrapper) CacheWrap() storetypes.CacheWrap {
return ms.CacheMultiStore().(storetypes.CacheWrap)
}
func (ms msWrapper) CacheWrapWithTrace(_ io.Writer, _ storetypes.TraceContext) storetypes.CacheWrap {
return ms.CacheWrap()
}
// GetStoreType returns the type of the store.
func (ms msWrapper) GetStoreType() storetypes.StoreType {
return storetypes.StoreTypeMulti
}
// Implements interface MultiStore
func (ms msWrapper) SetTracer(io.Writer) storetypes.MultiStore {
return nil
}
// Implements interface MultiStore
func (ms msWrapper) SetTracingContext(storetypes.TraceContext) storetypes.MultiStore {
return nil
}
// Implements interface MultiStore
func (ms msWrapper) TracingEnabled() bool {
return false
}
type stmMultiStoreWrapper struct {
storetypes.MultiStore
}
var _ blockstm.MultiStore = stmMultiStoreWrapper{}
func (ms stmMultiStoreWrapper) GetStore(key storetypes.StoreKey) storetypes.Store {
return ms.MultiStore.GetStore(key)
}
func (ms stmMultiStoreWrapper) GetKVStore(key storetypes.StoreKey) storetypes.KVStore {
return ms.MultiStore.GetKVStore(key)
}
func (ms stmMultiStoreWrapper) GetObjKVStore(key storetypes.StoreKey) storetypes.ObjKVStore {
return ms.MultiStore.GetObjKVStore(key)
}
// preEstimates returns a static estimation of the written keys for each transaction.
// NOTE: make sure it sync with the latest sdk logic when sdk upgrade.
func preEstimates(txs [][]byte, workers, authStore, bankStore int, evmDenom string, txDecoder sdk.TxDecoder) ([]sdk.Tx, []blockstm.MultiLocations) {
memTxs := make([]sdk.Tx, len(txs))
estimates := make([]blockstm.MultiLocations, len(txs))
job := func(start, end int) {
for i := start; i < end; i++ {
rawTx := txs[i]
tx, err := txDecoder(rawTx)
if err != nil {
continue
}
memTxs[i] = tx
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
continue
}
feePayer := sdk.AccAddress(feeTx.FeePayer())
// account key
accKey, err := collections.EncodeKeyWithPrefix(
authtypes.AddressStoreKeyPrefix,
sdk.AccAddressKey,
feePayer,
)
if err != nil {
continue
}
// balance key
balanceKey, err := collections.EncodeKeyWithPrefix(
banktypes.BalancesPrefix,
collections.PairKeyCodec(sdk.AccAddressKey, collections.StringKey),
collections.Join(feePayer, evmDenom),
)
if err != nil {
continue
}
estimates[i] = blockstm.MultiLocations{
authStore: {accKey},
bankStore: {balanceKey},
}
}
}
blockSize := len(txs)
chunk := (blockSize + workers - 1) / workers
var wg sync.WaitGroup
for i := 0; i < blockSize; i += chunk {
start := i
end := min(i+chunk, blockSize)
wg.Add(1)
go func() {
defer wg.Done()
job(start, end)
}()
}
wg.Wait()
return memTxs, estimates
}