Skip to content

Commit f3e3ee5

Browse files
authored
Update block time to be the same as linea and add retries for token cast send functions (#111)
1 parent e5bb677 commit f3e3ee5

File tree

2 files changed

+92
-72
lines changed

2 files changed

+92
-72
lines changed

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ services:
2424
--host=0.0.0.0
2525
--accounts=520
2626
--allow-origin=*
27-
--block-time=12
27+
--block-time=3
2828
--chain-id=1234
2929
--gas-limit=30000000
3030
--gas-price=1

tools/token-mint-service/init_node_tokens.py

Lines changed: 91 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -78,82 +78,102 @@ def wait_for_transaction(self, tx_hash: str, timeout: int = 120) -> bool:
7878

7979
def mint_tokens(self) -> bool:
8080
"""Mint tokens to this node's address using the node's own private key."""
81-
try:
82-
logger.info(f"Minting {self.mint_amount} tokens to {self.node_address}")
83-
84-
# Use the node's own private key since mint() is public
85-
nonce = self.w3.eth.get_transaction_count(self.node_address)
86-
87-
# Build mint transaction
88-
function_signature = self.w3.keccak(text="mint(address,uint256)")[:4]
89-
encoded_address = self.node_address[2:].lower().zfill(64)
90-
encoded_amount = hex(self.mint_amount)[2:].zfill(64)
91-
data = function_signature.hex() + encoded_address + encoded_amount
92-
93-
transaction = {
94-
'to': self.token_address,
95-
'value': 0,
96-
'gas': 200000,
97-
'gasPrice': self.w3.eth.gas_price,
98-
'nonce': nonce,
99-
'data': data,
100-
}
101-
102-
# Sign and send with node's own key
103-
signed_txn = self.w3.eth.account.sign_transaction(transaction, self.private_key)
104-
tx_hash = self.w3.eth.send_raw_transaction(signed_txn.rawTransaction)
105-
106-
logger.info(f"Mint transaction sent: {tx_hash.hex()}")
107-
108-
if self.wait_for_transaction(tx_hash.hex()):
109-
logger.info(f"✓ Mint successful for node {self.node_index}")
110-
return True
111-
else:
112-
logger.error(f"✗ Mint failed for node {self.node_index}")
113-
return False
81+
for attempt in range(3):
82+
try:
83+
logger.info(f"Minting {self.mint_amount} tokens to {self.node_address} (attempt {attempt + 1}/3)")
11484

115-
except Exception as e:
116-
logger.error(f"✗ Mint failed for node {self.node_index}: {str(e)}")
117-
return False
85+
# Use the node's own private key since mint() is public
86+
nonce = self.w3.eth.get_transaction_count(self.node_address)
87+
88+
# Build mint transaction
89+
function_signature = self.w3.keccak(text="mint(address,uint256)")[:4]
90+
encoded_address = self.node_address[2:].lower().zfill(64)
91+
encoded_amount = hex(self.mint_amount)[2:].zfill(64)
92+
data = function_signature.hex() + encoded_address + encoded_amount
93+
94+
transaction = {
95+
'to': self.token_address,
96+
'value': 0,
97+
'gas': 200000,
98+
'gasPrice': self.w3.eth.gas_price,
99+
'nonce': nonce,
100+
'data': data,
101+
}
102+
103+
# Sign and send with node's own key
104+
signed_txn = self.w3.eth.account.sign_transaction(transaction, self.private_key)
105+
tx_hash = self.w3.eth.send_raw_transaction(signed_txn.rawTransaction)
106+
107+
logger.info(f"Mint transaction sent: {tx_hash.hex()}")
108+
109+
if self.wait_for_transaction(tx_hash.hex()):
110+
logger.info(f"✓ Mint successful for node {self.node_index}")
111+
return True
112+
else:
113+
logger.error(f"✗ Mint failed for node {self.node_index} (attempt {attempt + 1})")
114+
if attempt < 2:
115+
logger.info(f"Retrying mint in 5 seconds...")
116+
time.sleep(5)
117+
continue
118+
119+
except Exception as e:
120+
logger.error(f"✗ Mint failed for node {self.node_index} (attempt {attempt + 1}): {str(e)}")
121+
if attempt < 2:
122+
logger.info(f"Retrying mint in 5 seconds...")
123+
time.sleep(5)
124+
continue
125+
126+
logger.error(f"✗ Mint failed for node {self.node_index} after 3 attempts")
127+
return False
118128

119129
def approve_tokens(self) -> bool:
120130
"""Approve RLN contract to spend tokens."""
121-
try:
122-
logger.info(f"Approving {self.mint_amount} tokens for contract {self.contract_address}")
123-
124-
nonce = self.w3.eth.get_transaction_count(self.node_address)
125-
126-
# Build approve transaction
127-
function_signature = self.w3.keccak(text="approve(address,uint256)")[:4]
128-
encoded_contract = self.contract_address[2:].lower().zfill(64)
129-
encoded_amount = hex(self.mint_amount)[2:].zfill(64)
130-
data = function_signature.hex() + encoded_contract + encoded_amount
131-
132-
transaction = {
133-
'to': self.token_address,
134-
'value': 0,
135-
'gas': 200000,
136-
'gasPrice': self.w3.eth.gas_price,
137-
'nonce': nonce,
138-
'data': data,
139-
}
140-
141-
# Sign and send with node's own key
142-
signed_txn = self.w3.eth.account.sign_transaction(transaction, self.private_key)
143-
tx_hash = self.w3.eth.send_raw_transaction(signed_txn.rawTransaction)
144-
145-
logger.info(f"Approve transaction sent: {tx_hash.hex()}")
146-
147-
if self.wait_for_transaction(tx_hash.hex()):
148-
logger.info(f"✓ Approval successful for node {self.node_index}")
149-
return True
150-
else:
151-
logger.error(f"✗ Approval failed for node {self.node_index}")
152-
return False
131+
for attempt in range(3):
132+
try:
133+
logger.info(f"Approving {self.mint_amount} tokens for contract {self.contract_address} (attempt {attempt + 1}/3)")
153134

154-
except Exception as e:
155-
logger.error(f"✗ Approval failed for node {self.node_index}: {str(e)}")
156-
return False
135+
nonce = self.w3.eth.get_transaction_count(self.node_address)
136+
137+
# Build approve transaction
138+
function_signature = self.w3.keccak(text="approve(address,uint256)")[:4]
139+
encoded_contract = self.contract_address[2:].lower().zfill(64)
140+
encoded_amount = hex(self.mint_amount)[2:].zfill(64)
141+
data = function_signature.hex() + encoded_contract + encoded_amount
142+
143+
transaction = {
144+
'to': self.token_address,
145+
'value': 0,
146+
'gas': 200000,
147+
'gasPrice': self.w3.eth.gas_price,
148+
'nonce': nonce,
149+
'data': data,
150+
}
151+
152+
# Sign and send with node's own key
153+
signed_txn = self.w3.eth.account.sign_transaction(transaction, self.private_key)
154+
tx_hash = self.w3.eth.send_raw_transaction(signed_txn.rawTransaction)
155+
156+
logger.info(f"Approve transaction sent: {tx_hash.hex()}")
157+
158+
if self.wait_for_transaction(tx_hash.hex()):
159+
logger.info(f"✓ Approval successful for node {self.node_index}")
160+
return True
161+
else:
162+
logger.error(f"✗ Approval failed for node {self.node_index} (attempt {attempt + 1})")
163+
if attempt < 2:
164+
logger.info(f"Retrying approval in 5 seconds...")
165+
time.sleep(5)
166+
continue
167+
168+
except Exception as e:
169+
logger.error(f"✗ Approval failed for node {self.node_index} (attempt {attempt + 1}): {str(e)}")
170+
if attempt < 2:
171+
logger.info(f"Retrying approval in 5 seconds...")
172+
time.sleep(5)
173+
continue
174+
175+
logger.error(f"✗ Approval failed for node {self.node_index} after 3 attempts")
176+
return False
157177

158178
def run(self) -> bool:
159179
"""Run the token initialization process."""

0 commit comments

Comments
 (0)