Skip to content

Commit 674d8bc

Browse files
anvacaruehildenb
authored andcommitted
Implement Custom RPC Methods (#443)
* implement evm_snapshot rpc method * implement evm_revert rpc method without arguments * web3.md: implement evm_revert rpc method with args (wip) * implement evm_increaseTime rpc method * fix evm_revert; formatting * web3: formatting
1 parent 0e5a833 commit 674d8bc

9 files changed

+66
-1
lines changed

tests/web3/evm_increaseTime.in.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{"jsonrpc":"2.0", "method":"evm_snapshot", "params":[], "id":1}
2+
{"jsonrpc":"2.0", "method":"evm_increaseTime", "params":[100], "id":2}
3+
{"jsonrpc":"2.0", "method":"evm_increaseTime", "params":[200], "id":3}
4+
{"jsonrpc":"2.0", "method":"evm_revert", "params":[], "id":4}
5+
{"jsonrpc":"2.0", "method":"evm_increaseTime", "params":[50], "id":5}
6+
7+

tests/web3/evm_increaseTime.out.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"jsonrpc":"2.0","id":1,"result":"0x0"}{"jsonrpc":"2.0","id":2,"result":"100"}{"jsonrpc":"2.0","id":3,"result":"300"}{"jsonrpc":"2.0","id":4,"result":"true"}{"jsonrpc":"2.0","id":5,"result":"350"}

tests/web3/evm_revert.in.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{"jsonrpc":"2.0", "method":"evm_snapshot", "params":[], "id":1}
2+
{"jsonrpc":"2.0", "method":"evm_snapshot", "params":[], "id":2}
3+
{"jsonrpc":"2.0", "method":"evm_revert", "params":[], "id":3}
4+
{"jsonrpc":"2.0", "method":"evm_snapshot", "params":[], "id":4}
5+
6+

tests/web3/evm_revert.out.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"jsonrpc":"2.0","id":1,"result":"0x0"}{"jsonrpc":"2.0","id":2,"result":"0x1"}{"jsonrpc":"2.0","id":3,"result":"true"}{"jsonrpc":"2.0","id":4,"result":"0x1"}

tests/web3/evm_revert2.in.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{"jsonrpc":"2.0", "method":"evm_snapshot", "params":[], "id":1}
2+
{"jsonrpc":"2.0", "method":"evm_snapshot", "params":[], "id":2}
3+
{"jsonrpc":"2.0", "method":"evm_snapshot", "params":[], "id":3}
4+
{"jsonrpc":"2.0", "method":"evm_revert", "params":["0x1"], "id":4}
5+
{"jsonrpc":"2.0", "method":"evm_snapshot", "params":[], "id":5}
6+
7+

tests/web3/evm_revert2.out.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"jsonrpc":"2.0","id":1,"result":"0x0"}{"jsonrpc":"2.0","id":2,"result":"0x1"}{"jsonrpc":"2.0","id":3,"result":"0x2"}{"jsonrpc":"2.0","id":4,"result":"true"}{"jsonrpc":"2.0","id":5,"result":"0x1"}

tests/web3/evm_snapshot.in.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{"jsonrpc":"2.0", "method":"evm_snapshot", "params":[], "id":1}
2+
{"jsonrpc":"2.0", "method":"evm_snapshot", "params":[], "id":2}
3+

tests/web3/evm_snapshot.out.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"jsonrpc":"2.0","id":1,"result":"0x0"}{"jsonrpc":"2.0","id":2,"result":"0x1"}

web3.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module WEB3
1515
<blockchain>
1616
<chainID> $CHAINID:Int </chainID>
1717
</blockchain>
18+
<snapshots> .List </snapshots>
1819
<web3socket> $SOCK:Int </web3socket>
1920
<web3clientsocket> 0:IOInt </web3clientsocket>
2021
<web3request>
@@ -119,7 +120,7 @@ module WEB3
119120
rule <k> #sendResponse(J) ~> _ => #loadFromBatch </k>
120121
<callid> CALLID </callid>
121122
<batch> [ _ ] </batch>
122-
<web3response>... .List => ListItem({ "jsonrpc": "2.0", "id": CALLID, J }) </web3response>
123+
<web3response> ... .List => ListItem({ "jsonrpc": "2.0", "id": CALLID, J }) </web3response>
123124
124125
rule <k> #sendResponse(_) ~> _ => #loadFromBatch </k>
125126
<callid> undef </callid>
@@ -160,6 +161,12 @@ module WEB3
160161
<method> "eth_getStorageAt" </method>
161162
rule <k> #runRPCCall => #eth_getCode ... </k>
162163
<method> "eth_getCode" </method>
164+
rule <k> #runRPCCall => #evm_snapshot ... </k>
165+
<method> "evm_snapshot" </method>
166+
rule <k> #runRPCCall => #evm_revert ... </k>
167+
<method> "evm_revert" </method>
168+
rule <k> #runRPCCall => #evm_increaseTime ... </k>
169+
<method> "evm_increaseTime" </method>
163170
164171
rule <k> #runRPCCall => #sendResponse( "error": {"code": -32601, "message": "Method not found"} ) ... </k> [owise]
165172
@@ -230,5 +237,36 @@ module WEB3
230237
<code> CODE </code>
231238
...
232239
</account>
240+
241+
syntax KItem ::= "#evm_snapshot"
242+
// --------------------------------
243+
rule <k> #evm_snapshot => #pushNetworkState ~> #sendResponse( "result" : #unparseQuantity( size ( SNAPSHOTS ) ) ) ... </k>
244+
<snapshots> SNAPSHOTS </snapshots>
245+
246+
syntax KItem ::= "#pushNetworkState"
247+
// ------------------------------------
248+
rule <k> #pushNetworkState => . ... </k>
249+
<snapshots> ... ( .List => ListItem(NETWORKSTATE)) </snapshots>
250+
<network> NETWORKSTATE </network>
251+
252+
syntax KItem ::= "#evm_revert"
253+
// ------------------------------
254+
rule <k> #evm_revert => #sendResponse( "result" : "true" ) ... </k>
255+
<params> [ .JSONList ] </params>
256+
<snapshots> ... ( ListItem(NETWORKSTATE) => .List ) </snapshots>
257+
<network> ( _ => NETWORKSTATE ) </network>
258+
259+
rule <k> #evm_revert ... </k>
260+
<params> [ (DATA => #parseHexWord(DATA)), .JSONList ] </params>
261+
262+
rule <k> #evm_revert ... </k>
263+
<params> ( [ DATA:Int, .JSONList ] => [ .JSONList ] ) </params>
264+
<snapshots> ( SNAPSHOTS => range(SNAPSHOTS, 0, DATA ) ) </snapshots>
265+
266+
syntax KItem ::= "#evm_increaseTime"
267+
// ------------------------------------
268+
rule <k> #evm_increaseTime => #sendResponse( "result" : Int2String(TS +Int DATA ) ) ... </k>
269+
<params> [ DATA:Int, .JSONList ] </params>
270+
<timestamp> ( TS:Int => ( TS +Int DATA ) ) </timestamp>
233271
endmodule
234272
```

0 commit comments

Comments
 (0)