Skip to content

Commit

Permalink
[tests] improve logging and documentation in interface_rest.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jnewbery authored and romanz committed Apr 3, 2018
1 parent abf190e commit 3fd4490
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions test/functional/interface_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def setup_network(self, split=False):

def run_test(self):
url = urllib.parse.urlparse(self.nodes[0].url)
self.log.info("Mining blocks...")

self.log.info("Mine blocks and send Bitcoin to node 1")

self.nodes[0].generate(1)
self.sync_all()
Expand All @@ -70,9 +71,10 @@ def run_test(self):
self.sync_all()
bb_hash = self.nodes[0].getbestblockhash()

assert_equal(self.nodes[1].getbalance(), Decimal("0.1")) # balance now should be 0.1 on node 1
assert_equal(self.nodes[1].getbalance(), Decimal("0.1"))

self.log.info("Load the transaction using the /tx URI")

# load the latest 0.1 tx over the REST API
json_string = http_get_call(url.hostname, url.port, '/rest/tx/' + txid + self.FORMAT_SEPARATOR + "json")
json_obj = json.loads(json_string)
vintx = json_obj['vin'][0]['txid'] # get the vin to later check for utxo (should be spent by then)
Expand All @@ -82,9 +84,8 @@ def run_test(self):
if vout['value'] == 0.1:
n = vout['n']

#######################################
# GETUTXOS: query an unspent outpoint #
#######################################
self.log.info("Query an unspent TXO using the /getutxos URI")

json_request = '/' + txid + '-' + str(n)
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json')
json_obj = json.loads(json_string)
Expand All @@ -96,9 +97,8 @@ def run_test(self):
assert_equal(len(json_obj['utxos']), 1)
assert_equal(json_obj['utxos'][0]['value'], 0.1)

#################################################
# GETUTXOS: now query an already spent outpoint #
#################################################
self.log.info("Query a spent TXO using the /getutxos URI")

json_request = '/' + vintx + '-0'
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json')
json_obj = json.loads(json_string)
Expand All @@ -112,16 +112,16 @@ def run_test(self):
# Check bitmap
assert_equal(json_obj['bitmap'], "0")

##################################################
# GETUTXOS: now check both with the same request #
##################################################
self.log.info("Query two TXOs using the /getutxos URI")

json_request = '/' + txid + '-' + str(n) + '/' + vintx + '-0'
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json')
json_obj = json.loads(json_string)
assert_equal(len(json_obj['utxos']), 1)
assert_equal(json_obj['bitmap'], "10")

# Test binary response
self.log.info("Query the TXOs using the /getutxos URI with a binary response")

bb_hash = self.nodes[0].getbestblockhash()

bin_request = b'\x01\x02'
Expand All @@ -140,9 +140,10 @@ def run_test(self):
assert_equal(bb_hash, response_hash) # check if getutxo's chaintip during calculation was fine
assert_equal(chain_height, 102) # chain height must be 102

############################
# GETUTXOS: mempool checks #
############################
self.log.info("Test the /getutxos URI with and without /checkmempool")
# Create a transaction, check that it's found with /checkmempool, but
# not found without. Then confirm the transaction and check that it's
# found with or without /checkmempool.

# do a tx and don't sync
txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1)
Expand All @@ -160,35 +161,35 @@ def run_test(self):
json_request = '/' + spending
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json')
json_obj = json.loads(json_string)
assert_equal(len(json_obj['utxos']), 0) # there should be no outpoint because it has just added to the mempool
assert_equal(len(json_obj['utxos']), 0)

json_request = '/checkmempool/' + spending
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json')
json_obj = json.loads(json_string)
assert_equal(len(json_obj['utxos']), 1) # there should be an outpoint because it has just added to the mempool
assert_equal(len(json_obj['utxos']), 1)

json_request = '/' + spent
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json')
json_obj = json.loads(json_string)
assert_equal(len(json_obj['utxos']), 1) # there should be an outpoint because its spending tx is not confirmed
assert_equal(len(json_obj['utxos']), 1)

json_request = '/checkmempool/' + spent
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json')
json_obj = json.loads(json_string)
assert_equal(len(json_obj['utxos']), 0) # there should be no outpoint because it has just spent (by mempool tx)
assert_equal(len(json_obj['utxos']), 0)

self.nodes[0].generate(1)
self.sync_all()

json_request = '/' + spending
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json')
json_obj = json.loads(json_string)
assert_equal(len(json_obj['utxos']), 1) # there should be an outpoint because it was mined
assert_equal(len(json_obj['utxos']), 1)

json_request = '/checkmempool/' + spending
json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json')
json_obj = json.loads(json_string)
assert_equal(len(json_obj['utxos']), 1) # there should be an outpoint because it was mined
assert_equal(len(json_obj['utxos']), 1)

# Do some invalid requests
json_request = '{"checkmempool'
Expand Down Expand Up @@ -220,9 +221,7 @@ def run_test(self):
self.nodes[0].generate(1) # generate block to not affect upcoming tests
self.sync_all()

################
# /rest/block/ #
################
self.log.info("Test the /block and /headers URIs")

# Check binary format
response = http_get_call(url.hostname, url.port, '/rest/block/' + bb_hash + self.FORMAT_SEPARATOR + "bin", True)
Expand Down Expand Up @@ -279,7 +278,8 @@ def run_test(self):
json_obj = json.loads(response_header_json_str)
assert_equal(len(json_obj), 5) # now we should have 5 header objects

# Do tx test
self.log.info("Test the /tx URI")

tx_hash = block_json_obj['tx'][0]['txid']
json_string = http_get_call(url.hostname, url.port, '/rest/tx/' + tx_hash + self.FORMAT_SEPARATOR + "json")
json_obj = json.loads(json_string)
Expand All @@ -290,8 +290,9 @@ def run_test(self):
assert_equal(hex_string.status, 200)
assert_greater_than(int(response.getheader('content-length')), 10)

# Check block tx details
# Let's make 3 tx and mine them on node 1
self.log.info("Test tx inclusion in the /mempool and /block URIs")

# Make 3 tx and mine them on node 1
txs = []
txs.append(self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11))
txs.append(self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11))
Expand Down Expand Up @@ -330,7 +331,8 @@ def run_test(self):
for tx in txs:
assert_equal(tx in json_obj['tx'], True)

# Test rest bestblock
self.log.info("Test the /chaininfo URI")

bb_hash = self.nodes[0].getbestblockhash()

json_string = http_get_call(url.hostname, url.port, '/rest/chaininfo.json')
Expand Down

0 comments on commit 3fd4490

Please sign in to comment.