Skip to content

Commit

Permalink
Backport v0.17.x: Enforce that unbonding tx always has version 2 (#311)…
Browse files Browse the repository at this point in the history
… (#312)

- Enforces that unbonding tx always have version `2`
  • Loading branch information
KonradStaniec authored Dec 2, 2024
1 parent ca1ef88 commit 23aa8a9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
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

0 comments on commit 23aa8a9

Please sign in to comment.