Skip to content

Commit

Permalink
event parsing check
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-cha committed Oct 11, 2024
1 parent 1aff865 commit 52bdb39
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 27 deletions.
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
45 changes: 45 additions & 0 deletions provider/child/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,32 @@ 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", missingAttrs)
}
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: {},
}

for _, attr := range eventAttrs {
switch attr.Key {
case opchildtypes.AttributeKeyL1Sequence:
Expand Down Expand Up @@ -44,15 +64,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 +91,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 +132,11 @@ func ParseInitiateWithdrawal(eventAttrs []abcitypes.EventAttribute) (
return
}
amount = coinAmount.Uint64()
default:
continue
}
delete(missingAttrs, attr.Key)
}
err = missingAttrsError(missingAttrs)
return
}
73 changes: 73 additions & 0 deletions provider/host/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,41 @@ package host

import (
"encoding/hex"
"fmt"
"strconv"

abcitypes "github.com/cometbft/cometbft/abci/types"
ophosttypes "github.com/initia-labs/OPinit/x/ophost/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", missingAttrs)
}
return nil
}

func ParseMsgRecordBatch(eventAttrs []abcitypes.EventAttribute) (
submitter string, err error,
) {
missingAttrs := map[string]struct{}{
ophosttypes.AttributeKeySubmitter: {},
}

for _, attr := range eventAttrs {
switch attr.Key {
case ophosttypes.AttributeKeySubmitter:
submitter = attr.Value
default:
continue
}
delete(missingAttrs, attr.Key)
}
err = missingAttrsError(missingAttrs)
return
}

Expand All @@ -25,6 +45,14 @@ func ParseMsgUpdateBatchInfo(eventAttrs []abcitypes.EventAttribute) (
outputIndex uint64,
l2BlockNumber int64,
err error) {
missingAttrs := map[string]struct{}{
ophosttypes.AttributeKeyBridgeId: {},
ophosttypes.AttributeKeyBatchChainType: {},
ophosttypes.AttributeKeyBatchSubmitter: {},
ophosttypes.AttributeKeyFinalizedOutputIndex: {},
ophosttypes.AttributeKeyFinalizedL2BlockNumber: {},
}

for _, attr := range eventAttrs {
switch attr.Key {
case ophosttypes.AttributeKeyBridgeId:
Expand All @@ -46,15 +74,29 @@ func ParseMsgUpdateBatchInfo(eventAttrs []abcitypes.EventAttribute) (
if err != nil {
return
}
default:
continue
}
delete(missingAttrs, attr.Key)
}
err = missingAttrsError(missingAttrs)
return
}

func ParseMsgInitiateDeposit(eventAttrs []abcitypes.EventAttribute) (
bridgeId, l1Sequence uint64,
from, to, l1Denom, l2Denom, amount string,
data []byte, err error) {
missingAttrs := map[string]struct{}{
ophosttypes.AttributeKeyBridgeId: {},
ophosttypes.AttributeKeyL1Sequence: {},
ophosttypes.AttributeKeyFrom: {},
ophosttypes.AttributeKeyTo: {},
ophosttypes.AttributeKeyL1Denom: {},
ophosttypes.AttributeKeyL2Denom: {},
ophosttypes.AttributeKeyAmount: {},
ophosttypes.AttributeKeyData: {},
}

for _, attr := range eventAttrs {
switch attr.Key {
Expand Down Expand Up @@ -83,8 +125,12 @@ func ParseMsgInitiateDeposit(eventAttrs []abcitypes.EventAttribute) (
if err != nil {
return
}
default:
continue
}
delete(missingAttrs, attr.Key)
}
err = missingAttrsError(missingAttrs)
return
}

Expand All @@ -95,6 +141,14 @@ func ParseMsgProposeOutput(eventAttrs []abcitypes.EventAttribute) (
proposer string,
outputRoot []byte,
err error) {
missingAttrs := map[string]struct{}{
ophosttypes.AttributeKeyProposer: {},
ophosttypes.AttributeKeyBridgeId: {},
ophosttypes.AttributeKeyOutputIndex: {},
ophosttypes.AttributeKeyL2BlockNumber: {},
ophosttypes.AttributeKeyOutputRoot: {},
}

for _, attr := range eventAttrs {
switch attr.Key {
case ophosttypes.AttributeKeyProposer:
Expand All @@ -119,15 +173,30 @@ func ParseMsgProposeOutput(eventAttrs []abcitypes.EventAttribute) (
if err != nil {
return
}
default:
continue
}
delete(missingAttrs, attr.Key)
}
err = missingAttrsError(missingAttrs)
return
}

func ParseMsgFinalizeWithdrawal(eventAttrs []abcitypes.EventAttribute) (
bridgeId, outputIndex, l2Sequence uint64,
from, to, l1Denom, l2Denom, amount string,
err error) {
missingAttrs := map[string]struct{}{
ophosttypes.AttributeKeyBridgeId: {},
ophosttypes.AttributeKeyOutputIndex: {},
ophosttypes.AttributeKeyL2Sequence: {},
ophosttypes.AttributeKeyFrom: {},
ophosttypes.AttributeKeyTo: {},
ophosttypes.AttributeKeyL1Denom: {},
ophosttypes.AttributeKeyL2Denom: {},
ophosttypes.AttributeKeyAmount: {},
}

for _, attr := range eventAttrs {
switch attr.Key {
case ophosttypes.AttributeKeyBridgeId:
Expand Down Expand Up @@ -155,7 +224,11 @@ func ParseMsgFinalizeWithdrawal(eventAttrs []abcitypes.EventAttribute) (
l2Denom = attr.Value
case ophosttypes.AttributeKeyAmount:
amount = attr.Value
default:
continue
}
delete(missingAttrs, attr.Key)
}
err = missingAttrsError(missingAttrs)
return
}

0 comments on commit 52bdb39

Please sign in to comment.