-
Notifications
You must be signed in to change notification settings - Fork 42
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
Properly validate whether tranactions are standard #185
Merged
KonradStaniec
merged 3 commits into
main
from
konradstaniec/fix-accepting-non-standard-tx
Oct 14, 2024
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -193,7 +193,7 @@ func testSlashingTx( | |
require.ErrorIs(t, err, btcstaking.ErrDustOutputFound) | ||
} else { | ||
require.NoError(t, err) | ||
err = btcstaking.CheckTransactions( | ||
err = btcstaking.CheckSlashingTxMatchFundingTx( | ||
slashingTx, | ||
stakingTx, | ||
uint32(stakingOutputIdx), | ||
|
@@ -303,7 +303,7 @@ func TestSlashingTxWithOverflowMustNotAccepted(t *testing.T) { | |
slashingTx.TxOut[0].Value = math.MaxInt64 / 8 | ||
slashingTx.TxOut[1].Value = math.MaxInt64 / 8 | ||
|
||
err = btcstaking.CheckTransactions( | ||
err = btcstaking.CheckSlashingTxMatchFundingTx( | ||
slashingTx, | ||
stakingTx, | ||
uint32(0), | ||
|
@@ -315,7 +315,7 @@ func TestSlashingTxWithOverflowMustNotAccepted(t *testing.T) { | |
&chaincfg.MainNetParams, | ||
) | ||
require.Error(t, err) | ||
require.EqualError(t, err, "slashing transaction does not obey BTC rules: transaction output value is higher than max allowed value: 1152921504606846975 > 2.1e+15 ") | ||
require.EqualError(t, err, "invalid slashing tx: btc transaction do not obey BTC rules: transaction output value is higher than max allowed value: 1152921504606846975 > 2.1e+15 ") | ||
} | ||
|
||
func TestNotAllowStakerKeyToBeFinalityProviderKey(t *testing.T) { | ||
|
@@ -413,3 +413,148 @@ func TestNotAllowFinalityProviderKeysAsCovenantKeys(t *testing.T) { | |
require.Error(t, err) | ||
require.True(t, errors.Is(err, btcstaking.ErrDuplicatedKeyInScript)) | ||
} | ||
|
||
func TestCheckPreSignedTxSanity(t *testing.T) { | ||
tests := []struct { | ||
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. t.Parallel() |
||
name string | ||
genTx func() *wire.MsgTx | ||
numInputs uint32 | ||
numOutputs uint32 | ||
maxTxVersion int32 | ||
wantErr bool | ||
expectedErrMsg string | ||
}{ | ||
{ | ||
name: "valid tx", | ||
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, | ||
maxTxVersion: 2, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "non standard version tx", | ||
genTx: func() *wire.MsgTx { | ||
tx := wire.NewMsgTx(0) | ||
tx.AddTxIn(wire.NewTxIn(wire.NewOutPoint(&chainhash.Hash{}, 0), nil, nil)) | ||
tx.AddTxOut(wire.NewTxOut(1000, nil)) | ||
return tx | ||
}, | ||
numInputs: 1, | ||
numOutputs: 1, | ||
maxTxVersion: 2, | ||
wantErr: true, | ||
expectedErrMsg: "tx version must be between 1 and 2", | ||
}, | ||
{ | ||
name: "transaction with locktime", | ||
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)) | ||
tx.LockTime = 1 | ||
return tx | ||
}, | ||
numInputs: 1, | ||
numOutputs: 1, | ||
maxTxVersion: 2, | ||
wantErr: true, | ||
expectedErrMsg: "pre-signed tx must not have locktime", | ||
}, | ||
{ | ||
name: "transaction with sig script", | ||
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)) | ||
tx.TxIn[0].SignatureScript = []byte{0x01, 0x02, 0x03} | ||
return tx | ||
}, | ||
numInputs: 1, | ||
numOutputs: 1, | ||
maxTxVersion: 2, | ||
wantErr: true, | ||
expectedErrMsg: "pre-signed tx must not have signature script", | ||
}, | ||
{ | ||
name: "transaction with invalid amount of inputs", | ||
genTx: func() *wire.MsgTx { | ||
tx := wire.NewMsgTx(2) | ||
tx.AddTxIn(wire.NewTxIn(wire.NewOutPoint(&chainhash.Hash{}, 0), nil, nil)) | ||
tx.AddTxIn(wire.NewTxIn(wire.NewOutPoint(&chainhash.Hash{}, 1), nil, nil)) | ||
tx.AddTxOut(wire.NewTxOut(1000, nil)) | ||
return tx | ||
}, | ||
numInputs: 1, | ||
numOutputs: 1, | ||
maxTxVersion: 2, | ||
wantErr: true, | ||
expectedErrMsg: "tx must have exactly 1 inputs", | ||
}, | ||
{ | ||
name: "transaction with invalid amount of outputs", | ||
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)) | ||
tx.AddTxOut(wire.NewTxOut(1000, nil)) | ||
return tx | ||
}, | ||
numInputs: 1, | ||
numOutputs: 1, | ||
maxTxVersion: 2, | ||
wantErr: true, | ||
expectedErrMsg: "tx must have exactly 1 outputs", | ||
}, | ||
{ | ||
name: "replacable transaction", | ||
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)) | ||
tx.TxIn[0].Sequence = wire.MaxTxInSequenceNum - 1 | ||
return tx | ||
}, | ||
numInputs: 1, | ||
numOutputs: 1, | ||
maxTxVersion: 2, | ||
wantErr: true, | ||
expectedErrMsg: "pre-signed tx must not be replaceable", | ||
}, | ||
{ | ||
name: "transaction with too big witness", | ||
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)) | ||
witness := [20000000]byte{} | ||
tx.TxIn[0].Witness = [][]byte{witness[:]} | ||
return tx | ||
}, | ||
numInputs: 1, | ||
numOutputs: 1, | ||
maxTxVersion: 2, | ||
wantErr: true, | ||
expectedErrMsg: "tx weight must not exceed 400000", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := btcstaking.CheckPreSignedTxSanity( | ||
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. t.Parallel() in subtest also |
||
tt.genTx(), tt.numInputs, tt.numOutputs, tt.maxTxVersion, | ||
) | ||
|
||
if tt.wantErr { | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), tt.expectedErrMsg) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.