Skip to content

Commit

Permalink
Initial implementation of StateDB mock (0xPolygonHermez#970)
Browse files Browse the repository at this point in the history
* Initial implementation of StateDB mock

* Fix service ports

* fix set method

* implement SetProgram and GetProgram

* new test vector format

* adapt to new genesis format

* add testvector.FindBytecode method

* FindBytecode should check Value instead of key and fixed oldRoot hash on initial genesis action

* make stateDB mock respond to queries about genesis items

* fixed balance checks

* fixed code and storage checks

* removed logs
  • Loading branch information
fgimenez authored Aug 8, 2022
1 parent 2364179 commit f4af863
Show file tree
Hide file tree
Showing 23 changed files with 1,105 additions and 228 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/push-docker-develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ jobs:
with:
filters: |
zkevmprovermock:
- 'zkevmprovermock/**'
- 'tools/zkevmprovermock/**'
- name: Build and push zkevmprovermock
if: steps.zkevmprovermock_changes.outputs.zkevmprovermock == 'true'
id: docker_build_zkevmprovermock
uses: docker/build-push-action@v2
with:
context: ./zkevmprovermock
context: .
file: tools/zkevmprovermock/Dockerfile
platforms: linux/amd64,linux/arm64
push: true
tags: |
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/push-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ jobs:
with:
filters: |
zkevmprovermock:
- 'zkevmprovermock/**'
- 'tools/zkevmprovermock/**'
- name: Build and push zkevmprovermock
if: steps.zkevmprovermock_changes.outputs.zkevmprovermock == 'true'
id: docker_build_zkevmprovermock
uses: docker/build-push-action@v2
with:
context: ./zkevmprovermock
context: .
file: tools/zkevmprovermock/Dockerfile
platforms: linux/amd64,linux/arm64
push: true
tags: |
Expand Down
17 changes: 15 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ DOCKERCOMPOSEEXPLORER := zkevm-explorer
DOCKERCOMPOSEEXPLORERDB := zkevm-explorer-db
DOCKERCOMPOSEEXPLORERRPC := zkevm-explorer-json-rpc
DOCKERCOMPOSEZKPROVER := zkevm-prover
DOCKERCOMPOSEZKPROVERMOCK := zkprover-mock
DOCKERCOMPOSEPERMISSIONLESSDB := zkevm-permissionless-db
DOCKERCOMPOSEPERMISSIONLESSNODE := zkevm-permissionless-node
DOCKERCOMPOSENODEAPPROVE := zkevm-approve
Expand All @@ -26,6 +27,7 @@ RUNEXPLORER := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEEXPLORER)
RUNEXPLORERDB := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEEXPLORERDB)
RUNEXPLORERJSONRPC := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEEXPLORERRPC)
RUNZKPROVER := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEZKPROVER)
RUNZKPROVERMOCK := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEZKPROVERMOCK)

RUNPERMISSIONLESSDB := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEPERMISSIONLESSDB)
RUNPERMISSIONLESSNODE := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEPERMISSIONLESSNODE)
Expand All @@ -46,6 +48,7 @@ STOPEXPLORER := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEEXPLORER) && $(DOCKERCOMPOS
STOPEXPLORERDB := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEEXPLORERDB) && $(DOCKERCOMPOSE) rm -f $(DOCKERCOMPOSEEXPLORERDB)
STOPEXPLORERRPC := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEEXPLORERRPC) && $(DOCKERCOMPOSE) rm -f $(DOCKERCOMPOSEEXPLORERRPC)
STOPZKPROVER := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEZKPROVER) && $(DOCKERCOMPOSE) rm -f $(DOCKERCOMPOSEZKPROVER)
STOPZKPROVERMOCK := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEZKPROVERMOCK) && $(DOCKERCOMPOSE) rm -f $(DOCKERCOMPOSEZKPROVERMOCK)

STOPPERMISSIONLESSDB := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEPERMISSIONLESSDB) && $(DOCKERCOMPOSE) rm -f $(DOCKERCOMPOSEPERMISSIONLESSDB)
STOPPERMISSIONLESSNODE := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEPERMISSIONLESSNODE) && $(DOCKERCOMPOSE) rm -f $(DOCKERCOMPOSEPERMISSIONLESSNODE)
Expand Down Expand Up @@ -95,17 +98,19 @@ test-full: build-docker compile-scs ## Runs all tests checking race conditions
$(STOPZKPROVER)
$(RUNDB); sleep 7
$(RUNZKPROVER); sleep 5
trap '$(STOPDB) && $(STOPZKPROVER)' EXIT; MallocNanoZone=0 go test -race -p 1 -timeout 1200s `go list ./... | grep -v \/ci\/e2e-group`
$(RUNZKPROVERMOCK)
trap '$(STOPDB) && $(STOPZKPROVER) && $(STOPZKPROVERMOCK)' EXIT; MallocNanoZone=0 go test -race -p 1 -timeout 1200s `go list ./... | grep -v \/ci\/e2e-group`

.PHONY: test-full-non-e2e
test-full-non-e2e: build-docker compile-scs ## Runs non-e2e tests checking race conditions
$(STOPDB)
$(STOPZKPROVER)
$(RUNDB); sleep 7
$(RUNZKPROVER)
$(RUNZKPROVERMOCK)
sleep 5
docker logs $(DOCKERCOMPOSEZKPROVER)
trap '$(STOPDB) && $(STOPZKPROVER)' EXIT; MallocNanoZone=0 go test -short -race -p 1 -timeout 600s ./...
trap '$(STOPDB) && $(STOPZKPROVER) && $(STOPZKPROVERMOCK)' EXIT; MallocNanoZone=0 go test -short -race -p 1 -timeout 600s ./...

