From a8ea07721e39a20ad63d7c61f8f03733ca9b49d4 Mon Sep 17 00:00:00 2001
From: Moul <moul@moul.re>
Date: Wed, 19 Oct 2022 21:34:58 +0200
Subject: [PATCH] Fix cmds requesting too quickly in a loop uids from pubkey
 with BMA (#438)

Specially happening in local condition, where there is no network
latency

blocks, history -u, and tx commands
Define BMA_SLEEP constant to 0.1 second
Set time.sleep(BMA_SLEEP) between the requests
---
 silkaj/blockchain/blocks.py | 4 +++-
 silkaj/constants.py         | 1 +
 silkaj/money/transfer.py    | 3 +++
 silkaj/wot/tools.py         | 3 +++
 4 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/silkaj/blockchain/blocks.py b/silkaj/blockchain/blocks.py
index 96ce7e0f..ed7434aa 100644
--- a/silkaj/blockchain/blocks.py
+++ b/silkaj/blockchain/blocks.py
@@ -13,6 +13,7 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
 
+import time
 from collections import OrderedDict
 from operator import itemgetter
 from typing import List
@@ -24,7 +25,7 @@ from pendulum import from_timestamp
 
 from silkaj import tui
 from silkaj.blockchain.tools import get_head_block
-from silkaj.constants import ALL
+from silkaj.constants import ALL, BMA_SLEEP
 from silkaj.network import client_instance
 from silkaj.wot.tools import identity_of
 
@@ -61,6 +62,7 @@ def list_blocks(number: int, detailed: bool) -> None:
         issuers.append(issuer)
     for pubkey in issuers_dict.keys():
         issuer = issuers_dict[pubkey]
+        time.sleep(BMA_SLEEP)
         try:
             idty = identity_of(issuer["pubkey"])
         except HTTPError:
diff --git a/silkaj/constants.py b/silkaj/constants.py
index fa0a4b37..3a889ae6 100644
--- a/silkaj/constants.py
+++ b/silkaj/constants.py
@@ -21,6 +21,7 @@ G1_TEST_DEFAULT_ENDPOINT = "BMAS g1-test.duniter.org 443"
 SUCCESS_EXIT_STATUS = 0
 FAILURE_EXIT_STATUS = 1
 BMA_MAX_BLOCKS_CHUNK_SIZE = 5000
+BMA_SLEEP = 0.1
 PUBKEY_MIN_LENGTH = 43
 PUBKEY_MAX_LENGTH = 44
 PUBKEY_PATTERN = f"[1-9A-HJ-NP-Za-km-z]{{{PUBKEY_MIN_LENGTH},{PUBKEY_MAX_LENGTH}}}"
diff --git a/silkaj/money/transfer.py b/silkaj/money/transfer.py
index 024ec0cb..bf93efdd 100644
--- a/silkaj/money/transfer.py
+++ b/silkaj/money/transfer.py
@@ -17,6 +17,7 @@
 import math
 import re
 import shlex
+import time
 from typing import List, Tuple
 
 import click
@@ -34,6 +35,7 @@ from duniterpy.key import SigningKey
 from silkaj import auth, network, public_key, tools, tui
 from silkaj.blockchain import tools as bc_tools
 from silkaj.constants import (
+    BMA_SLEEP,
     CENT_MULT_TO_UNIT,
     MINIMAL_ABSOLUTE_TX_AMOUNT,
     MINIMAL_RELATIVE_TX_AMOUNT,
@@ -357,6 +359,7 @@ def gen_confirmation_table(
     # display outputs and amounts
     for outputAddress, tx_amount in zip(outputAddresses, tx_amounts):
         m_tools.display_pubkey(tx, "To", outputAddress)
+        time.sleep(BMA_SLEEP)
         m_tools.display_amount(tx, "Amount", tx_amount, ud_value, currency_symbol)
     # display last informations
     if outputBackChange:
diff --git a/silkaj/wot/tools.py b/silkaj/wot/tools.py
index 566fe264..80296a48 100644
--- a/silkaj/wot/tools.py
+++ b/silkaj/wot/tools.py
@@ -13,6 +13,7 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
 
+import time
 import urllib
 from typing import Dict, List, Optional, Tuple
 from urllib.error import HTTPError
@@ -20,6 +21,7 @@ from urllib.error import HTTPError
 import click
 from duniterpy.api.bma import wot
 
+from silkaj.constants import BMA_SLEEP
 from silkaj.network import client_instance, exit_on_http_error
 from silkaj.public_key import gen_pubkey_checksum
 from silkaj.tui import Table
@@ -67,6 +69,7 @@ def identities_from_pubkeys(pubkeys: List[str], uids: bool) -> List:
     uniq_pubkeys = list(filter(None, set(pubkeys)))
     identities = []
     for pubkey in uniq_pubkeys:
+        time.sleep(BMA_SLEEP)
         try:
             identities.append(identity_of(pubkey))
         except HTTPError:
-- 
GitLab