Skip to content

Commit

Permalink
Merge pull request #9 from maticnetwork/rpc
Browse files Browse the repository at this point in the history
Move erigon namespace rpc endpoints to eth
  • Loading branch information
0xKrishna committed Jan 17, 2022
2 parents d551cff + 8798d5b commit 19a50d9
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 174 deletions.
1 change: 1 addition & 0 deletions cmd/rpcdaemon/commands/bor_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

// BorAPI Bor specific routines
type BorAPI interface {
// Bor snapshot related (see ./bor_snapshot.go)
GetSnapshot(number *rpc.BlockNumber) (*Snapshot, error)
GetAuthor(number *rpc.BlockNumber) (*common.Address, error)
GetSnapshotAtHash(hash common.Hash) (*Snapshot, error)
Expand Down
8 changes: 0 additions & 8 deletions cmd/rpcdaemon/commands/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func APIList(
base.EnableTevmExperiment()
}
ethImpl := NewEthAPI(base, db, eth, txPool, mining, cfg.Gascap)
erigonImpl := NewErigonAPI(base, db)
txpoolImpl := NewTxPoolAPI(base, db, txPool)
netImpl := NewNetAPIImpl(eth)
debugImpl := NewPrivateDebugAPI(base, db, cfg.Gascap)
Expand Down Expand Up @@ -94,13 +93,6 @@ func APIList(
Service: DBAPI(dbImpl),
Version: "1.0",
})
case "erigon":
defaultAPIList = append(defaultAPIList, rpc.API{
Namespace: "erigon",
Public: true,
Service: ErigonAPI(erigonImpl),
Version: "1.0",
})
case "engine":
defaultAPIList = append(defaultAPIList, rpc.API{
Namespace: "engine",
Expand Down
43 changes: 0 additions & 43 deletions cmd/rpcdaemon/commands/erigon_api.go

This file was deleted.

60 changes: 0 additions & 60 deletions cmd/rpcdaemon/commands/erigon_block.go

This file was deleted.

3 changes: 2 additions & 1 deletion cmd/rpcdaemon/commands/erigon_receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"github.com/ledgerwatch/erigon/core/types"
)

// disabled for now, subset of eth_getLogs
// GetLogsByHash implements erigon_getLogsByHash. Returns an array of arrays of logs generated by the transactions in the block given by the block's hash.
func (api *ErigonImpl) GetLogsByHash(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
func (api *APIImpl) GetLogsByHash(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
tx, err := api.db.BeginRo(ctx)
if err != nil {
return nil, err
Expand Down
31 changes: 0 additions & 31 deletions cmd/rpcdaemon/commands/erigon_system.go

This file was deleted.

6 changes: 6 additions & 0 deletions cmd/rpcdaemon/commands/eth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type EthAPI interface {
GetBlockByHash(ctx context.Context, hash rpc.BlockNumberOrHash, fullTx bool) (map[string]interface{}, error)
GetBlockTransactionCountByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*hexutil.Uint, error)
GetBlockTransactionCountByHash(ctx context.Context, blockHash common.Hash) (*hexutil.Uint, error)
GetHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error)
GetHeaderByHash(_ context.Context, hash common.Hash) (*types.Header, error)

// Transaction related (see ./eth_txs.go)
GetTransactionByHash(ctx context.Context, hash common.Hash) (*RPCTransaction, error)
Expand Down Expand Up @@ -73,6 +75,7 @@ type EthAPI interface {
ChainId(ctx context.Context) (hexutil.Uint64, error) /* called eth_protocolVersion elsewhere */
ProtocolVersion(_ context.Context) (hexutil.Uint, error)
GasPrice(_ context.Context) (*hexutil.Big, error)
Forks(ctx context.Context) (Forks, error)

// Sending related (see ./eth_call.go)
Call(ctx context.Context, args ethapi.CallArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides *map[common.Address]ethapi.Account) (hexutil.Bytes, error)
Expand All @@ -91,6 +94,9 @@ type EthAPI interface {
SubmitWork(ctx context.Context, nonce types.BlockNonce, powHash, digest common.Hash) (bool, error)
SubmitHashrate(ctx context.Context, hashRate hexutil.Uint64, id common.Hash) (bool, error)

// Issuance / reward related (see ./eth_issuance.go)
Issuance(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error)

// Deprecated commands in eth_ (proposed file: ./eth_deprecated.go)
GetCompilers(_ context.Context) ([]string, error)
CompileLLL(_ context.Context, _ string) (hexutil.Bytes, error)
Expand Down
49 changes: 49 additions & 0 deletions cmd/rpcdaemon/commands/eth_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,52 @@ func (api *APIImpl) GetBlockTransactionCountByHash(ctx context.Context, blockHas
n := hexutil.Uint(txAmount)
return &n, nil
}

// GetHeaderByNumber implements eth_getHeaderByNumber. Returns a block's header given a block number ignoring the block's transaction and uncle list (may be faster).
func (api *APIImpl) GetHeaderByNumber(ctx context.Context, blockNumber rpc.BlockNumber) (*types.Header, error) {
// Pending block is only known by the miner
if blockNumber == rpc.PendingBlockNumber {
block := api.pendingBlock()
if block == nil {
return nil, nil
}
return block.Header(), nil
}

tx, err := api.db.BeginRo(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback()

blockNum, err := getBlockNumber(blockNumber, tx)
if err != nil {
return nil, err
}

header := rawdb.ReadHeaderByNumber(tx, blockNum)
if header == nil {
return nil, fmt.Errorf("block header not found: %d", blockNum)
}

return header, nil
}

// GetHeaderByHash implements eth_getHeaderByHash. Returns a block's header given a block's hash.
func (api *APIImpl) GetHeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
tx, err := api.db.BeginRo(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback()

header, err := rawdb.ReadHeaderByHash(tx, hash)
if err != nil {
return nil, err
}
if header == nil {
return nil, fmt.Errorf("block header not found: %s", hash.String())
}

return header, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,15 @@ import (
"github.com/ledgerwatch/erigon/rpc"
)

// BlockReward returns the block reward for this block
// func (api *ErigonImpl) BlockReward(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error) {
// tx, err := api.db.Begin(ctx, ethdb.RO)
// if err != nil {
// return Issuance{}, err
// }
// defer tx.Rollback()
//
// return api.rewardCalc(tx, blockNr, "block") // nolint goconst
//}

// UncleReward returns the uncle reward for this block
// func (api *ErigonImpl) UncleReward(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error) {
// tx, err := api.db.Begin(ctx, ethdb.RO)
// if err != nil {
// return Issuance{}, err
// }
// defer tx.Rollback()
//
// return api.rewardCalc(tx, blockNr, "uncle") // nolint goconst
//}
// Issuance structure to return information about issuance
type Issuance struct {
BlockReward string `json:"blockReward,omitempty"`
UncleReward string `json:"uncleReward,omitempty"`
Issuance string `json:"issuance,omitempty"`
}

// Issuance implements erigon_issuance. Returns the total issuance (block reward plus uncle reward) for the given block.
func (api *ErigonImpl) Issuance(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error) {
// Issuance implements eth_issuance. Returns the total issuance (block reward plus uncle reward) for the given block.
func (api *APIImpl) Issuance(ctx context.Context, blockNr rpc.BlockNumber) (Issuance, error) {
tx, err := api.db.BeginRo(ctx)
if err != nil {
return Issuance{}, err
Expand Down Expand Up @@ -68,17 +53,10 @@ func (api *ErigonImpl) Issuance(ctx context.Context, blockNr rpc.BlockNumber) (I
return ret, nil
}

func (api *ErigonImpl) getBlockByRPCNumber(tx kv.Tx, blockNr rpc.BlockNumber) (*types.Block, error) {
func (api *APIImpl) getBlockByRPCNumber(tx kv.Tx, blockNr rpc.BlockNumber) (*types.Block, error) {
blockNum, err := getBlockNumber(blockNr, tx)
if err != nil {
return nil, err
}
return api.blockByNumberWithSenders(tx, blockNum)
}

// Issuance structure to return information about issuance
type Issuance struct {
BlockReward string `json:"blockReward,omitempty"`
UncleReward string `json:"uncleReward,omitempty"`
Issuance string `json:"issuance,omitempty"`
}
24 changes: 24 additions & 0 deletions cmd/rpcdaemon/commands/eth_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/erigon/core/forkid"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/eth/ethconfig"
Expand Down Expand Up @@ -222,3 +223,26 @@ func (b *GasPriceOracleBackend) GetReceipts(ctx context.Context, hash common.Has
func (b *GasPriceOracleBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) {
return nil, nil
}

// Forks is a data type to record a list of forks passed by this node
type Forks struct {
GenesisHash common.Hash `json:"genesis"`
Forks []uint64 `json:"forks"`
}

// Forks implements eth_forks. Returns the genesis block hash and a sorted list of all forks block numbers
func (api *APIImpl) Forks(ctx context.Context) (Forks, error) {
tx, err := api.db.BeginRo(ctx)
if err != nil {
return Forks{}, err
}
defer tx.Rollback()

chainConfig, genesis, err := api.chainConfigWithGenesis(tx)
if err != nil {
return Forks{}, err
}
forksBlocks := forkid.GatherForks(chainConfig)

return Forks{genesis.Hash(), forksBlocks}, nil
}

0 comments on commit 19a50d9

Please sign in to comment.