Skip to content

Commit

Permalink
StateV2 baseline (0xPolygonHermez#823)
Browse files Browse the repository at this point in the history
* WIP

* WIP

* add json rpc server v2 to integrate with state v2

* WIP

* organize the integration between state and json rpc server

* WIP

* fix TestBlockNumber unit test

* fix eth_Call unit tests

* fix eth_GetBalance unit test

* fix eth_GetBlockByHash unit test

* fix eth_GetBlockByNumber unit test

* fix eth_GetCode unit test

* fix eth_GetStorageAt unit test

* fix eth_Syncing unit test

* fix eth_GetTransactionByBlockHashAndIndex unit test

* fix eth_GetTransactionByBlockNumberAndIndex unit test

* fix eth_GetTransactionByHash unit test

* fix eth_GetBlockTransactionCountByHash unit test

* fix eth_GetBlockTransactionCountByNumber unit test

* fix eth_GetTransactionCount unit test

* fix eth_GetTransactionReceipt unit test

* fix TestGetNumericBlockNumber unit test

* update jsonrpcv2 with jsonrpcv1 latest filter methods and tests

* refactoring related to errors returned by JSON RPC endpoints to always be a rpcError

* refactoring

* Remove old mt proto, comment executor test

* Fix comments

* merge exercer and querier interfaces into execQuerier

* add dbTx to all methods that access db in state

Co-authored-by: tclemos <[email protected]>
  • Loading branch information
ToniRamirezM and tclemos authored Jun 29, 2022
1 parent eb960dd commit 7511164
Show file tree
Hide file tree
Showing 49 changed files with 3,875 additions and 3,675 deletions.
23 changes: 5 additions & 18 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/hermeznetwork/hermez-core/gasprice"
"github.com/hermeznetwork/hermez-core/jsonrpc"
"github.com/hermeznetwork/hermez-core/log"
"github.com/hermeznetwork/hermez-core/merkletree"
"github.com/hermeznetwork/hermez-core/pool"
"github.com/hermeznetwork/hermez-core/pool/pgpoolstorage"
"github.com/hermeznetwork/hermez-core/proverclient"
Expand All @@ -31,7 +32,6 @@ import (
"github.com/hermeznetwork/hermez-core/state/tree"
"github.com/hermeznetwork/hermez-core/statev2"
"github.com/hermeznetwork/hermez-core/statev2/runtime/executor"
executorclientpb "github.com/hermeznetwork/hermez-core/statev2/runtime/executor/pb"
"github.com/hermeznetwork/hermez-core/synchronizer"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -142,8 +142,10 @@ func start(ctx *cli.Context) error {
case BROADCAST:
log.Info("Running broadcast service")
stateDb := statev2.NewPostgresStorage(sqlDB)
executorClient, _ := newExecutorClient(c.Executor)
st := statev2.NewState(statev2.Config{}, stateDb, &executorClient)
executorClient, _ := executor.NewExecutorClient(c.Executor)
stateDBClient, _ := merkletree.NewStateDBServiceClient(merkletree.Config(c.MTClient))
stateTree := merkletree.NewStateTree(stateDBClient)
st := statev2.NewState(statev2.Config{}, stateDb, executorClient, stateTree)
go runBroadcastServer(c.BroadcastServer, st)
}
}
Expand Down Expand Up @@ -192,21 +194,6 @@ func newProverClient(c proverclient.Config) (proverclientpb.ZKProverServiceClien
return proverClient, proverConn
}

func newExecutorClient(c executor.Config) (executorclientpb.ExecutorServiceClient, *grpc.ClientConn) {
const maxMsgSize = 100000000
opts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMsgSize)),
}
executorConn, err := grpc.Dial(c.URI, opts...)
if err != nil {
log.Fatalf("fail to dial: %v", err)
}

executorClient := executorclientpb.NewExecutorServiceClient(executorConn)
return executorClient, executorConn
}