.PHONY: test-e2e-group-1
test-e2e-group-1: build-docker compile-scs ## Runs group 1 e2e tests checking race conditions
Expand Down Expand Up @@ -181,6 +186,14 @@ run-zkprover: ## Runs zkprover
stop-zkprover: ## Stops zkprover
$(STOPZKPROVER)

.PHONY: run-zkprover-mock
run-zkprover-mock: ## Runs zkprover-mock
$(RUNZKPROVERMOCK)

.PHONY: stop-zkprover-mock
stop-zkprover-mock: ## Stops zkprover-mock
$(STOPZKPROVERMOCK)

.PHONY: run-explorer
run-explorer: ## Runs the explorer
$(RUNEXPLORERJSONRPC)
Expand Down
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@ services:
command: >
zkprover input_executor.json
zkprover-mock:
container_name: zkprover-mock
image: hermeznetwork/zkprover-mock:latest
ports:
- 43061:43061 # MT
volumes:
- ./test/vectors/src:/app/testvectors
command: >
/app/zkprover-mock server --statedb-port 43061 --test-vector-path /app/testvectors
zkevm-approve:
container_name: zkevm-approve
image: zkevm-node
Expand Down
2 changes: 1 addition & 1 deletion merkletree/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func keyEthAddr(ethAddr common.Address, leafType leafType, key1Capacity [4]uint6
}

func defaultCapIn() ([4]uint64, error) {
capIn, err := stringToh4(HashPoseidonAllZeroes)
capIn, err := StringToh4(HashPoseidonAllZeroes)
if err != nil {
return [4]uint64{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion merkletree/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func Test_byteCodeHash(t *testing.T) {
t.Run(fmt.Sprintf("Test vector %d", ti), func(t *testing.T) {
hash, err := hashContractBytecode(common.Hex2Bytes(testVector.Bytecode))
require.NoError(t, err)
assert.Equal(t, common.HexToHash(testVector.ExpectedHash), common.HexToHash(h4ToString(hash)))
assert.Equal(t, common.HexToHash(testVector.ExpectedHash), common.HexToHash(H4ToString(hash)))
})
}
}
8 changes: 4 additions & 4 deletions merkletree/split.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ func h4ToScalar(h4 []uint64) *big.Int {
return result
}

// h4ToString converts array of 4 Scalars of 64 bits into an hex string.
func h4ToString(h4 []uint64) string {
// H4ToString converts array of 4 Scalars of 64 bits into an hex string.
func H4ToString(h4 []uint64) string {
sc := h4ToScalar(h4)

return fmt.Sprintf("0x%064s", hex.EncodeToString(sc.Bytes()))
}

// stringToh4 converts an hex string into array of 4 Scalars of 64 bits.
func stringToh4(str string) ([]uint64, error) {
// StringToh4 converts an hex string into array of 4 Scalars of 64 bits.
func StringToh4(str string) ([]uint64, error) {
if strings.HasPrefix(str, "0x") { // nolint
str = str[2:]
}
Expand Down
4 changes: 2 additions & 2 deletions merkletree/split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func Test_h4ToString(t *testing.T) {
for i, tc := range tcs {
tc := tc
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
actual := h4ToString(tc.input)
actual := H4ToString(tc.input)
require.Equal(t, tc.expected, actual)
})
}
Expand Down Expand Up @@ -217,7 +217,7 @@ func Test_stringToh4(t *testing.T) {
for _, tc := range tcs {
tc := tc
t.Run(tc.description, func(t *testing.T) {
actual, err := stringToh4(tc.input)
actual, err := StringToh4(tc.input)
require.NoError(t, testutils.CheckError(err, tc.expectedErr, tc.expectedErrMsg))

require.Equal(t, tc.expected, actual)
Expand Down
2 changes: 1 addition & 1 deletion merkletree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (tree *StateTree) SetCode(ctx context.Context, address common.Address, code
}
k := new(big.Int).SetBytes(key[:])

scCodeHash, err := hex.DecodeHex(h4ToString(scCodeHash4))
scCodeHash, err := hex.DecodeHex(H4ToString(scCodeHash4))
if err != nil {
return nil, nil, err
}
Expand Down
7 changes: 6 additions & 1 deletion state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,6 @@ func (s *State) SetGenesis(ctx context.Context, block Block, genesis Genesis, db
newRoot []byte
err error
)

if dbTx == nil {
return newRoot, ErrDBTxNil
}
Expand Down Expand Up @@ -826,7 +825,13 @@ func (s *State) SetGenesis(ctx context.Context, block Block, genesis Genesis, db
return newRoot, err
}
case int(merkletree.LeafTypeStorage):
if strings.HasPrefix(action.StoragePosition, "0x") { // nolint
action.StoragePosition = action.StoragePosition[2:]
}
positionBI := new(big.Int).SetBytes(common.Hex2Bytes(action.StoragePosition))
if strings.HasPrefix(action.Value, "0x") { // nolint
action.StoragePosition = action.Value[2:]
}
valueBI := new(big.Int).SetBytes(common.Hex2Bytes(action.Value))

newRoot, _, err = s.tree.SetStorageAt(ctx, address, positionBI, valueBI, newRoot)
Expand Down
Loading

0 comments on commit f4af863

Please sign in to comment.