-
Notifications
You must be signed in to change notification settings - Fork 3
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(submitter): refactoring points #57
Changes from 7 commits
3f5c38b
49dfedb
f5cd9aa
667eef6
3f13965
7753e04
f7c4a7c
4133833
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -33,6 +33,10 @@ const ( | |||||||||||||||||||||
dustThreshold btcutil.Amount = 546 | ||||||||||||||||||||||
) | ||||||||||||||||||||||
|
||||||||||||||||||||||
var ( | ||||||||||||||||||||||
TxNotFoundErr = errors.New("-5: No such mempool or blockchain transaction. Use gettransaction for wallet transactions") | ||||||||||||||||||||||
) | ||||||||||||||||||||||
|
||||||||||||||||||||||
type GetLatestCheckpointFunc func() (*store.StoredCheckpoint, bool, error) | ||||||||||||||||||||||
type GetRawTransactionFunc func(txHash *chainhash.Hash) (*btcutil.Tx, error) | ||||||||||||||||||||||
type SendTransactionFunc func(tx *wire.MsgTx) (*chainhash.Hash, error) | ||||||||||||||||||||||
|
@@ -149,6 +153,18 @@ func (rl *Relayer) SendCheckpointToBTC(ckpt *ckpttypes.RawCheckpointWithMetaResp | |||||||||||||||||||||
return nil | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
return nil | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
// MaybeResubmitSecondCheckpointTx based on the resend interval attempts to resubmit 2nd ckpt tx with a bumped fee | ||||||||||||||||||||||
func (rl *Relayer) MaybeResubmitSecondCheckpointTx(ckpt *ckpttypes.RawCheckpointWithMetaResponse) error { | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. only suggestion for naming
Suggested change
|
||||||||||||||||||||||
ckptEpoch := ckpt.Ckpt.EpochNum | ||||||||||||||||||||||
if ckpt.Status != ckpttypes.Sealed { | ||||||||||||||||||||||
rl.logger.Errorf("The checkpoint for epoch %v is not sealed", ckptEpoch) | ||||||||||||||||||||||
rl.metrics.InvalidCheckpointCounter.Inc() | ||||||||||||||||||||||
return nil | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we add a check that the epoch of the checkpoint equals that of the last submitted checkpoint? |
||||||||||||||||||||||
lastSubmittedEpoch := rl.lastSubmittedCheckpoint.Epoch | ||||||||||||||||||||||
if ckptEpoch < lastSubmittedEpoch { | ||||||||||||||||||||||
rl.logger.Errorf("The checkpoint for epoch %v is lower than the last submission for epoch %v", | ||||||||||||||||||||||
|
@@ -158,55 +174,51 @@ func (rl *Relayer) SendCheckpointToBTC(ckpt *ckpttypes.RawCheckpointWithMetaResp | |||||||||||||||||||||
return nil | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
// now that the checkpoint has been sent, we should try to resend it | ||||||||||||||||||||||
// if the resend interval has passed | ||||||||||||||||||||||
durSeconds := uint(time.Since(rl.lastSubmittedCheckpoint.Ts).Seconds()) | ||||||||||||||||||||||
if durSeconds >= rl.config.ResendIntervalSeconds { | ||||||||||||||||||||||
rl.logger.Debugf("The checkpoint for epoch %v was sent more than %v seconds ago but not included on BTC", | ||||||||||||||||||||||
ckptEpoch, rl.config.ResendIntervalSeconds) | ||||||||||||||||||||||
if durSeconds < rl.config.ResendIntervalSeconds { | ||||||||||||||||||||||
return nil | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
bumpedFee := rl.calculateBumpedFee(rl.lastSubmittedCheckpoint) | ||||||||||||||||||||||
rl.logger.Debugf("The checkpoint for epoch %v was sent more than %v seconds ago but not included on BTC", | ||||||||||||||||||||||
ckptEpoch, rl.config.ResendIntervalSeconds) | ||||||||||||||||||||||
|
||||||||||||||||||||||
// make sure the bumped fee is effective | ||||||||||||||||||||||
if !rl.shouldResendCheckpoint(rl.lastSubmittedCheckpoint, bumpedFee) { | ||||||||||||||||||||||
return nil | ||||||||||||||||||||||
} | ||||||||||||||||||||||
bumpedFee := rl.calculateBumpedFee(rl.lastSubmittedCheckpoint) | ||||||||||||||||||||||
|
||||||||||||||||||||||
rl.logger.Debugf("Resending the second tx of the checkpoint %v, old fee of the second tx: %v Satoshis, txid: %s", | ||||||||||||||||||||||
ckptEpoch, rl.lastSubmittedCheckpoint.Tx2.Fee, rl.lastSubmittedCheckpoint.Tx2.TxId.String()) | ||||||||||||||||||||||
// make sure the bumped fee is effective | ||||||||||||||||||||||
if !rl.shouldResendCheckpoint(rl.lastSubmittedCheckpoint, bumpedFee) { | ||||||||||||||||||||||
return nil | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
resubmittedTx2, err := rl.resendSecondTxOfCheckpointToBTC(rl.lastSubmittedCheckpoint.Tx2, bumpedFee) | ||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||
rl.metrics.FailedResentCheckpointsCounter.Inc() | ||||||||||||||||||||||
return fmt.Errorf("failed to re-send the second tx of the checkpoint %v: %w", rl.lastSubmittedCheckpoint.Epoch, err) | ||||||||||||||||||||||
} | ||||||||||||||||||||||
rl.logger.Debugf("Resending the second tx of the checkpoint %v, old fee of the second tx: %v Satoshis, txid: %s", | ||||||||||||||||||||||
ckptEpoch, rl.lastSubmittedCheckpoint.Tx2.Fee, rl.lastSubmittedCheckpoint.Tx2.TxId.String()) | ||||||||||||||||||||||
|
||||||||||||||||||||||
// record the metrics of the resent tx2 | ||||||||||||||||||||||
rl.metrics.NewSubmittedCheckpointSegmentGaugeVec.WithLabelValues( | ||||||||||||||||||||||
strconv.Itoa(int(ckptEpoch)), | ||||||||||||||||||||||
"1", | ||||||||||||||||||||||
resubmittedTx2.TxId.String(), | ||||||||||||||||||||||
strconv.Itoa(int(resubmittedTx2.Fee)), | ||||||||||||||||||||||
).SetToCurrentTime() | ||||||||||||||||||||||
rl.metrics.ResentCheckpointsCounter.Inc() | ||||||||||||||||||||||
|
||||||||||||||||||||||
rl.logger.Infof("Successfully re-sent the second tx of the checkpoint %v, txid: %s, bumped fee: %v Satoshis", | ||||||||||||||||||||||
rl.lastSubmittedCheckpoint.Epoch, resubmittedTx2.TxId.String(), resubmittedTx2.Fee) | ||||||||||||||||||||||
|
||||||||||||||||||||||
// update the second tx of the last submitted checkpoint as it is replaced | ||||||||||||||||||||||
rl.lastSubmittedCheckpoint.Tx2 = resubmittedTx2 | ||||||||||||||||||||||
|
||||||||||||||||||||||
err = storeCkptFunc( | ||||||||||||||||||||||
rl.lastSubmittedCheckpoint.Tx1.Tx, | ||||||||||||||||||||||
rl.lastSubmittedCheckpoint.Tx2.Tx, | ||||||||||||||||||||||
rl.lastSubmittedCheckpoint.Epoch, | ||||||||||||||||||||||
) | ||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||
return err | ||||||||||||||||||||||
} | ||||||||||||||||||||||
resubmittedTx2, err := rl.resendSecondTxOfCheckpointToBTC(rl.lastSubmittedCheckpoint.Tx2, bumpedFee) | ||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||
rl.metrics.FailedResentCheckpointsCounter.Inc() | ||||||||||||||||||||||
return fmt.Errorf("failed to re-send the second tx of the checkpoint %v: %w", rl.lastSubmittedCheckpoint.Epoch, err) | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
return nil | ||||||||||||||||||||||
// record the metrics of the resent tx2 | ||||||||||||||||||||||
rl.metrics.NewSubmittedCheckpointSegmentGaugeVec.WithLabelValues( | ||||||||||||||||||||||
strconv.Itoa(int(ckptEpoch)), | ||||||||||||||||||||||
"1", | ||||||||||||||||||||||
resubmittedTx2.TxId.String(), | ||||||||||||||||||||||
strconv.Itoa(int(resubmittedTx2.Fee)), | ||||||||||||||||||||||
).SetToCurrentTime() | ||||||||||||||||||||||
rl.metrics.ResentCheckpointsCounter.Inc() | ||||||||||||||||||||||
|
||||||||||||||||||||||
rl.logger.Infof("Successfully re-sent the second tx of the checkpoint %v, txid: %s, bumped fee: %v Satoshis", | ||||||||||||||||||||||
rl.lastSubmittedCheckpoint.Epoch, resubmittedTx2.TxId.String(), resubmittedTx2.Fee) | ||||||||||||||||||||||
|
||||||||||||||||||||||
// update the second tx of the last submitted checkpoint as it is replaced | ||||||||||||||||||||||
rl.lastSubmittedCheckpoint.Tx2 = resubmittedTx2 | ||||||||||||||||||||||
|
||||||||||||||||||||||
storedCkpt := store.NewStoredCheckpoint( | ||||||||||||||||||||||
rl.lastSubmittedCheckpoint.Tx1.Tx, | ||||||||||||||||||||||
rl.lastSubmittedCheckpoint.Tx2.Tx, | ||||||||||||||||||||||
rl.lastSubmittedCheckpoint.Epoch, | ||||||||||||||||||||||
) | ||||||||||||||||||||||
return rl.store.PutCheckpoint(storedCkpt) | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
func (rl *Relayer) shouldSendCompleteCkpt(ckptEpoch uint64) bool { | ||||||||||||||||||||||
|
@@ -240,6 +252,19 @@ func (rl *Relayer) calculateBumpedFee(ckptInfo *types.CheckpointInfo) btcutil.Am | |||||||||||||||||||||
|
||||||||||||||||||||||
// resendSecondTxOfCheckpointToBTC resends the second tx of the checkpoint with bumpedFee | ||||||||||||||||||||||
func (rl *Relayer) resendSecondTxOfCheckpointToBTC(tx2 *types.BtcTxInfo, bumpedFee btcutil.Amount) (*types.BtcTxInfo, error) { | ||||||||||||||||||||||
_, status, err := rl.TxDetails(rl.lastSubmittedCheckpoint.Tx2.TxId, | ||||||||||||||||||||||
rl.lastSubmittedCheckpoint.Tx2.Tx.TxOut[changePosition].PkScript) | ||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||
return nil, err | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
// No need to resend, transaction already confirmed | ||||||||||||||||||||||
if status != btcclient.TxInMemPool && status != btcclient.TxNotFound { | ||||||||||||||||||||||
rl.logger.Debugf("Transaction %v is already confirmed or has an unexpected state: %v", | ||||||||||||||||||||||
rl.lastSubmittedCheckpoint.Tx2.TxId, status) | ||||||||||||||||||||||
return nil, nil | ||||||||||||||||||||||
} | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Seems |
||||||||||||||||||||||
|
||||||||||||||||||||||
// set output value of the second tx to be the balance minus the bumped fee | ||||||||||||||||||||||
// if the bumped fee is higher than the balance, then set the bumped fee to | ||||||||||||||||||||||
// be equal to the balance to ensure the output value is not negative | ||||||||||||||||||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better to have a doc string to explain the idea of
MaybeResubmitSecondCheckpointTx