cspr-rad / kairos

Apache License 2.0
2 stars 0 forks source link

Periodic L1 synchronization #96

Closed koxu1996 closed 2 months ago

koxu1996 commented 4 months ago

This PR adds periodic L1 synchronization :arrows_clockwise:, based on casper-event-toolkit.

Details

The core component is EventManager, which allows tracking events from a specified smart contract. The process begins with the setup of CES-related data - schemas, toolkit fetcher, and the number of events - using initialize(). Subsequently, process_new_events() checks for new events based on its internal counter, next_event_id.

L1SyncService operates the event manager as a Tokio task and provides a safe API for shared access within an Axum server.

Future work

Demo :movie_camera:

https://github.com/cspr-rad/kairos/assets/6506780/d7793f3c-6a98-41ec-8cb7-f8204588c075

If you want to reproduce it locally, please see the next section.

Test environment

I used dockerized NCTL with bunch of shell scripts to run this demo - all available in this gist.

1. Cleanup old resources and compile example smart contract

$ ./cleanup.sh
$ ./prepare.sh

2. Run NCTL and deploy WASM

$ ./setup.sh
> Running NCTL container.
9e4fd0c153e63765ea13b565c70e2d2986ad929a2e74e74cb5e37938e3fe1edf

> Copying user keys.
Successfully copied 37.4kB to /tmp/casper-users

> Deploying session code.
- hash: f9531e06d8f533aa033106b40030ff6d4a9f6b63dc542ff3177d6d29412313ee

> Waiting for execution.
- attempt: 1/10
- attempt: 2/10
> Getting state root hash.
 - d196121dc9613867e228fb807c798fee4e9d1d7d84a2ba6e1a9067494a63c231

> Getting account hash.
 - c6ec39ce20e3bda02cd061e5c91698b0be8a5d5f9abf2b95b8a2a639ae88d060

> Getting contract hash.
 - 9397c7dbfe9ffd60fd03774d8b570bcb3a3a482c5eb3202c97779cb5c0dac008

At this point you have contract hash that is ready to be used in the next steps.

3. Initialize Casper Event Standard

$ ./call-init-entrypoint.sh 9397c7dbfe9ffd60fd03774d8b570bcb3a3a482c5eb3202c97779cb5c0dac008
> Calling 'init' entrypoint.
 - 24c05e37de1c453dd85ef9d93384583c3bbc7106dc916b219e02335cfd3a78ff

> Waiting for execution.
- attempt: 1/20
- attempt: 2/20
- attempt: 3/20
- attempt: 4/20

*-------*
| DONE! |
*-------*

4. Call deposit/batch entrypoint to emit test events

./call-batch-entrypoint.sh 9397c7dbfe9ffd60fd03774d8b570bcb3a3a482c5eb3202c97779cb5c0dac008
> Calling 'batch' entrypoint.
 - 63342404ed21eab1d3a29703a76f81834a0e8bb3bdf3ad1caa53357bf2bd4c38

> Waiting for execution.
- attempt: 1/20
- attempt: 2/20
- attempt: 3/20
- attempt: 4/20
- attempt: 5/20

*-------*
| DONE! |
*-------*

Additional changes for demo

In order to record nice and quick demo, I updated subscribed to include relevant logs:

diff --git a/kairos-server/src/main.rs b/kairos-server/src/main.rs
index 107e421..7b8f1ce 100644
--- a/kairos-server/src/main.rs
+++ b/kairos-server/src/main.rs
@@ -5,6 +5,11 @@ use kairos_server::config::ServerConfig;
 async fn main() {
     let subscriber = tracing_subscriber::fmt()
         .with_max_level(tracing::Level::INFO)
+        .with_env_filter(
+            "kairos_server::l1_sync=debug,kairos_server=warn"
+                .parse::<tracing_subscriber::EnvFilter>()
+                .unwrap(),
+        )
         .finish();

     tracing::subscriber::set_global_default(subscriber).expect("Failed to set subscriber");

I also reduced synchronization interval from 30 to 10 seconds:

diff --git a/kairos-server/src/l1_sync/interval_trigger.rs b/kairos-server/src/l1_sync/interval_trigger.rs
index 419f13c..08acc65 100644
--- a/kairos-server/src/l1_sync/interval_trigger.rs
+++ b/kairos-server/src/l1_sync/interval_trigger.rs
@@ -5,7 +5,7 @@ use std::sync::Arc;
 use super::service::L1SyncService;

 pub async fn run(sync_service: Arc<L1SyncService>) {
-    let mut interval = time::interval(Duration::from_secs(30));
+    let mut interval = time::interval(Duration::from_secs(10));

     loop {
         interval.tick().await;
github-actions[bot] commented 4 months ago

File Coverage
All files 51% :x:
kairos-crypto/src/implementations/casper.rs 6% :x:
kairos-server/src/config.rs 0% :x:
kairos-server/src/errors.rs 12% :x:
kairos-server/src/lib.rs 87% :white_check_mark:
kairos-server/src/utils.rs 22% :x:
kairos-server/src/routes/deposit.rs 88% :white_check_mark:
kairos-server/src/routes/transfer.rs 90% :white_check_mark:
kairos-server/src/state/transactions.rs 57% :x:
kairos-server/src/state/trie.rs 35% :x:
kairos-server/src/l1_sync/event_manager.rs 12% :x:
kairos-server/src/l1_sync/interval_trigger.rs 0% :x:
kairos-server/src/l1_sync/service.rs 56% :x:
kairos-test-utils/src/cctl.rs 87% :white_check_mark:
kairos-server/src/state/transactions/batch_state.rs 42% :x:
kairos-test-utils/src/cctl/parsers.rs 66% :white_check_mark:
kairos-tx/src/asn.rs 48% :x:
kairos-tx/src/error.rs 0% :x:
kairos-server/tests/transactions.rs 85% :white_check_mark:

Minimum allowed coverage is 60%

Generated by :monkey: cobertura-action against 70c7057e3516ecd853e5fccbfa051baf4bba2e7f

koxu1996 commented 3 months ago

@Avi-D-coder @marijanp Ready for re-review.