/
abci.go
278 lines (254 loc) · 12.5 KB
/
abci.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package contract
import (
"context"
"fmt"
"sync"
"time"
"github.com/sei-protocol/sei-chain/utils/logging"
"github.com/sei-protocol/sei-chain/utils/tracing"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sei-protocol/sei-chain/store/whitelist/multi"
seisync "github.com/sei-protocol/sei-chain/sync"
"github.com/sei-protocol/sei-chain/utils/datastructures"
dexcache "github.com/sei-protocol/sei-chain/x/dex/cache"
"github.com/sei-protocol/sei-chain/x/dex/keeper"
dexkeeperabci "github.com/sei-protocol/sei-chain/x/dex/keeper/abci"
dexkeeperutils "github.com/sei-protocol/sei-chain/x/dex/keeper/utils"
"github.com/sei-protocol/sei-chain/x/dex/types"
dextypesutils "github.com/sei-protocol/sei-chain/x/dex/types/utils"
dextypeswasm "github.com/sei-protocol/sei-chain/x/dex/types/wasm"
dexutils "github.com/sei-protocol/sei-chain/x/dex/utils"
"github.com/sei-protocol/sei-chain/x/store"
otrace "go.opentelemetry.io/otel/trace"
)
const LogRunnerRunAfter = 10 * time.Second
const LogExecSigSendAfter = 2 * time.Second
type environment struct {
validContractsInfo []types.ContractInfoV2
failedContractAddresses datastructures.SyncSet[string]
finalizeBlockMessages *datastructures.TypedSyncMap[string, *dextypeswasm.SudoFinalizeBlockMsg]
settlementsByContract *datastructures.TypedSyncMap[string, []*types.SettlementEntry]
executionTerminationSignals *datastructures.TypedSyncMap[string, chan struct{}]
registeredPairs *datastructures.TypedSyncMap[string, []types.Pair]
orderBooks *datastructures.TypedNestedSyncMap[string, dextypesutils.PairString, *types.OrderBook]
finalizeMsgMutex *sync.Mutex
eventManagerMutex *sync.Mutex
}
func EndBlockerAtomic(ctx sdk.Context, keeper *keeper.Keeper, validContractsInfo []types.ContractInfoV2, tracingInfo *tracing.Info) ([]types.ContractInfoV2, sdk.Context, bool) {
tracer := tracingInfo.Tracer
spanCtx, span := (*tracer).Start(tracingInfo.TracerContext, "DexEndBlockerAtomic")
defer span.End()
env := newEnv(ctx, validContractsInfo, keeper)
cachedCtx, msCached := cacheContext(ctx, env)
memStateCopy := dexutils.GetMemState(cachedCtx.Context()).DeepCopy()
handleDeposits(cachedCtx, env, keeper, tracer)
runner := NewParallelRunner(func(contract types.ContractInfoV2) {
orderMatchingRunnable(spanCtx, cachedCtx, env, keeper, contract, tracer)
}, validContractsInfo, cachedCtx)
_, err := logging.LogIfNotDoneAfter(ctx.Logger(), func() (struct{}, error) {
runner.Run()
return struct{}{}, nil
}, LogRunnerRunAfter, "runner run")
if err != nil {
// this should never happen
panic(err)
}
handleSettlements(spanCtx, cachedCtx, env, keeper, tracer)
handleUnfulfilledMarketOrders(spanCtx, cachedCtx, env, keeper, tracer)
handleFinalizedBlocks(spanCtx, cachedCtx, env, keeper, tracer)
// No error is thrown for any contract. This should happen most of the time.
if env.failedContractAddresses.Size() == 0 {
msCached.Write()
return env.validContractsInfo, ctx, true
}
// persistent contract rent charges for failed contracts and discard everything else
for _, failedContractAddress := range env.failedContractAddresses.ToOrderedSlice(datastructures.StringComparator) {
cachedContract, err := keeper.GetContract(cachedCtx, failedContractAddress)
if err != nil {
ctx.Logger().Error(fmt.Sprintf("error %s when getting updated contract %s to persist rent balance", err, failedContractAddress))
continue
}
contract, err := keeper.GetContract(ctx, failedContractAddress)
if err != nil {
ctx.Logger().Error(fmt.Sprintf("error %s when getting contract %s to persist rent balance", err, failedContractAddress))
continue
}
contract.RentBalance = cachedContract.RentBalance
err = keeper.SetContract(ctx, &contract)
if err != nil {
ctx.Logger().Error(fmt.Sprintf("error %s when persisting contract %s's rent balance", err, failedContractAddress))
continue
}
}
// restore keeper in-memory state
newGoContext := context.WithValue(ctx.Context(), dexutils.DexMemStateContextKey, memStateCopy)
return filterNewValidContracts(ctx, env), ctx.WithContext(newGoContext), false
}
func newEnv(ctx sdk.Context, validContractsInfo []types.ContractInfoV2, keeper *keeper.Keeper) *environment {
finalizeBlockMessages := datastructures.NewTypedSyncMap[string, *dextypeswasm.SudoFinalizeBlockMsg]()
settlementsByContract := datastructures.NewTypedSyncMap[string, []*types.SettlementEntry]()
executionTerminationSignals := datastructures.NewTypedSyncMap[string, chan struct{}]()
registeredPairs := datastructures.NewTypedSyncMap[string, []types.Pair]()
orderBooks := datastructures.NewTypedNestedSyncMap[string, dextypesutils.PairString, *types.OrderBook]()
for _, contract := range validContractsInfo {
finalizeBlockMessages.Store(contract.ContractAddr, dextypeswasm.NewSudoFinalizeBlockMsg())
settlementsByContract.Store(contract.ContractAddr, []*types.SettlementEntry{})
executionTerminationSignals.Store(contract.ContractAddr, make(chan struct{}, 1))
contractPairs := keeper.GetAllRegisteredPairs(ctx, contract.ContractAddr)
registeredPairs.Store(contract.ContractAddr, contractPairs)
for _, pair := range contractPairs {
pair := pair
orderBooks.StoreNested(contract.ContractAddr, dextypesutils.GetPairString(&pair), dexkeeperutils.PopulateOrderbook(
ctx, keeper, dextypesutils.ContractAddress(contract.ContractAddr), pair,
))
}
}
return &environment{
validContractsInfo: validContractsInfo,
failedContractAddresses: datastructures.NewSyncSet([]string{}),
finalizeBlockMessages: finalizeBlockMessages,
settlementsByContract: settlementsByContract,
executionTerminationSignals: executionTerminationSignals,
registeredPairs: registeredPairs,
orderBooks: orderBooks,
finalizeMsgMutex: &sync.Mutex{},
eventManagerMutex: &sync.Mutex{},
}
}
func cacheContext(ctx sdk.Context, env *environment) (sdk.Context, sdk.CacheMultiStore) {
cachedCtx, msCached := store.GetCachedContext(ctx)
goCtx := context.WithValue(cachedCtx.Context(), dexcache.CtxKeyExecTermSignal, env.executionTerminationSignals)
cachedCtx = cachedCtx.WithContext(goCtx)
return cachedCtx, msCached
}
func decorateContextForContract(ctx sdk.Context, contractInfo types.ContractInfoV2, gasLimit uint64) sdk.Context {
goCtx := context.WithValue(ctx.Context(), dexcache.CtxKeyExecutingContract, contractInfo)
whitelistedStore := multi.NewStore(ctx.MultiStore(), GetWhitelistMap(contractInfo.ContractAddr))
newEventManager := sdk.NewEventManager()
return ctx.WithContext(goCtx).WithMultiStore(whitelistedStore).WithEventManager(newEventManager).WithGasMeter(
seisync.NewGasWrapper(dexutils.GetGasMeterForLimit(gasLimit)),
)
}
func handleDeposits(ctx sdk.Context, env *environment, keeper *keeper.Keeper, tracer *otrace.Tracer) {
// Handle deposit sequentially since they mutate `bank` state which is shared by all contracts
keeperWrapper := dexkeeperabci.KeeperWrapper{Keeper: keeper}
for _, contract := range env.validContractsInfo {
if !contract.NeedOrderMatching {
continue
}
if err := keeperWrapper.HandleEBDeposit(ctx.Context(), ctx, tracer, contract.ContractAddr); err != nil {
env.failedContractAddresses.Add(contract.ContractAddr)
}
}
}
func handleSettlements(ctx context.Context, sdkCtx sdk.Context, env *environment, keeper *keeper.Keeper, tracer *otrace.Tracer) {
_, span := (*tracer).Start(ctx, "DexEndBlockerHandleSettlements")
defer span.End()
contractsNeedOrderMatching := datastructures.NewSyncSet([]string{})
for _, contract := range env.validContractsInfo {
if contract.NeedOrderMatching {
contractsNeedOrderMatching.Add(contract.ContractAddr)
}
}
env.settlementsByContract.Range(func(contractAddr string, settlements []*types.SettlementEntry) bool {
if !contractsNeedOrderMatching.Contains(contractAddr) {
return true
}
if err := HandleSettlements(sdkCtx, contractAddr, keeper, settlements); err != nil {
sdkCtx.Logger().Error(fmt.Sprintf("Error handling settlements for %s", contractAddr))
env.failedContractAddresses.Add(contractAddr)
}
return true
})
}
func handleUnfulfilledMarketOrders(ctx context.Context, sdkCtx sdk.Context, env *environment, keeper *keeper.Keeper, tracer *otrace.Tracer) {
// Cancel unfilled market orders
for _, contract := range env.validContractsInfo {
if contract.NeedOrderMatching {
registeredPairs, found := env.registeredPairs.Load(contract.ContractAddr)
if !found {
continue
}
if err := CancelUnfulfilledMarketOrders(ctx, sdkCtx, contract.ContractAddr, keeper, registeredPairs, tracer); err != nil {
sdkCtx.Logger().Error(fmt.Sprintf("Error cancelling unfulfilled market orders for %s", contract.ContractAddr))
env.failedContractAddresses.Add(contract.ContractAddr)
}
}
}
}
func handleFinalizedBlocks(ctx context.Context, sdkCtx sdk.Context, env *environment, keeper *keeper.Keeper, tracer *otrace.Tracer) {
_, span := (*tracer).Start(ctx, "DexEndBlockerHandleFinalizedBlocks")
defer span.End()
contractsNeedHook := datastructures.NewSyncSet([]string{})
for _, contract := range env.validContractsInfo {
if contract.NeedHook {
contractsNeedHook.Add(contract.ContractAddr)
}
}
env.finalizeBlockMessages.Range(func(contractAddr string, finalizeBlockMsg *dextypeswasm.SudoFinalizeBlockMsg) bool {
if !contractsNeedHook.Contains(contractAddr) {
return true
}
if _, err := dexkeeperutils.CallContractSudo(sdkCtx, keeper, contractAddr, finalizeBlockMsg, dexutils.ZeroUserProvidedGas); err != nil {
sdkCtx.Logger().Error(fmt.Sprintf("Error calling FinalizeBlock of %s", contractAddr))
env.failedContractAddresses.Add(contractAddr)
}
return true
})
}
func orderMatchingRunnable(ctx context.Context, sdkContext sdk.Context, env *environment, keeper *keeper.Keeper, contractInfo types.ContractInfoV2, tracer *otrace.Tracer) {
defer func() {
if channel, ok := env.executionTerminationSignals.Load(contractInfo.ContractAddr); ok {
_, err := logging.LogIfNotDoneAfter(sdkContext.Logger(), func() (struct{}, error) {
channel <- struct{}{}
return struct{}{}, nil
}, LogExecSigSendAfter, fmt.Sprintf("send execution terminal signal for %s", contractInfo.ContractAddr))
if err != nil {
// this should never happen
panic(err)
}
}
}()
if !contractInfo.NeedOrderMatching {
return
}
parentSdkContext := sdkContext
sdkContext = decorateContextForContract(sdkContext, contractInfo, keeper.GetParams(sdkContext).EndBlockGasLimit)
sdkContext.Logger().Debug(fmt.Sprintf("End block for %s with balance of %d", contractInfo.ContractAddr, contractInfo.RentBalance))
pairs, pairFound := env.registeredPairs.Load(contractInfo.ContractAddr)
orderBooks, found := env.orderBooks.Load(contractInfo.ContractAddr)
if !pairFound || !found {
sdkContext.Logger().Error(fmt.Sprintf("No pair or order book for %s", contractInfo.ContractAddr))
env.failedContractAddresses.Add(contractInfo.ContractAddr)
} else if orderResultsMap, settlements, err := HandleExecutionForContract(ctx, sdkContext, contractInfo, keeper, pairs, orderBooks, tracer); err != nil {
sdkContext.Logger().Error(fmt.Sprintf("Error for EndBlock of %s", contractInfo.ContractAddr))
env.failedContractAddresses.Add(contractInfo.ContractAddr)
} else {
for account, orderResults := range orderResultsMap {
// only add to finalize message for contract addresses
if msg, ok := env.finalizeBlockMessages.Load(account); ok {
// ordering of `AddContractResult` among multiple orderMatchingRunnable instances doesn't matter
// since it's not persisted as state, and it's only used for invoking registered contracts'
// FinalizeBlock sudo endpoints, whose state updates are gated by whitelist stores anyway.
msg.AddContractResult(orderResults, env.finalizeMsgMutex)
}
}
env.settlementsByContract.Store(contractInfo.ContractAddr, settlements)
}
// ordering of events doesn't matter since events aren't part of consensus
env.eventManagerMutex.Lock()
defer env.eventManagerMutex.Unlock()
parentSdkContext.EventManager().EmitEvents(sdkContext.EventManager().Events())
}
func filterNewValidContracts(ctx sdk.Context, env *environment) []types.ContractInfoV2 {
newValidContracts := []types.ContractInfoV2{}
for _, contract := range env.validContractsInfo {
if !env.failedContractAddresses.Contains(contract.ContractAddr) {
newValidContracts = append(newValidContracts, contract)
}
}
for _, failedContractAddress := range env.failedContractAddresses.ToOrderedSlice(datastructures.StringComparator) {
dexutils.GetMemState(ctx.Context()).DeepFilterAccount(ctx, failedContractAddress)
}
return newValidContracts
}