Skip to content

Commit 4c60a4d

Browse files
andreasgriffinrustaceanrob
authored andcommitted
test: add python custom persistence
Here we check that an arbitrary persister can persist and load a wallet correctly. The test applies two unconfirmed transactions and asserts that a first wallet that persists the changes and a second wallet that loads from these changes will have the same values.
1 parent e1db09f commit 4c60a4d

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import unittest
2+
from bdkpython import Descriptor, ChangeSet, Network, Persistence, Persister, Transaction, UnconfirmedTx, Wallet
3+
4+
initial_txs = [
5+
"0200000000010101d7eb881ab8cac7d6adc6a7f9aa13e694813d95330c7299cee3623e5d14bd590000000000fdffffff02c5e6c1010000000016001407103a1cccf6a1ea654bee964a4020d20c41fb055c819725010000001600146337ec04bf42015e5d077b90cae05c06925c491a0247304402206aae2bf32da4c3b71cb95e6633c22f9f5a4a4f459975965c0c39b0ab439737b702200c4b16d2029383190965b07adeb87e6d634c68c70d2742f25e456874e8dc273a012103930326d6d72f8663340ce4341d0d3bdb1a1c0734d46e5df8a3003ab6bb50073b00000000",
6+
"02000000000101b0db431cffebeeeeec19ee8a09a2ae4755722ea73232dbb99b8e754eaad6ac300100000000fdffffff024ad24201000000001600146a7b71a68b261b0b7c79e5bb00f0f3d65d5ae4a285ae542401000000160014e43ff61232ca20061ef1d241e73f322a149a23d902473044022059f4b2fa8b9da34dbb57e491f3d5b8a47a623d7e6ebc1b6adfe6d2be744c9640022073cfc8311c49a8d48d69076466d32be591d3c0092b965828cfbcaca69fd409c90121027aa62d03db46272fa31bc1a6cb095bb66bc5409dd74b25e88e3099d84a17a3e469000000",
7+
]
8+
descriptor: Descriptor = Descriptor(
9+
"wpkh([44250c36/84'/1'/0']tpubDCrUjjHLB1fxk1oRveETjw62z8jsUuqx7JkBUW44VBszGmcY3Eun3apwVcE5X2bfF5MsM3uvuQDed6Do33ZN8GiWcnj2QPqVDspFT1AyZJ9/0/*)",
10+
Network.REGTEST,
11+
)
12+
change_descriptor: Descriptor = Descriptor(
13+
"wpkh([44250c36/84'/1'/0']tpubDCrUjjHLB1fxk1oRveETjw62z8jsUuqx7JkBUW44VBszGmcY3Eun3apwVcE5X2bfF5MsM3uvuQDed6Do33ZN8GiWcnj2QPqVDspFT1AyZJ9/1/*)",
14+
Network.REGTEST,
15+
)
16+
17+
18+
class MyPersistence(Persistence):
19+
def __init__(self):
20+
self.memory = []
21+
22+
def merge_all(self) -> ChangeSet:
23+
total_changeset = ChangeSet()
24+
for changeset_dict in self.memory:
25+
total_changeset = ChangeSet.from_merge(total_changeset, changeset_dict)
26+
return total_changeset
27+
28+
def initialize(self) -> ChangeSet:
29+
return self.merge_all()
30+
31+
def persist(self, changeset: ChangeSet):
32+
self.memory.append(changeset)
33+
34+
35+
class PersistenceTest(unittest.TestCase):
36+
37+
def test_synced_transactions(self):
38+
39+
myp = MyPersistence()
40+
persister = Persister.custom(myp)
41+
42+
wallet: Wallet = Wallet(descriptor, change_descriptor, Network.REGTEST, persister)
43+
44+
wallet.apply_unconfirmed_txs(
45+
[UnconfirmedTx(tx=Transaction(bytes.fromhex(tx)), last_seen=0) for tx in initial_txs]
46+
)
47+
48+
wallet.persist(persister=persister)
49+
50+
# initialize new wallet with memory of myp
51+
myp2 = MyPersistence()
52+
myp2.memory = myp.memory.copy()
53+
persister2 = Persister.custom(myp2)
54+
55+
wallet2 = Wallet.load(
56+
descriptor=descriptor,
57+
change_descriptor=change_descriptor,
58+
persister=persister2,
59+
)
60+
61+
# check for equality
62+
outputs = wallet.list_output()
63+
outputs2 = wallet2.list_output()
64+
assert len(outputs) == len(outputs2)
65+
for o, o2 in zip(outputs, outputs2):
66+
assert o.outpoint.txid == o2.outpoint.txid
67+
assert o.outpoint.vout == o2.outpoint.vout
68+
69+
txs = wallet.transactions()
70+
txs2 = wallet2.transactions()
71+
assert txs, "Sync error"
72+
assert len(txs) == len(txs2)
73+
for tx, tx2 in zip(txs, txs2):
74+
assert tx.transaction.compute_txid().serialize() == tx2.transaction.compute_txid().serialize()
75+
76+
assert wallet.balance().total.to_sat() == 50641167
77+
78+
79+
if __name__ == "__main__":
80+
unittest.main()

0 commit comments

Comments
 (0)