forked from hyperledger-solang/solang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmilestone_3.rs
More file actions
88 lines (75 loc) · 2.7 KB
/
milestone_3.rs
File metadata and controls
88 lines (75 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! This test expects you to have a devnode running:
//! <https://docs.arbitrum.io/run-arbitrum-node/run-nitro-dev-node>
//!
//! It also expects `cargo-stylus` and `cast` to be installed:
//! - <https://github.com/OffchainLabs/cargo-stylus>
//! - <https://book.getfoundry.sh/cast/>
#![warn(clippy::pedantic)]
use crate::{call, deploy, send, MUTEX};
use std::path::PathBuf;
use tiny_keccak::{Hasher, Keccak};
#[test]
fn milestone_3() {
let _lock = MUTEX.lock();
let (tempdir, address) = deploy(
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("integration/stylus/milestone_3.sol"),
"C",
)
.unwrap();
let dir = &tempdir;
let stdout = send(dir, &address, ["accept_donation()", "--value=1000"]).unwrap();
println!("{}", &stdout);
let stdout = call(
dir,
&address,
[
"test()(uint256,bytes32,bytes32,uint256)",
"--gas-price=100000000",
],
)
.unwrap();
let labeled_stdout = label(&stdout);
println!("{}", &labeled_stdout);
let balance = get(&labeled_stdout, "balance").unwrap();
assert_eq!(1000, u64::from_str_radix(balance, 10).unwrap());
let codehash = get(&labeled_stdout, "codehash")
.map(|s| s.strip_prefix("0x"))
.flatten()
.unwrap();
let stdout = call(dir, &address, ["getCode()"]).unwrap();
let len_prefixed_code = stdout.strip_prefix("0x").unwrap();
let len = usize::from_str_radix(&len_prefixed_code[..64], 16).unwrap();
let code = hex::decode(&len_prefixed_code[64..].trim_end()).unwrap();
assert_eq!(len, code.len());
let digest = keccak256(&code);
assert_eq!(codehash, hex::encode(digest));
let gasprice = get(&labeled_stdout, "gasprice").unwrap();
let i = gasprice
.bytes()
.position(|c| c.is_ascii_whitespace())
.unwrap_or_else(|| gasprice.len());
assert_eq!(100000000, u64::from_str_radix(&gasprice[..i], 10).unwrap());
call(dir, &address, ["test_addmod()"]).unwrap();
call(dir, &address, ["test_mulmod()"]).unwrap();
}
fn label(stdout: &str) -> String {
const LABELS: &[&str] = &["balance", "codehash", "manual_codehash", "gasprice"];
let lines = stdout.lines().collect::<Vec<_>>();
assert_eq!(LABELS.len(), lines.len());
LABELS
.iter()
.zip(lines)
.map(|(label, line)| format!("{label} = {line}\n"))
.collect()
}
fn get<'a>(stdout: &'a str, label: &str) -> Option<&'a str> {
let prefix = format!("{label} = ");
stdout.lines().find_map(|line| line.strip_prefix(&prefix))
}
fn keccak256(input: &[u8]) -> [u8; 32] {
let mut output = [0u8; 32];
let mut hasher = Keccak::v256();
hasher.update(&input);
hasher.finalize(&mut output);
output
}