|
| 1 | +package integrationtests |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/bitcoin-sv/go-sdk/chainhash" |
| 8 | + trx "github.com/bitcoin-sv/go-sdk/transaction" |
| 9 | + "github.com/bitcoin-sv/spv-wallet/actions/v2/internal/integrationtests/testabilities" |
| 10 | + chainmodels "github.com/bitcoin-sv/spv-wallet/engine/chain/models" |
| 11 | + testengine "github.com/bitcoin-sv/spv-wallet/engine/testabilities" |
| 12 | + "github.com/bitcoin-sv/spv-wallet/engine/v2/transaction/txmodels" |
| 13 | +) |
| 14 | + |
| 15 | +func TestHandlingARCCallback(t *testing.T) { |
| 16 | + tests := map[string]struct { |
| 17 | + txInfo chainmodels.TXInfo |
| 18 | + expectStatus txmodels.TxStatus |
| 19 | + beforeCallback func(t *testing.T, txInfo *chainmodels.TXInfo) |
| 20 | + }{ |
| 21 | + "On Mined with BUMP": { |
| 22 | + txInfo: chainmodels.TXInfo{ |
| 23 | + TXStatus: chainmodels.Mined, |
| 24 | + BlockHeight: 885803, |
| 25 | + BlockHash: "00000000000000000f0905597b6cac80031f0f56834e74dce1a714c682a9ed38", |
| 26 | + Timestamp: time.Now().Add(10 * time.Minute), |
| 27 | + }, |
| 28 | + beforeCallback: calcBump, |
| 29 | + expectStatus: txmodels.TxStatusMined, |
| 30 | + }, |
| 31 | + "On SeenOnNetwork do nothing": { |
| 32 | + txInfo: minimalTxInfo(chainmodels.SeenOnNetwork), |
| 33 | + expectStatus: txmodels.TxStatusBroadcasted, |
| 34 | + }, |
| 35 | + "On Rejected mark as problematic": { |
| 36 | + txInfo: minimalTxInfo(chainmodels.Rejected), |
| 37 | + expectStatus: txmodels.TxStatusProblematic, |
| 38 | + }, |
| 39 | + } |
| 40 | + for name, test := range tests { |
| 41 | + t.Run(name, func(t *testing.T) { |
| 42 | + // given: |
| 43 | + given, when, then := testabilities.New(t) |
| 44 | + cleanup := given.StartedSPVWalletV2() |
| 45 | + defer cleanup() |
| 46 | + |
| 47 | + // when: |
| 48 | + receiveTxID := when.Alice().ReceivesFromExternal(10) |
| 49 | + |
| 50 | + // then: |
| 51 | + then.ARC().Broadcasted(). |
| 52 | + WithTxID(receiveTxID). |
| 53 | + WithCallbackURL("https://example.com/transaction/broadcast/callback"). |
| 54 | + WithCallbackToken(testengine.CallbackTestToken) |
| 55 | + |
| 56 | + // and: |
| 57 | + then.Alice().Operations().Last(). |
| 58 | + WithTxID(receiveTxID). |
| 59 | + WithTxStatus("BROADCASTED") |
| 60 | + |
| 61 | + // given: |
| 62 | + test.txInfo.TxID = receiveTxID |
| 63 | + if test.beforeCallback != nil { |
| 64 | + test.beforeCallback(t, &test.txInfo) |
| 65 | + } |
| 66 | + |
| 67 | + // when: |
| 68 | + test.txInfo.TxID = receiveTxID |
| 69 | + if test.beforeCallback != nil { |
| 70 | + test.beforeCallback(t, &test.txInfo) |
| 71 | + } |
| 72 | + when.ARC().SendsCallback(test.txInfo) |
| 73 | + |
| 74 | + // then: |
| 75 | + then.Alice().Operations().Last(). |
| 76 | + WithTxID(receiveTxID). |
| 77 | + WithTxStatus(string(test.expectStatus)) |
| 78 | + |
| 79 | + if test.expectStatus == txmodels.TxStatusMined { |
| 80 | + then.Alice().Operations().Last(). |
| 81 | + WithBlockHash(test.txInfo.BlockHash). |
| 82 | + WithBlockHeight(test.txInfo.BlockHeight) |
| 83 | + } |
| 84 | + |
| 85 | + // TODO: Assert for BEEF after Searching Transactions is implemented |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func minimalTxInfo(txStatus chainmodels.TXStatus) chainmodels.TXInfo { |
| 91 | + return chainmodels.TXInfo{ |
| 92 | + TXStatus: txStatus, |
| 93 | + Timestamp: time.Now().Add(10 * time.Minute), |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func calcBump(t *testing.T, txInfo *chainmodels.TXInfo) { |
| 98 | + t.Helper() |
| 99 | + |
| 100 | + txID, _ := chainhash.NewHashFromHex(txInfo.TxID) |
| 101 | + bump := trx.NewMerklePath(uint32(txInfo.BlockHeight), [][]*trx.PathElement{{ |
| 102 | + &trx.PathElement{ |
| 103 | + Hash: txID, |
| 104 | + Offset: 0, |
| 105 | + }, |
| 106 | + }}) |
| 107 | + txInfo.MerklePath = bump.Hex() |
| 108 | +} |
0 commit comments