Repository navigation
fix(utxo): pick in-batch duplicate spend winners from persisted state #2036
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
Open
ordishs
wants to merge
12
commits into
bsv-blockchain:main
Choose a base branch
from
ordishs:repro/utxo-respend-4934
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,101
−244
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c5945a0
test(utxo): repro for bitcoin-sv/teranode#4934, in-batch duplicate fi…
ordishs 1cae8a1
fix(utxo): pick in-batch duplicate spend winners from persisted state
ordishs f7ee91d
docs(changelog): a re-submitted spend is no longer refused by an earl…
ordishs 7d340fc
test(utxo): cover the in-batch re-spend fix on Postgres bulk and both…
ordishs b9f773b
fix(utxo): resolve competing batch claims through ordered backend writes
ordishs 7e8fab9
docs(changelog): reference PR 2036 in the batch conflict entry
ordishs 2c671d8
fix(utxo): reject competing batch claims locally once the stored owne…
ordishs bf03666
fix(aerospike): count each Lua spend record key once per batcher batch
ordishs b69248a
fix(utxo): keep unsplit breaker counts and match store checks before …
ordishs 655fb29
fix(utxo): preserve spend breaker ordering and recovery signals
ordishs f20c631
fix(utxo): bound failed spend retries and reject abandoned batch claims
ordishs f53b040
fix(aerospike): keep the recovery panic guard off the batcher spend path
ordishs 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| package aerospike_test | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/bsv-blockchain/go-bt/v2" | ||
| "github.com/bsv-blockchain/go-bt/v2/bscript" | ||
| "github.com/bsv-blockchain/go-bt/v2/chainhash" | ||
| "github.com/bsv-blockchain/teranode/errors" | ||
| "github.com/bsv-blockchain/teranode/stores/utxo" | ||
| "github.com/bsv-blockchain/teranode/ulogger" | ||
| "github.com/bsv-blockchain/teranode/util/test" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestRespendBehindLoserInSameBatch is the Aerospike half of bitcoin-sv/teranode#4934. | ||
| // | ||
| // S0 already owns both parent outputs. A batch then carries a double-spend | ||
| // attempt on output 0 AHEAD of S0's idempotent re-spend. The filter used to | ||
| // award the outpoint by batch position, so the legitimate re-spend was refused | ||
| // with ErrSpent naming the double-spend attempt, which itself was refused by the | ||
| // backend as already spent -- both claims lost, each blamed on the other. | ||
| // | ||
| // A test-only adapter supplies an exact batch to both backend paths. | ||
| func TestRespendBehindLoserInSameBatch(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| utxoBatchSize int | ||
| enableExpressions bool | ||
| }{ | ||
| {name: "lua", utxoBatchSize: 128, enableExpressions: false}, | ||
| {name: "expressions", utxoBatchSize: 1, enableExpressions: true}, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| tSettings := test.CreateBaseTestSettings(t) | ||
| tSettings.UtxoStore.UtxoBatchSize = tc.utxoBatchSize | ||
| tSettings.UtxoStore.SpendBatcherSize = 1 | ||
| tSettings.UtxoStore.SpendBatcherDurationMillis = 1 | ||
| tSettings.UtxoStore.SpendWaitTimeout = 30 * time.Second | ||
| tSettings.Aerospike.EnableSpendFilterExpressions = tc.enableExpressions | ||
|
|
||
| client, store, storeCtx, deferFn := initAerospike(t, tSettings, ulogger.NewErrorTestLogger(t)) | ||
| t.Cleanup(deferFn) | ||
| cleanDB(t, client) | ||
| require.NoError(t, store.SetBlockHeight(2)) | ||
|
|
||
| parent := bt.NewTx() | ||
| prev := chainhash.DoubleHashH([]byte("4934-as-" + tc.name)) | ||
| require.NoError(t, parent.From(prev.String(), 0, | ||
| "76a91471d7dd96d9edda09180fe9d57a477b5acc9cad1188ac", 1e8)) | ||
| parent.Inputs[0].UnlockingScript = bscript.NewFromBytes([]byte{bscript.OpTRUE}) | ||
| require.NoError(t, parent.PayToAddress("1BUBHgpCBWCsD2Ajbeu7ZgCgvMHsoYHm3W", 1000)) | ||
| require.NoError(t, parent.PayToAddress("1BUBHgpCBWCsD2Ajbeu7ZgCgvMHsoYHm3W", 2000)) | ||
| _, err := store.Create(storeCtx, parent, 1) | ||
| require.NoError(t, err) | ||
|
|
||
| spendOf := func(lockTime uint32, vouts ...uint32) *bt.Tx { | ||
| tx := bt.NewTx() | ||
| for _, v := range vouts { | ||
| require.NoError(t, tx.From(parent.TxIDChainHash().String(), v, | ||
| parent.Outputs[v].LockingScript.String(), parent.Outputs[v].Satoshis)) | ||
| } | ||
| require.NoError(t, tx.PayToAddress("1BUBHgpCBWCsD2Ajbeu7ZgCgvMHsoYHm3W", 1)) | ||
| tx.LockTime = lockTime | ||
|
|
||
| return tx | ||
| } | ||
|
|
||
| respend := spendOf(1, 0, 1) // legitimate spender of both outputs | ||
| loser := spendOf(2, 0) // double-spend attempt on output 0 | ||
|
|
||
| _, err = store.Spend(storeCtx, respend, 2) | ||
| require.NoError(t, err, "initial spend must succeed") | ||
|
|
||
| spends, err := utxo.GetSpends(respend) | ||
| require.NoError(t, err) | ||
| require.Len(t, spends, 2) | ||
| loserSpends, err := utxo.GetSpends(loser) | ||
| require.NoError(t, err) | ||
| store.SendSpendBatchForTest(t, []*utxo.Spend{loserSpends[0], spends[0], spends[1]}, 2) | ||
| require.NoError(t, spends[0].Err, "the persisted owner must be accepted") | ||
| require.NoError(t, spends[1].Err) | ||
| require.ErrorIs(t, loserSpends[0].Err, errors.ErrSpent) | ||
| require.Contains(t, loserSpends[0].Err.Error(), respend.TxID()) | ||
|
|
||
| for _, sp := range spends { | ||
| resp, getErr := store.GetSpend(storeCtx, sp) | ||
| require.NoError(t, getErr) | ||
| t.Logf("output %d: status=%v spendingData=%v", sp.Vout, resp.Status, resp.SpendingData) | ||
|
|
||
| require.Equal(t, int(utxo.Status_SPENT), resp.Status, | ||
| "output %d that S0 legitimately spent went back to unspent", sp.Vout) | ||
| require.NotNil(t, resp.SpendingData, "output %d must name its spender", sp.Vout) | ||
| require.Equal(t, respend.TxID(), resp.SpendingData.TxID.String(), | ||
| "output %d is owned by %s, not by S0", sp.Vout, resp.SpendingData.TxID) | ||
| } | ||
| // Neither claim matches the stored owner. Both errors must name it. | ||
| first, err := utxo.GetSpends(spendOf(3, 0)) | ||
| require.NoError(t, err) | ||
| second, err := utxo.GetSpends(spendOf(4, 0)) | ||
| require.NoError(t, err) | ||
| store.SendSpendBatchForTest(t, []*utxo.Spend{first[0], second[0]}, 2) | ||
| for _, sp := range []*utxo.Spend{first[0], second[0]} { | ||
| require.ErrorIs(t, sp.Err, errors.ErrSpent) | ||
| var data *errors.UtxoSpentErrData | ||
| require.True(t, errors.AsData(sp.Err, &data)) | ||
| require.Equal(t, respend.TxID(), data.SpendingData.TxID.String()) | ||
| } | ||
|
|
||
| }) | ||
| } | ||
| } |
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.
[Minor] This re-indents the sub-bullets of an unrelated entry (#2066) from 2 to 4 spaces. It renders the same, so it only adds noise to this PR's diff and may cause merge conflicts with other CHANGELOG edits. Consider reverting these two hunks.