Skip to content

Commit

Permalink
Merge pull request #17 from NAVCoin/master
Browse files Browse the repository at this point in the history
merging master to get PR's #450 and #459
  • Loading branch information
proletesseract committed May 2, 2019
2 parents 27c9476 + 96198f9 commit d6f723b
Show file tree
Hide file tree
Showing 20 changed files with 263 additions and 111 deletions.
1 change: 1 addition & 0 deletions qa/pull-tester/rpc-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
'cfund-paymentrequestvotelist.py',
'reject-version-bit.py',
'getcoldstakingaddress.py',
'getstakereport.py',
'coldstaking_staking.py',
'coldstaking_spending.py',
'staticr-staking-amount.py',
Expand Down
61 changes: 26 additions & 35 deletions qa/rpc-tests/coldstaking_spending.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#!/usr/bin/env python3
# copyright (c) 2018 the navcoin core developers
# distributed under the mit software license, see the accompanying
# file copying or http://www.opensource.org/licenses/mit-license.php.
# Copyright (c) 2018 The Navcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

import decimal
from test_framework.test_framework import NavCoinTestFramework
from test_framework.util import *
from test_framework.staticr_util import *

class ColdStakingSpending(NavCoinTestFramework):
"""Tests spending and staking to/from a spending wallet."""
# set up num of nodes
Expand All @@ -20,23 +22,12 @@ def setup_network(self, split=False):
def run_test(self):
self.nodes[0].staking(False)

"""generate first 300 blocks to lock in softfork, verify coldstaking is active"""

slow_gen(self.nodes[0], 100)
# verify that cold staking has started
assert(self.nodes[0].getblockchaininfo()["bip9_softforks"]["coldstaking"]["status"] == "started")
slow_gen(self.nodes[0], 100)
# verify that cold staking is locked_in
assert(self.nodes[0].getblockchaininfo()["bip9_softforks"]["coldstaking"]["status"] == "locked_in")
slow_gen(self.nodes[0], 100)
# verify that cold staking is active
assert(self.nodes[0].getblockchaininfo()["bip9_softforks"]["coldstaking"]["status"] == "active")

"""set up transaction-related constants and addresses"""
# Make it to the static rewards fork!
activate_staticr(self.nodes[0])

# declare transaction-related constants
SENDING_FEE= 0.00010000
MIN_COLDSTAKING_SENDING_FEE = 0.0028850
MIN_COLDSTAKING_SENDING_FEE = 0.0033947
BLOCK_REWARD = 50
# generate address owned by the wallet
spending_address_public_key = self.nodes[0].getnewaddress()
Expand All @@ -55,7 +46,7 @@ def run_test(self):
balance_before_send = self.nodes[0].getbalance()
staking_weight_before_send = self.nodes[0].getstakinginfo()["weight"]
# check wallet staking weight roughly equals wallet balance
assert(round(staking_weight_before_send / 100000000.0, -5) == round(balance_before_send, -5))
assert_equal(round(staking_weight_before_send / 100000000.0, -5), round(balance_before_send, -5))

"""send navcoin to our coldstaking address, grab balance & staking weight"""

Expand All @@ -70,22 +61,22 @@ def run_test(self):
assert(len(listunspent_txs) > 0)
# asserts that the number of utxo recieved is only 1:
assert(len(listunspent_txs) == 1)
# asserts if amount recieved is what it should be; ~59812449.99711600 NAV
assert(listunspent_txs[0]["amount"] <= Decimal('59812449.99711600'))
# asserts if amount recieved is what it should be; ~59814699.99660530 NAV
assert_equal(listunspent_txs[0]["amount"], Decimal('59814699.99660530'))
# grabs updated wallet balance and staking weight
balance_post_send_one = self.nodes[0].getbalance()
staking_weight_post_send = self.nodes[0].getstakinginfo()["weight"]

"""check balance decreased by just the fees"""

# difference between balance after sending and previous balance is the same when block reward is removed
# values are converted to string and "00" is added to right of == operand because values must have equal num of
# values are converted to string and "00" is added to right of == operand because values must have equal num of
# decimals
assert(str(balance_post_send_one - BLOCK_REWARD) <= (str(float(balance_before_send) - MIN_COLDSTAKING_SENDING_FEE) + "00"))

