|
| 1 | +package application |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/btcsuite/btcd/btcutil/psbt" |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/ark-network/ark/server/internal/core/domain" // adapt for your project |
| 11 | + "github.com/btcsuite/btcd/chaincfg/chainhash" |
| 12 | + "github.com/btcsuite/btcd/wire" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +//--------------------------------------------- |
| 17 | +// Test scenario: |
| 18 | +// |
| 19 | +// LeafTx (outpoint = vtxoA) [leaf, no RedeemTx] |
| 20 | +// \ |
| 21 | +// \---> RedeemTx1 (inputs: A) => produces vtxoB, vtxoC |
| 22 | +// \ |
| 23 | +// \---> RedeemTx2 (inputs: B) => produces vtxoD, vtxoE |
| 24 | +// \ |
| 25 | +// \---> RedeemTx3 (inputs: C, E) => produces vtxoF (final) |
| 26 | +//--------------------------------------------- |
| 27 | + |
| 28 | +func TestBuildChain(t *testing.T) { |
| 29 | + //TODO: test more complex scenarios |
| 30 | + ctx := context.Background() |
| 31 | + |
| 32 | + // |
| 33 | + // 1) Build all the PSBTs needed |
| 34 | + // |
| 35 | + // Leaf: vtxoA is not pending => no RedeemTx. It's identified by (Txid="leafTx", VOut=0). |
| 36 | + // RedeemTx1 => references [ (leafTx,0) ]. |
| 37 | + redeemTx1, redeemTx1ID, err := makePsbtReferencingMany([]domain.VtxoKey{ |
| 38 | + {Txid: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", VOut: 0}, |
| 39 | + }) |
| 40 | + require.NoError(t, err) |
| 41 | + |
| 42 | + // RedeemTx2 => references [ (RedeemTx1,0) ] (i.e. vtxoB). |
| 43 | + redeemTx2, redeemTx2ID, err := makePsbtReferencingMany([]domain.VtxoKey{ |
| 44 | + {Txid: redeemTx1ID, VOut: 0}, |
| 45 | + }) |
| 46 | + require.NoError(t, err) |
| 47 | + |
| 48 | + // RedeemTx3 => references [ (RedeemTx1,1), (RedeemTx2,1) ] (i.e. vtxoC, vtxoE). |
| 49 | + redeemTx3, redeemTx3ID, err := makePsbtReferencingMany([]domain.VtxoKey{ |
| 50 | + {Txid: redeemTx1ID, VOut: 0}, |
| 51 | + {Txid: redeemTx2ID, VOut: 1}, |
| 52 | + }) |
| 53 | + require.NoError(t, err) |
| 54 | + |
| 55 | + mockData := map[domain.VtxoKey]domain.Vtxo{ |
| 56 | + {Txid: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", VOut: 0}: { |
| 57 | + VtxoKey: domain.VtxoKey{Txid: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", VOut: 0}, |
| 58 | + RoundTxid: "roundTxid", |
| 59 | + CreatedAt: time.Now().Add(-5 * time.Hour).Unix(), |
| 60 | + }, |
| 61 | + {Txid: redeemTx1ID, VOut: 0}: { |
| 62 | + VtxoKey: domain.VtxoKey{Txid: redeemTx1ID, VOut: 0}, |
| 63 | + RedeemTx: redeemTx1, |
| 64 | + CreatedAt: time.Now().Add(-4 * time.Hour).Unix(), |
| 65 | + }, |
| 66 | + {Txid: redeemTx1ID, VOut: 1}: { |
| 67 | + VtxoKey: domain.VtxoKey{Txid: redeemTx1ID, VOut: 1}, |
| 68 | + RedeemTx: redeemTx1, |
| 69 | + CreatedAt: time.Now().Add(-4 * time.Hour).Unix(), |
| 70 | + }, |
| 71 | + |
| 72 | + {Txid: redeemTx2ID, VOut: 0}: { |
| 73 | + VtxoKey: domain.VtxoKey{Txid: redeemTx2ID, VOut: 0}, |
| 74 | + RedeemTx: redeemTx2, |
| 75 | + CreatedAt: time.Now().Add(-3 * time.Hour).Unix(), |
| 76 | + }, |
| 77 | + {Txid: redeemTx2ID, VOut: 1}: { |
| 78 | + VtxoKey: domain.VtxoKey{Txid: redeemTx2ID, VOut: 1}, |
| 79 | + RedeemTx: redeemTx2, |
| 80 | + CreatedAt: time.Now().Add(-3 * time.Hour).Unix(), |
| 81 | + }, |
| 82 | + |
| 83 | + {Txid: redeemTx3ID, VOut: 0}: { |
| 84 | + VtxoKey: domain.VtxoKey{Txid: redeemTx3ID, VOut: 0}, |
| 85 | + RedeemTx: redeemTx3, |
| 86 | + CreatedAt: time.Now().Add(-1 * time.Hour).Unix(), |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + vtxoRepo := &MockVtxoRepo{data: mockData} |
| 91 | + repoManager := &MockRepoManager{vtxoRepo: vtxoRepo} |
| 92 | + svc := indexerService{repoManager: repoManager} |
| 93 | + |
| 94 | + resp, err := svc.GetVtxoChain( |
| 95 | + ctx, VtxoChainReq{ |
| 96 | + VtxoKey: Outpoint{ |
| 97 | + Txid: redeemTx3ID, |
| 98 | + Vout: 0, |
| 99 | + }, |
| 100 | + Page: PageReq{}, |
| 101 | + }, |
| 102 | + ) |
| 103 | + require.NoError(t, err, "buildChain should succeed") |
| 104 | + |
| 105 | + redeemTx3Txs := resp.Transactions[redeemTx3ID] |
| 106 | + assert.Equal(t, len(redeemTx3Txs), 2) |
| 107 | + assert.Equal(t, redeemTx3Txs[0], redeemTx1ID) |
| 108 | + assert.Equal(t, redeemTx3Txs[1], redeemTx2ID) |
| 109 | + |
| 110 | + redeemTx2Txs := resp.Transactions[redeemTx2ID] |
| 111 | + assert.Equal(t, len(redeemTx2Txs), 1) |
| 112 | + assert.Equal(t, redeemTx2Txs[0], redeemTx1ID) |
| 113 | + |
| 114 | + redeemTx1Txs := resp.Transactions[redeemTx1ID] |
| 115 | + assert.Equal(t, len(redeemTx1Txs), 1) |
| 116 | + assert.Equal(t, redeemTx1Txs[0], "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") |
| 117 | + |
| 118 | + leafTxTxs := resp.Transactions["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"] |
| 119 | + assert.Equal(t, len(leafTxTxs), 1) |
| 120 | + assert.Equal(t, leafTxTxs[0], "roundTxid") |
| 121 | +} |
| 122 | + |
| 123 | +type MockRepoManager struct { |
| 124 | + vtxoRepo *MockVtxoRepo |
| 125 | +} |
| 126 | + |
| 127 | +func (m *MockRepoManager) Events() domain.RoundEventRepository { panic("not implemented") } |
| 128 | +func (m *MockRepoManager) Rounds() domain.RoundRepository { panic("not implemented") } |
| 129 | +func (m *MockRepoManager) Vtxos() domain.VtxoRepository { return m.vtxoRepo } |
| 130 | +func (m *MockRepoManager) Notes() domain.NoteRepository { panic("not implemented") } |
| 131 | +func (m *MockRepoManager) Entities() domain.EntityRepository { panic("not implemented") } |
| 132 | +func (m *MockRepoManager) MarketHourRepo() domain.MarketHourRepo { panic("not implemented") } |
| 133 | +func (m *MockRepoManager) RegisterEventsHandler(func(*domain.Round)) { panic("not implemented") } |
| 134 | +func (m *MockRepoManager) Close() {} |
| 135 | + |
| 136 | +type MockVtxoRepo struct { |
| 137 | + data map[domain.VtxoKey]domain.Vtxo |
| 138 | +} |
| 139 | + |
| 140 | +func (m *MockVtxoRepo) AddVtxos(ctx context.Context, vtxos []domain.Vtxo) error { |
| 141 | + panic("not implemented") |
| 142 | +} |
| 143 | +func (m *MockVtxoRepo) SpendVtxos(ctx context.Context, vtxos []domain.VtxoKey, txid string) error { |
| 144 | + panic("not implemented") |
| 145 | +} |
| 146 | +func (m *MockVtxoRepo) RedeemVtxos(ctx context.Context, vtxos []domain.VtxoKey) error { |
| 147 | + panic("not implemented") |
| 148 | +} |
| 149 | +func (m *MockVtxoRepo) GetVtxos(ctx context.Context, vtxos []domain.VtxoKey) ([]domain.Vtxo, error) { |
| 150 | + var out []domain.Vtxo |
| 151 | + for _, k := range vtxos { |
| 152 | + if v, ok := m.data[k]; ok { |
| 153 | + out = append(out, v) |
| 154 | + } |
| 155 | + } |
| 156 | + return out, nil |
| 157 | +} |
| 158 | +func (m *MockVtxoRepo) GetVtxosForRound(ctx context.Context, txid string) ([]domain.Vtxo, error) { |
| 159 | + panic("not implemented") |
| 160 | +} |
| 161 | +func (m *MockVtxoRepo) SweepVtxos(ctx context.Context, vtxos []domain.VtxoKey) error { |
| 162 | + panic("not implemented") |
| 163 | +} |
| 164 | +func (m *MockVtxoRepo) GetAllNonRedeemedVtxos(ctx context.Context, pubkey string) ([]domain.Vtxo, []domain.Vtxo, error) { |
| 165 | + panic("not implemented") |
| 166 | +} |
| 167 | +func (m *MockVtxoRepo) GetAllSweepableVtxos(ctx context.Context) ([]domain.Vtxo, error) { |
| 168 | + panic("not implemented") |
| 169 | +} |
| 170 | +func (m *MockVtxoRepo) GetAll(ctx context.Context) ([]domain.Vtxo, error) { panic("not implemented") } |
| 171 | +func (m *MockVtxoRepo) UpdateExpireAt(ctx context.Context, vtxos []domain.VtxoKey, expireAt int64) error { |
| 172 | + panic("not implemented") |
| 173 | +} |
| 174 | +func (m *MockVtxoRepo) Close() {} |
| 175 | + |
| 176 | +func makePsbtReferencingMany(parents []domain.VtxoKey) (string, string, error) { |
| 177 | + tx := wire.NewMsgTx(wire.TxVersion) |
| 178 | + for _, p := range parents { |
| 179 | + parentHash, err := chainhash.NewHashFromStr(p.Txid) |
| 180 | + if err != nil { |
| 181 | + return "", "", err |
| 182 | + } |
| 183 | + txIn := wire.NewTxIn(wire.NewOutPoint(parentHash, p.VOut), nil, nil) |
| 184 | + tx.AddTxIn(txIn) |
| 185 | + } |
| 186 | + |
| 187 | + tx.AddTxOut(wire.NewTxOut(1000, []byte{})) |
| 188 | + |
| 189 | + p, err := psbt.NewFromUnsignedTx(tx) |
| 190 | + if err != nil { |
| 191 | + return "", "", err |
| 192 | + } |
| 193 | + |
| 194 | + ptxHex, err := p.B64Encode() |
| 195 | + if err != nil { |
| 196 | + return "", "", err |
| 197 | + } |
| 198 | + |
| 199 | + return ptxHex, tx.TxHash().String(), nil |
| 200 | +} |
0 commit comments