Skip to content

Commit

Permalink
Fix block information provided by RPC (0xPolygonHermez#1129)
Browse files Browse the repository at this point in the history
  • Loading branch information
tclemos authored Sep 9, 2022
1 parent dac8338 commit 3627783
Show file tree
Hide file tree
Showing 8 changed files with 488 additions and 285 deletions.
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ DOCKERCOMPOSEAPPSYNC := zkevm-sync
DOCKERCOMPOSEAPPBROADCAST := zkevm-broadcast
DOCKERCOMPOSEDB := zkevm-db
DOCKERCOMPOSENETWORK := zkevm-mock-l1-network
DOCKERCOMPOSEEXPLORERL1 := zkevm-explorer-l1
DOCKERCOMPOSEEXPLORERL1DB := zkevm-explorer-l1-db
DOCKERCOMPOSEEXPLORER := zkevm-explorer
DOCKERCOMPOSEEXPLORERDB := zkevm-explorer-db
DOCKERCOMPOSEEXPLORERRPC := zkevm-explorer-json-rpc
Expand All @@ -23,6 +25,8 @@ RUNSYNC := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEAPPSYNC)
RUNBROADCAST := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEAPPBROADCAST)

RUNL1NETWORK := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSENETWORK)
RUNEXPLORERL1 := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEEXPLORERL1)
RUNEXPLORERL1DB := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEEXPLORERL1DB)
RUNEXPLORER := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEEXPLORER)
RUNEXPLORERDB := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEEXPLORERDB)
RUNEXPLORERJSONRPC := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEEXPLORERRPC)
Expand All @@ -44,6 +48,8 @@ STOPSYNC := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEAPPSYNC) && $(DOCKERCOMPOSE) rm
STOPBROADCAST := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEAPPBROADCAST) && $(DOCKERCOMPOSE) rm -f $(DOCKERCOMPOSEAPPBROADCAST)

STOPNETWORK := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSENETWORK) && $(DOCKERCOMPOSE) rm -f $(DOCKERCOMPOSENETWORK)
STOPEXPLORERL1 := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEEXPLORERL1) && $(DOCKERCOMPOSE) rm -f $(DOCKERCOMPOSEEXPLORERL1)
STOPEXPLORERL1DB := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEEXPLORERL1DB) && $(DOCKERCOMPOSE) rm -f $(DOCKERCOMPOSEEXPLORERL1DB)
STOPEXPLORER := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEEXPLORER) && $(DOCKERCOMPOSE) rm -f $(DOCKERCOMPOSEEXPLORER)
STOPEXPLORERDB := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEEXPLORERDB) && $(DOCKERCOMPOSE) rm -f $(DOCKERCOMPOSEEXPLORERDB)
STOPEXPLORERRPC := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEEXPLORERRPC) && $(DOCKERCOMPOSE) rm -f $(DOCKERCOMPOSEEXPLORERRPC)
Expand Down Expand Up @@ -202,13 +208,19 @@ stop-zkprover-mock: ## Stops zkprover-mock

.PHONY: run-explorer
run-explorer: ## Runs the explorer
$(RUNEXPLORERL1DB)
$(RUNEXPLORERDB)
$(RUNEXPLORERJSONRPC)
$(RUNEXPLORERL1)
$(RUNEXPLORER)

.PHONY: stop-explorer
stop-explorer: ## Stops the explorer
$(STOPEXPLORER)
$(STOPEXPLORERL1)
$(STOPEXPLORERRPC)
$(STOPEXPLORERDB)
$(STOPEXPLORERL1DB)

.PHONY: run-explorer-db
run-explorer-db: ## Runs the explorer database
Expand Down
36 changes: 33 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,40 @@ services:
- POSTGRES_DB=test_db
command: ["postgres", "-N", "500"]

zkevm-explorer-l1:
container_name: zkevm-explorer-l1
image: hermeznetwork/hermez-node-blockscout:latest
ports:
- 4000:4000
environment:
- NETWORK=ETH
- SUBNETWORK=Local Ethereum
- COIN=ETH
- ETHEREUM_JSONRPC_VARIANT=geth
- ETHEREUM_JSONRPC_HTTP_URL=http://zkevm-mock-l1-network:8545
- DATABASE_URL=postgres://test_user:test_password@zkevm-explorer-l1-db:5432/explorer
- ECTO_USE_SSL=false
- MIX_ENV=prod
command:
["/bin/sh", "-c", "mix do ecto.create, ecto.migrate; mix phx.server"]

