Skip to content

Commit

Permalink
Feat/audit2 (#31)
Browse files Browse the repository at this point in the history
* check if tx failed before handling it

* apply an exponential backoff to tx retry & block process

* change batch file permission

* event parsing check

* missisng finalize height key

* print missing attr string

* add timer stop & make retry count as const
  • Loading branch information
sh-cha authored Oct 16, 2024
1 parent 3e945e2 commit 19d59b9
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 35 deletions.
4 changes: 4 additions & 0 deletions challenger/child/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func (ch *Child) endBlockHandler(_ context.Context, args nodetypes.EndBlockArgs)
}

func (ch *Child) txHandler(_ context.Context, args nodetypes.TxHandlerArgs) error {
// ignore failed tx
if !args.Success {
return nil
}
txConfig := ch.Node().GetTxConfig()

tx, err := txutils.DecodeTx(txConfig, args.Tx)
Expand Down
27 changes: 0 additions & 27 deletions challenger/child/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import (
"context"
"encoding/base64"
"fmt"
"strconv"
"strings"
"time"

"cosmossdk.io/math"
opchildtypes "github.com/initia-labs/OPinit/x/opchild/types"
ophosttypes "github.com/initia-labs/OPinit/x/ophost/types"
challengertypes "github.com/initia-labs/opinit-bots/challenger/types"
"github.com/initia-labs/opinit-bots/types"
Expand All @@ -27,30 +24,6 @@ func (ch *Child) initiateWithdrawalHandler(_ context.Context, args nodetypes.Eve
if err != nil {
return err
}

for _, attr := range args.EventAttributes {
switch attr.Key {
case opchildtypes.AttributeKeyL2Sequence:
l2Sequence, err = strconv.ParseUint(attr.Value, 10, 64)
if err != nil {
return err
}
case opchildtypes.AttributeKeyFrom:
from = attr.Value
case opchildtypes.AttributeKeyTo:
to = attr.Value
case opchildtypes.AttributeKeyBaseDenom:
baseDenom = attr.Value
case opchildtypes.AttributeKeyAmount:
coinAmount, ok := math.NewIntFromString(attr.Value)
if !ok {
return fmt.Errorf("invalid amount %s", attr.Value)
}

amount = coinAmount.Uint64()
}
}

return ch.handleInitiateWithdrawal(l2Sequence, from, to, baseDenom, amount)
}

Expand Down
2 changes: 1 addition & 1 deletion executor/batch/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (bs *BatchSubmitter) Initialize(ctx context.Context, processedHeight int64,
}

fileFlag := os.O_CREATE | os.O_RDWR | os.O_APPEND
bs.batchFile, err = os.OpenFile(bs.homePath+"/batch", fileFlag, 0666)
bs.batchFile, err = os.OpenFile(bs.homePath+"/batch", fileFlag, 0640)
if err != nil {
return err
}
Expand Down
10 changes: 3 additions & 7 deletions node/broadcaster/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
rpccoretypes "github.com/cometbft/cometbft/rpc/core/types"

btypes "github.com/initia-labs/opinit-bots/node/broadcaster/types"
"github.com/initia-labs/opinit-bots/types"
)

func (b Broadcaster) GetHeight() int64 {
Expand Down Expand Up @@ -110,7 +111,7 @@ func (b *Broadcaster) Start(ctx context.Context) error {
return nil
case data := <-b.txChannel:
var err error
for retry := 1; retry <= 10; retry++ {
for retry := 1; retry <= 7; retry++ {
err = b.handleProcessedMsgs(ctx, data)
if err == nil {
break
Expand All @@ -122,12 +123,7 @@ func (b *Broadcaster) Start(ctx context.Context) error {
break
}
b.logger.Warn("retry to handle processed msgs after 30 seconds", zap.Int("count", retry), zap.String("error", err.Error()))
timer := time.NewTimer(30 * time.Second)
select {
case <-ctx.Done():
return nil
case <-timer.C:
}
types.SleepWithRetry(ctx, retry)
}
if err != nil {
return errors.Wrap(err, "failed to handle processed msgs")
Expand Down
9 changes: 9 additions & 0 deletions node/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ func (n *Node) blockProcessLooper(ctx context.Context, processType nodetypes.Blo
timer := time.NewTicker(types.PollingInterval(ctx))
defer timer.Stop()

consecutiveErrors := 0
for {
select {
case <-ctx.Done():
return nil
case <-timer.C:
types.SleepWithRetry(ctx, consecutiveErrors)
consecutiveErrors++
}

status, err := n.rpcClient.Status(ctx)
Expand Down Expand Up @@ -100,6 +103,7 @@ func (n *Node) blockProcessLooper(ctx context.Context, processType nodetypes.Blo
n.lastProcessedBlockHeight = i
}
}
consecutiveErrors = 0
}
}

Expand Down Expand Up @@ -153,6 +157,7 @@ func (n *Node) handleNewBlock(ctx context.Context, block *rpccoretypes.ResultBlo
LatestHeight: latestChainHeight,
TxIndex: int64(txIndex),
Tx: tx,
Success: blockResult.TxsResults[txIndex].Code == abcitypes.CodeTypeOK,
})
if err != nil {
return fmt.Errorf("failed to handle tx: tx_index: %d; %w", txIndex, err)
Expand Down Expand Up @@ -214,11 +219,14 @@ func (n *Node) txChecker(ctx context.Context) error {

timer := time.NewTicker(types.PollingInterval(ctx))
defer timer.Stop()
consecutiveErrors := 0
for {
select {
case <-ctx.Done():
return nil
case <-timer.C:
types.SleepWithRetry(ctx, consecutiveErrors)
consecutiveErrors++
}

pendingTx, res, blockTime, err := n.broadcaster.CheckPendingTx(ctx)
Expand Down Expand Up @@ -250,5 +258,6 @@ func (n *Node) txChecker(ctx context.Context) error {
if err != nil {
return err
}
consecutiveErrors = 0
}
}
1 change: 1 addition & 0 deletions node/types/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type TxHandlerArgs struct {
LatestHeight int64
TxIndex int64
Tx comettypes.Tx
Success bool
}

type TxHandlerFn func(context.Context, TxHandlerArgs) error
Expand Down
46 changes: 46 additions & 0 deletions provider/child/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,33 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

func missingAttrsError(missingAttrs map[string]struct{}) error {
if len(missingAttrs) != 0 {
missingAttrStr := ""
for attr := range missingAttrs {
missingAttrStr += attr + " "
}
return fmt.Errorf("missing attributes: %s", missingAttrStr)
}
return nil
}

func ParseFinalizeDeposit(eventAttrs []abcitypes.EventAttribute) (
l1BlockHeight int64,
l1Sequence uint64,
from, to, baseDenom string,
amount sdk.Coin,
err error) {
missingAttrs := map[string]struct{}{
opchildtypes.AttributeKeyL1Sequence: {},
opchildtypes.AttributeKeySender: {},
opchildtypes.AttributeKeyRecipient: {},
opchildtypes.AttributeKeyDenom: {},
opchildtypes.AttributeKeyBaseDenom: {},
opchildtypes.AttributeKeyAmount: {},
opchildtypes.AttributeKeyFinalizeHeight: {},
}

for _, attr := range eventAttrs {
switch attr.Key {
case opchildtypes.AttributeKeyL1Sequence:
Expand Down Expand Up @@ -44,15 +65,24 @@ func ParseFinalizeDeposit(eventAttrs []abcitypes.EventAttribute) (
if err != nil {
return
}
default:
continue
}
delete(missingAttrs, attr.Key)
}
err = missingAttrsError(missingAttrs)
return
}

func ParseUpdateOracle(eventAttrs []abcitypes.EventAttribute) (
l1BlockHeight int64,
from string,
err error) {
missingAttrs := map[string]struct{}{
opchildtypes.AttributeKeyHeight: {},
opchildtypes.AttributeKeyFrom: {},
}

for _, attr := range eventAttrs {
switch attr.Key {
case opchildtypes.AttributeKeyHeight:
Expand All @@ -62,15 +92,27 @@ func ParseUpdateOracle(eventAttrs []abcitypes.EventAttribute) (
}
case opchildtypes.AttributeKeyFrom:
from = attr.Value
default:
continue
}
delete(missingAttrs, attr.Key)
}
err = missingAttrsError(missingAttrs)
return
}

func ParseInitiateWithdrawal(eventAttrs []abcitypes.EventAttribute) (
l2Sequence, amount uint64,
from, to, baseDenom string,
err error) {
missingAttrs := map[string]struct{}{
opchildtypes.AttributeKeyL2Sequence: {},
opchildtypes.AttributeKeyFrom: {},
opchildtypes.AttributeKeyTo: {},
opchildtypes.AttributeKeyBaseDenom: {},
opchildtypes.AttributeKeyAmount: {},
}

for _, attr := range eventAttrs {
switch attr.Key {
case opchildtypes.AttributeKeyL2Sequence:
Expand All @@ -91,7 +133,11 @@ func ParseInitiateWithdrawal(eventAttrs []abcitypes.EventAttribute) (
return
}
amount = coinAmount.Uint64()
default:
continue
}
delete(missingAttrs, attr.Key)
}
err = missingAttrsError(missingAttrs)
return
}
Loading

0 comments on commit 19d59b9

Please sign in to comment.