7个版本

0.1.6 2019年6月4日
0.1.5 2019年6月4日
0.1.4 2019年5月31日
0.1.1 2019年4月28日

#5 in #u256

Apache-2.0

135KB
3K SLoC

cita-vm

Build Status

为CITA快速实现EVM。针对高性能优化,比parity快5倍。完全基于以太坊黄皮书(版本:2d0661f 2018-11-08)

[dependencies]
cita-vm = "0.3.3"

示例

让我们从一个最简单的例子开始。首先,我们在内存中初始化一个数据库

let db = Arc::new(cita_vm::state::MemoryDB::new(false));

然后,将此db包装为WorldState

let mut state = cita_vm::state::State::new(db).unwrap();

WorldState中创建一些账户,其中一个存储了SimpleStorage代码。

let code = "6080604052600436106049576000357c0100000000000000000000000000000\
            000000000000000000000000000900463ffffffff16806360fe47b114604e57\
            80636d4ce63c146078575b600080fd5b348015605957600080fd5b506076600\
            4803603810190808035906020019092919050505060a0565b005b3480156083\
            57600080fd5b50608a60aa565b6040518082815260200191505060405180910\
            390f35b8060008190555050565b600080549050905600a165627a7a72305820\
            99c66a25d59f0aa78f7ebc40748fa1d1fbc335d8d780f284841b30e0365acd9\
            60029";
state.new_contract(
    &Address::from_str("0xBd770416a3345F91E4B34576cb804a576fa48EB1").unwrap(),
    U256::from(10),
    U256::from(1),
    hex::decode(code).unwrap(),
);
state.new_contract(
    &Address::from_str("0x1000000000000000000000000000000000000000").unwrap(),
    U256::from(1000000000000000u64),
    U256::from(1),
    vec![],
);

发送一个交易调用SimpleStorage.set(42)

let block_data_provider: Arc<cita_vm::BlockDataProvider> =
        Arc::new(cita_vm::BlockDataProviderMock::default());
let state_data_provider = Arc::new(RefCell::new(state));
let context = cita_vm::evm::Context::default();
let config = cita_vm::Config {
    block_gas_limit: 8000000,
};

let tx = cita_vm::Transaction {
    from: Address::from_str("0x1000000000000000000000000000000000000000").unwrap(),
    to: Some(Address::from("0xBd770416a3345F91E4B34576cb804a576fa48EB1")),
    value: U256::from(0),
    nonce: U256::from(1),
    gas_limit: 80000,
    gas_price: U256::from(1),
    input: hex::decode(
        "60fe47b1000000000000000000000000000000000000000000000000000000000000002a",
    )
    .unwrap(),
};
let r = cita_vm::exec(
    block_data_provider.clone(),
    state_data_provider.clone(),
    context.clone(),
    config.clone(),
    tx,
)
.unwrap();

发送一个交易调用SimpleStorage.get()

let tx = cita_vm::Transaction {
    from: Address::from_str("0x1000000000000000000000000000000000000000").unwrap(),
    to: Some(Address::from_str("0xBd770416a3345F91E4B34576cb804a576fa48EB1").unwrap()),
    value: U256::from(0),
    nonce: U256::from(2),
    gas_limit: 80000,
    gas_price: U256::from(1),
    input: hex::decode("6d4ce63c").unwrap(),
};
let r = cita_vm::exec(
    block_data_provider.clone(),
    state_data_provider.clone(),
    context.clone(),
    config.clone(),
    tx,
)
.unwrap();
println!("return={:?}", r); // 42 is outputed

完整代码可以在这里找到。您可以通过以下方式运行它:

$ cargo run --example simplestorage

与parity和geth的性能比较

简而言之,cita-vm是最快的EVM。以下是在benchmark测试结果,在Intel(R) Xeon(R) CPU E5-26xx v3上。

img

img

测试

$ make testdata
$ cargo test

许可证

Apache License 2.0

依赖关系

~2.8–4MB
~69K SLoC