def test_register_address(): ''' test address registration with the directory server at 127.0.0.69:8081' ''' #test 1 ret = networknode.hclient( "http://127.0.0.69:8081", '{"jsonrpc":"2.0","method":"register_address","params":{"address":"127.0.0.19:8081"},"id":1}' ) result = (json.loads(ret))["result"] print(result) assert result.find("error") == -1 #test 2 ret = networknode.hclient( "http://127.0.0.69:8081", '{"jsonrpc":"2.0","method":"register_address","params":{"address":"127.0.0.19"},"id":2}' ) result = (json.loads(ret))["result"] print(result) assert result.find("error") >= 0 #test 3 ret = networknode.hclient( "http://127.0.0.69:8081", '{"jsonrpc":"2.0","method":"register_address","params":{"address":"127.0.0:8081"},"id":3}' ) result = (json.loads(ret))["result"] print(result) assert result.find("error") >= 0
def test_get_blockchain_height(): """ test get the height of some node's blockchain""" blocks = make_blocks(2) assert len(blocks) == 2 # clear the primary and secondary blockchains for this test ret = networknode.hclient( "http://127.0.0.51:8081", '{"jsonrpc": "2.0", "method": "clear_blockchain", "params": {}, "id": 10}' ) assert ret.find("ok") != -1 rpc = json.dumps({ "jsonrpc": "2.0", "method": "receive_block", "params": { "block": blocks[0] }, "id": 11 }) ret = networknode.hclient("http://127.0.0.51:8081", rpc) assert ret.find("ok") != -1 ret = networknode.hclient( "http://127.0.0.51:8081", '{"jsonrpc": "2.0", "method": "get_blockchain_height", "params": {}, "id": 12}' ) assert ret.find("error") == -1 height = (json.loads(ret))["result"] assert height == 0 rpc = json.dumps({ "jsonrpc": "2.0", "method": "receive_block", "params": { "block": blocks[1] }, "id": 13 }) ret = networknode.hclient("http://127.0.0.51:8081", rpc) assert ret.find("ok") != -1 ret = networknode.hclient( "http://127.0.0.51:8081", '{"jsonrpc": "2.0", "method": "get_blockchain_height", "params": {}, "id": 14}' ) assert ret.find("error") == -1 height = (json.loads(ret))["result"] assert height == 1
def propagate_transaction(txn: "dictionary"): """ propagates a transaction that is received """ if len(address_list) % 20 == 0: get_address_list() cmd = {} cmd["jsonrpc"] = "2.0" cmd["method"] = "receive_transaction" cmd["params"] = {"trx": txn} cmd["id"] = 0 for addr in address_list: rpc = json.dumps(cmd) networknode.hclient(addr, rpc) return
def propagate_mined_block(block): """ sends a block to other mining nodes so that they may add this block to their blockchain. We refresh the list of known node addresses periodically """ if len(address_list) % 20 == 0: get_address_list() cmd = {} cmd["jsonrpc"] = "2.0" cmd["method"] = "receive_block" cmd["params"] = {"block": block} cmd["id"] = 0 for addr in address_list: rpc = json.dumps(cmd) networknode.hclient(addr, rpc) return
def test_get_address_list(): ''' get an address list from a directory server. These are synthetic addresses, servers are not running at these addresses. ''' ret = networknode.hclient( "http://127.0.0.69:8081", '{"jsonrpc":"2.0","method":"get_address_list","params":{},"id":4}') assert (ret.find("error")) == -1 retd = json.loads(ret) assert "127.0.0.10:8081" in retd["result"] print(ret)
def test_receive_transaction(): """ send a transaction to a remote node. The remote node returns ok if the transaction is appended to its mempool. Otherwise returns an error string """ blocks = make_blocks(2) assert len(blocks) == 2 # clear the primary and secondary blockchains for this test ret = networknode.hclient( "http://127.0.0.51:8081", '{"jsonrpc":"2.0", "method": "clear_blockchain", "params": {}, "id": 15}' ) assert ret.find("ok") >= 0 rpc = json.dumps({ "jsonrpc": "2.0", "method": "receive_transaction", "params": { "trx": blocks[0]["tx"][0] }, "id": 16 }) ret = networknode.hclient("http://127.0.0.51:8081", rpc) assert ret.find("ok") >= 0 rpc = json.dumps({ "jsonrpc": "2.0", "method": "receive_transaction", "params": { "trx": blocks[0]["tx"][1] }, "id": 17 }) ret = networknode.hclient("http://127.0.0.51:8081", rpc) assert ret.find("ok") >= 0
def test_receive_block(): """ send a block to a remote node. The remote node returns ok if the block is appended to its received_blocks list, otherwise returns an error string """ blocks = make_blocks(2) assert len(blocks) == 2 # clear the primary and secondary blockchains for this test ret = networknode.hclient( "http://127.0.0.51:8081", '{"jsonrpc": "2.0", "method": "clear_blockchain", "params": {}, "id": 10}' ) assert ret.find("ok") != -1 rpc = json.dumps({ "jsonrpc": "2.0", "method": "receive_block", "params": { "block": blocks[0] }, "id": 8 }) ret = networknode.hclient("http://127.0.0.51:8081", rpc) assert ret.find("ok") != -1 rpc = json.dumps({ "jsonrpc": "2.0", "method": "receive_block", "params": { "block": blocks[1] }, "id": 9 }) ret = networknode.hclient("http://127.0.0.51:8081", rpc) assert ret.find("ok") != -1
def get_address_list(): ''' update the list of node addresses that are known AMEND IP ADDRESS AND PORT AS REQUIRED. ''' ret = networknode.hclient( "http://127.0.0.69:8081", '{"jsonrpc":"2.0","method":"get_address_list","params":{},"id":1}') if ret.find("error") != -1: return retd = json.loads(ret) for addr in retd["result"]: if address_list.count(addr) == 0: address_list.append(addr) return
def test_clear_blockchain(monkeypatch): """ builds and clears the primary and secondary blockchains of a node """ # clear the primary and secondary blockchains for this test ret = networknode.hclient( "http://127.0.0.51:8081", '{"jsonrpc":"2.0", "method": "clear_blockchain", "params": {}, "id": 19}' ) assert ret.find("ok") >= 0 monkeypatch.setattr(tx, "validate_transaction", lambda x, y: True) num_blocks = 20 blocks = make_blocks(num_blocks) assert len(blocks) == num_blocks tmp = hblockchain.blockheader_hash(blocks[0]) assert tmp == blocks[1]["prevblockhash"] assert blocks[1]["height"] == 1 rpc = json.dumps({ "jsonrpc": "2.0", "method": "receive_block", "params": { "block": blocks[0] }, "id": 21 }) ret = networknode.hclient("http://127.0.0.51:8081", rpc) assert ret.find("ok") != -1 ret = networknode.hclient( "http://127.0.0.51:8081", '{"jsonrpc": "2.0", "method": "get_blockchain_height", "params": {}, "id": 22}' ) assert ret.find("error") == -1 height = (json.loads(ret))["result"] # value of height attribute of the latest block in the blockchain # meaning there is one block in the blockchain assert height == 0 rpc = json.dumps({ "jsonrpc": "2.0", "method": "receive_block", "params": { "block": blocks[1] }, "id": 23 }) ret = networknode.hclient("http://127.0.0.51:8081", rpc) assert ret.find("ok") != -1 ret = networknode.hclient( "http://127.0.0.51:8081", '{"jsonrpc": "2.0", "method": "get_blockchain_height", "params": {}, "id": 24}' ) assert ret.find("error") == -1 height = (json.loads(ret))["result"] # value of height attribute of the latest block in the blockchain # meaning there are two blocks in the blockchain assert height == 1 # clear the primary and secondary blockchains for this test ret = networknode.hclient( "http://127.0.0.51:8081", '{"jsonrpc":"2.0", "method": "clear_blockchain", "params": {}, "id": 25}' ) assert ret.find("ok") >= 0