- 
      
- 
        Save tavakyan/bdd2bae16cfe04d516491809f9c7b503 to your computer and use it in GitHub Desktop. 
    How to use the evm crate
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | extern crate evm; | |
| extern crate hex; | |
| extern crate primitive_types; | |
| use evm::backend::{MemoryBackend, MemoryVicinity, MemoryAccount}; | |
| use primitive_types::{H160, U256}; | |
| use std::error::Error; | |
| use std::collections::BTreeMap; | |
| use evm::executor::StackExecutor; | |
| use evm::{Config, Runtime, Context}; | |
| use std::rc::Rc; | |
| fn main() -> Result<(), Box<dyn Error>> { | |
| println!("hello world"); | |
| let vicinity = MemoryVicinity { | |
| gas_price: U256::zero(), | |
| /// Origin. | |
| origin: H160::random(), | |
| /// Chain ID. | |
| chain_id: U256::from(1), | |
| /// Environmental block hashes. | |
| block_hashes: vec![ | |
| "00000000000000001ebf88508a03865c71d452e25f4d51194196a1d22b6653dc" | |
| .parse() | |
| .unwrap(), | |
| "00000000000000010ff5414c5cfbe9eae982e8cef7eb2399a39118e1206c8247" | |
| .parse() | |
| .unwrap(), | |
| ], | |
| /// Environmental block number. | |
| block_number: U256::from(0), | |
| /// Environmental coinbase. | |
| block_coinbase: H160::zero(), | |
| /// Environmental block timestamp. | |
| block_timestamp: U256::from(1529891469), | |
| /// Environmental block difficulty. | |
| block_difficulty: U256::zero(), | |
| /// Environmental block gas limit. | |
| block_gas_limit: U256::zero(), | |
| }; | |
| println!("vicinity => {:#?}", vicinity); | |
| let state = BTreeMap::new(); | |
| let backend = MemoryBackend::new(&vicinity, state); | |
| let config = Config::odyssey_3_7(); | |
| let gas_limit = 1_000_000; | |
| let mut executor = StackExecutor::new(&backend, gas_limit, &config); | |
| println!("gas => {}", executor.gas()); | |
| let ctx = Context { | |
| address: "5cbdd86a2fa8dc4bddd8a8f69dba48572eec07fb".parse().unwrap(), | |
| caller: H160::random(), | |
| // call value, if nonzero, must have a payable | |
| call_value: U256::from(0), // call value | |
| call_token_id: U256::from(0), | |
| call_token_value: U256::from(0), | |
| }; | |
| println!("ctx => {:#?}", ctx); | |
| println!("==========================================================================="); | |
| // TUVjJmodgMGfpnU2tdrivRVmM5v5Leht2G | |
| let code = Rc::new(hex::decode("6080604052348015600f57600080fd5b506004361060325760003560e01c80632096525514603757806355241077146053575b600080fd5b603d607e565b6040518082815260200191505060405180910390f35b607c60048036036020811015606757600080fd5b81019080803590602001909291905050506087565b005b60008054905090565b806000819055505056fea265627a7a72315820fae59b6c1b73f3b64ed8a4c322273a91bd474c7848feb439a4856550274874be64736f6c63430005100032").unwrap()); | |
| // getValue => 229, Succeed(Returned) | |
| // d14a0470500c4f6f6603587b2d8e8176a5a5aa623738ab97489ef0aba5483cc9 | |
| // let data = Rc::new(hex::decode("20965255").unwrap()); | |
| // setValue => 20239, Succeed(Stopped), 0 => 20239 | |
| // 1954ba5e6c187b8ea46470d6ee33067209cd9480b04da36f282e9f1bb8c2f82e | |
| // tvm: some to some: 5239 | |
| // : some to 0: 5239 | |
| // : 0 to some: 20239 | |
| let data = Rc::new(hex::decode("55241077000000000000000000000000000000000000000000000000000000000000008c").unwrap()); | |
| let mut rt = Runtime::new(code.clone(), data.clone(), ctx.clone(), &config); | |
| let exit_reason = executor.execute(&mut rt); | |
| println!("exit => {:?}", exit_reason); | |
| let ret_val = rt.machine().return_value(); | |
| println!("return => {:?}", hex::encode(ret_val)); | |
| println!("stack => {:?}", rt.machine().stack()); | |
| println!("memory => {:?}", rt.machine().memory()); | |
| // 2290 | |
| println!("consumed gas/energy => {}", gas_limit - executor.gas()); | |
| let gas_limit = executor.gas(); | |
| { | |
| let data = Rc::new(hex::decode("552410770000000000000000000000000000000000000000000000000000000000000001").unwrap()); | |
| let mut rt = Runtime::new(code, data, ctx, &config); | |
| let exit_reason = executor.execute(&mut rt); | |
| println!("exit => {:?}", exit_reason); | |
| println!("consumed gas/energy => {}", gas_limit - executor.gas()); | |
| } | |
| /* | |
| { | |
| let data = Rc::new(hex::decode("20965255").unwrap()); | |
| let mut rt = Runtime::new(code, data, ctx, &config); | |
| let exit_reason = executor.execute(&mut rt); | |
| println!("exit => {:?}", exit_reason); | |
| let ret_val = rt.machine().return_value(); | |
| println!("return => {:?}", hex::encode(ret_val)); | |
| } | |
| */ | |
| Ok(()) | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment