Skip to content

Commit

Permalink
Return all exit event IDs in the bridge commands (#1863)
Browse files Browse the repository at this point in the history
* Return all exit event IDs in the bridge commands

* CR changes
  • Loading branch information
jelacamarko authored Sep 1, 2023
1 parent ece1c5e commit 21c5c33
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 27 deletions.
15 changes: 11 additions & 4 deletions command/bridge/common/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,13 @@ func (bp *ERC1155BridgeParams) Validate() error {
return nil
}

// ExtractExitEventID tries to extract exit event id from provided receipt
func ExtractExitEventID(receipt *ethgo.Receipt) (*big.Int, error) {
var exitEvent contractsapi.L2StateSyncedEvent
// ExtractExitEventIDs tries to extract all exit event ids from provided receipt
func ExtractExitEventIDs(receipt *ethgo.Receipt) ([]*big.Int, error) {
exitEventIDs := make([]*big.Int, 0, len(receipt.Logs))

for _, log := range receipt.Logs {
var exitEvent contractsapi.L2StateSyncedEvent

doesMatch, err := exitEvent.ParseLog(log)
if err != nil {
return nil, err
Expand All @@ -155,7 +158,11 @@ func ExtractExitEventID(receipt *ethgo.Receipt) (*big.Int, error) {
continue
}

return exitEvent.ID, nil
exitEventIDs = append(exitEventIDs, exitEvent.ID)
}

if len(exitEventIDs) != 0 {
return exitEventIDs, nil
}

return nil, errors.New("failed to find exit event log")
Expand Down
4 changes: 2 additions & 2 deletions command/bridge/deposit/erc1155/deposit_erc1155.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,14 @@ func runCommand(cmd *cobra.Command, _ []string) {
}

if dp.ChildChainMintable {
exitEventID, err := common.ExtractExitEventID(receipt)
exitEventIDs, err := common.ExtractExitEventIDs(receipt)
if err != nil {
outputter.SetError(fmt.Errorf("failed to extract exit event: %w", err))

return
}

res.ExitEventIDs = []*big.Int{exitEventID}
res.ExitEventIDs = exitEventIDs
}

// populate child token address if a token is mapped alongside with deposit
Expand Down
12 changes: 6 additions & 6 deletions command/bridge/deposit/erc20/deposit_erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
}

type bridgeTxData struct {
exitEventID *big.Int
exitEventIDs []*big.Int
blockNumber uint64
childTokenAddr *types.Address
}
Expand Down Expand Up @@ -204,10 +204,10 @@ func runCommand(cmd *cobra.Command, _ []string) {
return fmt.Errorf("receiver: %s, amount: %s", receiver, amount)
}

var exitEventID *big.Int
var exitEventIDs []*big.Int

if dp.ChildChainMintable {
if exitEventID, err = common.ExtractExitEventID(receipt); err != nil {
if exitEventIDs, err = common.ExtractExitEventIDs(receipt); err != nil {
return fmt.Errorf("failed to extract exit event: %w", err)
}
}
Expand All @@ -221,7 +221,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
// send aggregated data to channel if everything went ok
bridgeTxCh <- bridgeTxData{
blockNumber: receipt.BlockNumber,
exitEventID: exitEventID,
exitEventIDs: exitEventIDs,
childTokenAddr: childToken,
}

Expand All @@ -241,8 +241,8 @@ func runCommand(cmd *cobra.Command, _ []string) {
var childToken *types.Address

for x := range bridgeTxCh {
if x.exitEventID != nil {
exitEventIDs = append(exitEventIDs, x.exitEventID)
if x.exitEventIDs != nil {
exitEventIDs = append(exitEventIDs, x.exitEventIDs...)
}

blockNumbers = append(blockNumbers, x.blockNumber)
Expand Down
4 changes: 2 additions & 2 deletions command/bridge/deposit/erc721/deposit_erc721.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,14 @@ func runCommand(cmd *cobra.Command, _ []string) {
}

if dp.ChildChainMintable {
exitEventID, err := common.ExtractExitEventID(receipt)
exitEventIDs, err := common.ExtractExitEventIDs(receipt)
if err != nil {
outputter.SetError(fmt.Errorf("failed to extract exit event: %w", err))

return
}

res.ExitEventIDs = []*big.Int{exitEventID}
res.ExitEventIDs = exitEventIDs
}

// populate child token address if a token is mapped alongside with deposit
Expand Down
4 changes: 2 additions & 2 deletions command/bridge/withdraw/erc1155/withdraw_erc1155.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,14 @@ func runCommand(cmd *cobra.Command, _ []string) {
}

if !wp.ChildChainMintable {
exitEventID, err := common.ExtractExitEventID(receipt)
exitEventIDs, err := common.ExtractExitEventIDs(receipt)
if err != nil {
outputter.SetError(fmt.Errorf("failed to extract exit event: %w", err))

return
}

res.ExitEventIDs = []*big.Int{exitEventID}
res.ExitEventIDs = exitEventIDs
}

outputter.SetCommandResult(res)
Expand Down
4 changes: 2 additions & 2 deletions command/bridge/withdraw/erc20/withdraw_erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ func runCommand(cmd *cobra.Command, _ []string) {
}

if !wp.ChildChainMintable {
exitEventID, err := common.ExtractExitEventID(receipt)
extractedExitEventIDs, err := common.ExtractExitEventIDs(receipt)
if err != nil {
outputter.SetError(fmt.Errorf("failed to extract exit event: %w", err))

return
}

exitEventIDs = append(exitEventIDs, exitEventID)
exitEventIDs = append(exitEventIDs, extractedExitEventIDs...)
}

blockNumbers[i] = receipt.BlockNumber
Expand Down
4 changes: 2 additions & 2 deletions command/bridge/withdraw/erc721/withdraw_erc721.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ func run(cmd *cobra.Command, _ []string) {
}

if !wp.ChildChainMintable {
exitEventID, err := common.ExtractExitEventID(receipt)
exitEventIDs, err := common.ExtractExitEventIDs(receipt)
if err != nil {
outputter.SetError(fmt.Errorf("failed to extract exit event: %w", err))

return
}

res.ExitEventIDs = []*big.Int{exitEventID}
res.ExitEventIDs = exitEventIDs
}

outputter.SetCommandResult(res)
Expand Down
10 changes: 5 additions & 5 deletions command/sidechain/withdraw/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ func (w *withdrawParams) validateFlags() error {
}

type withdrawResult struct {
ValidatorAddress string `json:"validatorAddress"`
Amount *big.Int `json:"amount"`
ExitEventID *big.Int `json:"exitEventID"`
BlockNumber uint64 `json:"blockNumber"`
ValidatorAddress string `json:"validatorAddress"`
Amount *big.Int `json:"amount"`
ExitEventIDs []*big.Int `json:"exitEventIDs"`
BlockNumber uint64 `json:"blockNumber"`
}

func (r *withdrawResult) GetOutput() string {
Expand All @@ -34,7 +34,7 @@ func (r *withdrawResult) GetOutput() string {
vals := make([]string, 0, 4)
vals = append(vals, fmt.Sprintf("Validator Address|%s", r.ValidatorAddress))
vals = append(vals, fmt.Sprintf("Amount Withdrawn|%d", r.Amount))
vals = append(vals, fmt.Sprintf("Exit Event ID|%d", r.ExitEventID))
vals = append(vals, fmt.Sprintf("Exit Event IDs|%d", r.ExitEventIDs))
vals = append(vals, fmt.Sprintf("Inclusion Block Number|%d", r.BlockNumber))

buffer.WriteString(helper.FormatKV(vals))
Expand Down
4 changes: 2 additions & 2 deletions command/sidechain/withdraw/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func runCommand(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("could not find an appropriate log in receipt that withdraw happened on ValidatorSet")
}

exitEventID, err := common.ExtractExitEventID(receipt)
exitEventIDs, err := common.ExtractExitEventIDs(receipt)
if err != nil {
return fmt.Errorf("withdrawal failed: %w", err)
}
Expand All @@ -123,7 +123,7 @@ func runCommand(cmd *cobra.Command, _ []string) error {
&withdrawResult{
ValidatorAddress: validatorAccount.Ecdsa.Address().String(),
Amount: withdrawalEvent.Amount,
ExitEventID: exitEventID,
ExitEventIDs: exitEventIDs,
BlockNumber: receipt.BlockNumber,
})

Expand Down

0 comments on commit 21c5c33

Please sign in to comment.