diff --git a/sto/cli/main.py b/sto/cli/main.py index 12d6c94..8272d5a 100644 --- a/sto/cli/main.py +++ b/sto/cli/main.py @@ -445,8 +445,11 @@ def last(config: BoardCommmadConfiguration, limit): @cli.command(name="tx-restart-nonce") +@click.option('--reset-pending-tx', required=False, help="Delete's any pending tx", type=bool, default=None) @click.pass_obj -def restart_nonce(config: BoardCommmadConfiguration): +def restart_nonce(config: BoardCommmadConfiguration, reset_pending_tx): + + """Resets the broadcasting account nonce.""" assert is_ethereum_network(config.network) @@ -463,7 +466,8 @@ def restart_nonce(config: BoardCommmadConfiguration): ethereum_node_url=config.ethereum_node_url, ethereum_private_key=config.ethereum_private_key, ethereum_gas_limit=config.ethereum_gas_limit, - ethereum_gas_price=config.ethereum_gas_price) + ethereum_gas_price=config.ethereum_gas_price, + reset_pending_tx=reset_pending_tx) @cli.command(name="tx-next-nonce") @click.pass_obj diff --git a/sto/ethereum/nonce.py b/sto/ethereum/nonce.py index 59d70c6..fc27135 100644 --- a/sto/ethereum/nonce.py +++ b/sto/ethereum/nonce.py @@ -25,7 +25,10 @@ def restart_nonce(logger: Logger, ethereum_private_key: str, ethereum_gas_limit: str, ethereum_gas_price: str, + reset_pending_tx: bool, + ): + check_good_private_key(ethereum_private_key) web3 = create_web3(ethereum_node_url) @@ -36,8 +39,9 @@ def restart_nonce(logger: Logger, account = service.get_or_create_broadcast_account() txs = service.get_last_transactions(limit=1) - if txs.count() > 0: - raise HistoryDeleteNeeded("Cannot reset nonce as the database contains txs for {}. Delete database to restart.".format(service.address)) + if txs.count() > 0 and reset_pending_tx = True: + service.delete_pending_broadcasts() + # read nonce from the network and record to the database tx_count = web3.eth.getTransactionCount(service.address) diff --git a/sto/ethereum/txservice.py b/sto/ethereum/txservice.py index 4fc00dd..7ee7e93 100644 --- a/sto/ethereum/txservice.py +++ b/sto/ethereum/txservice.py @@ -88,7 +88,6 @@ def get_next_nonce(self): def ensure_accounts_in_sync(self): """Make sure that our internal nonce and external nonce looks correct.""" - broadcast_account = self.get_or_create_broadcast_account() tx_count = self.web3.eth.getTransactionCount(broadcast_account.address) @@ -96,6 +95,8 @@ def ensure_accounts_in_sync(self): if tx_count != broadcast_account.current_nonce: NetworkAndDatabaseNonceOutOfSync("Nonced out of sync. Network: {}, database: {}. Maybe you have a pending broadcasts propagating?".format(tx_count, broadcast_account.current_nonce)) + + def allocate_transaction(self, broadcast_account: _BroadcastAccount, receiver: Optional[str], @@ -319,6 +320,14 @@ def get_pending_broadcasts(self) -> Query: """All transactions that need to be broadcasted.""" return self.dbsession.query(self.prepared_tx_model).filter_by(broadcasted_at=None).order_by(self.prepared_tx_model.nonce).join(self.broadcast_account_model).filter_by(network=self.network) + + + def delete_pending_broadcasts(self) -> Query: + """All transactions that need to be deleted.""" + return self.dbsession.delete(self.prepared_tx_model).filter_by(broadcasted_at=None).order_by(self.prepared_tx_model.nonce).join(self.broadcast_account_model).filter_by(network=self.network) + + + def get_unmined_txs(self) -> Query: """All transactions that do not yet have a block assigned.""" return self.dbsession.query(self.prepared_tx_model).filter(self.prepared_tx_model.txid != None).filter_by(result_block_num=None).join(self.broadcast_account_model).filter_by(network=self.network)