Skip to content

Commit 82a2ba8

Browse files
authored
Merge pull request #485 from mxaddict/patch-12
Added new attributes to the `getstakinginfo` and `getstakereport` commands
2 parents e393fe4 + 18cc110 commit 82a2ba8

4 files changed

Lines changed: 223 additions & 40 deletions

File tree

qa/rpc-tests/getstakereport.py

Lines changed: 118 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,14 @@ def run_test(self):
4848
# Create the cold address
4949
coldstaking_address_staking = self.nodes[1].getcoldstakingaddress(staking_address_public_key, spending_address_public_key)
5050

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

56-
# Turn staking on
57-
self.nodes[1].staking(True)
58-
5956
# Stake a block
6057
self.stake_block(self.nodes[1])
6158

62-
# Turn staking off again
63-
self.nodes[1].staking(False)
64-
6559
# Load the last 24h stake amount for the wallets/nodes
6660
merged_address_last_24h = self.nodes[0].getstakereport()['Last 24H']
6761
spending_address_last_24h = self.nodes[1].getstakereport()['Last 24H']
@@ -83,15 +77,9 @@ def run_test(self):
8377
self.nodes[1].generate(1)
8478
self.sync_all()
8579

86-
# Turn staking on
87-
self.nodes[2].staking(True)
88-
8980
# Stake a block
9081
self.stake_block(self.nodes[2])
9182

92-
# Turn staking off again
93-
self.nodes[2].staking(False)
94-
9583
# Load the last 24h stake amount for the wallets/nodes
9684
merged_address_last_24h = self.nodes[0].getstakereport()['Last 24H']
9785
spending_address_last_24h = self.nodes[1].getstakereport()['Last 24H']
@@ -108,10 +96,124 @@ def run_test(self):
10896
assert_equal('4.00', spending_address_last_24h)
10997
assert_equal('2.00', staking_address_last_24h)
11098

99+
# Time travel 2 days in the future
100+
cur_time = int(time.time())
101+
self.nodes[0].setmocktime(cur_time + 172800)
102+
self.nodes[1].setmocktime(cur_time + 172800)
103+
self.nodes[2].setmocktime(cur_time + 172800)
104+
105+
# Stake a block
106+
self.stake_block(self.nodes[2])
107+
108+
# Load the last 24h stake amount for the wallets/nodes
109+
merged_address_last_24h = self.nodes[0].getstakereport()['Last 24H']
110+
spending_address_last_24h = self.nodes[1].getstakereport()['Last 24H']
111+
staking_address_last_24h = self.nodes[2].getstakereport()['Last 24H']
112+
113+
# Check the amounts
114+
assert_equal('2.00', merged_address_last_24h)
115+
assert_equal('2.00', spending_address_last_24h)
116+
assert_equal('2.00', staking_address_last_24h)
117+
118+
# Load the last 7 days stake amount for the wallets/nodes
119+
merged_address_last_7d = self.nodes[0].getstakereport()['Last 7 Days']
120+
spending_address_last_7d = self.nodes[1].getstakereport()['Last 7 Days']
121+
staking_address_last_7d = self.nodes[2].getstakereport()['Last 7 Days']
122+
123+
# Check the amounts
124+
assert_equal('6.00', merged_address_last_7d)
125+
assert_equal('6.00', spending_address_last_7d)
126+
assert_equal('4.00', staking_address_last_7d)
127+
128+
# Load the averages for stake amounts
129+
avg_last7d = self.nodes[0].getstakereport()['Last 7 Days Avg']
130+
avg_last30d = self.nodes[0].getstakereport()['Last 30 Days Avg']
131+
avg_last365d = self.nodes[0].getstakereport()['Last 365 Days Avg']
132+
133+
# Check the amounts
134+
assert_equal('3.00', avg_last7d)
135+
assert_equal('3.00', avg_last30d)
136+
assert_equal('3.00', avg_last365d)
137+
138+
# Time travel 8 days in the future
139+
cur_time = int(time.time())
140+
self.nodes[0].setmocktime(cur_time + 691200)
141+
self.nodes[1].setmocktime(cur_time + 691200)
142+
self.nodes[2].setmocktime(cur_time + 691200)
143+
144+
# Load the last 24h stake amount for the wallets/nodes
145+
merged_address_last_24h = self.nodes[0].getstakereport()['Last 24H']
146+
spending_address_last_24h = self.nodes[1].getstakereport()['Last 24H']
147+
staking_address_last_24h = self.nodes[2].getstakereport()['Last 24H']
148+
149+
# Check the amounts
150+
assert_equal('0.00', merged_address_last_24h)
151+
assert_equal('0.00', spending_address_last_24h)
152+
assert_equal('0.00', staking_address_last_24h)
153+
154+
# Load the last 7 days stake amount for the wallets/nodes
155+
merged_address_last_7d = self.nodes[0].getstakereport()['Last 7 Days']
156+
spending_address_last_7d = self.nodes[1].getstakereport()['Last 7 Days']
157+
staking_address_last_7d = self.nodes[2].getstakereport()['Last 7 Days']
158+
159+
# Check the amounts
160+
assert_equal('2.00', merged_address_last_7d)
161+
assert_equal('2.00', spending_address_last_7d)
162+
assert_equal('2.00', staking_address_last_7d)
163+
164+
# Load the averages for stake amounts
165+
avg_last7d = self.nodes[0].getstakereport()['Last 7 Days Avg']
166+
avg_last30d = self.nodes[0].getstakereport()['Last 30 Days Avg']
167+
avg_last365d = self.nodes[0].getstakereport()['Last 365 Days Avg']
168+
169+
# Check the amounts
170+
assert_equal('0.28571428', avg_last7d)
171+
assert_equal('0.75', avg_last30d)
172+
assert_equal('0.75', avg_last365d)
173+
174+
# Time travel 31 days in the future
175+
cur_time = int(time.time())
176+
self.nodes[0].setmocktime(cur_time + 2678400)
177+
self.nodes[1].setmocktime(cur_time + 2678400)
178+
self.nodes[2].setmocktime(cur_time + 2678400)
179+
180+
# Load the last 24h stake amount for the wallets/nodes
181+
merged_address_last_24h = self.nodes[0].getstakereport()['Last 24H']
182+
spending_address_last_24h = self.nodes[1].getstakereport()['Last 24H']
183+
staking_address_last_24h = self.nodes[2].getstakereport()['Last 24H']
184+
185+
# Check the amounts
186+
assert_equal('0.00', merged_address_last_24h)
187+
assert_equal('0.00', spending_address_last_24h)
188+
assert_equal('0.00', staking_address_last_24h)
189+
190+
# Load the last 7 days stake amount for the wallets/nodes
191+
merged_address_last_7d = self.nodes[0].getstakereport()['Last 7 Days']
192+
spending_address_last_7d = self.nodes[1].getstakereport()['Last 7 Days']
193+
staking_address_last_7d = self.nodes[2].getstakereport()['Last 7 Days']
194+
195+
# Check the amounts
196+
assert_equal('0.00', merged_address_last_7d)
197+
assert_equal('0.00', spending_address_last_7d)
198+
assert_equal('0.00', staking_address_last_7d)
199+
200+
# Load the averages for stake amounts
201+
avg_last7d = self.nodes[0].getstakereport()['Last 7 Days Avg']
202+
avg_last30d = self.nodes[0].getstakereport()['Last 30 Days Avg']
203+
avg_last365d = self.nodes[0].getstakereport()['Last 365 Days Avg']
204+
205+
# Check the amounts
206+
assert_equal('0.00', avg_last7d)
207+
assert_equal('0.06666666', avg_last30d)
208+
assert_equal('0.19354838', avg_last365d)
209+
111210
def stake_block(self, node):
112211
# Get the current block count to check against while we wait for a stake
113212
blockcount = node.getblockcount()
114213

