Skip to content
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

chore(relayer): prevent tx1 resubmission #28

Merged
merged 26 commits into from
Sep 2, 2024
Merged
Changes from 23 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
31 changes: 21 additions & 10 deletions submitter/relayer/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,15 @@ func (rl *Relayer) SendCheckpointToBTC(ckpt *ckpttypes.RawCheckpointWithMetaResp
return nil
}

if rl.lastSubmittedCheckpoint == nil || rl.lastSubmittedCheckpoint.Epoch < ckptEpoch {
if rl.lastSubmittedCheckpoint == nil ||
rl.lastSubmittedCheckpoint.Tx1 == nil ||
rl.lastSubmittedCheckpoint.Epoch < ckptEpoch {
rl.logger.Infof("Submitting a raw checkpoint for epoch %v for the first time", ckptEpoch)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
rl.logger.Infof("Submitting a raw checkpoint for epoch %v for the first time", ckptEpoch)
rl.logger.Infof("Submitting a raw checkpoint for epoch %v", ckptEpoch)

maybe it's not the first time due to failure


if rl.lastSubmittedCheckpoint == nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we separate the two cases? if shouldSendCompleCheckpoint() --> convertCkptToTwoTxAndSubmit, else if shouldSendTx2() --> retrySendTx2. I think this would be logically cleaner and some low-level funtions can be reused.

rl.lastSubmittedCheckpoint = &types.CheckpointInfo{}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Init the lastSubmittedCheckpoint

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add this to the default structure at New(

wallet btcclient.BTCWallet,

and then simplify the check by removing rl.lastSubmittedCheckpoint == nil

}

submittedCheckpoint, err := rl.convertCkptToTwoTxAndSubmit(ckpt.Ckpt)
if err != nil {
return err
Expand Down Expand Up @@ -295,14 +301,21 @@ func (rl *Relayer) convertCkptToTwoTxAndSubmit(ckpt *ckpttypes.RawCheckpointResp
func (rl *Relayer) ChainTwoTxAndSend(data1 []byte, data2 []byte) (*types.BtcTxInfo, *types.BtcTxInfo, error) {
// recipient is a change address that all the
// remaining balance of the utxo is sent to
tx1, err := rl.buildTxWithData(data1, nil)
if err != nil {
return nil, nil, fmt.Errorf("failed to add data to tx1: %w", err)
}
tx1 := rl.lastSubmittedCheckpoint.Tx1
// prevent resending tx1 if it was successful
if rl.lastSubmittedCheckpoint.Tx1 == nil {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relevant changes here, cache the TX1 if successful

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any chance for rl.lastSubmittedCheckpoint.Tx1 be loaded with the tx1 sent from the previous epoch?

var err error
tx1, err = rl.buildTxWithData(data1, nil)
if err != nil {
return nil, nil, fmt.Errorf("failed to add data to tx1: %w", err)
}

tx1.TxId, err = rl.sendTxToBTC(tx1.Tx)
if err != nil {
return nil, nil, fmt.Errorf("failed to send tx1 to BTC: %w", err)
tx1.TxId, err = rl.sendTxToBTC(tx1.Tx)
if err != nil {
return nil, nil, fmt.Errorf("failed to send tx1 to BTC: %w", err)
}
// cache a successful tx1 submission
rl.lastSubmittedCheckpoint.Tx1 = tx1
}

// the second tx consumes the second output (index 1)
Expand All @@ -317,8 +330,6 @@ func (rl *Relayer) ChainTwoTxAndSend(data1 []byte, data2 []byte) (*types.BtcTxIn
return nil, nil, fmt.Errorf("failed to send tx2 to BTC: %w", err)
}

// TODO: if tx1 succeeds but tx2 fails, we should not resent tx1

return tx1, tx2, nil
}

Expand Down
Loading