Skip to content

Commit 70e3f47

Browse files
gtreptaehildenb
authored andcommitted
Data.md refactoring (#632)
* data: Factor out Word operations, WordStack, ByteArray, and Account into evm-types.md data,evm-types: Move #addr from data to evm-types * data,evm: Move M3:2048 bloomfilter from data.md to evm.md, the only place it is used * data,driver: Move #removeZeros from data to driver, the only place it is used * data,serialization: Move various helpers from data to serialization * data,evm: Move #lookup to evm, next to the opcode that uses it * data,web3: Move JSON and JSON-RPC modules into json.md data,json: move JSONKEY ::= Int to JSON module * data: Move parsing/unparsing, rlp, and merkle tree stuff to serialization.md * Update concrete-rules.txt files * tests/interactive/sumTo10.evm.parse-expected: Change EVM-DATA to EVM-TYPES * evm,evm-types: Move #lookup to EVM-TYPES
1 parent 3949e08 commit 70e3f47

File tree

12 files changed

+1386
-1340
lines changed

12 files changed

+1386
-1340
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ MAIN_MODULE := ETHEREUM-SIMULATION
153153
SYNTAX_MODULE := $(MAIN_MODULE)
154154
export MAIN_DEFN_FILE := driver
155155

156-
k_files := driver.k data.k network.k evm.k krypto.k edsl.k evm-node.k web3.k asm.k state-loader.k
156+
k_files := driver.k data.k network.k evm.k evm-types.k json.k krypto.k edsl.k evm-node.k web3.k asm.k state-loader.k serialization.k
157157
EXTRA_K_FILES += $(MAIN_DEFN_FILE).k
158158
ALL_K_FILES := $(k_files) $(EXTRA_K_FILES)
159159

data.md

Lines changed: 5 additions & 1239 deletions
Large diffs are not rendered by default.

driver.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,18 @@ Note that `TEST` is sorted here so that key `"network"` comes before key `"pre"`
401401
</account>
402402
```
403403

404+
- `#removeZeros` removes any entries in a map with zero values.
405+
406+
```k
407+
syntax Map ::= #removeZeros ( Map ) [function]
408+
| #removeZeros ( List , Map ) [function, klabel(#removeZerosAux)]
409+
// ------------------------------------------------------------------------------
410+
rule #removeZeros( M ) => #removeZeros(Set2List(keys(M)), M)
411+
rule #removeZeros( .List, .Map ) => .Map
412+
rule #removeZeros( ListItem(KEY) L, KEY |-> 0 REST ) => #removeZeros(L, REST)
413+
rule #removeZeros( ListItem(KEY) L, KEY |-> VALUE REST ) => KEY |-> VALUE #removeZeros(L, REST) requires VALUE =/=K 0
414+
```
415+
404416
Here we check the other post-conditions associated with an EVM test.
405417

406418
```k

evm-types.md

Lines changed: 642 additions & 0 deletions
Large diffs are not rendered by default.

evm.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,22 @@ After executing a transaction, it's necessary to have the effect of the substate
709709
rule #bloomFilter(ListItem(WS:ByteArray) L, B) => #bloomFilter(L, B |Int M3:2048(WS))
710710
```
711711

712+
- `M3:2048` computes the 2048-bit hash of a log entry in which exactly 3 bits are set. This is used to compute the Bloom filter of a log entry.
713+
714+
```k
715+
syntax Int ::= "M3:2048" "(" ByteArray ")" [function]
716+
// -----------------------------------------------------
717+
rule M3:2048(WS) => setBloomFilterBits(#parseByteStack(Keccak256(#unparseByteStack(WS))))
718+
719+
syntax Int ::= setBloomFilterBits(ByteArray) [function]
720+
// -------------------------------------------------------
721+
rule setBloomFilterBits(HASH) => (1 <<Int getBloomFilterBit(HASH, 0)) |Int (1 <<Int getBloomFilterBit(HASH, 2)) |Int (1 <<Int getBloomFilterBit(HASH, 4))
722+
723+
syntax Int ::= getBloomFilterBit(ByteArray, Int) [function]
724+
// -----------------------------------------------------------
725+
rule getBloomFilterBit(X, I) => #asInteger(X [ I .. 2 ]) %Int 2048
726+
```
727+
712728
EVM Programs
713729
============
714730

json.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
### JSON Formatting
2+
3+
The JSON format is used extensively for communication in the Ethereum circles.
4+
Writing a JSON-ish parser in K takes 6 lines.
5+
6+
```k
7+
module JSON
8+
imports INT
9+
imports STRING
10+
imports BOOL
11+
12+
syntax JSONs ::= List{JSON,","} [klabel(JSONs) , symbol]
13+
syntax JSONKey ::= String
14+
syntax JSON ::= "null" [klabel(JSONnull) , symbol]
15+
| String | Int | Bool
16+
| JSONKey ":" JSON [klabel(JSONEntry) , symbol]
17+
| "{" JSONs "}" [klabel(JSONObject) , symbol]
18+
| "[" JSONs "]" [klabel(JSONList) , symbol]
19+
// --------------------------------------------------------------------
20+
```
21+
22+
**TODO**: Adding `Int` to `JSONKey` is a hack to make certain parts of semantics easier.
23+
24+
```k
25+
syntax JSONKey ::= Int
26+
// ----------------------
27+
28+
endmodule
29+
```
30+
31+
JSON-RPC
32+
--------
33+
34+
```k
35+
module JSON-RPC
36+
imports K-IO
37+
imports LIST
38+
imports JSON
39+
40+
configuration
41+
<json-rpc>
42+
<web3socket> $SOCK:Int </web3socket>
43+
<web3clientsocket> 0:IOInt </web3clientsocket>
44+
<web3request>
45+
<jsonrpc> "":JSON </jsonrpc>
46+
<callid> 0:JSON </callid>
47+
<method> "":JSON </method>
48+
<params> [ .JSONs ] </params>
49+
<batch> undef </batch>
50+
</web3request>
51+
<web3response> .List </web3response>
52+
</json-rpc>
53+
54+
syntax JSON ::= "undef" [klabel(JSON-RPCundef), symbol]
55+
// -------------------------------------------------------
56+
57+
syntax Bool ::= isProperJson ( JSON ) [function]
58+
| isProperJsonList ( JSONs ) [function]
59+
// -----------------------------------------------------
60+
rule isProperJson(_) => false [owise]
61+
62+
rule isProperJson(null) => true
63+
64+
rule isProperJson(_:Int) => true
65+
rule isProperJson(_:Bool) => true
66+
rule isProperJson(_:String) => true
67+
68+
rule isProperJson(_:JSONKey : J) => isProperJson(J)
69+
70+
rule isProperJson([ JS ]) => isProperJsonList(JS)
71+
rule isProperJson({ JS }) => isProperJsonList(JS)
72+
73+
rule isProperJsonList(.JSONs) => true
74+
rule isProperJsonList(J, JS) => isProperJson(J) andBool isProperJsonList(JS)
75+
endmodule
76+
```

0 commit comments

Comments
 (0)