Skip to content

Commit

Permalink
Merging the develop branch into the main branch (rs v1.4.2, wasm v1.4.2)
Browse files Browse the repository at this point in the history
This merge contains the following set of changes:
- Fix db transaction timeout error on low resource devices by making explicit commit (#69)
- Tron pools support (#71)
  • Loading branch information
EvgenKor authored Oct 9, 2023
2 parents a89eba8 + 0271600 commit 726852f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 39 deletions.
5 changes: 4 additions & 1 deletion libs/kvdb-web/src/indexed_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use js_sys::{Array, ArrayBuffer, Uint8Array};
use wasm_bindgen::{closure::Closure, JsCast, JsValue};
use web_sys::console;
use web_sys::{Event, IdbCursorWithValue, IdbDatabase, IdbKeyRange, IdbOpenDbRequest, IdbRequest, IdbTransactionMode};

use futures::channel;
Expand Down Expand Up @@ -183,11 +184,13 @@ pub fn idb_commit_transaction(idb: &IdbDatabase, txn: &DBTransaction, columns: u
on_complete.forget();

let on_error = Closure::once(move || {
warn!("Failed to commit a transaction to IndexedDB");
console::error_1(&"Failed to commit a transaction to IndexedDB".into());
});
idb_txn.set_onerror(Some(on_error.as_ref().unchecked_ref()));
on_error.forget();

// Commit transaction explicitly to prevent timeout errors due to auto-commit delaying
idb_txn.commit().expect("IDBTransaction.commit failed");
rx.map(|_| ())
}

Expand Down
2 changes: 1 addition & 1 deletion libzkbob-rs-wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "libzkbob-rs-wasm"
description = "A higher level zkBob API for Wasm"
version = "1.4.1"
version = "1.4.2"
authors = ["Dmitry Vdovin <voidxnull@gmail.com>"]
repository = "https://github.com/zkBob/libzkbob-rs/"
license = "MIT OR Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion libzkbob-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "libzkbob-rs"
description = "A higher level zkBob API"
version = "1.4.1"
version = "1.4.2"
authors = ["Dmitry Vdovin <voidxnull@gmail.com>"]
repository = "https://github.com/zkBob/libzkbob-rs/"
license = "MIT OR Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion libzkbob-rs/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub fn parse_address_ext<P: PoolParams>(
}

// the old format should be acceptable on the Polygon BOB pool only
Ok((d, p_d, Some(Pool::PolygonBOB), AddressFormat::Old, checksum))
Ok((d, p_d, Some(Pool::PolygonUSDC), AddressFormat::Old, checksum))
}
}

