From aad5900f80afc75c59b6e0e30b783d7ee925faa7 Mon Sep 17 00:00:00 2001 From: Hyeonyeob Kim Date: Mon, 25 May 2020 17:48:38 +0900 Subject: [PATCH] Reveal pre-images on a timer The having pre-image is currently being checked in FullNode.putTransaction to see if pre-image is required. It's call location is inefficient And that function is only needed by the validator. Therefore, it is checked using a timer that is called periodically. --- source/agora/node/FullNode.d | 10 ---------- source/agora/node/Validator.d | 24 ++++++++++++++++++++++++ source/agora/test/EnrollmentManager.d | 17 ++--------------- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/source/agora/node/FullNode.d b/source/agora/node/FullNode.d index beeeb71a762..3c7e9bcb6ca 100644 --- a/source/agora/node/FullNode.d +++ b/source/agora/node/FullNode.d @@ -217,16 +217,6 @@ public class FullNode : API this.network.gossipTransaction(tx); this.onAcceptedTransaction(); } - - if (this.enroll_man.needRevealPreimage(this.ledger.getBlockHeight())) - { - PreImageInfo preimage; - if (this.enroll_man.getNextPreimage(preimage)) - { - this.receivePreimage(preimage); - this.enroll_man.increaseNextRevealHeight(); - } - } } /// GET: /has_transaction_hash diff --git a/source/agora/node/Validator.d b/source/agora/node/Validator.d index c7af2f8f170..8d46c3dbc9f 100644 --- a/source/agora/node/Validator.d +++ b/source/agora/node/Validator.d @@ -33,6 +33,8 @@ import scpd.types.Stellar_SCP; import ocean.util.log.Logger; +import core.time; + mixin AddLogger!(); /******************************************************************************* @@ -81,6 +83,8 @@ public class Validator : FullNode, API log.info("Doing network discovery.."); this.network.discover(required_peer_keys); + // call checkRevealPreimage periodically every 10 seconds + this.taskman.timer(10.seconds, &this.checkRevealPreimage, Periodic.Yes); this.network.startPeriodicCatchup(this.ledger, &this.nominator.isNominating); }); } @@ -171,4 +175,24 @@ public class Validator : FullNode, API foreach (sub_conf; quorum_conf.quorums) buildRequiredKeys(filter, sub_conf, nodes); } + + /*************************************************************************** + + Periodically check for pre-images revelation + Increase the next reveal height by revelation period if necessary. + + ***************************************************************************/ + + private void checkRevealPreimage () @safe + { + if (!this.enroll_man.needRevealPreimage(this.ledger.getBlockHeight())) + return; + + PreImageInfo preimage; + if (this.enroll_man.getNextPreimage(preimage)) + { + this.receivePreimage(preimage); + this.enroll_man.increaseNextRevealHeight(); + } + } } diff --git a/source/agora/test/EnrollmentManager.d b/source/agora/test/EnrollmentManager.d index 6a34dd599b8..81f42efe94b 100644 --- a/source/agora/test/EnrollmentManager.d +++ b/source/agora/test/EnrollmentManager.d @@ -89,21 +89,8 @@ unittest txs2.each!(tx => node_1.putTransaction(tx)); containSameBlocks(nodes, 2).retryFor(8.seconds); - // Currently a new transaction needs to be sent to trigger - // the reveal of a pre-image - // See https://github.com/bpfkorea/agora/issues/582 - Transaction tx = - { - TxType.Payment, - [Input(hashFull(txs2[0]), 0)], - [Output(Amount(100), pubkey_1)] - }; - auto signature = gen_key_pair.secret.sign(hashFull(tx)[]); - tx.inputs[0].signature = signature; - node_1.putTransaction(tx); - - // check if nodes have a pre-image newly sent - // during creating transactions for the new block + // Check if nodes have a pre-image newly sent + // While the timer is running on the taskmanager nodes.each!(node => retryFor(node.getPreimage(enroll.utxo_key) != PreImageInfo.init, 5.seconds));