func runSynchronizer(networkConfig config.NetworkConfig, etherman *etherman.Client, st *state.State, cfg synchronizer.Config, gpe gasPriceEstimator) {
genesisBlock, err := etherman.EtherClient.BlockByNumber(context.Background(), big.NewInt(0).SetUint64(networkConfig.GenBlockNumber))
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion db/migrations/0002.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ CREATE TABLE statev2.transaction ( --transaction abstraction. transaction == L2
header jsonb,
uncles jsonb,
received_at TIMESTAMP WITH TIME ZONE NOT NULL,
batch_num BIGINT NOT NULL REFERENCES statev2.batch (batch_num) ON DELETE CASCADE
batch_num BIGINT NOT NULL REFERENCES statev2.batch (batch_num) ON DELETE CASCADE,
l2_block_num BIGINT
);

CREATE TABLE statev2.exit_root
Expand Down
21 changes: 21 additions & 0 deletions merkletree/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package merkletree

import (
"github.com/hermeznetwork/hermez-core/log"
"github.com/hermeznetwork/hermez-core/merkletree/pb"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

func NewStateDBServiceClient(c Config) (pb.StateDBServiceClient, *grpc.ClientConn) {
opts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
}
executorConn, err := grpc.Dial(c.URI, opts...)
if err != nil {
log.Fatalf("fail to dial: %v", err)
}

executorClient := pb.NewStateDBServiceClient(executorConn)
return executorClient, executorConn
}
6 changes: 6 additions & 0 deletions merkletree/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package merkletree

// ServerConfig represents the configuration of the merkletree server
type Config struct {
URI string `mapstructure:"URI"`
}
9 changes: 6 additions & 3 deletions proto/src/proto/executor/v1/executor.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ service ExecutorService {
}

message ProcessBatchRequest {
uint32 batch_num = 1;
uint64 batch_num = 1;
string coinbase = 2;
bytes batch_l2_data = 3;
bytes old_state_root = 4;
Expand Down Expand Up @@ -62,10 +62,12 @@ message TransactionContext {
bytes output = 8;
// Total gas used as result of execution
uint64 gas_used = 9;
// Gas Price
uint64 gas_price = 10;
// Execution Time
uint32 execution_time = 10;
uint32 execution_time = 11;
// Starting state root
bytes old_state_root = 11;
bytes old_state_root = 12;
}

message TransactionStep {
Expand Down Expand Up @@ -99,6 +101,7 @@ message Contract {
string caller = 2;
uint64 value = 3;
bytes data = 4;
uint64 gas = 5;
}

message ProcessTransactionResponse {
Expand Down
20 changes: 10 additions & 10 deletions state/runtime/instrumentation/executortrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ type ExecutorTrace struct {
}

type Context struct {
Type string `json:"type"`
From string `json:"from"`
To string `json:"to"`
Input string `json:"input"`
Gas string `json:"gas"`
Value string `json:"value"`
Output string `json:"output"`
Nonce uint64 `json:"nonce"`
GasPrice string `json:"gasPrice"`
ChainID uint64 `json:"chainId"`
Type string `json:"type"`
From string `json:"from"`
To string `json:"to"`
Input string `json:"input"`
Gas string `json:"gas"`
Value string `json:"value"`
Output string `json:"output"`
Nonce uint64 `json:"nonce"`
GasPrice string `json:"gasPrice"`
// ChainID uint64 `json:"chainId"`
OldStateRoot string `json:"oldStateRoot"`
Time uint64 `json:"time"`
GasUsed string `json:"gasUsed"`
Expand Down
1 change: 0 additions & 1 deletion state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,6 @@ func (s *State) DebugTransaction(ctx context.Context, transactionHash common.Has
context.Value = tx.Value().String()
context.Output = "0x" + hex.EncodeToString(result.ReturnValue)
context.GasPrice = tx.GasPrice().String()
context.ChainID = tx.ChainId().Uint64()
context.OldStateRoot = "0x" + hex.EncodeToString(stateRoot)
context.Time = uint64(endTime.Sub(startTime))
context.GasUsed = strconv.FormatUint(result.GasUsed, encoding.Base10)
Expand Down
228 changes: 0 additions & 228 deletions state/tree/adapter.go

This file was deleted.

Loading

0 comments on commit 7511164

Please sign in to comment.