"""check staking weight now == 0 (we don't hold the staking key)"""
# sent ~all funds to coldstaking address where we do not own the staking key hence our

# sent ~all funds to coldstaking address where we do not own the staking key hence our
# staking weight will be 0 as our recieved BLOCK_REWARD navcoin isn't mature enough to count towards
# our staking weight
assert((staking_weight_post_send / 100000000.0) - BLOCK_REWARD <= 1)
Expand All @@ -98,10 +89,10 @@ def run_test(self):
to_be_sent = round(float(balance_post_send_one) * float(0.5) - SENDING_FEE, 8)
self.nodes[0].sendtoaddress(address_Y_public_key, (to_be_sent))
# put transaction in new block & update blockchain
slow_gen(self.nodes[0], 1)
slow_gen(self.nodes[0], 1)
# wallet balance after sending
balance_post_send_two = self.nodes[0].getbalance()
#check balance will not be less than ~half our balance before sending - this
#check balance will not be less than ~half our balance before sending - this
# will occurs if we send to an address we do not own
assert(balance_post_send_two - BLOCK_REWARD >= (float(balance_post_send_one) * float(0.5) - SENDING_FEE))

Expand All @@ -111,7 +102,7 @@ def run_test(self):
self.nodes[0].sendtoaddress(coldstaking_address_spending, round(float(balance_post_send_two) - SENDING_FEE, 8))
slow_gen(self.nodes[0], 1)
listunspent_txs = [n for n in self.nodes[0].listunspent() if n["address"] == coldstaking_address_spending]
# send funds to a third party address using a signed raw transaction
# send funds to a third party address using a signed raw transaction
# get unspent tx inputs
self.send_raw_transaction(decoded_raw_transaction = listunspent_txs[0], \
to_address = address_Y_public_key, \
Expand All @@ -120,7 +111,7 @@ def run_test(self):
)
# put transaction in new block & update blockchain
slow_gen(self.nodes[0], 1)
# get new balance
# get new balance
balance_post_send_three = self.nodes[0].getbalance()
# we expect our balance to be zero
assert(balance_post_send_three - (BLOCK_REWARD * 2) == 0)
Expand All @@ -138,13 +129,13 @@ def run_test(self):
self.nodes[0].sendtoaddress(spending_address_public_key, float(current_balance) * 0.5 - 1)
slow_gen(self.nodes[0], 1)
# our balance should be the same minus fees, as we own the address we sent to
assert(self.nodes[0].getbalance() >= current_balance - 1 + BLOCK_REWARD)
assert(self.nodes[0].getbalance() >= current_balance - 1 + BLOCK_REWARD)
send_worked = True
except Exception as e:
print(e)

assert(send_worked == True)

slow_gen(self.nodes[0], 1)

# send to our staking address
Expand All @@ -155,11 +146,11 @@ def run_test(self):
self.nodes[0].sendtoaddress(staking_address_public_key, float(self.nodes[0].getbalance()) * 0.5 - 1)
slow_gen(self.nodes[0], 1)
# our balance should be half minus fees, as we dont own the address we sent to
assert(self.nodes[0].getbalance() - BLOCK_REWARD <= float(current_balance) * 0.5 - 1 + 2)
assert(self.nodes[0].getbalance() - BLOCK_REWARD <= float(current_balance) * 0.5 - 1 + 2)
send_worked = True
except Exception as e:
print(e)

assert(send_worked == True)

