|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + |
| 7 | + "cosmossdk.io/store/cachekv" |
| 8 | + "cosmossdk.io/store/cachemulti" |
| 9 | + "cosmossdk.io/store/tracekv" |
| 10 | + storetypes "cosmossdk.io/store/types" |
| 11 | + abci "github.com/cometbft/cometbft/abci/types" |
| 12 | + "github.com/cosmos/cosmos-sdk/baseapp" |
| 13 | + evmtypes "github.com/evmos/ethermint/x/evm/types" |
| 14 | + block_stm "github.com/yihuang/go-block-stm" |
| 15 | +) |
| 16 | + |
| 17 | +func DefaultTxExecutor(ctx context.Context, |
| 18 | + blockSize int, |
| 19 | + ms storetypes.MultiStore, |
| 20 | + deliverTxWithMultiStore func(int, storetypes.MultiStore) *abci.ExecTxResult, |
| 21 | +) ([]*abci.ExecTxResult, error) { |
| 22 | + results := make([]*abci.ExecTxResult, blockSize) |
| 23 | + for i := 0; i < blockSize; i++ { |
| 24 | + results[i] = deliverTxWithMultiStore(i, ms) |
| 25 | + } |
| 26 | + return evmtypes.PatchTxResponses(results), nil |
| 27 | +} |
| 28 | + |
| 29 | +func STMTxExecutor(stores []storetypes.StoreKey, workers int) baseapp.TxExecutor { |
| 30 | + return func( |
| 31 | + ctx context.Context, |
| 32 | + blockSize int, |
| 33 | + ms storetypes.MultiStore, |
| 34 | + deliverTxWithMultiStore func(int, storetypes.MultiStore) *abci.ExecTxResult, |
| 35 | + ) ([]*abci.ExecTxResult, error) { |
| 36 | + if blockSize == 0 { |
| 37 | + return nil, nil |
| 38 | + } |
| 39 | + results := make([]*abci.ExecTxResult, blockSize) |
| 40 | + if err := block_stm.ExecuteBlock( |
| 41 | + ctx, |
| 42 | + blockSize, |
| 43 | + stores, |
| 44 | + stmMultiStoreWrapper{ms}, |
| 45 | + workers, |
| 46 | + func(txn block_stm.TxnIndex, ms block_stm.MultiStore) { |
| 47 | + result := deliverTxWithMultiStore(int(txn), newMultiStoreWrapper(ms, stores)) |
| 48 | + results[txn] = result |
| 49 | + }, |
| 50 | + ); err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + |
| 54 | + return evmtypes.PatchTxResponses(results), nil |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +type storeWrapper struct { |
| 59 | + block_stm.KVStore |
| 60 | +} |
| 61 | + |
| 62 | +var ( |
| 63 | + _ storetypes.Store = storeWrapper{} |
| 64 | + _ storetypes.KVStore = storeWrapper{} |
| 65 | +) |
| 66 | + |
| 67 | +func (s storeWrapper) GetStoreType() storetypes.StoreType { |
| 68 | + return storetypes.StoreTypeIAVL |
| 69 | +} |
| 70 | + |
| 71 | +func (s storeWrapper) CacheWrap() storetypes.CacheWrap { |
| 72 | + return cachekv.NewStore(storetypes.KVStore(s)) |
| 73 | +} |
| 74 | + |
| 75 | +func (s storeWrapper) CacheWrapWithTrace(w io.Writer, tc storetypes.TraceContext) storetypes.CacheWrap { |
| 76 | + return cachekv.NewStore(tracekv.NewStore(s, w, tc)) |
| 77 | +} |
| 78 | + |
| 79 | +type msWrapper struct { |
| 80 | + block_stm.MultiStore |
| 81 | + stores []storetypes.StoreKey |
| 82 | + keysByName map[string]storetypes.StoreKey |
| 83 | +} |
| 84 | + |
| 85 | +var _ storetypes.MultiStore = msWrapper{} |
| 86 | + |
| 87 | +func newMultiStoreWrapper(ms block_stm.MultiStore, stores []storetypes.StoreKey) msWrapper { |
| 88 | + keysByName := make(map[string]storetypes.StoreKey) |
| 89 | + for _, k := range stores { |
| 90 | + keysByName[k.Name()] = k |
| 91 | + } |
| 92 | + return msWrapper{ms, stores, keysByName} |
| 93 | +} |
| 94 | + |
| 95 | +func (ms msWrapper) GetStore(key storetypes.StoreKey) storetypes.Store { |
| 96 | + return storetypes.Store(ms.GetKVStore(key)) |
| 97 | +} |
| 98 | + |
| 99 | +func (ms msWrapper) GetKVStore(key storetypes.StoreKey) storetypes.KVStore { |
| 100 | + return storeWrapper{ms.MultiStore.GetKVStore(key)} |
| 101 | +} |
| 102 | + |
| 103 | +func (ms msWrapper) CacheMultiStore() storetypes.CacheMultiStore { |
| 104 | + stores := make(map[storetypes.StoreKey]storetypes.CacheWrapper) |
| 105 | + for _, k := range ms.stores { |
| 106 | + store := ms.GetKVStore(k) |
| 107 | + stores[k] = store |
| 108 | + } |
| 109 | + return cachemulti.NewStore(nil, stores, ms.keysByName, nil, nil) |
| 110 | +} |
| 111 | + |
| 112 | +func (ms msWrapper) CacheMultiStoreWithVersion(_ int64) (storetypes.CacheMultiStore, error) { |
| 113 | + panic("cannot branch cached multi-store with a version") |
| 114 | +} |
| 115 | + |
| 116 | +// Implements CacheWrapper. |
| 117 | +func (ms msWrapper) CacheWrap() storetypes.CacheWrap { |
| 118 | + return ms.CacheMultiStore().(storetypes.CacheWrap) |
| 119 | +} |
| 120 | + |
| 121 | +// CacheWrapWithTrace implements the CacheWrapper interface. |
| 122 | +func (ms msWrapper) CacheWrapWithTrace(_ io.Writer, _ storetypes.TraceContext) storetypes.CacheWrap { |
| 123 | + return ms.CacheWrap() |
| 124 | +} |
| 125 | + |
| 126 | +// GetStoreType returns the type of the store. |
| 127 | +func (ms msWrapper) GetStoreType() storetypes.StoreType { |
| 128 | + return storetypes.StoreTypeMulti |
| 129 | +} |
| 130 | + |
| 131 | +// LatestVersion returns the branch version of the store |
| 132 | +func (ms msWrapper) LatestVersion() int64 { |
| 133 | + panic("cannot get latest version from branch cached multi-store") |
| 134 | +} |
| 135 | + |
| 136 | +// Implements interface MultiStore |
| 137 | +func (ms msWrapper) SetTracer(w io.Writer) storetypes.MultiStore { |
| 138 | + return nil |
| 139 | +} |
| 140 | + |
| 141 | +// Implements interface MultiStore |
| 142 | +func (ms msWrapper) SetTracingContext(storetypes.TraceContext) storetypes.MultiStore { |
| 143 | + return nil |
| 144 | +} |
| 145 | + |
| 146 | +// Implements interface MultiStore |
| 147 | +func (ms msWrapper) TracingEnabled() bool { |
| 148 | + return false |
| 149 | +} |
| 150 | + |
| 151 | +type stmMultiStoreWrapper struct { |
| 152 | + inner storetypes.MultiStore |
| 153 | +} |
| 154 | + |
| 155 | +var _ block_stm.MultiStore = stmMultiStoreWrapper{} |
| 156 | + |
| 157 | +func (ms stmMultiStoreWrapper) GetKVStore(key storetypes.StoreKey) block_stm.KVStore { |
| 158 | + return block_stm.KVStore(ms.inner.GetKVStore(key)) |
| 159 | +} |
0 commit comments