Skip to content

Commit

Permalink
Only count stakes in main chain (navcoin#636)
Browse files Browse the repository at this point in the history
* Only count stakes in main chain

* Fixed a function call

* Updated the test to include the scenario for navcoin#636
  • Loading branch information
alex v committed Nov 20, 2019
1 parent 3e8eb00 commit ab941f5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
49 changes: 42 additions & 7 deletions qa/rpc-tests/getstakereport.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def run_test(self):
assert_equal('6.00', spending_address_last_7d)
assert_equal('4.00', staking_address_last_7d)

# Load the averages for stake amounts
# Load the averages for stake amounts
avg_last7d = self.nodes[0].getstakereport()['Last 7 Days Avg']
avg_last30d = self.nodes[0].getstakereport()['Last 30 Days Avg']
avg_last365d = self.nodes[0].getstakereport()['Last 365 Days Avg']
Expand Down Expand Up @@ -161,7 +161,7 @@ def run_test(self):
assert_equal('2.00', spending_address_last_7d)
assert_equal('2.00', staking_address_last_7d)

# Load the averages for stake amounts
# Load the averages for stake amounts
avg_last7d = self.nodes[0].getstakereport()['Last 7 Days Avg']
avg_last30d = self.nodes[0].getstakereport()['Last 30 Days Avg']
avg_last365d = self.nodes[0].getstakereport()['Last 365 Days Avg']
Expand Down Expand Up @@ -197,7 +197,7 @@ def run_test(self):
assert_equal('0.00', spending_address_last_7d)
assert_equal('0.00', staking_address_last_7d)

# Load the averages for stake amounts
# Load the averages for stake amounts
avg_last7d = self.nodes[0].getstakereport()['Last 7 Days Avg']
avg_last30d = self.nodes[0].getstakereport()['Last 30 Days Avg']
avg_last365d = self.nodes[0].getstakereport()['Last 365 Days Avg']
Expand All @@ -207,7 +207,34 @@ def run_test(self):
assert_equal('0.06666666', avg_last30d)
assert_equal('0.19354838', avg_last365d)

def stake_block(self, node):
# Disconnect the nodes
for node in self.nodes[0].getpeerinfo():
self.nodes[0].disconnectnode(node['addr'])
time.sleep(2) #disconnecting a node needs a little bit of time
assert(self.nodes[0].getpeerinfo() == [])

# Stake a block on node 0
orphaned_block_hash = self.stake_block(self.nodes[0], False)

# Generate some blocks on node 1
self.nodes[1].generate(100)

# Reconnect the nodes
connect_nodes(self.nodes[0], 1)
connect_nodes(self.nodes[1], 2)
connect_nodes(self.nodes[2], 0)

# Wait for blocks to sync
self.sync_all()

# Make sure the block was orphaned
assert(self.nodes[0].getblock(orphaned_block_hash)['confirmations'] == -1)

# Check the staked amount
# Should be 0 (Zero) as the last staked block is orphaned
assert_equal('0.00', self.nodes[0].getstakereport()['Last 7 Days'])

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

Expand All @@ -225,9 +252,17 @@ def stake_block(self, node):
# Turn staking off
node.staking(False)

# Make sure the blocks are mature before we check the report
slow_gen(node, 5, 0.5)
self.sync_all()
# Get the staked block
block_hash = node.getbestblockhash()

# Only mature the blocks if we asked for it
if (mature):
# Make sure the blocks are mature before we check the report
slow_gen(node, 5, 0.5)
self.sync_all()

# return the block hash to the function caller
return block_hash


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3186,7 +3186,7 @@ bool IsTxCountedAsStaked(const CWalletTx* tx)
LOCK(cs_main);

// orphan block or immature
if ((!tx->GetDepthInMainChain()) || (tx->GetBlocksToMaturity() > 0))
if ((!tx->GetDepthInMainChain()) || (tx->GetBlocksToMaturity() > 0) || !tx->IsInMainChain())
return false;

// abandoned transactions
Expand Down

0 comments on commit ab941f5

Please sign in to comment.