Skip to content

Commit

Permalink
(fix) Improve log validation
Browse files Browse the repository at this point in the history
(feat) Configurable gas margins for legacy gas estimation
(fix) Remove spurious debug from web interface
(fix) Fix decimals for test eZIL token.
  • Loading branch information
rrw-zilliqa committed Aug 8, 2024
1 parent e089bb5 commit f17e225
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 39 deletions.
7 changes: 4 additions & 3 deletions bridge-validators/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
[[chain_configs]]
chain_gateway_block_deployed = 36696045
rpc_url = "https://bsc-prebsc-dataseed.bnbchain.org"
chain_gateway_address = "0x72B9B59e48779A8b64554A3e2bC8b5297A04c68a"
chain_gateway_address = "0xa9A14C90e53EdCD89dFd201A3bF94D867f8098fE"
legacy_gas_estimation_percent=130

# BSC Mainnet
# [[chain_configs]]
Expand All @@ -27,9 +28,9 @@ chain_gateway_address = "0x72B9B59e48779A8b64554A3e2bC8b5297A04c68a"
[[chain_configs]]
chain_gateway_block_deployed = 6542681
rpc_url = "https://dev-api.zilliqa.com"
chain_gateway_address = "0x10917A34FE60eE8364a401a6b1d3adaf80D84eb6"
chain_gateway_address = "0x7370e69565BB2313C4dA12F9062C282513919230"
block_instant_finality = true
legacy_gas_estimation = true
legacy_gas_estimation_percent=130

# Zilliqa Mainnet
# [[chain_configs]]
Expand Down
3 changes: 2 additions & 1 deletion bridge-validators/infra/config-leader.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
[[chain_configs]]
chain_gateway_block_deployed = 36696045
rpc_url = "https://bsc-prebsc-dataseed.bnbchain.org"
chain_gateway_address = "0x72B9B59e48779A8b64554A3e2bC8b5297A04c68a"
chain_gateway_address = "0xa9A14C90e53EdCD89dFd201A3bF94D867f8098fE"
legacy_gas_estimation_percent=150

# BSC Mainnet
# [[chain_configs]]
Expand Down
7 changes: 4 additions & 3 deletions bridge-validators/infra/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
[[chain_configs]]
chain_gateway_block_deployed = 36696045
rpc_url = "https://bsc-prebsc-dataseed.bnbchain.org"
chain_gateway_address = "0x72B9B59e48779A8b64554A3e2bC8b5297A04c68a"
chain_gateway_address = "0xa9A14C90e53EdCD89dFd201A3bF94D867f8098fE"
legacy_gas_estimation_percent = 150

# BSC Mainnet
# [[chain_configs]]
Expand All @@ -27,9 +28,9 @@ chain_gateway_address = "0x72B9B59e48779A8b64554A3e2bC8b5297A04c68a"
[[chain_configs]]
chain_gateway_block_deployed = 6542681
rpc_url = "https://dev-api.zilliqa.com"
chain_gateway_address = "0x10917A34FE60eE8364a401a6b1d3adaf80D84eb6"
chain_gateway_address = "0x7370e69565BB2313C4dA12F9062C282513919230"
block_instant_finality = true
legacy_gas_estimation = true
legacy_gas_estimation_percent = 130

# Zilliqa Mainnet
# [[chain_configs]]
Expand Down
44 changes: 31 additions & 13 deletions bridge-validators/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ use async_trait::async_trait;
use anyhow::{anyhow, Result};
use ethers::{
providers::Middleware,
types::{Block, BlockNumber, Filter, Log, TransactionReceipt, TxHash, ValueOrArray, U64},
types::{
Address, Block, BlockNumber, Filter, Log, TransactionReceipt, TxHash, ValueOrArray, H160,
U64,
},
};
use ethers_contract::{parse_log, EthEvent};
use futures::{Stream, StreamExt, TryStreamExt};
use serde::{Deserialize, Serialize};
use tokio::time::interval;
use tracing::{info, warn};
use tracing::{debug, info, warn};

use crate::client::{ChainClient, LogStrategy};