def send_raw_transaction(self, decoded_raw_transaction, to_address, change_address, amount):
Expand All @@ -172,7 +163,7 @@ def send_raw_transaction(self, decoded_raw_transaction, to_address, change_addre
assert(signresult["complete"])
# send raw transaction
return self.nodes[0].sendrawtransaction(signresult['hex'])


if __name__ == '__main__':
ColdStakingSpending().main()
Expand Down
129 changes: 129 additions & 0 deletions qa/rpc-tests/getstakereport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#!/usr/bin/env python3
# Copyright (c) 2018 The Navcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

from test_framework.test_framework import NavCoinTestFramework
from test_framework.staticr_util import *

#import time

class GetStakeReport(NavCoinTestFramework):
"""Tests getstakereport accounting."""

def __init__(self):
super().__init__()
self.setup_clean_chain = True
self.num_nodes = 3

def setup_network(self, split=False):
self.nodes = self.setup_nodes()
connect_nodes(self.nodes[0], 1)
connect_nodes(self.nodes[1], 2)
connect_nodes(self.nodes[2], 0)
self.is_network_split = False

def run_test(self):
# Turn off staking until we need it
self.nodes[0].staking(False)
self.nodes[1].staking(False)
self.nodes[2].staking(False)

# Make it to the static rewards fork!
activate_staticr(self.nodes[0])
self.sync_all()

# Use THE spending address
spending_address_public_key = self.nodes[1].getnewaddress()
spending_address_private_key = self.nodes[1].dumpprivkey(spending_address_public_key)

# Create a staking address
staking_address_public_key = self.nodes[2].getnewaddress()
staking_address_private_key = self.nodes[2].dumpprivkey(staking_address_public_key)

# Import the 2 keys into a third wallet
self.nodes[0].importprivkey(spending_address_private_key)
self.nodes[0].importprivkey(staking_address_private_key)

# Create the cold address
coldstaking_address_staking = self.nodes[1].getcoldstakingaddress(staking_address_public_key, spending_address_public_key)

# Send funds to the spending address (leave some NAV for fees)
self.nodes[0].sendtoaddress(spending_address_public_key, self.nodes[0].getbalance() - 1)
self.nodes[0].generate(1)
self.sync_all()

# Turn staking on
self.nodes[1].staking(True)

# Stake a block
self.stake_block(self.nodes[1])

# Turn staking off again
self.nodes[1].staking(False)

# Load the last 24h stake amount for the wallets/nodes
merged_address_last_24h = self.nodes[0].getstakereport()['Last 24H']
spending_address_last_24h = self.nodes[1].getstakereport()['Last 24H']
staking_address_last_24h = self.nodes[2].getstakereport()['Last 24H']
# print('spending', spending_address_last_24h)
# print('staking', staking_address_last_24h)
# print('merged', merged_address_last_24h)

# Make sure we have staked 2 NAV to the spending address
# So that means spending last 24h == 2
# And staking last 24h == 0 We have not sent any coins yet
# And merged will have the total of the spending + staking
assert_equal('2.00', merged_address_last_24h)
assert_equal('2.00', spending_address_last_24h)
assert_equal('0.00', staking_address_last_24h)

# Send funds to the cold staking address (leave some NAV for fees)
self.nodes[1].sendtoaddress(coldstaking_address_staking, self.nodes[1].getbalance() - 1)
self.nodes[1].generate(1)
self.sync_all()

# Turn staking on
self.nodes[2].staking(True)

# Stake a block
self.stake_block(self.nodes[2])

# Turn staking off again
self.nodes[2].staking(False)

# Load the last 24h stake amount for the wallets/nodes
merged_address_last_24h = self.nodes[0].getstakereport()['Last 24H']
spending_address_last_24h = self.nodes[1].getstakereport()['Last 24H']
staking_address_last_24h = self.nodes[2].getstakereport()['Last 24H']
# print('spending', spending_address_last_24h)
# print('staking', staking_address_last_24h)
# print('merged', merged_address_last_24h)

# Make sure we staked 4 NAV in spending address (2 NAV via COLD Stake)
# So that means spending last 24h == 4
# And staking last 24h == 2 We stake 2 NAV via COLD already
# And merged will have the total of the spending + staking
assert_equal('4.00', merged_address_last_24h)
assert_equal('4.00', spending_address_last_24h)
assert_equal('2.00', staking_address_last_24h)

def stake_block(self, node):
# Get the current block count to check against while we wait for a stake
blockcount = node.getblockcount()

# wait for a new block to be mined
while node.getblockcount() == blockcount:
# print("waiting for a new block...")
time.sleep(1)

# We got one
# print("found a new block...")

# Make sure the blocks are mature before we check the report
slow_gen(node, 5, 0.5)
self.sync_all()


if __name__ == '__main__':
GetStakeReport().main()
24 changes: 12 additions & 12 deletions qa/rpc-tests/mempool_spendcoinbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,33 @@ def setup_network(self):
self.is_network_split = False

def run_test(self):
slow_gen(self.nodes[0], 200)
slow_gen(self.nodes[0], 10)
chain_height = self.nodes[0].getblockcount()
assert_equal(chain_height, 200)
assert_equal(chain_height, 10)
node0_address = self.nodes[0].getnewaddress()

# Coinbase at height chain_height-150+1 ok in mempool, should
# get mined. Coinbase at height chain_height-150+2 is
# Coinbase at height chain_height-5+1 ok in mempool, should
# get mined. Coinbase at height chain_height-5+2 is
# is too immature to spend.
b = [ self.nodes[0].getblockhash(n) for n in range(151, 153) ]
b = [ self.nodes[0].getblockhash(n) for n in range(6, 8) ]
coinbase_txids = [ self.nodes[0].getblock(h)['tx'][0] for h in b ]
spends_raw = [ create_tx(self.nodes[0], txid, node0_address, 49.99) for txid in coinbase_txids ]

spend_151_id = self.nodes[0].sendrawtransaction(spends_raw[0])
spend_6_id = self.nodes[0].sendrawtransaction(spends_raw[0])

# coinbase at height 152 should be too immature to spend
# coinbase at height 7 should be too immature to spend
assert_raises(JSONRPCException, self.nodes[0].sendrawtransaction, spends_raw[1])

# mempool should have just spend_151:
assert_equal(self.nodes[0].getrawmempool(), [ spend_151_id ])
# mempool should have just spend_6:
assert_equal(self.nodes[0].getrawmempool(), [ spend_6_id ])

# mine a block, spend_151 should get confirmed
slow_gen(self.nodes[0], 1)
assert_equal(set(self.nodes[0].getrawmempool()), set())

# ... and now height 152 can be spent:
spend_152_id = self.nodes[0].sendrawtransaction(spends_raw[1])
assert_equal(self.nodes[0].getrawmempool(), [ spend_152_id ])
# ... and now height 7 can be spent:
spend_7_id = self.nodes[0].sendrawtransaction(spends_raw[1])
assert_equal(self.nodes[0].getrawmempool(), [ spend_7_id ])

if __name__ == '__main__':
MempoolSpendCoinbaseTest().main()
8 changes: 4 additions & 4 deletions qa/rpc-tests/sendtoaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ def run_test (self):
slow_gen(self.nodes[0], 1)
time.sleep(2)
self.sync_all()
slow_gen(self.nodes[1], 75)
slow_gen(self.nodes[1], 30)
self.sync_all()

# Assert correct amount of NAV generated
assert_equal(self.nodes[0].getbalance(), 59800000)
assert_equal(self.nodes[1].getbalance(), 1250)

# Make transactions to valid addresses
txid0 = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 60)
txid1 = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), Decimal("10.0"))
Expand Down Expand Up @@ -155,4 +155,4 @@ def run_test (self):


if __name__ == '__main__':
SendToAddressTest ().main ()
SendToAddressTest ().main ()
6 changes: 3 additions & 3 deletions qa/rpc-tests/stakeimmaturebalance.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/python3

from test_framework.test_framework import NavCoinTestFramework
from test_framework.util import *
from test_framework.staticr_util import *
import logging

'''
Expand All @@ -10,7 +10,7 @@
node0 checks that immature balance does not affect stake weight
'''

SENDING_FEE= 0.003
SENDING_FEE= 0.003393
BLOCK_REWARD = 50

logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO, stream=sys.stdout)
Expand All @@ -28,7 +28,7 @@ def setup_network(self, split=False):

def run_test(self):
addr = self.nodes[1].getnewaddress()
slow_gen(self.nodes[0], 300) #300 for soft fork voting
activate_staticr(self.nodes[0])
self.nodes[0].sendtoaddress(addr, satoshi_round(float(self.nodes[0].getbalance()) - SENDING_FEE))
slow_gen(self.nodes[0], 1)
logging.info('Checking stake weight')
Expand Down

0 comments on commit d6f723b

Please sign in to comment.