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

Backport v0.17.x: Enforce that unbonding tx always has version 2 (#311) #312

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## Unreleased

### Improvements

- [#305](https://github.com/babylonlabs-io/babylon/pull/305) chore: add more error logs to `VerifyInclusionProofAndGetHeight`
- [#304](https://github.com/babylonlabs-io/babylon/pull/304) Add highest voted height to finality provider
- [#311](https://github.com/babylonlabs-io/babylon/pull/311) Enforce version 2
for unbonding transactions

## v0.17.1

### Bug fixes
Expand Down
12 changes: 8 additions & 4 deletions btcstaking/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,14 @@ func IsSimpleTransfer(tx *wire.MsgTx) error {
// - the transaction has exactly numInputs inputs.
// - the transaction has exactly numOutputs outputs.
// - the transaction lock time is 0.
// - the transaction version is between 1 and maxTxVersion.
// - the transaction version is between minTxVersion and maxTxVersion.
// - each input has a sequence number equal to MaxTxInSequenceNum.
// - each input has an empty signature script.
// - each input has an empty witness.
func CheckPreSignedTxSanity(
tx *wire.MsgTx,
numInputs, numOutputs uint32,
maxTxVersion int32,
minTxVersion, maxTxVersion int32,
) error {
if tx == nil {
return fmt.Errorf("tx must not be nil")
Expand All @@ -261,8 +261,8 @@ func CheckPreSignedTxSanity(
return fmt.Errorf("pre-signed tx must not have locktime")
}

if tx.Version > maxTxVersion || tx.Version < 1 {
return fmt.Errorf("tx version must be between 1 and %d", maxTxVersion)
if tx.Version > maxTxVersion || tx.Version < minTxVersion {
return fmt.Errorf("tx version must be between %d and %d", minTxVersion, maxTxVersion)
}

txWeight := blockchain.GetTransactionWeight(transaction)
Expand Down Expand Up @@ -293,6 +293,8 @@ func CheckPreSignedUnbondingTxSanity(tx *wire.MsgTx) error {
tx,
1,
1,
// Unbonding tx is always version 2
MaxTxVersion,
MaxTxVersion,
)
}
Expand All @@ -302,6 +304,8 @@ func CheckPreSignedSlashingTxSanity(tx *wire.MsgTx) error {
tx,
1,
2,
// slashing tx version can be between 1 and 2
1,
MaxTxVersion,
)
}
Expand Down
40 changes: 39 additions & 1 deletion btcstaking/staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ func TestCheckPreSignedTxSanity(t *testing.T) {
genTx func() *wire.MsgTx
numInputs uint32
numOutputs uint32
minTxVersion int32
maxTxVersion int32
wantErr bool
expectedErrMsg string
Expand All @@ -435,9 +436,39 @@ func TestCheckPreSignedTxSanity(t *testing.T) {
},
numInputs: 1,
numOutputs: 1,
minTxVersion: 1,
maxTxVersion: 2,
wantErr: false,
},
{
name: "valid tx with required specific version 2",
genTx: func() *wire.MsgTx {
tx := wire.NewMsgTx(2)
tx.AddTxIn(wire.NewTxIn(wire.NewOutPoint(&chainhash.Hash{}, 0), nil, nil))
tx.AddTxOut(wire.NewTxOut(1000, nil))
return tx
},
numInputs: 1,
numOutputs: 1,
minTxVersion: 2,
maxTxVersion: 2,
wantErr: false,
},
{
name: "invalid tx when requireing specific version 2",
genTx: func() *wire.MsgTx {
tx := wire.NewMsgTx(3)
tx.AddTxIn(wire.NewTxIn(wire.NewOutPoint(&chainhash.Hash{}, 0), nil, nil))
tx.AddTxOut(wire.NewTxOut(1000, nil))
return tx
},
numInputs: 1,
numOutputs: 1,
minTxVersion: 2,
maxTxVersion: 2,
wantErr: true,
expectedErrMsg: "tx version must be between 2 and 2",
},
{
name: "non standard version tx",
genTx: func() *wire.MsgTx {
Expand All @@ -448,6 +479,7 @@ func TestCheckPreSignedTxSanity(t *testing.T) {
},
numInputs: 1,
numOutputs: 1,
minTxVersion: 1,
maxTxVersion: 2,
wantErr: true,
expectedErrMsg: "tx version must be between 1 and 2",
Expand All @@ -463,6 +495,7 @@ func TestCheckPreSignedTxSanity(t *testing.T) {
},
numInputs: 1,
numOutputs: 1,
minTxVersion: 1,
maxTxVersion: 2,
wantErr: true,
expectedErrMsg: "pre-signed tx must not have locktime",
Expand All @@ -478,6 +511,7 @@ func TestCheckPreSignedTxSanity(t *testing.T) {
},
numInputs: 1,
numOutputs: 1,
minTxVersion: 1,
maxTxVersion: 2,
wantErr: true,
expectedErrMsg: "pre-signed tx must not have signature script",
Expand All @@ -493,6 +527,7 @@ func TestCheckPreSignedTxSanity(t *testing.T) {
},
numInputs: 1,
numOutputs: 1,
minTxVersion: 1,
maxTxVersion: 2,
wantErr: true,
expectedErrMsg: "tx must have exactly 1 inputs",
Expand All @@ -508,6 +543,7 @@ func TestCheckPreSignedTxSanity(t *testing.T) {
},
numInputs: 1,
numOutputs: 1,
minTxVersion: 1,
maxTxVersion: 2,
wantErr: true,
expectedErrMsg: "tx must have exactly 1 outputs",
Expand All @@ -523,6 +559,7 @@ func TestCheckPreSignedTxSanity(t *testing.T) {
},
numInputs: 1,
numOutputs: 1,
minTxVersion: 1,
maxTxVersion: 2,
wantErr: true,
expectedErrMsg: "pre-signed tx must not be replaceable",
Expand All @@ -539,6 +576,7 @@ func TestCheckPreSignedTxSanity(t *testing.T) {
},
numInputs: 1,
numOutputs: 1,
minTxVersion: 1,
maxTxVersion: 2,
wantErr: true,
expectedErrMsg: "tx weight must not exceed 400000",
Expand All @@ -549,7 +587,7 @@ func TestCheckPreSignedTxSanity(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := btcstaking.CheckPreSignedTxSanity(
tt.genTx(), tt.numInputs, tt.numOutputs, tt.maxTxVersion,
tt.genTx(), tt.numInputs, tt.numOutputs, tt.minTxVersion, tt.maxTxVersion,
)

if tt.wantErr {
Expand Down