def test_airdrop(): bank = Bank(id=0, private_key=identities.bank_private_key(0)) tx = identities.airdrop_tx() bank.airdrop(tx) assert 500_000 == bank.fetch_balance(identities.alice_public_key) assert 500_000 == bank.fetch_balance(identities.bob_public_key)
def main(args): if args["serve"]: global bank bank_id = int(os.environ["BANK_ID"]) bank = Bank(id=bank_id, private_key=bank_private_key(bank_id)) bank.airdrop(airdrop_tx()) bank.schedule_next_block() serve() elif args["ping"]: send_message("ping", "") elif args["balance"]: name = args["<name>"] public_key = user_public_key(name) send_message("balance", public_key) elif args["tx"]: sender_private_key = user_private_key(args["<from>"]) recipient_public_key = user_public_key(args["<to>"]) amount = int(args["<amount>"]) sender_public_key = sender_private_key.get_verifying_key() utxo_response = send_message("utxo", sender_public_key) utxo = utxo_response["data"] tx = prepare_simple_tx(utxo, sender_private_key, recipient_public_key, amount) response = send_message("tx", tx) print("response") else: print("invalid command")
def main(args): # Can't use 0.0.0.0 from the outside to talk to the docker containers local_address = ("127.0.0.1", 5000) if args["serve"]: global bank bank_id = int(os.environ["BANK_ID"]) bank = Bank( id=bank_id, private_key=bank_private_key(bank_id), ) bank.airdrop(airdrop_tx()) bank.schedule_next_block() serve() elif args["ping"]: response = send_message(local_address, "ping", "", response=True) print(f"Response to ping: {response}") elif args["balance"]: name = args["<name>"] public_key = user_public_key(name) response = send_message(local_address, "balance", public_key, response=True) print(f"Response to balance: {response}") elif args["tx"]: # Grab parameters sender_private_key = user_private_key(args["<from>"]) sender_public_key = sender_private_key.get_verifying_key() recipient_private_key = user_private_key(args["<to>"]) recipient_public_key = recipient_private_key.get_verifying_key() amount = int(args["<amount>"]) # Fetch utxos available to spend response = send_message(local_address, "utxos", sender_public_key, response=True) utxos = response["data"] # Prepare transaction tx = prepare_simple_tx(utxos, sender_private_key, recipient_public_key, amount) # send to bank response = send_message(local_address, "tx", tx, response=True) print(f"Response to tx: {response}") else: print("Invalid commands")
def test_bad_tx(): bank = Bank(id=0, private_key=identities.bank_private_key(0)) tx = identities.airdrop_tx() bank.airdrop(tx) tx = prepare_simple_tx( utxos=bank.fetch_utxos(identities.alice_public_key), sender_private_key=identities.alice_private_key, recipient_public_key=identities.bob_public_key, amount=10, ) # Put in a phony signature tx.tx_ins[0].signature = identities.alice_private_key.sign(b"bad") with pytest.raises(ecdsa.keys.BadSignatureError): bank.handle_tx(tx)
def test_utxo(): bank = Bank(id=0, private_key=identities.bank_private_key(0)) tx = identities.airdrop_tx() bank.airdrop(tx) assert len(bank.blocks) == 1 # Alice sends 10 to Bob tx = prepare_simple_tx(utxos=bank.fetch_utxos(identities.alice_public_key), sender_private_key=identities.alice_private_key, recipient_public_key=identities.bob_public_key, amount=10) block = Block(txns=[tx]) block.sign(identities.bank_private_key(1)) bank.handle_block(block) assert 500_000 - 10 == bank.fetch_balance(identities.alice_public_key) assert 500_000 + 10 == bank.fetch_balance(identities.bob_public_key)
def main(args): if args["serve"]: global bank bank_id = int(os.environ["BANK_ID"]) bank = Bank(id=bank_id, private_key=bank_private_key(bank_id)) bank.airdrop(airdrop_tx()) bank.schedule_next_block() serve() elif args["ping"]: response = send_message(ADDRESS, "ping", "", response=True) print(response) elif args["balance"]: name = args["<name>"] address = external_address(args["--node"]) public_key = user_public_key(name) response = send_message(address, "balance", public_key, response=True) print(response) elif args["tx"]: # Grab parameters nodes = args["--node"].split(',') addresses = list(map(external_address, nodes)) sender_private_key = user_private_key(args["<from>"]) sender_public_key = sender_private_key.get_verifying_key() recipient_private_key = user_private_key(args["<to>"]) recipient_public_key = recipient_private_key.get_verifying_key() amount = int(args["<amount>"]) # Fetch utxos available to spend response = send_message(addresses[0], "utxos", sender_public_key, response=True) utxos = response["data"] # Prepare transaction tx = prepare_simple_tx(utxos, sender_private_key, recipient_public_key, amount) # send to bank for addr in addresses: response = send_message(addr, "tx", tx, response=True) print(response) else: print("Invalid commands")
def main(args): if args["serve"]: global bank bank_id = int(os.environ["BANK_ID"]) bank = Bank(id=bank_id, private_key=bank_private_key(bank_id)) # Airdrop starting balances bank.airdrop(airdrop_tx()) # Start producing blocks bank.schedule_next_block() serve() elif args["ping"]: address = address_from_host(args["--node"]) send_message(address, "ping", "") elif args["balance"]: public_key = user_public_key(args["<name>"]) address = external_address(args["--node"]) response = send_message(address, "balance", public_key, response=True) print(response["data"]) elif args["tx"]: # Grab parameters sender_private_key = user_private_key(args["<from>"]) sender_public_key = sender_private_key.get_verifying_key() recipient_private_key = user_private_key(args["<to>"]) recipient_public_key = recipient_private_key.get_verifying_key() amount = int(args["<amount>"]) address = external_address(args["--node"]) # Fetch utxos available to spend response = send_message(address, "utxos", sender_public_key, response=True) utxos = response["data"] # Prepare transaction tx = prepare_simple_tx(utxos, sender_private_key, recipient_public_key, amount) # send to bank send_message(address, "tx", tx) else: print("Invalid command")