Expand Down
22 changes: 11 additions & 11 deletions libzkbob-rs/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ mod tests {
#[test]
fn test_create_tx_deposit_zero() {
let state = State::init_test(POOL_PARAMS.clone());
let acc = UserAccount::new(Num::ZERO, Pool::PolygonBOB, state, POOL_PARAMS.clone());
let acc = UserAccount::new(Num::ZERO, Pool::PolygonUSDC, state, POOL_PARAMS.clone());

acc.create_tx(
TxType::Deposit(
Expand All @@ -666,7 +666,7 @@ mod tests {
#[test]
fn test_create_tx_deposit_one() {
let state = State::init_test(POOL_PARAMS.clone());
let acc = UserAccount::new(Num::ZERO, Pool::PolygonBOB, state, POOL_PARAMS.clone());
let acc = UserAccount::new(Num::ZERO, Pool::PolygonUSDC, state, POOL_PARAMS.clone());

acc.create_tx(
TxType::Deposit(
Expand All @@ -684,7 +684,7 @@ mod tests {
#[test]
fn test_create_tx_transfer_zero() {
let state = State::init_test(POOL_PARAMS.clone());
let acc = UserAccount::new(Num::ZERO, Pool::PolygonBOB, state, POOL_PARAMS.clone());
let acc = UserAccount::new(Num::ZERO, Pool::PolygonUSDC, state, POOL_PARAMS.clone());

let addr = acc.generate_address();

Expand All @@ -705,7 +705,7 @@ mod tests {
#[should_panic]
fn test_create_tx_transfer_one_no_balance() {
let state = State::init_test(POOL_PARAMS.clone());
let acc = UserAccount::new(Num::ZERO, Pool::PolygonBOB, state, POOL_PARAMS.clone());
let acc = UserAccount::new(Num::ZERO, Pool::PolygonUSDC, state, POOL_PARAMS.clone());

let addr = acc.generate_address();

Expand Down Expand Up @@ -754,7 +754,7 @@ mod tests {
let state = State::init_test(POOL_PARAMS.clone());
let mut user_account = UserAccount::new(
Num::ZERO,
Pool::OptimismBOB,
Pool::OptimismUSDC,
state,
POOL_PARAMS.clone()
);
Expand Down Expand Up @@ -806,9 +806,9 @@ mod tests {

#[test]
fn test_chain_specific_addresses() {
let acc_polygon = UserAccount::new(Num::ZERO, Pool::PolygonBOB, State::init_test(POOL_PARAMS.clone()), POOL_PARAMS.clone());
let acc_polygon = UserAccount::new(Num::ZERO, Pool::PolygonUSDC, State::init_test(POOL_PARAMS.clone()), POOL_PARAMS.clone());
let acc_sepolia = UserAccount::new(Num::ZERO, Pool::SepoliaBOB, State::init_test(POOL_PARAMS.clone()), POOL_PARAMS.clone());
let acc_optimism = UserAccount::new(Num::ZERO, Pool::OptimismBOB, State::init_test(POOL_PARAMS.clone()), POOL_PARAMS.clone());
let acc_optimism = UserAccount::new(Num::ZERO, Pool::OptimismUSDC, State::init_test(POOL_PARAMS.clone()), POOL_PARAMS.clone());
let acc_optimism_eth = UserAccount::new(Num::ZERO, Pool::OptimismETH, State::init_test(POOL_PARAMS.clone()), POOL_PARAMS.clone());

assert!(acc_polygon.validate_address("PtfsqyJhA2yvmLtXBm55pkvFDX6XZrRMaib9F1GvwzmU8U4witUf8Jyse5kxBwa"));
Expand Down Expand Up @@ -847,14 +847,14 @@ mod tests {
#[test]
fn test_chain_specific_address_ownable() {
let accs = [
UserAccount::new(Num::ZERO, Pool::PolygonBOB, State::init_test(POOL_PARAMS.clone()), POOL_PARAMS.clone()),
UserAccount::new(Num::ZERO, Pool::OptimismBOB, State::init_test(POOL_PARAMS.clone()), POOL_PARAMS.clone()),
UserAccount::new(Num::ZERO, Pool::PolygonUSDC, State::init_test(POOL_PARAMS.clone()), POOL_PARAMS.clone()),
UserAccount::new(Num::ZERO, Pool::OptimismUSDC, State::init_test(POOL_PARAMS.clone()), POOL_PARAMS.clone()),
UserAccount::new(Num::ZERO, Pool::OptimismETH, State::init_test(POOL_PARAMS.clone()), POOL_PARAMS.clone()),
UserAccount::new(Num::ZERO, Pool::SepoliaBOB, State::init_test(POOL_PARAMS.clone()), POOL_PARAMS.clone()),
UserAccount::new(Num::ZERO, Pool::GoerliBOB, State::init_test(POOL_PARAMS.clone()), POOL_PARAMS.clone()),
UserAccount::new(Num::ZERO, Pool::GoerliOptimismBOB, State::init_test(POOL_PARAMS.clone()), POOL_PARAMS.clone()),
UserAccount::new(Num::ZERO, Pool::GoerliOptimismUSDC, State::init_test(POOL_PARAMS.clone()), POOL_PARAMS.clone()),
];
let acc2 = UserAccount::new(Num::ONE, Pool::OptimismBOB, State::init_test(POOL_PARAMS.clone()), POOL_PARAMS.clone());
let acc2 = UserAccount::new(Num::ONE, Pool::OptimismUSDC, State::init_test(POOL_PARAMS.clone()), POOL_PARAMS.clone());
let pool_addresses: Vec<String> = accs.iter().map(|acc| acc.generate_address()).collect();
let universal_addresses: Vec<String> = accs.iter().map(|acc| acc.generate_universal_address()).collect();

Expand Down
54 changes: 30 additions & 24 deletions libzkbob-rs/src/pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,62 +14,66 @@ pub const GENERIC_ADDRESS_PREFIX: &str = "zkbob";
// It used to support multipool shielded addresses
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Pool {
PolygonBOB,
OptimismBOB,
OptimismETH,
PolygonUSDC,
OptimismUSDC,
OptimismETH,
TronUSDT,
SepoliaBOB,
GoerliBOB,
GoerliOptimismBOB,
GoerliOptimismUSDC,
GoerliETH,
GoerliUSDC,
ShastaUSDT,
}

impl Pool {
pub fn from_pool_id(pool_id: u32) -> Option<Pool> {
match pool_id {
0x000000 => Some(Pool::PolygonBOB),
0x000001 => Some(Pool::OptimismBOB),
0x000000 => Some(Pool::PolygonUSDC),
0x000001 => Some(Pool::OptimismUSDC),
0x000002 => Some(Pool::OptimismETH),
0x000003 => Some(Pool::PolygonUSDC),
0x000003 => Some(Pool::TronUSDT),
// pool_id duplication, use this method with caution
// (it will never produce Pool::SepoliaBOB object)
//0x000000 => Some(Pool::SepoliaBOB),
0xffff02 => Some(Pool::GoerliBOB),
0xffff03 => Some(Pool::GoerliOptimismBOB),
0xffff03 => Some(Pool::GoerliOptimismUSDC),
0xffff04 => Some(Pool::GoerliETH),
0xffff05 => Some(Pool::GoerliUSDC),
0xffff06 => Some(Pool::ShastaUSDT),
_ => None,
}
}

pub fn from_prefix(address_prefix: &str) -> Option<Pool> {
match address_prefix.to_lowercase().as_str() {
"zkbob_polygon" => Some(Pool::PolygonBOB),
"zkbob_optimism" => Some(Pool::OptimismBOB),
"zkbob_polygon" => Some(Pool::PolygonUSDC),
"zkbob_optimism" => Some(Pool::OptimismUSDC),
"zkbob_optimism_eth" => Some(Pool::OptimismETH),
"zkbob_polygon_usdc" => Some(Pool::PolygonUSDC),
"zkbob_tron" => Some(Pool::TronUSDT),
"zkbob_sepolia" => Some(Pool::SepoliaBOB),
"zkbob_goerli" => Some(Pool::GoerliBOB),
"zkbob_goerli_optimism" => Some(Pool::GoerliOptimismBOB),
"zkbob_goerli_optimism" => Some(Pool::GoerliOptimismUSDC),
"zkbob_goerli_eth" => Some(Pool::GoerliETH),
"zkbob_goerli_usdc" => Some(Pool::GoerliUSDC),
"zkbob_shasta" => Some(Pool::ShastaUSDT),
_ => None,
}
}

pub fn pool_id(&self) -> u32 {
match self {
Pool::PolygonBOB => 0x000000,
Pool::OptimismBOB => 0x000001,
Pool::PolygonUSDC => 0x000000,
Pool::OptimismUSDC => 0x000001,
Pool::OptimismETH => 0x000002,
Pool::PolygonUSDC => 0x000003,
Pool::TronUSDT => 0x000003,
// here is an issue with Sepolia pool deployment
Pool::SepoliaBOB => 0x000000,
Pool::GoerliBOB => 0xffff02,
Pool::GoerliOptimismBOB => 0xffff03,
Pool::GoerliOptimismUSDC => 0xffff03,
Pool::GoerliETH => 0xffff04,
Pool::GoerliUSDC => 0xffff05,
Pool::ShastaUSDT => 0xffff06,
}
}

Expand All @@ -93,29 +97,31 @@ impl Pool {

pub fn address_prefix(&self) -> &str {
match self {
Pool::PolygonBOB => "zkbob_polygon",
Pool::OptimismBOB => "zkbob_optimism",
Pool::PolygonUSDC => "zkbob_polygon",
Pool::OptimismUSDC => "zkbob_optimism",
Pool::OptimismETH => "zkbob_optimism_eth",
Pool::PolygonUSDC => "zkbob_polygon_usdc",
Pool::TronUSDT => "zkbob_tron",
Pool::SepoliaBOB => "zkbob_sepolia",
Pool::GoerliBOB => "zkbob_goerli",
Pool::GoerliOptimismBOB => "zkbob_goerli_optimism",
Pool::GoerliOptimismUSDC => "zkbob_goerli_optimism",
Pool::GoerliETH => "zkbob_goerli_eth",
Pool::GoerliUSDC => "zkbob_goerli_usdc",
Pool::ShastaUSDT => "zkbob_shasta",
}
}

pub fn human_readable(&self) -> &str {
match self {
Pool::PolygonBOB => "BOB on Polygon",
Pool::OptimismBOB => "BOB on Optimism",
Pool::OptimismETH => "WETH on Optimism",
Pool::PolygonUSDC => "USDC on Polygon",
Pool::OptimismUSDC => "USDC on Optimism",
Pool::OptimismETH => "WETH on Optimism",
Pool::TronUSDT => "USDT on Tron",
Pool::SepoliaBOB => "BOB on Sepolia testnet",
Pool::GoerliBOB => "BOB on Goerli testnet",
Pool::GoerliOptimismBOB => "BOB on Goerli Optimism testnet",
Pool::GoerliOptimismUSDC => "USDC on Goerli Optimism testnet",
Pool::GoerliETH => "WETH on Goerli testnet",
Pool::GoerliUSDC => "USDC on Goerli testnet",
Pool::ShastaUSDT => "USDT on Shasta testnet",
}
}
}
Expand Down

0 comments on commit 726852f

Please sign in to comment.