Skip to content

wip #2162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: v6.0.5-hotfix-5-oracle-fix-idx-fix
Choose a base branch
from
Open

wip #2162

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1437,19 +1437,7 @@ func (app *App) PartitionPrioritizedTxs(_ sdk.Context, txs [][]byte, typedTxs []
continue
}

prioritized := false
// if all messages are prioritized, we want to add to prioritizedTxs
msgLoop:
for _, msg := range typedTxs[idx].GetMsgs() {
switch msg.(type) {
case *oracletypes.MsgAggregateExchangeRateVote:
prioritized = true
default:
prioritized = false
break msgLoop
}
}
if prioritized {
if utils.IsTxPrioritized(typedTxs[idx]) {
prioritizedTxs = append(prioritizedTxs, tx)
prioritizedTypedTxs = append(prioritizedTypedTxs, typedTxs[idx])
prioritizedIndices = append(prioritizedIndices, idx)
Expand Down Expand Up @@ -1854,14 +1842,14 @@ func (app *App) RegisterTxService(clientCtx client.Context) {

func (app *App) RPCContextProvider(i int64) sdk.Context {
if i == evmrpc.LatestCtxHeight {
return app.GetCheckCtx().WithIsTracing(true)
return app.GetCheckCtx().WithIsTracing(true).WithIsCheckTx(false)
}
ctx, err := app.CreateQueryContext(i, false)
if err != nil {
app.Logger().Error(fmt.Sprintf("failed to create query context for EVM; using latest context instead: %v+", err.Error()))
return app.GetCheckCtx().WithIsTracing(true)
return app.GetCheckCtx().WithIsTracing(true).WithIsCheckTx(false)
}
return ctx.WithIsEVM(true).WithIsTracing(true)
return ctx.WithIsEVM(true).WithIsTracing(true).WithIsCheckTx(false)
}

// RegisterTendermintService implements the Application.RegisterTendermintService method.
Expand Down
1 change: 1 addition & 0 deletions evmrpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ func (a *BlockAPI) GetBlockReceipts(ctx context.Context, blockNrOrHash rpc.Block
wg.Add(1)
go func(i int, hash common.Hash) {
defer wg.Done()
defer recoverAndLog()
receipt, err := a.keeper.GetReceipt(a.ctxProvider(height), hash)
if err != nil {
// When the transaction doesn't exist, skip it
Expand Down
37 changes: 29 additions & 8 deletions evmrpc/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
func (a *FilterAPI) timeoutLoop(timeout time.Duration) {
ticker := time.NewTicker(timeout)
defer ticker.Stop()
defer recoverAndLog()
for {
<-ticker.C
a.filtersMu.Lock()
Expand Down Expand Up @@ -306,6 +307,7 @@
}
close(runner.Queue)
go func() {
defer recoverAndLog()
runner.Done.Wait()
close(resultsChan)
}()
Expand All @@ -321,8 +323,8 @@
})

// Apply rate limit
if applyOpenEndedLogLimit && int64(len(res)) >= f.filterConfig.maxLog {
res = res[:int(f.filterConfig.maxLog)]
if applyOpenEndedLogLimit && f.filterConfig.maxLog > 0 && int64(len(res)) > f.filterConfig.maxLog {
return nil, 0, fmt.Errorf("requested range has %d logs which is more than the maximum of %d", len(res), f.filterConfig.maxLog)
}

return res, end, err
Expand Down Expand Up @@ -361,7 +363,7 @@
res = append(res, logs...)
}
totalLogs += uint(len(receipt.Logs))
txCount++
txCount++
}
return
}
Expand Down Expand Up @@ -405,20 +407,22 @@
begin = lastToHeight
}
if !applyOpenEndedLogLimit && f.filterConfig.maxBlock > 0 && end >= (begin+f.filterConfig.maxBlock) {
end = begin + f.filterConfig.maxBlock - 1
return nil, 0, false, fmt.Errorf("a maximum of %d blocks worth of logs may be requested at a time", f.filterConfig.maxBlock)
}
// begin should always be <= end block at this point
if begin > end {
return nil, 0, false, fmt.Errorf("fromBlock %d is after toBlock %d", begin, end)
}
res := make(chan *coretypes.ResultBlock, end-begin+1)
defer close(res)
errChan := make(chan error, 1)
runner := NewParallelRunner(MaxNumOfWorkers, int(end-begin+1))
defer runner.Done.Wait()
defer close(runner.Queue)
var wg sync.WaitGroup

for height := begin; height <= end; height++ {
h := height
wg.Add(1)
runner.Queue <- func() {
defer wg.Done()
if h == 0 {
return
}
Expand All @@ -431,11 +435,28 @@
}
block, err := blockByNumberWithRetry(ctx, f.tmClient, &h, 1)
if err != nil {
panic(err)
select {
case errChan <- fmt.Errorf("failed to fetch block at height %d: %w", h, err):
default:
}
return
}
res <- block
}
}
close(runner.Queue)
go func() {
defer recoverAndLog()
wg.Wait()
close(res)
close(errChan)
}()
Comment on lines +448 to +453

Check notice

Code scanning / CodeQL

Spawning a Go routine Note

Spawning a Go routine may be a possible source of non-determinism

// block until either an error arrives or errChan is closed (i.e. all done)
if err, ok := <-errChan; ok {
return nil, 0, false, err
}

return res, end, applyOpenEndedLogLimit, nil
}

