Skip to content

Commit 52bdb39

Browse files
committed
event parsing check
1 parent 1aff865 commit 52bdb39

File tree

3 files changed

+118
-27
lines changed

3 files changed

+118
-27
lines changed

challenger/child/withdraw.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ import (
44
"context"
55
"encoding/base64"
66
"fmt"
7-
"strconv"
87
"strings"
98
"time"
109

11-
"cosmossdk.io/math"
12-
opchildtypes "github.com/initia-labs/OPinit/x/opchild/types"
1310
ophosttypes "github.com/initia-labs/OPinit/x/ophost/types"
1411
challengertypes "github.com/initia-labs/opinit-bots/challenger/types"
1512
"github.com/initia-labs/opinit-bots/types"
@@ -27,30 +24,6 @@ func (ch *Child) initiateWithdrawalHandler(_ context.Context, args nodetypes.Eve
2724
if err != nil {
2825
return err
2926
}
30-
31-
for _, attr := range args.EventAttributes {
32-
switch attr.Key {
33-
case opchildtypes.AttributeKeyL2Sequence:
34-
l2Sequence, err = strconv.ParseUint(attr.Value, 10, 64)
35-
if err != nil {
36-
return err
37-
}
38-
case opchildtypes.AttributeKeyFrom:
39-
from = attr.Value
40-
case opchildtypes.AttributeKeyTo:
41-
to = attr.Value
42-
case opchildtypes.AttributeKeyBaseDenom:
43-
baseDenom = attr.Value
44-
case opchildtypes.AttributeKeyAmount:
45-
coinAmount, ok := math.NewIntFromString(attr.Value)
46-
if !ok {
47-
return fmt.Errorf("invalid amount %s", attr.Value)
48-
}
49-
50-
amount = coinAmount.Uint64()
51-
}
52-
}
53-
5427
return ch.handleInitiateWithdrawal(l2Sequence, from, to, baseDenom, amount)
5528
}
5629

provider/child/parse.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,32 @@ import (
1111
sdk "github.com/cosmos/cosmos-sdk/types"
1212
)
1313

14+
func missingAttrsError(missingAttrs map[string]struct{}) error {
15+
if len(missingAttrs) != 0 {
16+
missingAttrStr := ""
17+
for attr := range missingAttrs {
18+
missingAttrStr += attr + " "
19+
}
20+
return fmt.Errorf("missing attributes: %s", missingAttrs)
21+
}
22+
return nil
23+
}
24+
1425
func ParseFinalizeDeposit(eventAttrs []abcitypes.EventAttribute) (
1526
l1BlockHeight int64,
1627
l1Sequence uint64,
1728
from, to, baseDenom string,
1829
amount sdk.Coin,
1930
err error) {
31+
missingAttrs := map[string]struct{}{
32+
opchildtypes.AttributeKeyL1Sequence: {},
33+
opchildtypes.AttributeKeySender: {},
34+
opchildtypes.AttributeKeyRecipient: {},
35+
opchildtypes.AttributeKeyDenom: {},
36+
opchildtypes.AttributeKeyBaseDenom: {},
37+
opchildtypes.AttributeKeyAmount: {},
38+
}
39+
2040
for _, attr := range eventAttrs {
2141
switch attr.Key {
2242
case opchildtypes.AttributeKeyL1Sequence:
@@ -44,15 +64,24 @@ func ParseFinalizeDeposit(eventAttrs []abcitypes.EventAttribute) (
4464
if err != nil {
4565
return
4666
}
67+
default:
68+
continue
4769
}
70+
delete(missingAttrs, attr.Key)
4871
}
72+
err = missingAttrsError(missingAttrs)
4973
return
5074
}
5175

5276
func ParseUpdateOracle(eventAttrs []abcitypes.EventAttribute) (
5377
l1BlockHeight int64,
5478
from string,
5579
err error) {
80+
missingAttrs := map[string]struct{}{
81+
opchildtypes.AttributeKeyHeight: {},
82+
opchildtypes.AttributeKeyFrom: {},
83+
}
84+
5685
for _, attr := range eventAttrs {
5786
switch attr.Key {
5887
case opchildtypes.AttributeKeyHeight:
@@ -62,15 +91,27 @@ func ParseUpdateOracle(eventAttrs []abcitypes.EventAttribute) (
6291
}
6392
case opchildtypes.AttributeKeyFrom:
6493
from = attr.Value
94+
default:
95+
continue
6596
}
97+
delete(missingAttrs, attr.Key)
6698
}
99+
err = missingAttrsError(missingAttrs)
67100
return
68101
}
69102

70103
func ParseInitiateWithdrawal(eventAttrs []abcitypes.EventAttribute) (
71104
l2Sequence, amount uint64,
72105
from, to, baseDenom string,
73106
err error) {
107+
missingAttrs := map[string]struct{}{
108+
opchildtypes.AttributeKeyL2Sequence: {},
109+
opchildtypes.AttributeKeyFrom: {},
110+
opchildtypes.AttributeKeyTo: {},
111+
opchildtypes.AttributeKeyBaseDenom: {},
112+
opchildtypes.AttributeKeyAmount: {},
113+
}
114+
74115
for _, attr := range eventAttrs {
75116
switch attr.Key {
76117
case opchildtypes.AttributeKeyL2Sequence:
@@ -91,7 +132,11 @@ func ParseInitiateWithdrawal(eventAttrs []abcitypes.EventAttribute) (
91132
return
92133
}
93134
amount = coinAmount.Uint64()
135+
default:
136+
continue
94137
}
138+
delete(missingAttrs, attr.Key)
95139
}
140+
err = missingAttrsError(missingAttrs)
96141
return
97142
}

provider/host/parse.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,41 @@ package host
22

33
import (
44
"encoding/hex"
5+
"fmt"
56
"strconv"
67

78
abcitypes "github.com/cometbft/cometbft/abci/types"
89
ophosttypes "github.com/initia-labs/OPinit/x/ophost/types"
910
)
1011

12+
func missingAttrsError(missingAttrs map[string]struct{}) error {
13+
if len(missingAttrs) != 0 {
14+
missingAttrStr := ""
15+
for attr := range missingAttrs {
16+
missingAttrStr += attr + " "
17+
}
18+
return fmt.Errorf("missing attributes: %s", missingAttrs)
19+
}
20+
return nil
21+
}
22+
1123
func ParseMsgRecordBatch(eventAttrs []abcitypes.EventAttribute) (
1224
submitter string, err error,
1325
) {
26+
missingAttrs := map[string]struct{}{
27+
ophosttypes.AttributeKeySubmitter: {},
28+
}
29+
1430
for _, attr := range eventAttrs {
1531
switch attr.Key {
1632
case ophosttypes.AttributeKeySubmitter:
1733
submitter = attr.Value
34+
default:
35+
continue
1836
}
37+
delete(missingAttrs, attr.Key)
1938
}
39+
err = missingAttrsError(missingAttrs)
2040
return
2141
}
2242

@@ -25,6 +45,14 @@ func ParseMsgUpdateBatchInfo(eventAttrs []abcitypes.EventAttribute) (
2545
outputIndex uint64,
2646
l2BlockNumber int64,
2747
err error) {
48+
missingAttrs := map[string]struct{}{
49+
ophosttypes.AttributeKeyBridgeId: {},
50+
ophosttypes.AttributeKeyBatchChainType: {},
51+
ophosttypes.AttributeKeyBatchSubmitter: {},
52+
ophosttypes.AttributeKeyFinalizedOutputIndex: {},
53+
ophosttypes.AttributeKeyFinalizedL2BlockNumber: {},
54+
}
55+
2856
for _, attr := range eventAttrs {
2957
switch attr.Key {
3058
case ophosttypes.AttributeKeyBridgeId:
@@ -46,15 +74,29 @@ func ParseMsgUpdateBatchInfo(eventAttrs []abcitypes.EventAttribute) (
4674
if err != nil {
4775
return
4876
}
77+
default:
78+
continue
4979
}
80+
delete(missingAttrs, attr.Key)
5081
}
82+
err = missingAttrsError(missingAttrs)
5183
return
5284
}
5385

5486
func ParseMsgInitiateDeposit(eventAttrs []abcitypes.EventAttribute) (
5587
bridgeId, l1Sequence uint64,
5688
from, to, l1Denom, l2Denom, amount string,
5789
data []byte, err error) {
90+
missingAttrs := map[string]struct{}{
91+
ophosttypes.AttributeKeyBridgeId: {},
92+
ophosttypes.AttributeKeyL1Sequence: {},
93+
ophosttypes.AttributeKeyFrom: {},
94+
ophosttypes.AttributeKeyTo: {},
95+
ophosttypes.AttributeKeyL1Denom: {},
96+
ophosttypes.AttributeKeyL2Denom: {},
97+
ophosttypes.AttributeKeyAmount: {},
98+
ophosttypes.AttributeKeyData: {},
99+
}
58100

59101
for _, attr := range eventAttrs {
60102
switch attr.Key {
@@ -83,8 +125,12 @@ func ParseMsgInitiateDeposit(eventAttrs []abcitypes.EventAttribute) (
83125
if err != nil {
84126
return
85127
}
128+
default:
129+
continue
86130
}
131+
delete(missingAttrs, attr.Key)
87132
}
133+
err = missingAttrsError(missingAttrs)
88134
return
89135
}
90136

@@ -95,6 +141,14 @@ func ParseMsgProposeOutput(eventAttrs []abcitypes.EventAttribute) (
95141
proposer string,
96142
outputRoot []byte,
97143
err error) {
144+
missingAttrs := map[string]struct{}{
145+
ophosttypes.AttributeKeyProposer: {},
146+
ophosttypes.AttributeKeyBridgeId: {},
147+
ophosttypes.AttributeKeyOutputIndex: {},
148+
ophosttypes.AttributeKeyL2BlockNumber: {},
149+
ophosttypes.AttributeKeyOutputRoot: {},
150+
}
151+
98152
for _, attr := range eventAttrs {
99153
switch attr.Key {
100154
case ophosttypes.AttributeKeyProposer:
@@ -119,15 +173,30 @@ func ParseMsgProposeOutput(eventAttrs []abcitypes.EventAttribute) (
119173
if err != nil {
120174
return
121175
}
176+
default:
177+
continue
122178
}
179+
delete(missingAttrs, attr.Key)
123180
}
181+
err = missingAttrsError(missingAttrs)
124182
return
125183
}
126184

127185
func ParseMsgFinalizeWithdrawal(eventAttrs []abcitypes.EventAttribute) (
128186
bridgeId, outputIndex, l2Sequence uint64,
129187
from, to, l1Denom, l2Denom, amount string,
130188
err error) {
189+
missingAttrs := map[string]struct{}{
190+
ophosttypes.AttributeKeyBridgeId: {},
191+
ophosttypes.AttributeKeyOutputIndex: {},
192+
ophosttypes.AttributeKeyL2Sequence: {},
193+
ophosttypes.AttributeKeyFrom: {},
194+
ophosttypes.AttributeKeyTo: {},
195+
ophosttypes.AttributeKeyL1Denom: {},
196+
ophosttypes.AttributeKeyL2Denom: {},
197+
ophosttypes.AttributeKeyAmount: {},
198+
}
199+
131200
for _, attr := range eventAttrs {
132201
switch attr.Key {
133202
case ophosttypes.AttributeKeyBridgeId:
@@ -155,7 +224,11 @@ func ParseMsgFinalizeWithdrawal(eventAttrs []abcitypes.EventAttribute) (
155224
l2Denom = attr.Value
156225
case ophosttypes.AttributeKeyAmount:
157226
amount = attr.Value
227+
default:
228+
continue
158229
}
230+
delete(missingAttrs, attr.Key)
159231
}
232+
err = missingAttrsError(missingAttrs)
160233
return
161234
}

0 commit comments

Comments
 (0)