forked from BuxOrg/bux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeef_tx_test.go
107 lines (86 loc) · 2.89 KB
/
beef_tx_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package bux
import (
"context"
"testing"
"github.com/libsv/go-bc"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_ToBeefHex(t *testing.T) {
t.Run("all parents txs are already mined", func(t *testing.T) {
//given
ctx, client, deferMe := initSimpleTestCase(t)
defer deferMe()
ancestorTx := addGrandpaTx(ctx, t, client)
minedParentTx := createTxWithDraft(ctx, t, client, ancestorTx, true)
newTx := createTxWithDraft(ctx, t, client, minedParentTx, false)
//when
hex, err := ToBeefHex(ctx, newTx)
//then
assert.NoError(t, err)
assert.NotEmpty(t, hex)
})
t.Run("some parents txs are not mined yet", func(t *testing.T) {
// Error expected! this should be changed in the future. right now the test case has been written to make sure the system doesn't panic in such a situation
//given
ctx, client, deferMe := initSimpleTestCase(t)
defer deferMe()
ancestorTx := addGrandpaTx(ctx, t, client)
notMinedParentTx := createTxWithDraft(ctx, t, client, ancestorTx, false)
newTx := createTxWithDraft(ctx, t, client, notMinedParentTx, false)
//when
hex, err := ToBeefHex(ctx, newTx)
//then
assert.Error(t, err)
assert.Empty(t, hex)
})
}
func addGrandpaTx(ctx context.Context, t *testing.T, client ClientInterface) *Transaction {
// great ancestor
grandpaTx := newTransaction(testTx2Hex, append(client.DefaultModelOptions(), New())...)
grandpaTx.BlockHeight = 1
// mark it as mined
grandpaTxMp := bc.MerkleProof{
TxOrID: "111111111111111111111111111111111111111",
Nodes: []string{"n1", "n2"},
}
grandpaTx.MerkleProof = MerkleProof(grandpaTxMp)
err := grandpaTx.Save(ctx)
require.NoError(t, err)
return grandpaTx
}
func createTxWithDraft(ctx context.Context, t *testing.T, client ClientInterface, parentTx *Transaction, mined bool) *Transaction {
draftTransaction := newDraftTransaction(
testXPub, &TransactionConfig{
Inputs: []*TransactionInput{{Utxo: *newUtxoFromTxID(parentTx.GetID(), 0, append(client.DefaultModelOptions(), New())...)}},
Outputs: []*TransactionOutput{{
To: "1A1PjKqjWMNBzTVdcBru27EV1PHcXWc63W",
Satoshis: 1000,
}},
ChangeNumberOfDestinations: 1,
Sync: &SyncConfig{
Broadcast: true,
BroadcastInstant: false,
PaymailP2P: false,
SyncOnChain: false,
},
},
append(client.DefaultModelOptions(), New())...,
)
err := draftTransaction.Save(ctx)
require.NoError(t, err)
var transaction *Transaction
transaction, err = client.RecordTransaction(ctx, testXPub, draftTransaction.Hex, draftTransaction.ID, client.DefaultModelOptions()...)
require.NoError(t, err)
assert.NotEmpty(t, transaction)
if mined {
transaction.BlockHeight = 128
mp := bc.MerkleProof{
TxOrID: "423542156234627frafserg6gtrdsbd", Nodes: []string{"n1", "n2"},
}
transaction.MerkleProof = MerkleProof(mp)
}
err = transaction.Save(ctx)
require.NoError(t, err)
return transaction
}