forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctx.fe
More file actions
203 lines (177 loc) · 5.44 KB
/
Copy pathctx.fe
File metadata and controls
203 lines (177 loc) · 5.44 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use std::evm::{Evm, Ctx, Address}
msg CtxMsg {
#[selector = 0x01]
GetOrigin -> u256,
#[selector = 0x02]
GetCoinbase -> u256,
#[selector = 0x03]
GetTimestamp -> u256,
#[selector = 0x04]
GetBlockNumber -> u256,
#[selector = 0x05]
GetPrevrandao -> u256,
#[selector = 0x06]
GetGaslimit -> u256,
#[selector = 0x07]
GetChainid -> u256,
#[selector = 0x08]
GetBasefee -> u256,
#[selector = 0x09]
GetBlockhash { block: u256 } -> u256,
#[selector = 0x0a]
GetAddress -> u256,
#[selector = 0x0b]
GetCaller -> u256,
#[selector = 0x0c]
GetGas -> u256,
}
pub contract CtxBox uses (ctx: Ctx) {
init() {}
recv CtxMsg {
GetOrigin {} -> u256
uses (ctx)
{
ctx.origin().inner
}
GetCoinbase {} -> u256
uses (ctx)
{
ctx.coinbase().inner
}
GetTimestamp {} -> u256
uses (ctx)
{
ctx.timestamp()
}
GetBlockNumber {} -> u256
uses (ctx)
{
ctx.block_number()
}
GetPrevrandao {} -> u256
uses (ctx)
{
ctx.prevrandao()
}
GetGaslimit {} -> u256
uses (ctx)
{
ctx.gaslimit()
}
GetChainid {} -> u256
uses (ctx)
{
ctx.chainid()
}
GetBasefee {} -> u256
uses (ctx)
{
ctx.basefee()
}
GetBlockhash { block } -> u256
uses (ctx)
{
ctx.blockhash(block)
}
GetAddress {} -> u256
uses (ctx)
{
ctx.address().inner
}
GetCaller {} -> u256
uses (ctx)
{
ctx.caller().inner
}
GetGas {} -> u256
uses (ctx)
{
ctx.gas()
}
}
}
// Test: ctx.origin() returns a value (may be zero in test env)
#[test]
fn test_origin() uses (evm: mut Evm) {
let addr = evm.create2<CtxBox>(value: 0, args: (), salt: 0)
let origin: u256 = evm.call(addr: addr, gas: 100000, value: 0, message: CtxMsg::GetOrigin {})
assert!(origin == origin)
}
// Test: ctx.coinbase() returns a value (may be zero in test env)
#[test]
fn test_coinbase() uses (evm: mut Evm) {
let addr = evm.create2<CtxBox>(value: 0, args: (), salt: 0)
let coinbase: u256 = evm.call(addr: addr, gas: 100000, value: 0, message: CtxMsg::GetCoinbase {})
// coinbase may be zero in test environment, just verify the call succeeds
assert!(coinbase == coinbase)
}
// Test: ctx.timestamp() returns a non-zero value
#[test]
fn test_timestamp() uses (evm: mut Evm) {
let addr = evm.create2<CtxBox>(value: 0, args: (), salt: 0)
let ts: u256 = evm.call(addr: addr, gas: 100000, value: 0, message: CtxMsg::GetTimestamp {})
assert!(ts != 0)
}
// Test: ctx.block_number() returns a value
#[test]
fn test_block_number() uses (evm: mut Evm) {
let addr = evm.create2<CtxBox>(value: 0, args: (), salt: 0)
let num: u256 = evm.call(addr: addr, gas: 100000, value: 0, message: CtxMsg::GetBlockNumber {})
assert!(num == num)
}
// Test: ctx.prevrandao() returns a value
#[test]
fn test_prevrandao() uses (evm: mut Evm) {
let addr = evm.create2<CtxBox>(value: 0, args: (), salt: 0)
let rand: u256 = evm.call(addr: addr, gas: 100000, value: 0, message: CtxMsg::GetPrevrandao {})
assert!(rand == rand)
}
// Test: ctx.gaslimit() returns a non-zero value
#[test]
fn test_gaslimit() uses (evm: mut Evm) {
let addr = evm.create2<CtxBox>(value: 0, args: (), salt: 0)
let limit: u256 = evm.call(addr: addr, gas: 100000, value: 0, message: CtxMsg::GetGaslimit {})
assert!(limit != 0)
}
// Test: ctx.chainid() returns a non-zero value
#[test]
fn test_chainid() uses (evm: mut Evm) {
let addr = evm.create2<CtxBox>(value: 0, args: (), salt: 0)
let id: u256 = evm.call(addr: addr, gas: 100000, value: 0, message: CtxMsg::GetChainid {})
assert!(id != 0)
}
// Test: ctx.basefee() returns a value
#[test]
fn test_basefee() uses (evm: mut Evm) {
let addr = evm.create2<CtxBox>(value: 0, args: (), salt: 0)
let fee: u256 = evm.call(addr: addr, gas: 100000, value: 0, message: CtxMsg::GetBasefee {})
assert!(fee == fee)
}
// Test: ctx.blockhash() returns a value for a given block
#[test]
fn test_blockhash() uses (evm: mut Evm) {
let addr = evm.create2<CtxBox>(value: 0, args: (), salt: 0)
let hash: u256 = evm.call(addr: addr, gas: 100000, value: 0, message: CtxMsg::GetBlockhash { block: 0 })
assert!(hash == hash)
}
// Test: ctx.address() returns the contract's own address
#[test]
fn test_address() uses (evm: mut Evm) {
let addr = evm.create2<CtxBox>(value: 0, args: (), salt: 0)
let reported: u256 = evm.call(addr: addr, gas: 100000, value: 0, message: CtxMsg::GetAddress {})
assert!(reported == addr.inner)
}
// Test: ctx.caller() returns the caller address
#[test]
fn test_caller() uses (evm: mut Evm) {
let addr = evm.create2<CtxBox>(value: 0, args: (), salt: 0)
let caller: u256 = evm.call(addr: addr, gas: 100000, value: 0, message: CtxMsg::GetCaller {})
assert!(caller != 0)
}
// Test: ctx.gas() returns remaining gas (non-zero)
#[test]
fn test_gas() uses (evm: mut Evm) {
let addr = evm.create2<CtxBox>(value: 0, args: (), salt: 0)
let remaining: u256 = evm.call(addr: addr, gas: 100000, value: 0, message: CtxMsg::GetGas {})
assert!(remaining != 0)
}