Vara入门实战安装https://get.gear.rs/https://wiki.gear-tech.io/docs/node/setting-up/install-with-pre-build-binarycurlhttps://get.gear.rs/gear-v1.4.1
curl https://get.gear.rs/gear-v1.4.1-aarch64-apple-darwin.tar.xz | tar xJ
gear --version
gear 1.4.1-a25a431f157
函数 | 作用 | 支持的操作 |
---|---|---|
init | 初始化状态 | load, send, reply |
handle | 处理收到的消息 | load, send, reply |
handle_reply | 处理收到的回复 | load, send |
state | 查询状态(只读) | load, reply |
static mut STATE: Option<HashMap<ActorId, u128>> = None;
#[no_mangle]
extern fn init() {
unsafe { STATE = Some(Default::default()) }
}
#[no_mangle]
extern fn handle() {
let payload = msg::load().expect("Failed to load payload");
if let PingPong::Ping = payload {
let pingers = unsafe { STATE.as_mut().expect("State isn't initialized") };
pingers
.entry(msg::source())
.and_modify(|ping_count| *ping_count = ping_count.saturating_add(1))
.or_insert(1);
msg::reply(PingPong::Pong, 0).expect("Failed to reply from `handle()`");
}
}
#[no_mangle]
extern fn state() {
let state = unsafe { STATE.take().expect("State isn't initialized") };
msg::reply(State::from_iter(state), 0).expect("Failed to reply from `state()`");
}
请求: A 发送消息 (init, handle) => B 接收消息 (handle)
回复: A 获取回复 (handle_reply) <= B 回复消息 (handle)
gstd::debug!()
gstd::dbg!()
cargo build
或
cargo build --release --features=debug
查看 debug 信息:
RUST_LOG="gwasm=debug"
启动本地节点gtest
单元测试是一个轻量级的测试框架,在链下环境模拟:
[dev-dependencies]
gtest = { version = "1.4.1" }
let sys = System::new();
let program = Program::from_file(
&sys,
"./target/wasm32-unknown-unknown/release/demo_ping.wasm",
);
let program_id = program.id();
let res = program.send_bytes(100001, "INIT MESSAGE");
assert!(res.log().is_empty());
assert!(!res.main_failed());
let res = program.send_bytes(100001, "INIT MESSAGE");
assert!(res.log().is_empty());
assert!(!res.main_failed());
let expected_log = Log::builder()
.source(ping_pong_id)
.dest(100001)
.payload_bytes("PONG");
assert!(res.contains(&expected_log));
assert!(!res.main_failed());
sys.spend_blocks(150);
sys.mint_to(42, 5000);
let prog = Program::current(&sys);
prog.mint(1000);
sys.spend_blocks(150);
sys.mint_to(42, 5000);
let prog = Program::current(&sys);
prog.mint(1000);
gclient 是一个支持在链上对合约进行端到端测试的工具
[dev-dependencies]
gclient = { version = "1.4.1" }
tokio = { version = "1", features = ["full"] }
# 检查节点版本与 gclient 一致
./gear --version
// Create API instance
let api = GearApi::dev().await?;
let mut listener = api.subscribe().await?;
// Check that blocks are still running
assert!(listener.blocks_running().await?);
// Calculate gas amount needed for initialization
let gas_info = api
.calculate_upload_gas(
None,
gclient::code_from_os(WASM_PATH)?,
vec![],
0,
true,
)
.await?;
let payload = b"inc".to_vec();
// Calculate gas amount needed for handling the message
let gas_info = api
.calculate_handle_gas(None, program_id, payload.clone(), 0, true)
.await?;
// Upload and init the program
let (message_id, program_id, _hash) = api
.upload_program_bytes_by_path(
WASM_PATH,
gclient::now_micros().to_le_bytes(),
vec![],
gas_info.min_limit,
0,
)
.await?;
assert!(listener.message_processed(message_id).await?.succeed());
// Send the inc message
let (message_id, _hash) = api
.send_message_bytes(program_id, payload, gas_info.min_limit, 0)
.await?;
assert!(listener.message_processed(message_id).await?.succeed());
// Listen and verify the returned message
if let (message_id, result, value) = listener.reply_bytes_on(message_id).await? {
if let Ok(data) = result {
println!("Data: {:?}", data);
assert_eq!(data, b"1");
} else if let Err(error) = result {
println!("Error: {:?}", error);
}
}
wasm-opt
https://github.com/WebAssembly/binaryenrustup
https://rustup.rs/gear
https://get.gear.rs/# Vara Network Testnet
gear
# Vara Network Mainnet
gear --chain vara
# 生成地址
gear key generate
# 检查地址
gear key inspect
gear --help
Usage: gear [OPTIONS]
gear <COMMAND>
Commands:
key Key management cli utilities
build-spec Build a chain specification
check-block Validate blocks
export-blocks Export blocks
export-state Export the state of a given block into a chain spec
import-blocks Import blocks
purge-chain Remove the whole chain
revert Revert the chain to a previous state
try-runtime Try-runtime has migrated to a standalone CLI (<https://github.com/paritytech/try-runtime-cli>). The subcommand exists as a stub and deprecation notice. It will be removed entirely some time after January 2024
chain-info Db meta columns information
gcli Run gear program cli.
help Print this message or the help of the given subcommand(s)
Options:
--validator
https://idea.gear-tech.io/programs?node=ws://localhost:9944
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!