Skip to content

Commit

Permalink
add custom methods to json RPC with prefix hez (okx#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
tclemos authored Dec 16, 2021
1 parent 2621d3a commit abdb546
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 46 deletions.
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ GOENVVARS := GOBIN=$(GOBIN)
GOBINARY := hezcore
GOCMD := $(GOBASE)/cmd

LINT := $$(go env GOPATH)/bin/golangci-lint run --timeout=5m -E whitespace -E gosec -E gci -E misspell -E gomnd -E gofmt -E goimports -E golint --exclude-use-default=false --max-same-issues 0
BUILD := $(GOENVVARS) go build $(LDFLAGS) -o $(GOBIN)/$(GOBINARY) $(GOCMD)
RUN := $(GOBIN)/$(GOBINARY) run --network local --cfg ./cmd/config.toml

.PHONY: build
build: ## Build the binary
$(GOENVVARS) go build $(LDFLAGS) -o $(GOBIN)/$(GOBINARY) $(GOCMD)
$(BUILD)

.PHONY: build-docker
build-docker: ## Build the binary
Expand All @@ -40,14 +44,14 @@ install-linter: ## install linter

.PHONY: lint
lint: ## runs linter
$$(go env GOPATH)/bin/golangci-lint run --timeout=5m -E whitespace -E gosec -E gci -E misspell -E gomnd -E gofmt -E goimports -E golint --exclude-use-default=false --max-same-issues 0
$(LINT)

.PHONY: deploy
deploy: lint test-full build ## Validate and create the binary to be deployed

.PHONY: run
run: ## Runs the application
$(GOBIN)/$(GOBINARY) run
$(RUN)

.PHONY: start-db
start-db: ## starts a docker container to run the db instance
Expand Down
2 changes: 1 addition & 1 deletion cmd/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Port = "5432"

[Etherman]
URL = "http://localhost"
PrivateKeyPath = "../test/test.keystore"
PrivateKeyPath = "./test/test.keystore"
PrivateKeyPassword = "testonly"

[RPC]
Expand Down
6 changes: 3 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func start(ctx *cli.Context) error {

//proverClient, conn := newProverClient(c.Prover)
go runSynchronizer(c.NetworkConfig.GenBlockNumber, etherman, st)
go runJSONRpcServer(c.RPC, c.Etherman, pool, st)
go runJSONRpcServer(c.RPC, c.Etherman, c.NetworkConfig, pool, st)
go runSequencer(c.Sequencer, etherman, pool, st)
//go runAggregator(c.Aggregator, etherman, proverClient, state)
//waitSignal(conn)
Expand Down Expand Up @@ -185,7 +185,7 @@ func runSynchronizer(genBlockNumber uint64, etherman *etherman.ClientEtherMan, s
}
}

func runJSONRpcServer(jc jsonrpc.Config, ec etherman.Config, pool pool.Pool, st state.State) {
func runJSONRpcServer(jc jsonrpc.Config, ec etherman.Config, nc config.NetworkConfig, pool pool.Pool, st state.State) {
var err error
var seq *state.Sequencer

Expand All @@ -208,7 +208,7 @@ func runJSONRpcServer(jc jsonrpc.Config, ec etherman.Config, pool pool.Pool, st
break
}

if err := jsonrpc.NewServer(jc, seq.ChainID.Uint64(), pool, st).Start(); err != nil {
if err := jsonrpc.NewServer(jc, nc.L2DefaultChainID, seq.ChainID.Uint64(), pool, st).Start(); err != nil {
log.Fatal(err)
}
}
Expand Down
11 changes: 5 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package config

import (
"bytes"
"log"
"path/filepath"
"strings"

"github.com/hermeznetwork/hermez-core/aggregator"
"github.com/hermeznetwork/hermez-core/db"
"github.com/hermeznetwork/hermez-core/etherman"
"github.com/hermeznetwork/hermez-core/jsonrpc"
logger "github.com/hermeznetwork/hermez-core/log"
"github.com/hermeznetwork/hermez-core/log"
"github.com/hermeznetwork/hermez-core/proverclient"
"github.com/hermeznetwork/hermez-core/sequencer"
"github.com/mitchellh/mapstructure"
Expand All @@ -19,7 +18,7 @@ import (

// Config represents the configuration of the entire Hermez Node
type Config struct {
Log logger.Config
Log log.Config
Database db.Config
Etherman etherman.Config
RPC jsonrpc.Config
Expand Down Expand Up @@ -59,9 +58,9 @@ func Load(configFilePath string, network string) (*Config, error) {
if err != nil {
_, ok := err.(viper.ConfigFileNotFoundError)
if ok {
log.Println("config file not found")
log.Infof("config file not found")
} else {
log.Println("error reading config file: ", err)
log.Infof("error reading config file: ", err)
return nil, err
}
}
Expand All @@ -73,6 +72,6 @@ func Load(configFilePath string, network string) (*Config, error) {
// Load genesis parameters
cfg.loadNetworkConfig(network)

log.Printf("Configuration loaded: %+v", cfg)
log.Infof("Configuration loaded: %+v", cfg)
return &cfg, nil
}
4 changes: 0 additions & 4 deletions encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ const (
Base10 = 10
// BitSize64 64 bits
BitSize64 = 64
// TenToThePowerOf9 is 10 ^ 9 which represents a gWei
TenToThePowerOf9 = 10 ^ 9
// TenToThePowerOf18 is 10 ^ 18 which represents an Ether
TenToThePowerOf18 = 10 ^ 18
)

// DecodeUint64orHex decodes a string uint64 or hex string into a uint64
Expand Down
2 changes: 1 addition & 1 deletion jsonrpc/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (e *Eth) GetBalance(address common.Address, number *BlockNumber) (interface
return nil, err
}

return hex.EncodeUint64(balance.Uint64()), nil
return hex.EncodeBig(balance), nil
}

// GetBlockByHash returns information about a block by hash
Expand Down
3 changes: 2 additions & 1 deletion jsonrpc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ type Handler struct {
serviceMap map[string]*serviceData
}

func newJSONRpcHandler(e *Eth, n *Net) *Handler {
func newJSONRpcHandler(e *Eth, n *Net, h *Hez) *Handler {
d := &Handler{
serviceMap: map[string]*serviceData{},
}

d.registerService("eth", e)
d.registerService("net", n)
d.registerService("hez", h)

return d
}
Expand Down
31 changes: 31 additions & 0 deletions jsonrpc/hez.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package jsonrpc

import (
"context"

"github.com/hermeznetwork/hermez-core/hex"
"github.com/hermeznetwork/hermez-core/state"
)

// Hez contains implementations for the "hez" RPC endpoints
type Hez struct {
defaultChainID uint64
state state.State
}

// DefaultChainId returns the default chain id that is allowed to be used by all the sequencers
func (h *Hez) DefaultChainId() (interface{}, error) { //nolint:golint
return hex.EncodeUint64(h.defaultChainID), nil
}

// ConsolidatedBlockNumber returns current block number for consolidated batches
func (h *Hez) ConsolidatedBlockNumber() (interface{}, error) {
ctx := context.Background()

lastBatchNumber, err := h.state.GetLastConsolidatedBatchNumber(ctx)
if err != nil {
return nil, err
}

return hex.EncodeUint64(lastBatchNumber), nil
}
5 changes: 3 additions & 2 deletions jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ type Server struct {
}

// NewServer returns the JsonRPC server
func NewServer(config Config, chainID uint64, p pool.Pool, s state.State) *Server {
func NewServer(config Config, defaultChainID uint64, chainID uint64, p pool.Pool, s state.State) *Server {
ethEndpoints := &Eth{chainID: chainID, pool: p, state: s}
netEndpoints := &Net{chainID: chainID}
hezEndpoints := &Hez{defaultChainID: defaultChainID, state: s}

handler := newJSONRpcHandler(ethEndpoints, netEndpoints)
handler := newJSONRpcHandler(ethEndpoints, netEndpoints, hezEndpoints)

srv := &Server{
config: config,
Expand Down
2 changes: 1 addition & 1 deletion jsonrpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type argUint64 uint64

// MarshalText marshals into text
func (b argUint64) MarshalText() ([]byte, error) {
buf := make([]byte, 2, 10) //nolint:gomnd
buf := make([]byte, 2, encoding.Base10) //nolint:gomnd
copy(buf, `0x`)
buf = strconv.AppendUint(buf, uint64(b), hex.Base)
return buf, nil
Expand Down
54 changes: 33 additions & 21 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type State interface {
GetBatchByHash(ctx context.Context, hash common.Hash) (*Batch, error)
GetBatchByNumber(ctx context.Context, batchNumber uint64) (*Batch, error)
GetLastBatchNumber(ctx context.Context) (uint64, error)
GetLastConsolidatedBatchNumber(ctx context.Context) (uint64, error)
GetTransactionByBatchHashAndIndex(ctx context.Context, batchHash common.Hash, index uint64) (*types.Transaction, error)
GetTransactionByBatchNumberAndIndex(ctx context.Context, batchNumber uint64, index uint64) (*types.Transaction, error)
GetTransactionByHash(ctx context.Context, transactionHash common.Hash) (*types.Transaction, error)
Expand All @@ -49,26 +50,27 @@ type State interface {
}

const (
getLastBlockSQL = "SELECT * FROM state.block ORDER BY block_num DESC LIMIT 1"
getPreviousBlockSQL = "SELECT * FROM state.block ORDER BY block_num DESC LIMIT 1 OFFSET $1"
getBlockByHashSQL = "SELECT * FROM state.block WHERE block_hash = $1"
getBlockByNumberSQL = "SELECT * FROM state.block WHERE block_num = $1"
getLastBlockNumberSQL = "SELECT MAX(block_num) FROM state.block"
getLastVirtualBatchSQL = "SELECT * FROM state.batch ORDER BY batch_num DESC LIMIT 1"
getLastConsolidatedBatchSQL = "SELECT * FROM state.batch WHERE consolidated_tx_hash != $1 ORDER BY batch_num DESC LIMIT 1"
getPreviousVirtualBatchSQL = "SELECT * FROM state.batch ORDER BY batch_num DESC LIMIT 1 OFFSET $1"
getPreviousConsolidatedBatchSQL = "SELECT * FROM state.batch WHERE consolidated_tx_hash != $1 ORDER BY batch_num DESC LIMIT 1 OFFSET $2"
getBatchByHashSQL = "SELECT * FROM state.batch WHERE batch_hash = $1"
getBatchByNumberSQL = "SELECT * FROM state.batch WHERE batch_num = $1"
getLastBatchNumberSQL = "SELECT COALESCE(MAX(batch_num), 0) FROM state.batch"
getTransactionByHashSQL = "SELECT transaction.encoded FROM state.transaction WHERE hash = $1"
getTransactionCountSQL = "SELECT COUNT(*) FROM state.transaction WHERE from_address = $1"
consolidateBatchSQL = "UPDATE state.batch SET consolidated_tx_hash = $1 WHERE batch_num = $2"
getTxsByBatchNumSQL = "SELECT transaction.encoded FROM state.transaction WHERE batch_num = $1"
addBlockSQL = "INSERT INTO state.block (block_num, block_hash, parent_hash, received_at) VALUES ($1, $2, $3, $4)"
addSequencerSQL = "INSERT INTO state.sequencer (address, url, chain_id, block_num) VALUES ($1, $2, $3, $4)"
getSequencerSQL = "SELECT * FROM state.sequencer WHERE address = $1"
getReceiptSQL = "SELECT * FROM state.receipt WHERE tx_hash = $1"
getLastBlockSQL = "SELECT * FROM state.block ORDER BY block_num DESC LIMIT 1"
getPreviousBlockSQL = "SELECT * FROM state.block ORDER BY block_num DESC LIMIT 1 OFFSET $1"
getBlockByHashSQL = "SELECT * FROM state.block WHERE block_hash = $1"
getBlockByNumberSQL = "SELECT * FROM state.block WHERE block_num = $1"
getLastBlockNumberSQL = "SELECT MAX(block_num) FROM state.block"
getLastVirtualBatchSQL = "SELECT * FROM state.batch ORDER BY batch_num DESC LIMIT 1"
getLastConsolidatedBatchSQL = "SELECT * FROM state.batch WHERE consolidated_tx_hash != $1 ORDER BY batch_num DESC LIMIT 1"
getPreviousVirtualBatchSQL = "SELECT * FROM state.batch ORDER BY batch_num DESC LIMIT 1 OFFSET $1"
getPreviousConsolidatedBatchSQL = "SELECT * FROM state.batch WHERE consolidated_tx_hash != $1 ORDER BY batch_num DESC LIMIT 1 OFFSET $2"
getBatchByHashSQL = "SELECT * FROM state.batch WHERE batch_hash = $1"
getBatchByNumberSQL = "SELECT * FROM state.batch WHERE batch_num = $1"
getLastVirtualBatchNumberSQL = "SELECT COALESCE(MAX(batch_num), 0) FROM state.batch"
getLastConsolidatedBatchNumberSQL = "SELECT COALESCE(MAX(batch_num), 0) FROM state.batch WHERE consolidated_tx_hash != $1"
getTransactionByHashSQL = "SELECT transaction.encoded FROM state.transaction WHERE hash = $1"
getTransactionCountSQL = "SELECT COUNT(*) FROM state.transaction WHERE from_address = $1"
consolidateBatchSQL = "UPDATE state.batch SET consolidated_tx_hash = $1 WHERE batch_num = $2"
getTxsByBatchNumSQL = "SELECT transaction.encoded FROM state.transaction WHERE batch_num = $1"
addBlockSQL = "INSERT INTO state.block (block_num, block_hash, parent_hash, received_at) VALUES ($1, $2, $3, $4)"
addSequencerSQL = "INSERT INTO state.sequencer (address, url, chain_id, block_num) VALUES ($1, $2, $3, $4)"
getSequencerSQL = "SELECT * FROM state.sequencer WHERE address = $1"
getReceiptSQL = "SELECT * FROM state.receipt WHERE tx_hash = $1"
)

var (
Expand Down Expand Up @@ -272,7 +274,17 @@ func (s *BasicState) GetBatchByNumber(ctx context.Context, batchNumber uint64) (
// GetLastBatchNumber gets the latest batch number
func (s *BasicState) GetLastBatchNumber(ctx context.Context) (uint64, error) {
var lastBatchNumber uint64
err := s.db.QueryRow(ctx, getLastBatchNumberSQL).Scan(&lastBatchNumber)
err := s.db.QueryRow(ctx, getLastVirtualBatchNumberSQL).Scan(&lastBatchNumber)
if err != nil {
return 0, err
}
return lastBatchNumber, nil
}

// GetLastConsolidatedBatchNumber gets the latest consolidated batch number
func (s *BasicState) GetLastConsolidatedBatchNumber(ctx context.Context) (uint64, error) {
var lastBatchNumber uint64
err := s.db.QueryRow(ctx, getLastConsolidatedBatchNumberSQL, common.Hash{}).Scan(&lastBatchNumber)
if err != nil {
return 0, err
}
Expand Down
7 changes: 4 additions & 3 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/hermeznetwork/hermez-core/aggregator"
"github.com/hermeznetwork/hermez-core/config"
"github.com/hermeznetwork/hermez-core/db"
"github.com/hermeznetwork/hermez-core/encoding"
"github.com/hermeznetwork/hermez-core/etherman"
"github.com/hermeznetwork/hermez-core/jsonrpc"
"github.com/hermeznetwork/hermez-core/log"
Expand Down Expand Up @@ -171,7 +172,7 @@ func TestStateTransition(t *testing.T) {
stSeq, err := st.GetSequencer(ctx, common.HexToAddress(testCase.SequencerAddress))
require.NoError(t, err)

rpcServer := jsonrpc.NewServer(cfg.RPC, stSeq.ChainID.Uint64(), pl, st)
rpcServer := jsonrpc.NewServer(cfg.RPC, testCase.DefaultChainID, stSeq.ChainID.Uint64(), pl, st)
go func(t *testing.T, s *jsonrpc.Server) {
err := s.Start()
require.NoError(t, err)
Expand Down Expand Up @@ -220,11 +221,11 @@ func TestStateTransition(t *testing.T) {

actualBalance, err := st.GetBalance(addr, batchNumber)
require.NoError(t, err)
assert.Equal(t, 0, leaf.Balance.Cmp(actualBalance), fmt.Sprintf("addr: %s expected: %s found: %s", addr.Hex(), leaf.Balance.Text(10), actualBalance.Text(10)))
assert.Equal(t, 0, leaf.Balance.Cmp(actualBalance), fmt.Sprintf("addr: %s expected: %s found: %s", addr.Hex(), leaf.Balance.Text(encoding.Base10), actualBalance.Text(encoding.Base10)))

actualNonce, err := st.GetNonce(addr, batchNumber)
require.NoError(t, err)
assert.Equal(t, leaf.Nonce, strconv.FormatUint(actualNonce, 10), fmt.Sprintf("addr: %s expected: %s found: %d", addr.Hex(), leaf.Nonce, actualNonce))
assert.Equal(t, leaf.Nonce, strconv.FormatUint(actualNonce, encoding.Base10), fmt.Sprintf("addr: %s expected: %s found: %d", addr.Hex(), leaf.Nonce, actualNonce))
}
})
}
Expand Down
1 change: 1 addition & 0 deletions test/vectors/vectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type StateTransitionTestCase struct {
Description string `json:"description"`
Arity uint8 `json:"arity"`
ChanIDSequencer uint64 `json:"chainIdSequencer"`
DefaultChainID uint64 `json:"defaultChainId"`
SequencerAddress string `json:"sequencerAddress"`
SequencerPrivateKey string `json:"sequencerPvtKey"`

Expand Down

0 comments on commit abdb546

Please sign in to comment.