Expand Down Expand Up @@ -59,7 +62,7 @@ impl ChainClient {
// go through alll the transactions
for txn_hash in block.transactions {
// We have a transaction. Did it have any logs?
println!("block {} txn {:#x}", block_number, txn_hash);
debug!("block {} txn {:#x}", block_number, txn_hash);
// Get the receipt
let maybe_receipt = self
.client
Expand All @@ -68,24 +71,23 @@ impl ChainClient {
.await?;
if let Some(receipt) = maybe_receipt {
// Yay!
println!("Got receipt for txn {:#x}", txn_hash);
info!("Got receipt for txn {:#x}", txn_hash);
for log in receipt.logs {
// Because FML, the filter doesn't actually include the address.
// so we have to include it manually.
let address_matches = log.address == self.chain_gateway_address;
if !address_matches {
println!("Address does not match");
if log.address != self.chain_gateway_address {
info!(
"[1] event from {0:#x} != chain_gateway({1:#x})",
log.address, self.chain_gateway_address
);
continue;
}
println!("Case 3");
let mut matches: bool = true;
for topic_idx in 0..event.topics.len() {
if let Some(x) = &event.topics[topic_idx] {
if let Some(y) = &log.topics.get(topic_idx) {
let match_this_topic = match x {
ValueOrArray::Value(xv) => {
if let Some(xxv) = xv {
println!("Case 4 {:#x} vs {:#x}", xxv, y);
xxv == *y
} else {
true
Expand All @@ -111,17 +113,16 @@ impl ChainClient {
// If there's no filter element for this topic, we're fine.
}
if matches {
println!("Match!");
result.push(log);
}
}
} else {
println!("WARNING: txn {:#x} has no receipt", txn_hash);
warn!("WARNING: txn {:#x} has no receipt", txn_hash);
}
}
}
}
Ok(vec![])
Ok(result)
}
}

Expand Down Expand Up @@ -163,6 +164,23 @@ impl BlockPolling for ChainClient {
.request("eth_getLogs", [event])
.await?;
logs.into_iter()
.filter(|log| {
log.get("address")
.and_then(|val| val.as_str())
.and_then(|val| val.parse::<Address>().ok())
.map(|from_address| {
if from_address == self.chain_gateway_address {
true
} else {
info!(
"event from {0:#x} , chain gateway {1:#x}",
from_address, self.chain_gateway_address
);
false
}
})
.unwrap_or(false)
})
.map(|log| {
// Parse log values
let mut log = log;
Expand Down
5 changes: 0 additions & 5 deletions bridge-validators/src/bridge_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,6 @@ impl BridgeNode {
return Ok(());
}

info!(
"Chain: {} event found to be broadcasted: {}",
self.chain_client.chain_id, event
);

if let Some(RelayEventSignatures {
dispatched: true, ..
}) = self.event_signatures.get(&event.nonce)
Expand Down
9 changes: 5 additions & 4 deletions bridge-validators/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct ChainClient {
pub wallet: LocalWallet,
pub chain_gateway_block_deployed: u64,
pub block_instant_finality: bool,
pub legacy_gas_estimation: bool,
pub legacy_gas_estimation_percent: Option<u64>,
pub scan_behind_blocks: u64,
pub log_strategy: LogStrategy,
}
Expand All @@ -46,8 +46,9 @@ impl fmt::Display for ChainClient {
impl ChainClient {
pub async fn new(config: &ChainConfig, wallet: LocalWallet) -> Result<Self> {
info!(
"initialising chain client for URL {0} ... ",
config.rpc_url.as_str()
"initialising chain client for URL {0} with gateway {1:#x} ... ",
config.rpc_url.as_str(),
config.chain_gateway_address
);
let provider = Provider::<Http>::try_from(config.rpc_url.as_str())?;
// let provider = Provider::<Ws>::connect(&config.rpc_url).await?;
Expand Down Expand Up @@ -76,7 +77,7 @@ impl ChainClient {
wallet,
chain_gateway_block_deployed: config.chain_gateway_block_deployed,
block_instant_finality: config.block_instant_finality.unwrap_or_default(),
legacy_gas_estimation: config.legacy_gas_estimation.unwrap_or_default(),
legacy_gas_estimation_percent: config.legacy_gas_estimation_percent,
scan_behind_blocks: config.scan_behind_blocks.unwrap_or_default(),
log_strategy: strategy,
})
Expand Down
2 changes: 1 addition & 1 deletion bridge-validators/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct ChainConfig {
pub chain_gateway_address: Address,
pub chain_gateway_block_deployed: u64,
pub block_instant_finality: Option<bool>,
pub legacy_gas_estimation: Option<bool>,
pub legacy_gas_estimation_percent: Option<u64>,
pub scan_behind_blocks: Option<u64>,
pub use_get_transactions: Option<bool>,
}
Expand Down
10 changes: 5 additions & 5 deletions bridge-validators/src/validator_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl ValidatorNode {
event.target_chain_id, event.nonce
);

let function_call = if client.legacy_gas_estimation {
let function_call = if client.legacy_gas_estimation_percent.is_some() {
function_call.legacy()
} else {
function_call
Expand All @@ -190,16 +190,16 @@ impl ValidatorNode {

// Get gas estimate
// TODO: refactor configs specifically for zilliqa
let _function_call = if client.legacy_gas_estimation {
let _function_call = if let Some(percent) = client.legacy_gas_estimation_percent {
let gas_estimate = match function_call.estimate_gas().await {
Ok(estimate) => estimate,
Err(err) => {
warn!("Failed to estimate gas, {:?}", err);
return Ok(());
}
};
info!("Gas estimate {:?}", gas_estimate);
function_call.clone().gas(gas_estimate * 130 / 100) // Apply multiplier
info!("Legacy gas estimation: estimate {:?}", gas_estimate);
function_call.clone().gas(gas_estimate * percent / 100) // Apply multiplier
} else {
let function_call = function_call.clone();
// `eth_call` does not seem to work on ZQ so it had to be skipped
Expand Down Expand Up @@ -230,7 +230,7 @@ impl ValidatorNode {
// Make the actual call
match _function_call.send().await {
Ok(tx) => {
println!(
info!(
"Transaction Sent {}.{} {:?}",
event.target_chain_id,
event.nonce,
Expand Down
2 changes: 0 additions & 2 deletions bridge-web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,7 @@ function App() {
disabled={showLoadingButton}
onClick={async () => {
if (approve) {
alert('approving');
const tx = await approve();
alert('done');
console.log('....');
console.log(tx.hash);
console.log('....');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ contract Deployment is Script, TestnetConfig {
console.log(
" address public constant bscBridgedZRC2Address = %s", address(bridgedFromZilliqa));

// Bridged ZIL
SwitcheoToken bridgedZIL = new SwitcheoToken(bscLockProxyAddress, "eZIL", "Bridged ZIL", 12);
// Bridged ZIL - this is EVM ZIL, so scale = 18
SwitcheoToken bridgedZIL = new SwitcheoToken(bscLockProxyAddress, "eZIL", "Bridged ZIL", 18);
console.log(
" address public constant bscBridgedZILAddress = %s", address(bridgedZIL));
}
Expand Down

0 comments on commit f17e225

Please sign in to comment.