Skip to content

Commit

Permalink
Merge pull request #485 from mxaddict/patch-12
Browse files Browse the repository at this point in the history
Added new attributes to the `getstakinginfo` and `getstakereport` commands
  • Loading branch information
mxaddict authored Jun 12, 2019
2 parents e393fe4 + 18cc110 commit 82a2ba8
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 40 deletions.
131 changes: 118 additions & 13 deletions qa/rpc-tests/getstakereport.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,14 @@ def run_test(self):
# 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)
# Send funds to the spending address (leave me 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']
Expand All @@ -83,15 +77,9 @@ def run_test(self):
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']
Expand All @@ -108,10 +96,124 @@ def run_test(self):
assert_equal('4.00', spending_address_last_24h)
assert_equal('2.00', staking_address_last_24h)

# Time travel 2 days in the future
cur_time = int(time.time())
self.nodes[0].setmocktime(cur_time + 172800)
self.nodes[1].setmocktime(cur_time + 172800)
self.nodes[2].setmocktime(cur_time + 172800)

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

# 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']

# Check the amounts
assert_equal('2.00', merged_address_last_24h)
assert_equal('2.00', spending_address_last_24h)
assert_equal('2.00', staking_address_last_24h)

# Load the last 7 days stake amount for the wallets/nodes
merged_address_last_7d = self.nodes[0].getstakereport()['Last 7 Days']
spending_address_last_7d = self.nodes[1].getstakereport()['Last 7 Days']
staking_address_last_7d = self.nodes[2].getstakereport()['Last 7 Days']

# Check the amounts
assert_equal('6.00', merged_address_last_7d)
assert_equal('6.00', spending_address_last_7d)
assert_equal('4.00', staking_address_last_7d)

# 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']

# Check the amounts
assert_equal('3.00', avg_last7d)
assert_equal('3.00', avg_last30d)
assert_equal('3.00', avg_last365d)

# Time travel 8 days in the future
cur_time = int(time.time())
self.nodes[0].setmocktime(cur_time + 691200)
self.nodes[1].setmocktime(cur_time + 691200)
self.nodes[2].setmocktime(cur_time + 691200)

# 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']

# Check the amounts
assert_equal('0.00', merged_address_last_24h)
assert_equal('0.00', spending_address_last_24h)
assert_equal('0.00', staking_address_last_24h)

# Load the last 7 days stake amount for the wallets/nodes
merged_address_last_7d = self.nodes[0].getstakereport()['Last 7 Days']
spending_address_last_7d = self.nodes[1].getstakereport()['Last 7 Days']
staking_address_last_7d = self.nodes[2].getstakereport()['Last 7 Days']

# Check the amounts
assert_equal('2.00', merged_address_last_7d)
assert_equal('2.00', spending_address_last_7d)
assert_equal('2.00', staking_address_last_7d)

# 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']

# Check the amounts
assert_equal('0.28571428', avg_last7d)
assert_equal('0.75', avg_last30d)
assert_equal('0.75', avg_last365d)

# Time travel 31 days in the future
cur_time = int(time.time())
self.nodes[0].setmocktime(cur_time + 2678400)
self.nodes[1].setmocktime(cur_time + 2678400)
self.nodes[2].setmocktime(cur_time + 2678400)

# 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']

# Check the amounts
assert_equal('0.00', merged_address_last_24h)
assert_equal('0.00', spending_address_last_24h)
assert_equal('0.00', staking_address_last_24h)

# Load the last 7 days stake amount for the wallets/nodes
merged_address_last_7d = self.nodes[0].getstakereport()['Last 7 Days']
spending_address_last_7d = self.nodes[1].getstakereport()['Last 7 Days']
staking_address_last_7d = self.nodes[2].getstakereport()['Last 7 Days']

# Check the amounts
assert_equal('0.00', merged_address_last_7d)
assert_equal('0.00', spending_address_last_7d)
assert_equal('0.00', staking_address_last_7d)

# 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']

# Check the amounts
assert_equal('0.00', avg_last7d)
assert_equal('0.06666666', avg_last30d)
assert_equal('0.19354838', avg_last365d)

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

# Turn staking on
node.staking(True)

# wait for a new block to be mined
while node.getblockcount() == blockcount:
# print("waiting for a new block...")
Expand All @@ -120,6 +222,9 @@ def stake_block(self, node):
# We got one
# print("found a new block...")

# 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()
Expand Down
8 changes: 7 additions & 1 deletion qa/rpc-tests/getstakinginfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ def run_test(self):

# Check for staking after we have matured coins
assert(self.nodes[0].getstakinginfo()['enabled'])
assert(not self.nodes[0].getstakinginfo()['staking'])
# Wait for the node to start staking
while not self.nodes[0].getstakinginfo()['staking']:
time.sleep(0.5)
assert(self.nodes[0].getstakinginfo()['staking'])
assert_equal("", self.nodes[0].getstakinginfo()['errors'])

# Get the current block count to check against while we wait for a stake
Expand All @@ -62,6 +65,9 @@ def run_test(self):
assert(self.nodes[0].getstakinginfo()['staking'])
assert_equal("", self.nodes[0].getstakinginfo()['errors'])

# Check expecteddailyreward
assert_equal(86400 / (self.nodes[0].getstakinginfo()['expectedtime'] + 1) * 2, self.nodes[0].getstakinginfo()['expecteddailyreward'])

# LOCK the wallet
self.nodes[0].encryptwallet("password")
stop_nodes(self.nodes)
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ UniValue getstakinginfo(const UniValue& params, bool fHelp)
uint64_t nNetworkWeight = GetPoSKernelPS();
bool staking = nLastCoinStakeSearchInterval && nWeight;
uint64_t nExpectedTime = staking ? (GetTargetSpacing(pindexBestHeader->nHeight) * nNetworkWeight / nWeight) : 0;
CAmount nExpectedDailyReward = staking ? ((double) 86400 / (nExpectedTime + 1)) * Params().GetConsensus().nStaticReward : 0.0;

UniValue obj(UniValue::VOBJ);

Expand All @@ -661,6 +662,7 @@ UniValue getstakinginfo(const UniValue& params, bool fHelp)
obj.push_back(Pair("netstakeweight", (uint64_t)nNetworkWeight));

obj.push_back(Pair("expectedtime", nExpectedTime));
obj.push_back(Pair("expecteddailyreward", (double) nExpectedDailyReward / COIN));

return obj;
}
Expand Down
Loading

0 comments on commit 82a2ba8

Please sign in to comment.