async def test_balance(self): address = await self.account.receiving.get_or_create_usable_address() hash160 = self.ledger.address_to_hash160(address) tx = Transaction(is_verified=True)\ .add_outputs([Output.pay_pubkey_hash(100, hash160)]) await self.ledger.db.insert_transaction(tx) await self.ledger.db.save_transaction_io( tx, address, hash160, f'{tx.id}:1:' ) self.assertEqual(await self.account.get_balance(), 100) tx = Transaction(is_verified=True)\ .add_outputs([Output.pay_claim_name_pubkey_hash(100, 'foo', b'', hash160)]) await self.ledger.db.insert_transaction(tx) await self.ledger.db.save_transaction_io( tx, address, hash160, f'{tx.id}:1:' ) self.assertEqual(await self.account.get_balance(), 100) # claim names don't count towards balance self.assertEqual(await self.account.get_balance(include_claims=True), 200)
async def test_get_utxo(self): address = yield self.account.receiving.get_or_create_usable_address() hash160 = self.ledger.address_to_hash160(address) tx = Transaction(is_verified=True)\ .add_outputs([Output.pay_pubkey_hash(100, hash160)]) await self.ledger.db.save_transaction_io('insert', tx, address, hash160, f'{tx.id}:1:') utxos = await self.account.get_utxos() self.assertEqual(len(utxos), 1) tx = Transaction(is_verified=True)\ .add_inputs([Input.spend(utxos[0])]) await self.ledger.db.save_transaction_io('insert', tx, address, hash160, f'{tx.id}:1:') self.assertEqual(await self.account.get_balance(include_claims=True), 0) utxos = await self.account.get_utxos() self.assertEqual(len(utxos), 0)
async def test_sign(self): account = Account.from_dict( self.ledger, Wallet(), { "seed": "carbon smart garage balance margin twelve chest sword toas" "t envelope bottom stomach absent" }) await account.ensure_address_gap() address1, address2 = await account.receiving.get_addresses(limit=2) pubkey_hash1 = self.ledger.address_to_hash160(address1) pubkey_hash2 = self.ledger.address_to_hash160(address2) tx = Transaction() \ .add_inputs([Input.spend(get_output(int(2*COIN), pubkey_hash1))]) \ .add_outputs([Output.pay_pubkey_hash(int(1.9*COIN), pubkey_hash2)]) await tx.sign([account]) self.assertEqual( hexlify(tx.inputs[0].script.values['signature']), b'304402200dafa26ad7cf38c5a971c8a25ce7d85a076235f146126762296b1223c42ae21e022020ef9eeb8' b'398327891008c5c0be4357683f12cb22346691ff23914f457bf679601')
def get_transaction(txo=None): return Transaction() \ .add_inputs([get_input()]) \ .add_outputs([txo or Output.pay_pubkey_hash(CENT, NULL_HASH32)])
def get_output(amount=CENT, pubkey_hash=NULL_HASH32, height=-2): return Transaction(height=height) \ .add_outputs([Output.pay_pubkey_hash(amount, pubkey_hash)]) \ .outputs[0]
async def test_nodes_with_same_account_stay_in_sync(self): # destination node/account for receiving TXs node0 = await self.make_wallet_node() account0 = node0.account # main node/account creating TXs node1 = self.wallet_node account1 = self.wallet_node.account # mirror node/account, expected to reflect everything in main node as it happens node2 = await self.make_wallet_node(account1.seed) account2 = node2.account self.assertNotEqual(account0.id, account1.id) self.assertEqual(account1.id, account2.id) await self.assertBalance(account0, '0.0') await self.assertBalance(account1, '0.0') await self.assertBalance(account2, '0.0') self.assertEqual(await account0.get_address_count(chain=0), 20) self.assertEqual(await account1.get_address_count(chain=0), 20) self.assertEqual(await account2.get_address_count(chain=0), 20) self.assertEqual(await account1.get_address_count(chain=1), 6) self.assertEqual(await account2.get_address_count(chain=1), 6) # check that main node and mirror node generate 5 address to fill gap fifth_address = (await account1.receiving.get_addresses())[4] await self.blockchain.send_to_address(fifth_address, 1.00) await asyncio.wait([ account1.ledger.on_address.first, account2.ledger.on_address.first ]) self.assertEqual(await account1.get_address_count(chain=0), 25) self.assertEqual(await account2.get_address_count(chain=0), 25) await self.assertBalance(account1, '1.0') await self.assertBalance(account2, '1.0') await self.generate(1) # pay 0.01 from main node to receiving node, would have increased change addresses address0 = (await account0.receiving.get_addresses())[0] hash0 = self.ledger.address_to_hash160(address0) tx = await Transaction.create([], [Output.pay_pubkey_hash(CENT, hash0)], [account1], account1) await self.broadcast(tx) await asyncio.wait([ account0.ledger.wait(tx), account1.ledger.wait(tx), account2.ledger.wait(tx), ]) await self.generate(1) await asyncio.wait([ account0.ledger.wait(tx), account1.ledger.wait(tx), account2.ledger.wait(tx), ]) self.assertEqual(await account0.get_address_count(chain=0), 21) self.assertGreater(await account1.get_address_count(chain=1), 6) self.assertGreater(await account2.get_address_count(chain=1), 6) await self.assertBalance(account0, '0.01') await self.assertBalance(account1, '0.989876') await self.assertBalance(account2, '0.989876') await self.generate(1) # create a new mirror node and see if it syncs to same balance from scratch node3 = await self.make_wallet_node(account1.seed) account3 = node3.account await self.assertBalance(account3, '0.989876')