zkevm-explorer-l1-db:
container_name: zkevm-explorer-l1-db
image: postgres
ports:
- 5433:5432
environment:
# In order to update this values, you may need to run: docker rm -f -v postgres
- POSTGRES_USER=test_user
- POSTGRES_PASSWORD=test_password
- POSTGRES_DB=explorer
command: ["postgres", "-N", "500"]

zkevm-explorer:
container_name: zkevm-explorer
image: hermeznetwork/hermez-node-blockscout:latest
ports:
- 4000:4000
- 4001:4000
environment:
- NETWORK=POE
- SUBNETWORK=Polygon Hermez
Expand Down Expand Up @@ -162,13 +191,13 @@ services:
command:
- "/bin/sh"
- "-c"
- "/app/zkevm-node run --network local --cfg /app/config.toml --components rpc --http.api eth,net,debug,hez,txpool,web3"
- "/app/zkevm-node run --network local --cfg /app/config.toml --components rpc --http.api eth,net,debug,zkevm,txpool,web3"

zkevm-explorer-db:
container_name: zkevm-explorer-db
image: postgres
ports:
- 5433:5432
- 5434:5432
environment:
# In order to update this values, you may need to run: docker rm -f -v postgres
- POSTGRES_USER=test_user
Expand All @@ -181,6 +210,7 @@ services:
image: hermeznetwork/geth-zkevm-contracts@sha256:b88d03a42ef4975d0b92c0dd192e1bf4d673fc4b761aaf4bcfdd606989c1b006
ports:
- 8545:8545
command: ["--http", "--http.api", "admin,eth,debug,miner,net,txpool,personal,web3", "--http.addr", "0.0.0.0","--http.corsdomain", "*", "--http.vhosts" ,"*", "--ws", "--ws.origins", "*", "--ws.addr", "0.0.0.0", "--dev", "--datadir", "/geth_data"]

zkevm-prover:
container_name: zkevm-prover
Expand Down
225 changes: 144 additions & 81 deletions jsonrpc/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@ package jsonrpc
import (
"context"

"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/hex"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/jackc/pgx/v4"
)

// Debug is the debug jsonrpc endpoint
type Debug struct {
state stateInterface
txMan dbTxManager
}

type traceConfig struct {
Tracer *string `json:"tracer"`
}

type traceTransactionResponse struct {
Gas uint64 `json:"gas"`
Failed bool `json:"failed"`
ReturnValue argBytes `json:"returnValue"`
StructLogs []StructLogRes `json:"structLogs"`
}
// type traceTransactionResponse struct {
// Gas uint64 `json:"gas"`
// Failed bool `json:"failed"`
// ReturnValue interface{} `json:"returnValue"`
// StructLogs []StructLogRes `json:"structLogs"`
// }

// StructLogRes represents a log response.
type StructLogRes struct {
Expand All @@ -40,81 +44,140 @@ type StructLogRes struct {
// TraceTransaction creates a response for debug_traceTransaction request.
// See https://geth.ethereum.org/docs/rpc/ns-debug#debug_tracetransaction
func (d *Debug) TraceTransaction(hash common.Hash, cfg *traceConfig) (interface{}, rpcError) {
ctx := context.Background()

tracer := ""
if cfg != nil && cfg.Tracer != nil {
tracer = *cfg.Tracer
}

result, err := d.state.DebugTransaction(ctx, hash, tracer)
if err != nil {
const errorMessage = "failed to debug trace the transaction"
log.Debugf("%v: %v", errorMessage, err)
return nil, newRPCError(defaultErrorCode, errorMessage)
}

if tracer != "" && len(result.ExecutorTraceResult) > 0 {
return result.ExecutorTraceResult, nil
}

failed := result.Failed()
// structLogs := make([]StructLogRes, 0, len(result.StructLogs))
// for _, structLog := range result.StructLogs {
// var stackRes *[]argBig
// if len(structLog.Stack) > 0 {
// stack := make([]argBig, 0, len(structLog.Stack))
// for _, stackItem := range structLog.Stack {
// if stackItem != nil {
// stack = append(stack, argBig(*stackItem))
// }
// }
// stackRes = &stack
// }

// var memoryRes *argBytes
// if len(structLog.Memory) > 0 {
// memory := make(argBytes, 0, len(structLog.Memory))
// for _, memoryItem := range structLog.Memory {
// memory = append(memory, memoryItem)
// }
// memoryRes = &memory
// }

// var storageRes *map[string]string
// if len(structLog.Storage) > 0 {
// storage := make(map[string]string, len(structLog.Storage))
// for storageKey, storageValue := range structLog.Storage {
// storage[storageKey.Hex()] = storageValue.Hex()
// }
// storageRes = &storage
// }

// errRes := ""
// if structLog.Err != nil {
// errRes = structLog.Err.Error()
// }

// structLogs = append(structLogs, StructLogRes{
// Pc: structLog.Pc,
// Op: structLog.Op,
// Gas: structLog.Gas,
// GasCost: structLog.GasCost,
// Depth: structLog.Depth,
// Error: errRes,
// Stack: stackRes,
// Memory: memoryRes,
// Storage: storageRes,
// RefundCounter: structLog.RefundCounter,
// })
return d.txMan.NewDbTxScope(d.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
tx, err := d.state.GetTransactionByHash(ctx, hash, dbTx)
if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to get tx", err)
}

receipt, err := d.state.GetTransactionReceipt(ctx, hash, dbTx)
if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to get tx receipt", err)
}

if receipt.Status == types.ReceiptStatusSuccessful {
return []interface{}{}, nil
} else {
from, err := state.GetSender(*tx)
if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to get tx sender", err)
}

var to string
if tx.To() != nil {
to = tx.To().String()
}

return []interface{}{
map[string]interface{}{
"type": "call",
"callType": "call",
"from": from.String(),
"to": to,
"input": "0xa9059cbb000000000000000000000000617b3a3528f9cdd6630fd3301b9c8911f7bf063d000000000000000000000000000000000000000000000030ca024f987b900000",
"error": "generic error message",
"traceAddress": []string{},
"value": tx.Value(),
"gas": hex.EncodeUint64(tx.Gas()),
"gasUsed": hex.EncodeUint64(receipt.GasUsed),
},
}, nil
}
})

