Skip to content

Commit c9db1a6

Browse files
committed
Add simple wallet tests
1 parent da5080d commit c9db1a6

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

node/tests/wallet_tests.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use std::str::FromStr;
2+
use protocol::bitcoin::{Address, Amount};
3+
use spaced::rpc::RpcClient;
4+
use spaced::wallets::AddressKind;
5+
use testutil::TestRig;
6+
7+
async fn setup() -> anyhow::Result<TestRig> {
8+
// we need to mine at least 101 blocks
9+
let rig = TestRig::new().await?;
10+
rig.mine_blocks(101, None).await?;
11+
Ok(rig)
12+
}
13+
14+
async fn it_should_create_and_fund_wallet(rig: &TestRig) -> anyhow::Result<()> {
15+
let name = "example".to_string();
16+
rig.spaced.client.wallet_create(name.clone()).await?;
17+
18+
// get an address from the wallet to fund it
19+
let addr = Address::from_str(
20+
&rig.spaced
21+
.client
22+
.wallet_get_new_address(name.clone(), AddressKind::Coin)
23+
.await?,
24+
)?
25+
.assume_checked();
26+
// have the rig send some coins
27+
rig.send(&addr, Amount::from_sat(1000_000)).await?;
28+
// mine the transaction
29+
rig.mine_blocks(1, None).await?;
30+
// wait for the wallet to sync
31+
rig.wait_until_wallet_synced(&name).await?;
32+
33+
let balance = rig.spaced.client.wallet_get_balance(name.clone()).await?;
34+
assert_eq!(
35+
balance.confirmed.total,
36+
Amount::from_sat(1000_000),
37+
"expected balance to match"
38+
);
39+
40+
Ok(())
41+
}
42+
43+
async fn it_should_handle_simple_reorg(rig: &TestRig) -> anyhow::Result<()> {
44+
// we mined the funding transaction on block 102
45+
// lets mark this block as invalid. This will
46+
// return any transactions that are still valid back
47+
// to the mempool. So lets re-mine two empty blocks
48+
// afterwards and see what the wallet thinks
49+
rig.invalidate_blocks(1).await?;
50+
rig.mine_empty_block().await?;
51+
rig.mine_empty_block().await?;
52+
assert_eq!(103, rig.get_block_count().await?);
53+
54+
let name = "example".to_string();
55+
rig.wait_until_wallet_synced(&name).await?;
56+
57+
let balance = rig.spaced.client.wallet_get_balance(name.clone()).await?;
58+
assert_eq!(
59+
balance.confirmed.total,
60+
Amount::from_sat(0),
61+
"expected balance to match"
62+
);
63+
64+
// Now lets mine a block which will pull back our tx from mempool
65+
rig.mine_blocks(1, None).await?;
66+
rig.wait_until_wallet_synced(&name).await?;
67+
68+
let balance = rig.spaced.client.wallet_get_balance(name.clone()).await?;
69+
assert_eq!(
70+
balance.confirmed.total,
71+
Amount::from_sat(1000_000),
72+
"expected balance to match"
73+
);
74+
Ok(())
75+
}
76+
77+
#[tokio::test]
78+
async fn run_wallet_tests() -> anyhow::Result<()> {
79+
let rig = setup().await?;
80+
it_should_create_and_fund_wallet(&rig).await?;
81+
it_should_handle_simple_reorg(&rig).await?;
82+
Ok(())
83+
}

0 commit comments

Comments
 (0)