Skip to content

Commit da1ffae

Browse files
dwightguthehildenb
authored andcommitted
web3 Event loop (#436)
* Makefile: build system for web3 client * deps/plugin: update submodule * web3: event loop in evm-semantics * Dockerfile: tests depend on netcat * Makefile, tests/web3: add tests for web3 client * Jenkinsfile: run tests for web3 * Makefile: update to latest option parsing * web3: fix for json getters * web3: refactor #sendResponse * web3: add strategies * web3: checkRPCCall * web3: method not found * deps/k: update submodule * deps/plugin: update submodule * web3: better fix for invalid request with invalid id * Makefile: fix bug * tests/web3: update output files * web3: workaround bug in k * tests/web3: add more tests * deps/plugin: update submodule * fix kevm web3 * don't use strategy * Jenkinsfile: fix make target name * Dockerfile: missing dependency * deps/k: update submodule * web3: formatting * Makefile: formatting * tests/web3/get_port: use python3 * kevm: formatting, documentation
1 parent ea55a76 commit da1ffae

26 files changed

+226
-75
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ RUN apt-get update \
88
libprotobuf-dev libsecp256k1-dev libssl-dev libtool libyaml-dev lld-8 \
99
llvm-8-tools make maven opam openjdk-11-jdk pandoc pkg-config \
1010
protobuf-compiler python3 python-pygments python-recommonmark \
11-
python-sphinx time zlib1g-dev
11+
python-sphinx time zlib1g-dev netcat-openbsd rapidjson-dev
1212

1313
ADD deps/k/haskell-backend/src/main/native/haskell-backend/scripts/install-stack.sh /.install-stack/
1414
RUN /.install-stack/install-stack.sh

Jenkinsfile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pipeline {
3838
stage('Build') {
3939
steps {
4040
sh '''
41-
make build build-llvm build-haskell build-node -j4
41+
make build build-llvm build-haskell build-node build-web3 -j4
4242
'''
4343
}
4444
}
@@ -63,6 +63,15 @@ pipeline {
6363
'''
6464
}
6565
}
66+
stage('Conformance (Web3)') {
67+
steps {
68+
sh '''
69+
nprocs=$(nproc)
70+
[ "$nprocs" -gt '16' ] && nprocs='16'
71+
make test-web3 -j"$nprocs"
72+
'''
73+
}
74+
}
6675
}
6776
}
6877
stage('Test Proofs (Java)') {

Makefile

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ export LUA_PATH
3737
.PHONY: all clean clean-submodules distclean install uninstall \
3838
deps all-deps llvm-deps haskell-deps repo-deps system-deps k-deps ocaml-deps plugin-deps libsecp256k1 libff \
3939
build build-ocaml build-java build-node build-llvm build-web3 split-tests \
40-
defn java-defn ocaml-defn node-defn haskell-defn llvm-defn \
40+
defn java-defn ocaml-defn node-defn web3-defn haskell-defn llvm-defn \
4141
test test-all test-conformance test-slow-conformance test-all-conformance \
4242
test-vm test-slow-vm test-all-vm test-bchain test-slow-bchain test-all-bchain \
43+
test-web3 \
4344
test-prove test-klab-prove test-parse test-failure \
4445
test-interactive test-interactive-help test-interactive-run test-interactive-prove test-interactive-search test-interactive-firefly \
4546
media media-pdf sphinx metropolis-theme
@@ -156,20 +157,18 @@ LLVM_KOMPILE_OPTS:=
156157
ocaml_kompiled:=$(DEFN_DIR)/ocaml/$(MAIN_DEFN_FILE)-kompiled/interpreter
157158
java_kompiled:=$(DEFN_DIR)/java/$(MAIN_DEFN_FILE)-kompiled/timestamp
158159
node_kompiled:=$(DEFN_DIR)/vm/kevm-vm
160+
web3_kompiled:=$(DEFN_DIR)/web3/kevm-client
159161
haskell_kompiled:=$(DEFN_DIR)/haskell/$(MAIN_DEFN_FILE)-kompiled/definition.kore
160162
llvm_kompiled:=$(DEFN_DIR)/llvm/$(MAIN_DEFN_FILE)-kompiled/interpreter
161163

162164
build: build-ocaml build-java
163165
build-ocaml: $(ocaml_kompiled)
164166
build-java: $(java_kompiled)
165167
build-node: $(node_kompiled)
168+
build-web3: $(web3_kompiled)
166169
build-haskell: $(haskell_kompiled)
167170
build-llvm: $(llvm_kompiled)
168171

169-
build-web3: MAIN_MODULE=WEB3
170-
build-web3: MAIN_DEFN_FILE=web3
171-
build-web3: $(llvm_kompiled)
172-
173172
# Tangle definition from *.md files
174173

175174
concrete_tangle:=.k:not(.node):not(.symbolic),.standalone,.concrete
@@ -185,14 +184,16 @@ llvm_files=$(patsubst %, $(DEFN_DIR)/llvm/%, $(ALL_K_FILES))
185184
java_files=$(patsubst %, $(DEFN_DIR)/java/%, $(ALL_K_FILES))
186185
haskell_files=$(patsubst %, $(DEFN_DIR)/haskell/%, $(ALL_K_FILES))
187186
node_files=$(patsubst %, $(DEFN_DIR)/node/%, $(ALL_K_FILES))
188-
defn_files=$(ocaml_files) $(llvm_file) $(java_files) $(haskell_files) $(node_files)
187+
web3_files=$(patsubst %, $(DEFN_DIR)/web3/%, $(ALL_K_FILES))
188+
defn_files=$(ocaml_files) $(llvm_file) $(java_files) $(haskell_files) $(node_files) $(web3_files)
189189

190190
defn: $(defn_files)
191191
ocaml-defn: $(ocaml_files)
192192
llvm-defn: $(llvm_files)
193193
java-defn: $(java_files)
194194
haskell-defn: $(haskell_files)
195195
node-defn: $(node_files)
196+
web3-defn: $(web3_files)
196197

197198
$(DEFN_DIR)/ocaml/%.k: %.md $(TANGLER)
198199
@echo "== tangle: $@"
@@ -219,6 +220,11 @@ $(DEFN_DIR)/node/%.k: %.md $(TANGLER)
219220
mkdir -p $(dir $@)
220221
pandoc --from markdown --to "$(TANGLER)" --metadata=code:"$(node_tangle)" $< > $@
221222

223+
$(DEFN_DIR)/web3/%.k: %.md $(TANGLER)
224+
@echo "== tangle: $@"
225+
mkdir -p $(dir $@)
226+
pandoc --from markdown --to "$(TANGLER)" --metadata=code:"$(node_tangle)" $< > $@
227+
222228
# Java Backend
223229

224230
$(java_kompiled): $(java_files)
@@ -309,13 +315,36 @@ $(DEFN_DIR)/node/$(MAIN_DEFN_FILE)-kompiled/plugin/proto/msg.pb.cc: $(PLUGIN_SUB
309315

310316
$(node_kompiled): $(DEFN_DIR)/node/$(MAIN_DEFN_FILE)-kompiled/interpreter $(libff_out)
311317
mkdir -p $(DEFN_DIR)/vm
312-
$(K_BIN)/llvm-kompile $(DEFN_DIR)/node/$(MAIN_DEFN_FILE)-kompiled/definition.kore $(DEFN_DIR)/node/$(MAIN_DEFN_FILE)-kompiled/dt library $(PLUGIN_SUBMODULE)/vm-c/main.cpp $(PLUGIN_SUBMODULE)/vm-c/vm.cpp \
318+
$(K_BIN)/llvm-kompile $(DEFN_DIR)/node/$(MAIN_DEFN_FILE)-kompiled/definition.kore $(DEFN_DIR)/node/$(MAIN_DEFN_FILE)-kompiled/dt library $(PLUGIN_SUBMODULE)/vm-c/init.cpp $(PLUGIN_SUBMODULE)/vm-c/main.cpp $(PLUGIN_SUBMODULE)/vm-c/vm.cpp \
313319
$(PLUGIN_SUBMODULE)/plugin-c/*.cpp $(DEFN_DIR)/node/$(MAIN_DEFN_FILE)-kompiled/plugin/proto/msg.pb.cc $(PLUGIN_SUBMODULE)/vm-c/kevm/semantics.cpp -o $@ -g -O2 \
314320
-I $(PLUGIN_SUBMODULE)/plugin-c/ -I $(DEFN_DIR)/node/$(MAIN_DEFN_FILE)-kompiled/plugin -I $(PLUGIN_SUBMODULE)/vm-c/ -I $(PLUGIN_SUBMODULE)/vm-c/kevm/ -I node/ \
315321
$(LLVM_KOMPILE_OPTS) \
316322
-L$(LIBRARY_PATH) \
317323
-lff -lprotobuf -lgmp $(LINK_PROCPS) -lcryptopp -lsecp256k1
318324

325+
# Web3 Backend
326+
327+
$(DEFN_DIR)/web3/web3-kompiled/interpreter: $(web3_files) $(libff_out)
328+
@echo "== kompile: $@"
329+
$(K_BIN)/kompile --debug --main-module WEB3 --backend llvm \
330+
--syntax-module WEB3 $(DEFN_DIR)/web3/web3.k \
331+
--directory $(DEFN_DIR)/web3 -I $(DEFN_DIR)/web3 \
332+
--hook-namespaces "KRYPTO BLOCKCHAIN JSON" \
333+
--iterated \
334+
$(KOMPILE_OPTS) \
335+
-ccopt $(PLUGIN_SUBMODULE)/plugin-c/crypto.cpp -ccopt $(PLUGIN_SUBMODULE)/client-c/json.cpp \
336+
-ccopt -L$(LIBRARY_PATH) -ccopt -I -ccopt $(PLUGIN_SUBMODULE)/vm-c \
337+
-ccopt -lff -ccopt -lcryptopp -ccopt -lsecp256k1 $(addprefix -ccopt ,$(LINK_PROCPS)) -ccopt -g -ccopt -std=c++11 -ccopt -O2
338+
339+
$(web3_kompiled): $(DEFN_DIR)/web3/web3-kompiled/interpreter $(libff_out)
340+
mkdir -p $(DEFN_DIR)/web3
341+
$(K_BIN)/llvm-kompile $(DEFN_DIR)/web3/web3-kompiled/definition.kore $(DEFN_DIR)/web3/web3-kompiled/dt library $(PLUGIN_SUBMODULE)/vm-c/init.cpp $(PLUGIN_SUBMODULE)/client-c/main.cpp $(PLUGIN_SUBMODULE)/client-c/json.cpp \
342+
$(PLUGIN_SUBMODULE)/plugin-c/crypto.cpp -o $@ -g -O2 \
343+
-I $(PLUGIN_SUBMODULE)/vm-c/ -I $(PLUGIN_SUBMODULE)/plugin-c/ -I node/ \
344+
$(LLVM_KOMPILE_OPTS) \
345+
-L$(LIBRARY_PATH) \
346+
-lff -lgmp $(LINK_PROCPS) -lcryptopp -lsecp256k1
347+
319348
# LLVM Backend
320349

321350
$(llvm_kompiled): $(llvm_files) $(libff_out)
@@ -390,6 +419,16 @@ tests/%.run-expected: tests/% tests/%.expected
390419
|| $(CHECK) tests/$*.expected tests/$*.$(TEST_CONCRETE_BACKEND)-out
391420
rm -rf tests/$*.$(TEST_CONCRETE_BACKEND)-out
392421

422+
tests/%.run-web3: tests/%.in.json
423+
PORT=`tests/web3/get_port.py`; \
424+
./kevm web3 -p $$PORT & \
425+
while ! netcat -z 127.0.0.1 $$PORT; do \
426+
sleep 0.1; \
427+
done; \
428+
cat $^ | netcat 127.0.0.1 $$PORT -q 0 | diff - tests/$*.out.json; RESULT=$$? ; \
429+
pkill kevm-client -P $$$$ ; \
430+
[ $$? -eq 0 ]
431+
393432
tests/%.parse: tests/%
394433
$(TEST) kast --backend $(TEST_CONCRETE_BACKEND) $< kast > $@-out
395434
$(CHECK) $@-expected $@-out
@@ -443,10 +482,14 @@ failing_bchain_tests=$(shell cat tests/failing.$(TEST_CONCRETE_BACKEND))
443482
all_bchain_tests=$(filter-out $(bad_bchain_tests), $(filter-out $(failing_bchain_tests), $(bchain_tests)))
444483
quick_bchain_tests=$(filter-out $(slow_bchain_tests), $(all_bchain_tests))
445484

485+
web3_tests=$(wildcard tests/web3/*.in.json)
486+
446487
test-all-bchain: $(all_bchain_tests:=.run)
447488
test-slow-bchain: $(slow_bchain_tests:=.run)
448489
test-bchain: $(quick_bchain_tests:=.run)
449490

491+
test-web3: $(web3_tests:.in.json=.run-web3)
492+
450493
# Proof Tests
451494

452495
prove_specs_dir:=tests/specs

deps/k

Submodule k updated from ab08f9a to c7006f2

kevm

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ run_search() {
6868
}
6969

7070
run_web3() {
71-
run_krun -cCHAINID="$cCHAINID" -pCHAINID='printf %s' "$@"
71+
exec -a "$0 web3" ${defn_dir}/web3/kevm-client "$@"
7272
}
7373

7474
run_klab() {
@@ -144,7 +144,7 @@ if [[ "$run_command" == 'help' ]]; then
144144
$0 kast [--backend (ocaml|java)] <pgm> <output format> <K arg>*
145145
$0 prove [--backend (java|haskell)] <spec> <K arg>* -m <def_module>
146146
$0 search [--backend (java|haskell)] <pgm> <pattern> <K arg>*
147-
$0 web3 [--backend (llvm)] <pgm> <K arg>*
147+
$0 web3 [--backend (llvm)] -p <port>
148148
$0 klab-run <pgm> <K arg>*
149149
$0 klab-prove <spec> <K arg>* -m <def_module>
150150
$0 klab-view <spec>
@@ -164,6 +164,7 @@ if [[ "$run_command" == 'help' ]]; then
164164
<interpreter arg> is an argument you want to pass to the derived interpreter.
165165
<output format> is the format for Kast to output the term in.
166166
<pattern> is the configuration pattern to search for.
167+
<port> is the port to listen on for Web3 JSON RPC requests.
167168
<def_module> is the module to take as axioms when doing verification.
168169
169170
klab-view: Make sure that the 'klab/bin' directory is on your PATH to use this option.
@@ -174,6 +175,7 @@ fi
174175
backend="ocaml"
175176
[[ ! "$run_command" == 'prove' ]] || backend='java'
176177
[[ ! "$run_command" =~ klab* ]] || backend='java'
178+
[[ ! "$run_command" =~ web3* ]] || backend='llvm'
177179
if [[ $# -gt 0 ]] && [[ $1 == '--backend' ]]; then
178180
backend="$2"
179181
shift 2
@@ -182,8 +184,10 @@ backend_dir="$defn_dir/$backend"
182184
[[ ! "$backend" == "ocaml" ]] || eval $(opam config env)
183185

184186
# get the run file
185-
run_file="$1" ; shift
186-
[[ -f "$run_file" ]] || fatal "File does not exist: $run_file"
187+
if [[ ! "$run_command" == 'web3' ]]; then
188+
run_file="$1" ; shift
189+
[[ -f "$run_file" ]] || fatal "File does not exist: $run_file"
190+
fi
187191

188192
cMODE="\`${MODE:-NORMAL}\`(.KList)"
189193
cSCHEDULE="\`${SCHEDULE:-BYZANTIUM}_EVM\`(.KList)"

tests/web3/get_port.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env python3
2+
import socket
3+
4+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
5+
s.bind(('', 0))
6+
addr = s.getsockname()
7+
print(str(addr[1]))
8+
s.close()

tests/web3/invalid_id.in.json

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

tests/web3/invalid_id.out.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid Request"}}

tests/web3/invalid_method.in.json

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

0 commit comments

Comments
 (0)