/*
[
{
"type": "call",
"callType": "call",
"from": "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266",
"to": "0x9a676e781a523b5d0c0e43731313a708cb607508",
"input": "0xa9059cbb000000000000000000000000617b3a3528f9cdd6630fd3301b9c8911f7bf063d000000000000000000000000000000000000000000000030ca024f987b900000",
"error": "execution reverted",
"traceAddress": [],
"value": "0x0",
"gas": "0x747a",
"gasUsed": "0xa80"
}
]
*/

// ctx := context.Background()

// tracer := ""
// if cfg != nil && cfg.Tracer != nil {
// tracer = *cfg.Tracer
// }

// result, err := d.state.DebugTransaction(ctx, hash, tracer)
// if err != nil {
// const errorMessage = "failed to debug trace the transaction"
// log.Debugf("%v: %v", errorMessage, err)
// return nil, newRPCError(defaultErrorCode, errorMessage)
// }

resp := traceTransactionResponse{
Gas: result.GasUsed,
Failed: failed,
ReturnValue: result.ReturnValue,
// StructLogs: structLogs,
}
// if tracer != "" && len(result.ExecutorTraceResult) > 0 {
// return result.ExecutorTraceResult, nil
// }

// failed := result.Failed()
// // structLogs := make([]StructLogRes, 0, len(result.StructLogs))
// // for _, structLog := range result.StructLogs {
// // var stackRes *[]argBig
// // if len(structLog.Stack) > 0 {
// // stack := make([]argBig, 0, len(structLog.Stack))
// // for _, stackItem := range structLog.Stack {
// // if stackItem != nil {
// // stack = append(stack, argBig(*stackItem))
// // }
// // }
// // stackRes = &stack
// // }

// // var memoryRes *argBytes
// // if len(structLog.Memory) > 0 {
// // memory := make(argBytes, 0, len(structLog.Memory))
// // for _, memoryItem := range structLog.Memory {
// // memory = append(memory, memoryItem)
// // }
// // memoryRes = &memory
// // }

// // var storageRes *map[string]string
// // if len(structLog.Storage) > 0 {
// // storage := make(map[string]string, len(structLog.Storage))
// // for storageKey, storageValue := range structLog.Storage {
// // storage[storageKey.Hex()] = storageValue.Hex()
// // }
// // storageRes = &storage
// // }

// // errRes := ""
// // if structLog.Err != nil {
// // errRes = structLog.Err.Error()
// // }

// // structLogs = append(structLogs, StructLogRes{
// // Pc: structLog.Pc,
// // Op: structLog.Op,
// // Gas: structLog.Gas,
// // GasCost: structLog.GasCost,
// // Depth: structLog.Depth,
// // Error: errRes,
// // Stack: stackRes,
// // Memory: memoryRes,
// // Storage: storageRes,
// // RefundCounter: structLog.RefundCounter,
// // })
// // }

// resp := traceTransactionResponse{
// Gas: result.GasUsed,
// Failed: failed,
// ReturnValue: result.ReturnValue,
// // StructLogs: structLogs,
// }

return resp, nil
// return resp, nil
}
Loading

0 comments on commit 3627783

Please sign in to comment.