214+
# Turn staking on
215+
node.staking(True)
216+
115217
# wait for a new block to be mined
116218
while node.getblockcount() == blockcount:
117219
# print("waiting for a new block...")
@@ -120,6 +222,9 @@ def stake_block(self, node):
120222
# We got one
121223
# print("found a new block...")
122224

225+
# Turn staking off
226+
node.staking(False)
227+
123228
# Make sure the blocks are mature before we check the report
124229
slow_gen(node, 5, 0.5)
125230
self.sync_all()

qa/rpc-tests/getstakinginfo.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ def run_test(self):
4040

4141
# Check for staking after we have matured coins
4242
assert(self.nodes[0].getstakinginfo()['enabled'])
43-
assert(not self.nodes[0].getstakinginfo()['staking'])
43+
# Wait for the node to start staking
44+
while not self.nodes[0].getstakinginfo()['staking']:
45+
time.sleep(0.5)
46+
assert(self.nodes[0].getstakinginfo()['staking'])
4447
assert_equal("", self.nodes[0].getstakinginfo()['errors'])
4548

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

68+
# Check expecteddailyreward
69+
assert_equal(86400 / (self.nodes[0].getstakinginfo()['expectedtime'] + 1) * 2, self.nodes[0].getstakinginfo()['expecteddailyreward'])
70+
6571
# LOCK the wallet
6672
self.nodes[0].encryptwallet("password")
6773
stop_nodes(self.nodes)

src/rpc/net.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ UniValue getstakinginfo(const UniValue& params, bool fHelp)
644644
uint64_t nNetworkWeight = GetPoSKernelPS();
645645
bool staking = nLastCoinStakeSearchInterval && nWeight;
646646
uint64_t nExpectedTime = staking ? (GetTargetSpacing(pindexBestHeader->nHeight) * nNetworkWeight / nWeight) : 0;
647+
CAmount nExpectedDailyReward = staking ? ((double) 86400 / (nExpectedTime + 1)) * Params().GetConsensus().nStaticReward : 0.0;
647648

648649
UniValue obj(UniValue::VOBJ);
649650

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

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

665667
return obj;
666668
}

0 commit comments

Comments
 (0)