Skip to content

Commit 7b31332

Browse files
committed
signet/miner: add Generate.gbt function
1 parent 85c5c0b commit 7b31332

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

contrib/signet/miner

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,22 @@ class Generate:
290290
# can't mine a block 2h in the future; 1h55m for some safety
291291
self.action_time = max(self.action_time, self.mine_time - 6900)
292292

293+
def gbt(self, bcli, bestblockhash, now):
294+
tmpl = json.loads(bcli("getblocktemplate", '{"rules":["signet","segwit"]}'))
295+
if tmpl["previousblockhash"] != bestblockhash:
296+
logging.warning("GBT based off unexpected block (%s not %s), retrying", tmpl["previousblockhash"], bci["bestblockhash"])
297+
time.sleep(1)
298+
return None
299+
300+
if tmpl["mintime"] > self.mine_time:
301+
logging.info("Updating block time from %d to %d", self.mine_time, tmpl["mintime"])
302+
self.mine_time = tmpl["mintime"]
303+
if self.mine_time > now:
304+
logging.error("GBT mintime is in the future: %d is %d seconds later than %d", self.mine_time, (self.mine_time-now), now)
305+
return None
306+
307+
return tmpl
308+
293309
def do_generate(args):
294310
if args.max_blocks is not None:
295311
if args.ongoing:
@@ -389,21 +405,12 @@ def do_generate(args):
389405
continue
390406

391407
# gbt
392-
tmpl = json.loads(args.bcli("getblocktemplate", '{"rules":["signet","segwit"]}'))
393-
if tmpl["previousblockhash"] != bci["bestblockhash"]:
394-
logging.warning("GBT based off unexpected block (%s not %s), retrying", tmpl["previousblockhash"], bci["bestblockhash"])
395-
time.sleep(1)
408+
tmpl = gen.gbt(args.bcli, bci["bestblockhash"], now)
409+
if tmpl is None:
396410
continue
397411

398412
logging.debug("GBT template: %s", tmpl)
399413

400-
if tmpl["mintime"] > gen.mine_time:
401-
logging.info("Updating block time from %d to %d", gen.mine_time, tmpl["mintime"])
402-
gen.mine_time = tmpl["mintime"]
403-
if gen.mine_time > now:
404-
logging.error("GBT mintime is in the future: %d is %d seconds later than %d", gen.mine_time, (gen.mine_time-now), now)
405-
return 1
406-
407414
# address for reward
408415
reward_addr, reward_spk = get_reward_addr_spk(args, tmpl["height"])
409416

0 commit comments

Comments
 (0)