Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Research on Light Client for Mobile #393

Open
Danie0918 opened this issue May 27, 2024 · 6 comments
Open

Research on Light Client for Mobile #393

Danie0918 opened this issue May 27, 2024 · 6 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@Danie0918
Copy link
Contributor

Danie0918 commented May 27, 2024

Nowadays, mobile has become the main use of the user's scene, we need to complete the light client mobile adaptation to develop mobile Neuron.

@Danie0918 Danie0918 added the documentation Improvements or additions to documentation label May 27, 2024
@yanguoyu
Copy link

yanguoyu commented Jun 6, 2024

There may be two approaches to running a light client on Mobile

  1. Build a Mobile arch target to run it.
  2. Depend on a virtual environment to run computer arch target.

Here is the reference:
Build the ios target: https://doc.rust-lang.org/rustc/platform-support/arm64e-apple-ios.html
A virtual environment on ios: https://github.com/utmapp/UTM This will start a new OS, so it does not belong to run a node at Mobile.

I will try to see which approach works best.

Update 06-11:
Build target x86_64-apple-ios failed with ckb-rocksdb. I will check why the ckb-rocksdb build failed.

@yanguoyu
Copy link

yanguoyu commented Jun 17, 2024

Recording the compilation process:

  1. Encountered an issue with not finding the openssl library corresponding to iOS, requiring manual compilation of the openssl library or using a precompiled version from someone else. https://github.com/x2on/OpenSSL-for-iPhone
export OPENSSL_LIB_DIR=OpenSSL-for-iPhone/bin/iPhoneSimulator17.4-x86_64.sdk/lib
export OPENSSL_INCLUDE_DIR=OpenSSL-for-iPhone/bin/iPhoneSimulator17.4-x86_64.sdk/include
  1. ckb-rocksdb does not support iOS compilation targets, requiring modification of the compilation script. Here is the fork repo's PR
  2. Currently, iOS does not support the thread_local keyword. I have removed this keyword for now, and further research is needed to determine if it can be replaced with another method.
  3. Currently, it can be compiled into a library and used in an iOS project. The build is successful, but the call fails due to the subCommand not being passed. Reference: https://mozilla.github.io/firefox-browser-architecture/experiments/2017-09-06-rust-on-ios.html

@Keith-CY
Copy link
Member

Keith-CY commented Jul 2, 2024

Please share the solution of the compilation for iOS, because there's a community team interested in running the light client on mobile. Maybe we can push it together.

@yanguoyu
Copy link

yanguoyu commented Jul 3, 2024

Please share the solution of the compilation for iOS, because there's a community team interested in running the light client on mobile. Maybe we can push it together.

#393 (comment)
By referring to the above, we can build a light client for iOS and invoke it in the iOS application.

@yanguoyu
Copy link

yanguoyu commented Jul 19, 2024

Currently, the light client was called by the iOS app, but the light client run failed with some exceptions. It may be caused by the ckb-rocksdb because I have removed the thread_local locally, I will find a way to replace the thread_local .

@yanguoyu
Copy link

yanguoyu commented Aug 2, 2024

Major progress: The light client can now run successfully on iOS. Here is the demo

2024-08-01.21.37.36.mov

Here is the full process:

  1. Using a precompiled version from here. https://github.com/x2on/OpenSSL-for-iPhone
export OPENSSL_LIB_DIR=OpenSSL-for-iPhone/bin/iPhoneSimulator17.4-x86_64.sdk/lib
export OPENSSL_INCLUDE_DIR=OpenSSL-for-iPhone/bin/iPhoneSimulator17.4-x86_64.sdk/include
  1. Fork and edit ckb-rocksdb
  1. Fork and edit https://github.com/nervosnetwork/ckb-light-client
  • Edit Cargo.toml; change rocksdb refer to local change repo and add exort for lib
[patch.crates-io]
rocksdb = { package = "ckb-rocksdb", path="the ckb-rocksdk that fork and edit in previous", version ="=0.21.1" }

[lib]
name = "ckb_light_client"
crate-type = ["staticlib", "cdylib"]
  • Add a lib.rs to build for lib
#![allow(clippy::mutable_key_type)]

#[cfg(test)]
#[macro_use]
mod tests;

mod config;
mod error;
mod protocols;
mod service;
mod storage;
mod subcmds;
mod types;
mod utils;
mod verify;

use ckb_app_config::HeaderMapConfig;
use ckb_app_config::NetworkConfig;
use ckb_app_config::SyncConfig;
use config::RunConfig;
use config::parse_config_file;
use env_logger::{Builder, Env, Target};
use types::RpcConfig;
use types::RunEnv;
use types::StoreConfig;
use std::path::PathBuf;
use std::path::Path;
use ckb_types::U256;
use std::os::raw::c_char;
use std::ffi::{CString, CStr};

#[no_mangle]
pub extern fn run_light(to: *const c_char) -> anyhow::Result<()> {
let mut builder = Builder::from_env(Env::default());
builder.target(Target::Stdout);
builder
    .try_init()
    .expect("env_logger builder init should be ok");


let c_str = unsafe { CStr::from_ptr(to) };
let recipient = match c_str.to_str() {
    Err(_) => "there",
    Ok(string) => string,
};
println!("Starting run light client ..., config path is {}", recipient.to_string());
let run_env = parse_config_file::<RunEnv>(recipient)?;
match (RunConfig{ run_env }).execute() {
  Ok(_) => {
    println!("start light client success");
  }
  Err(e) => {
    println!("start light client failed ERROR: {:?}", e)
  }
};

println!("Done.");

Ok(())
}
  • export parse_config_file from config.rs
          pub(crate) fn parse_config_file<T: FromStr>(file: &str) -> Result<T>
          where
              <T as FromStr>::Err: Display,
          {
              OpenOptions::new()
              .read(true)
              .open(file)
              .map_err(|err| Error::config(format!("failed to open {} since {}", file, err)))
              .and_then(|mut f| {
                  let mut buffer = String::new();
                  f.read_to_string(&mut buffer)
                      .map_err(|err| {
                          Error::config(format!("failed to read {} since {}", file, err))
                      })
                      .map(|_| buffer)
              })
              .and_then(|data| T::from_str(&data).map_err(Error::config))
          }
  1. Build the ckb-light-client for ios and use it in the iOS project refer to https://mozilla.github.io/firefox-browser-architecture/experiments/2017-09-06-rust-on-ios.html and here is some code snippet https://github.com/yanguoyu/demo-run-light-iOS
cargo build --target x86_64-apple-ios

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
Status: 📫Hold On
Development

No branches or pull requests

3 participants