Expand Down
57 changes: 49 additions & 8 deletions evmrpc/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/eth/tracers"
"github.com/ethereum/go-ethereum/eth/tracers/tracersutils"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/lib/ethapi"
"github.com/ethereum/go-ethereum/params"
Expand Down Expand Up @@ -252,23 +253,37 @@ func (b Backend) ConvertBlockNumber(bn rpc.BlockNumber) int64 {
return blockNum
}

func (b Backend) BlockByNumber(ctx context.Context, bn rpc.BlockNumber) (*ethtypes.Block, error) {
func (b Backend) BlockByNumber(ctx context.Context, bn rpc.BlockNumber) (*ethtypes.Block, []tracersutils.TraceBlockMetadata, error) {
blockNum := b.ConvertBlockNumber(bn)
tmBlock, err := blockByNumber(ctx, b.tmClient, &blockNum)
if err != nil {
return nil, err
return nil, nil, err
}
blockRes, err := b.tmClient.BlockResults(ctx, &tmBlock.Block.Height)
if err != nil {
return nil, err
return nil, nil, err
}
sdkCtx := b.ctxProvider(LatestCtxHeight)
var txs []*ethtypes.Transaction
var metadata []tracersutils.TraceBlockMetadata
for i := range blockRes.TxsResults {
decoded, err := b.txConfig.TxDecoder()(tmBlock.Block.Txs[i])
if err != nil {
return nil, err
return nil, nil, err
}
isPrioritized := utils.IsTxPrioritized(decoded)
// if isPrioritized {
// continue
// }
shouldRunMidBlock := false
if isPrioritized && (i+1 < len(blockRes.TxsResults)) {
nextDecoded, err := b.txConfig.TxDecoder()(tmBlock.Block.Txs[i+1])
if err != nil {
return nil, nil, err
}
shouldRunMidBlock = !utils.IsTxPrioritized(nextDecoded)
}
shouldTrace := false
for _, msg := range decoded.GetMsgs() {
switch m := msg.(type) {
case *types.MsgEVMTransaction:
Expand All @@ -280,23 +295,41 @@ func (b Backend) BlockByNumber(ctx context.Context, bn rpc.BlockNumber) (*ethtyp
if err != nil || receipt.BlockNumber != uint64(tmBlock.Block.Height) || isReceiptFromAnteError(receipt) {
continue
}
shouldTrace = true
metadata = append(metadata, tracersutils.TraceBlockMetadata{
ShouldIncludeInTraceResult: true,
IdxInEthBlock: len(txs),
})
txs = append(txs, ethtx)
}
}
if !shouldTrace {
metadata = append(metadata, tracersutils.TraceBlockMetadata{
ShouldIncludeInTraceResult: false,
IdxInEthBlock: -1,
TraceRunnable: func(sd vm.StateDB) {
typedStateDB := sd.(*state.DBImpl)
_ = b.app.DeliverTx(typedStateDB.Ctx(), abci.RequestDeliverTx{}, decoded, sha256.Sum256(tmBlock.Block.Txs[i]))
if shouldRunMidBlock {
_ = b.app.MidBlock(typedStateDB.Ctx(), blockNum)
}
},
})
}
}
header := b.getHeader(big.NewInt(blockNum))
block := &ethtypes.Block{
Header_: header,
Txs: txs,
}
block.OverwriteHash(common.BytesToHash(tmBlock.BlockID.Hash))
return block, nil
return block, metadata, nil
}

func (b Backend) BlockByHash(ctx context.Context, hash common.Hash) (*ethtypes.Block, error) {
func (b Backend) BlockByHash(ctx context.Context, hash common.Hash) (*ethtypes.Block, []tracersutils.TraceBlockMetadata, error) {
tmBlock, err := blockByHash(ctx, b.tmClient, hash.Bytes())
if err != nil {
return nil, err
return nil, nil, err
}
blockNumber := rpc.BlockNumber(tmBlock.Block.Height)
return b.BlockByNumber(ctx, blockNumber)
Expand Down Expand Up @@ -346,6 +379,9 @@ func (b *Backend) StateAtTransaction(ctx context.Context, block *ethtypes.Block,
if err != nil {
panic(err)
}
if utils.IsTxPrioritized(sdkTx) {
continue
}
if idx == txIndex {
var evmMsg *types.MsgEVMTransaction
if msgs := sdkTx.GetMsgs(); len(msgs) != 1 {
Expand Down Expand Up @@ -390,6 +426,11 @@ func (b *Backend) initializeBlock(ctx context.Context, block *ethtypes.Block) (s
reqBeginBlock.Simulate = true
sdkCtx := b.ctxProvider(prevBlockHeight).WithBlockHeight(blockNumber).WithBlockTime(tmBlock.Block.Time)
_ = b.app.BeginBlock(sdkCtx, reqBeginBlock)
sdkCtx = sdkCtx.WithNextMs(
b.ctxProvider(sdkCtx.BlockHeight()+1).MultiStore(),
// []string{"oracle", "oracle_mem"},
[]string{},
)
return sdkCtx, tmBlock, nil
}

Expand Down Expand Up @@ -439,7 +480,7 @@ func (b *Backend) getHeader(blockNumber *big.Int) *ethtypes.Header {
header := &ethtypes.Header{
Difficulty: common.Big0,
Number: blockNumber,
BaseFee: b.keeper.GetCurrBaseFeePerGas(b.ctxProvider(LatestCtxHeight)).TruncateInt().BigInt(),
BaseFee: nil,
GasLimit: b.config.GasCap,
Time: uint64(time.Now().Unix()),
ExcessBlobGas: &zeroExcessBlobGas,
Expand Down
4 changes: 4 additions & 0 deletions evmrpc/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func NewSubscriptionAPI(tmClient rpcclient.Client, k *keeper.Keeper, ctxProvider
panic(err)
}
go func() {
defer recoverAndLog()
defer func() {
_ = api.subscriptionManager.Unsubscribe(context.Background(), id)
}()
Expand Down Expand Up @@ -116,6 +117,7 @@ func (a *SubscriptionAPI) NewHeads(ctx context.Context) (s *rpc.Subscription, er
a.newHeadListeners[rpcSub.ID] = listener

go func() {
defer recoverAndLog()
OUTER:
for {
select {
Expand Down Expand Up @@ -170,6 +172,7 @@ func (a *SubscriptionAPI) Logs(ctx context.Context, filter *filters.FilterCriter

if filter.BlockHash != nil {
go func() {
defer recoverAndLog()
logs, _, err := a.logFetcher.GetLogsByFilters(ctx, *filter, 0)
if err != nil {
_ = notifier.Notify(rpcSub.ID, err)
Expand All @@ -185,6 +188,7 @@ func (a *SubscriptionAPI) Logs(ctx context.Context, filter *filters.FilterCriter
}

go func() {
defer recoverAndLog()
begin := int64(0)
for {
logs, lastToHeight, err := a.logFetcher.GetLogsByFilters(ctx, *filter, begin)
Expand Down
9 changes: 9 additions & 0 deletions evmrpc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/hex"
"fmt"
"math/big"
"runtime/debug"
"sync"
"time"

Expand Down Expand Up @@ -250,10 +251,18 @@ func NewParallelRunner(cnt int, capacity int) *ParallelRunner {
for i := 0; i < cnt; i++ {
go func() {
defer pr.Done.Done()
defer recoverAndLog()
for f := range pr.Queue {
f()
}
}()
}
return pr
}

func recoverAndLog() {
if e := recover(); e != nil {
fmt.Printf("Panic recovered: %s\n", e)
debug.PrintStack()
}
}
9 changes: 9 additions & 0 deletions evmrpc/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,12 @@ func TestCheckVersion(t *testing.T) {
ctx = ctx.WithBlockHeight(2)
require.NotNil(t, evmrpc.CheckVersion(ctx, k))
}

func TestParallelRunnerPanicRecovery(t *testing.T) {
r := evmrpc.NewParallelRunner(10, 10)
r.Queue <- func() {
panic("should be handled")
}
close(r.Queue)
require.NotPanics(t, r.Done.Wait)
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,13 @@ require (
)

replace (
github.com/CosmWasm/wasmd => github.com/sei-protocol/sei-wasmd v0.3.0-hotfix-3
github.com/CosmWasm/wasmd => github.com/sei-protocol/sei-wasmd v0.3.0-hotfix-6
github.com/CosmWasm/wasmvm => github.com/sei-protocol/sei-wasmvm v1.5.4-sei.0.0.1
github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0
github.com/cosmos/cosmos-sdk => github.com/sei-protocol/sei-cosmos v0.3.55-hotfix
github.com/cosmos/cosmos-sdk => github.com/sei-protocol/sei-cosmos v0.3.55-hotfix-6
github.com/cosmos/iavl => github.com/sei-protocol/sei-iavl v0.2.0
github.com/cosmos/ibc-go/v3 => github.com/sei-protocol/sei-ibc-go/v3 v3.3.5
github.com/ethereum/go-ethereum => github.com/sei-protocol/go-ethereum v1.13.5-sei-35
github.com/ethereum/go-ethereum => github.com/sei-protocol/go-ethereum v1.13.5-sei-38
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
github.com/sei-protocol/sei-db => github.com/sei-protocol/sei-db v0.0.48
// Latest goleveldb is broken, we have to stick to this version
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1344,12 +1344,12 @@ github.com/seccomp/libseccomp-golang v0.9.2-0.20220502022130-f33da4d89646/go.mod
github.com/securego/gosec/v2 v2.11.0 h1:+PDkpzR41OI2jrw1q6AdXZCbsNGNGT7pQjal0H0cArI=
github.com/securego/gosec/v2 v2.11.0/go.mod h1:SX8bptShuG8reGC0XS09+a4H2BoWSJi+fscA+Pulbpo=
github.com/segmentio/fasthash v1.0.3/go.mod h1:waKX8l2N8yckOgmSsXJi7x1ZfdKZ4x7KRMzBtS3oedY=
github.com/sei-protocol/go-ethereum v1.13.5-sei-35 h1:gbw7LPm+SFzP5nM1ebfrG6wkrUUBt0/iZtj97qoElE0=
github.com/sei-protocol/go-ethereum v1.13.5-sei-35/go.mod h1:kcRZmuzRn1lVejiFNTz4l4W7imnpq1bDAnuKS/RyhbQ=
github.com/sei-protocol/go-ethereum v1.13.5-sei-38 h1:QmgqsnzhP/lGOuKHtsSB9FwKc+KgraQLEVFCmOhECng=
github.com/sei-protocol/go-ethereum v1.13.5-sei-38/go.mod h1:kcRZmuzRn1lVejiFNTz4l4W7imnpq1bDAnuKS/RyhbQ=
github.com/sei-protocol/goutils v0.0.2 h1:Bfa7Sv+4CVLNM20QcpvGb81B8C5HkQC/kW1CQpIbXDA=
github.com/sei-protocol/goutils v0.0.2/go.mod h1:iYE2DuJfEnM+APPehr2gOUXfuLuPsVxorcDO+Tzq9q8=
github.com/sei-protocol/sei-cosmos v0.3.55-hotfix h1:SaBzeLy8wca3rdvKgrUGnRxqGHX4aua15rTJPz9dTno=
github.com/sei-protocol/sei-cosmos v0.3.55-hotfix/go.mod h1:Z+0XynKuhMu9m2XHIvUBwk4A+iLPc4YnzFlabpMOWqw=
github.com/sei-protocol/sei-cosmos v0.3.55-hotfix-6 h1:kASn0duQ0tMdUtvLHwZdG3C83MOwcZ2LAjQLrEGs8Mo=
github.com/sei-protocol/sei-cosmos v0.3.55-hotfix-6/go.mod h1:Z+0XynKuhMu9m2XHIvUBwk4A+iLPc4YnzFlabpMOWqw=
github.com/sei-protocol/sei-db v0.0.48 h1:BgSF5jq9hiPz0JMmbf5f34CnZijqTO+3zOo8IG62lFY=
github.com/sei-protocol/sei-db v0.0.48/go.mod h1:m5g7p0QeAS3dNJHIl28zQpzOgxQmvYqPb7t4hwgIOCA=
github.com/sei-protocol/sei-iavl v0.2.0 h1:OisPjXiDT+oe+aeckzDEFgkZCYuUjHgs/PP8DPicN+I=
Expand All @@ -1360,8 +1360,8 @@ github.com/sei-protocol/sei-tendermint v0.5.5 h1:JBORWwZBMjaeB7ASEgjZu4Tae2hT8Jw
github.com/sei-protocol/sei-tendermint v0.5.5/go.mod h1:4LSlJdhl3nf3OmohliwRNUFLOB1XWlrmSodrIP7fLh4=
github.com/sei-protocol/sei-tm-db v0.0.5 h1:3WONKdSXEqdZZeLuWYfK5hP37TJpfaUa13vAyAlvaQY=
github.com/sei-protocol/sei-tm-db v0.0.5/go.mod h1:Cpa6rGyczgthq7/0pI31jys2Fw0Nfrc+/jKdP1prVqY=
github.com/sei-protocol/sei-wasmd v0.3.0-hotfix-3 h1:KdQApNmUsnCheycOhGTWqrlEyBMVlrGATdJvpL7mATU=
github.com/sei-protocol/sei-wasmd v0.3.0-hotfix-3/go.mod h1:QFXKoSy8+9RbGcsBiGXw+L5EkYXa/pn/Rtul8Oa/fH8=
github.com/sei-protocol/sei-wasmd v0.3.0-hotfix-6 h1:jJVZpTIoBHguEAXGSZs2BYsOLYazbV9yPhSmVkRR7CQ=
github.com/sei-protocol/sei-wasmd v0.3.0-hotfix-6/go.mod h1:QFXKoSy8+9RbGcsBiGXw+L5EkYXa/pn/Rtul8Oa/fH8=
github.com/sei-protocol/sei-wasmvm v1.5.4-sei.0.0.1 h1:OhDRa2VSvc5cjygSY15+ymLlWqsuVJNm3PibTPlR2IE=
github.com/sei-protocol/sei-wasmvm v1.5.4-sei.0.0.1/go.mod h1:Q0bSEtlktzh7W2hhEaifrFp1Erx11ckQZmjq8FLCyys=
github.com/sei-protocol/tm-db v0.0.4 h1:7Y4EU62Xzzg6wKAHEotm7SXQR0aPLcGhKHkh3qd0tnk=
Expand Down
Loading
Loading