Skip to content

Commit

Permalink
Fix/fix scroll call contract bug (#590)
Browse files Browse the repository at this point in the history
* feat: add doc store

* fix: add test case

* fix: add use legacy tx

* fix: update the readme

* fix: fix the metastore case
  • Loading branch information
imotai authored Jul 28, 2023
1 parent ddbe612 commit c6f9c41
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 29 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,19 @@ if you have any questions, please feel free to ask us for help and you can find
* the doc about [queryDoc](https://docs.db3.network/functions/queryDoc.html)


# Try Our Cloud Sandbox
# Try the testnet

* [Console](https://console.cloud.db3.network/console/home):https://console.cloud.db3.network/console/home
* Data Rollup Node: https://rollup.cloud.db3.network
* Data Index Node: https://index.cloud.db3.network

You can connect to the Data Rollup Node and Data Index Node with db3.js
| Public Chains | Testnet | Mainnet |
|----------|:-------------:|:------:|
| zksync |data rollup node:`https://zksync.rollup.testnet.db3.network` <br> data index node:`https://zksync.index.testnet.db3.network` | :soon: |
| scroll |data rollup node:`https://scroll.rollup.testnet.db3.network` <br> data index node:`https://scroll.index.testnet.db3.network` | :soon: |


You can connect to the Data Rollup Node and Data Index Node with db3.js
Note: the cloud sandbox is just for testing and unstable


# How it works

The DB3 Network has two roles:
Expand Down
7 changes: 7 additions & 0 deletions src/node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ pub enum DB3Command {
/// this is just for upgrade the node
#[clap(long, default_value = "100000")]
doc_id_start: i64,
/// use the legacy transaction format
#[clap(long, default_value = "false")]
use_legacy_tx: bool,
},

/// Start the data index node
Expand Down Expand Up @@ -241,6 +244,7 @@ impl DB3Command {
key_root_path,
admin_addr,
doc_id_start,
use_legacy_tx,
} => {
let log_level = if verbose {
LevelFilter::DEBUG
Expand All @@ -261,6 +265,7 @@ impl DB3Command {
key_root_path.as_str(),
admin_addr.as_str(),
doc_id_start,
use_legacy_tx,
)
.await;
let running = Arc::new(AtomicBool::new(true));
Expand Down Expand Up @@ -479,11 +484,13 @@ impl DB3Command {
key_root_path: &str,
admin_addr: &str,
doc_start_id: i64,
use_legacy_tx: bool,
) {
let listen_addr = format!("{bind_host}:{listening_port}");
let rollup_config = RollupExecutorConfig {
temp_data_path: rollup_data_path.to_string(),
key_root_path: key_root_path.to_string(),
use_legacy_tx,
};

let store_config = MutationStoreConfig {
Expand Down
1 change: 1 addition & 0 deletions src/node/src/node_test_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub mod tests {
let rollup_config = RollupExecutorConfig {
temp_data_path: format!("{real_path}/rollup_data_path"),
key_root_path: key_root_path.to_string(),
use_legacy_tx: false,
};
if let Err(_e) = std::fs::create_dir_all(rollup_config.temp_data_path.as_str()) {
println!("create dir error");
Expand Down
3 changes: 2 additions & 1 deletion src/node/src/recover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ impl Recover {
format!("0x{}", hex::encode(wallet.address().as_bytes()))
);
let meta_store = Arc::new(
MetaStoreClient::new(contract_addr.as_str(), evm_node_url.as_str(), wallet).await?,
MetaStoreClient::new(contract_addr.as_str(), evm_node_url.as_str(), wallet, false)
.await?,
);
let ar_fs_config = ArFileSystemConfig {
arweave_url: ar_node_url,
Expand Down
21 changes: 17 additions & 4 deletions src/node/src/rollup_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use tracing::{info, warn}; // Workaround to use prinltn! for logs.
pub struct RollupExecutorConfig {
pub temp_data_path: String,
pub key_root_path: String,
pub use_legacy_tx: bool,
}

pub struct RollupExecutor {
Expand Down Expand Up @@ -71,8 +72,13 @@ impl RollupExecutor {
let wallet = system_store.get_evm_wallet(c.chain_id)?;
let min_rollup_size = c.min_rollup_size;
let meta_store = ArcSwapOption::from(Some(Arc::new(
MetaStoreClient::new(c.contract_addr.as_str(), c.evm_node_url.as_str(), wallet)
.await?,
MetaStoreClient::new(
c.contract_addr.as_str(),
c.evm_node_url.as_str(),
wallet,
config.use_legacy_tx,
)
.await?,
)));
let ar_fs_config = ArFileSystemConfig {
arweave_url: c.ar_node_url.clone(),
Expand Down Expand Up @@ -134,8 +140,13 @@ impl RollupExecutor {
self.rollup_max_interval
.store(c.rollup_max_interval, Ordering::Relaxed);
let meta_store = Some(Arc::new(
MetaStoreClient::new(c.contract_addr.as_str(), c.evm_node_url.as_str(), wallet)
.await?,
MetaStoreClient::new(
c.contract_addr.as_str(),
c.evm_node_url.as_str(),
wallet,
self.config.use_legacy_tx,
)
.await?,
));
self.min_gc_round_offset
.store(c.min_gc_offset, Ordering::Relaxed);
Expand Down Expand Up @@ -303,10 +314,12 @@ impl RollupExecutor {
network_id,
)
.await?;

let (evm_cost, tx_hash) = meta_store
.update_rollup_step(id.as_str(), network_id)
.await?;
let tx_str = format!("0x{}", hex::encode(tx_hash.as_bytes()));

info!("the process rollup done with num mutations {num_rows}, raw data size {memory_size}, compress data size {size} and processed time {} id {} ar cost {} and evm tx {} and cost {}", now.elapsed().as_secs(),
id.as_str(), reward,
tx_str.as_str(),
Expand Down
1 change: 1 addition & 0 deletions src/node/src/storage_node_light_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,7 @@ mod tests {
let rollup_config = RollupExecutorConfig {
temp_data_path: format!("{real_path}/data_path"),
key_root_path: format!("{real_path}/keys"),
use_legacy_tx: false,
};

let system_store_config = SystemStoreConfig {
Expand Down
84 changes: 66 additions & 18 deletions src/storage/src/meta_store_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,19 @@ abigen!(Events, "abi/Events.json");
pub struct MetaStoreClient {
address: Address,
client: Arc<SignerMiddleware<Arc<NonceManagerMiddleware<Provider<Ws>>>, LocalWallet>>,
use_legacy_tx: bool,
}

unsafe impl Sync for MetaStoreClient {}
unsafe impl Send for MetaStoreClient {}

impl MetaStoreClient {
pub async fn new(contract_addr: &str, rpc_url: &str, wallet: LocalWallet) -> Result<Self> {
pub async fn new(
contract_addr: &str,
rpc_url: &str,
wallet: LocalWallet,
use_legacy_tx: bool,
) -> Result<Self> {
let address = contract_addr
.parse::<Address>()
.map_err(|_| DB3Error::InvalidAddress)?;
Expand All @@ -51,7 +57,11 @@ impl MetaStoreClient {
let provider_arc = Arc::new(provider);
let signable_client = SignerMiddleware::new(provider_arc, wallet);
let client = Arc::new(signable_client);
Ok(Self { address, client })
Ok(Self {
address,
client,
use_legacy_tx,
})
}

pub async fn register_data_network(
Expand All @@ -74,11 +84,21 @@ impl MetaStoreClient {
empty_index_addresses,
desc,
);
tx.send()
.await
.map_err(|e| DB3Error::StoreEventError(format!("fail to register data network {e}")))?;
match self.use_legacy_tx {
true => {
tx.legacy().send().await.map_err(|e| {
DB3Error::StoreEventError(format!("fail to register data network {e}"))
})?;
}
false => {
tx.send().await.map_err(|e| {
DB3Error::StoreEventError(format!("fail to register data network {e}"))
})?;
}
}
Ok(())
}

pub async fn create_database(&self, network: u64, desc: &str) -> Result<(U256, TxHash)> {
let store = DB3MetaStore::new(self.address, self.client.clone());
let desc_bytes = desc.as_bytes();
Expand All @@ -88,12 +108,25 @@ impl MetaStoreClient {
let mut desc_bytes32: [u8; 32] = Default::default();
desc_bytes32[..desc_bytes.len()].clone_from_slice(desc_bytes);
let tx = store.create_doc_database(network.into(), desc_bytes32);
let pending_tx = tx.send().await.map_err(|e| {
DB3Error::StoreEventError(format!(
"fail to send create doc database request with error {e}"
))
})?;
let tx_hash = pending_tx.tx_hash();
let tx_hash = match self.use_legacy_tx {
true => {
let tx = tx.legacy();
let pending_tx = tx.send().await.map_err(|e| {
DB3Error::StoreEventError(format!(
"fail to send create doc database request with error {e}"
))
})?;
pending_tx.tx_hash()
}
false => {
let pending_tx = tx.send().await.map_err(|e| {
DB3Error::StoreEventError(format!(
"fail to send create doc database request with error {e}"
))
})?;
pending_tx.tx_hash()
}
};
let mut count_down: i32 = 5;
loop {
if count_down <= 0 {
Expand Down Expand Up @@ -159,11 +192,25 @@ impl MetaStoreClient {
ar_tx, network
);
let tx = store.update_rollup_steps(network_id, ar_tx_binary);
//TODO set gas limit
let pending_tx = tx.send().await.map_err(|e| {
DB3Error::StoreEventError(format!("fail to send update rollup request with error {e}"))
})?;
let tx_hash = pending_tx.tx_hash();
let tx_hash = match self.use_legacy_tx {
true => {
let tx = tx.legacy();
let pending_tx = tx.send().await.map_err(|e| {
DB3Error::StoreEventError(format!(
"fail to send create doc database request with error {e}"
))
})?;
pending_tx.tx_hash()
}
false => {
let pending_tx = tx.send().await.map_err(|e| {
DB3Error::StoreEventError(format!(
"fail to send create doc database request with error {e}"
))
})?;
pending_tx.tx_hash()
}
};
info!("update rollup step done! tx hash: {}", tx_hash);
let mut count_down: i32 = 5;
loop {
Expand Down Expand Up @@ -195,6 +242,7 @@ mod tests {
use super::*;
use fastcrypto::encoding::{Base64, Encoding};
use tokio::time::{sleep, Duration as TokioDuration};

#[tokio::test]
async fn register_no1_data_network() {
let data = hex::decode("ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80")
Expand All @@ -206,7 +254,7 @@ mod tests {
let contract_addr = "0x5fbdb2315678afecb367f032d93f642f64180aa3";
let rpc_url = "ws://127.0.0.1:8545";
sleep(TokioDuration::from_millis(10 * 1000)).await;
let client = MetaStoreClient::new(contract_addr, rpc_url, wallet)
let client = MetaStoreClient::new(contract_addr, rpc_url, wallet, false)
.await
.unwrap();
let result = client
Expand All @@ -226,7 +274,7 @@ mod tests {
let rollup_node_address = wallet.address();
let contract_addr = "0x5FbDB2315678afecb367f032d93F642f64180aa3";
let rpc_url = "ws://127.0.0.1:8545";
let client = MetaStoreClient::new(contract_addr, rpc_url, wallet)
let client = MetaStoreClient::new(contract_addr, rpc_url, wallet, false)
.await
.unwrap();
let result = client
Expand Down

0 comments on commit c6f9c41

Please sign in to comment.