Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grps eth_sendTransaction #882

Merged
merged 8 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func initGenesis(ctx *cli.Context) error {
utils.Fatalf("invalid genesis file: %v", err)
}
// Open an initialise both full and light databases
stack := makeFullNode(ctx)
stack, _ := makeFullNode(ctx)
defer stack.Close()

for _, name := range []string{"chaindata", "lightchaindata"} {
Expand Down Expand Up @@ -269,7 +269,7 @@ func importChain(ctx *cli.Context) error {
utils.SetupMetrics(ctx)
// Start system runtime metrics collection
go metrics.CollectProcessMetrics(3 * time.Second)
stack := makeFullNode(ctx)
stack, _ := makeFullNode(ctx)
defer stack.Close()

_, chain, db := utils.MakeChain(ctx, stack, false)
Expand Down Expand Up @@ -327,7 +327,7 @@ func exportChain(ctx *cli.Context) error {
if len(ctx.Args()) < 1 {
utils.Fatalf("This command requires an argument.")
}
stack := makeFullNode(ctx)
stack, _ := makeFullNode(ctx)
defer stack.Close()

_, chain, _ := utils.MakeChain(ctx, stack, true)
Expand Down Expand Up @@ -362,7 +362,7 @@ func importPreimages(ctx *cli.Context) error {
if len(ctx.Args()) < 1 {
utils.Fatalf("This command requires an argument.")
}
stack := makeFullNode(ctx)
stack, _ := makeFullNode(ctx)
defer stack.Close()

diskdb := utils.MakeChainDatabase(ctx, stack)
Expand All @@ -380,7 +380,7 @@ func exportPreimages(ctx *cli.Context) error {
if len(ctx.Args()) < 1 {
utils.Fatalf("This command requires an argument.")
}
stack := makeFullNode(ctx)
stack, _ := makeFullNode(ctx)
defer stack.Close()

diskdb := utils.MakeChainDatabase(ctx, stack)
Expand All @@ -402,7 +402,7 @@ func copyDb(ctx *cli.Context) error {
utils.Fatalf("Source ancient chain directory path argument missing")
}
// Initialize a new chain for the running node to sync into
stack := makeFullNode(ctx)
stack, _ := makeFullNode(ctx)
defer stack.Close()

chainConfig, chain, chainDb := utils.MakeChain(ctx, stack, false)
Expand Down Expand Up @@ -500,7 +500,7 @@ func confirmAndRemoveDB(database string, kind string) {
}

func dump(ctx *cli.Context) error {
stack := makeFullNode(ctx)
stack, _ := makeFullNode(ctx)
defer stack.Close()

_, chain, chainDb := utils.MakeChain(ctx, stack, true)
Expand Down
6 changes: 3 additions & 3 deletions cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
return stack, cfg
}

func makeFullNode(ctx *cli.Context) *node.Node {
func makeFullNode(ctx *cli.Context) (*node.Node, *eth.Ethereum) {
stack, cfg := makeConfigNode(ctx)
utils.RegisterEthService(stack, &cfg.Eth)
service := utils.RegisterEthService(stack, &cfg.Eth)

// Configure GraphQL if required
if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) {
Expand All @@ -143,7 +143,7 @@ func makeFullNode(ctx *cli.Context) *node.Node {
if cfg.Ethstats.URL != "" {
utils.RegisterEthStatsService(stack, cfg.Ethstats.URL)
}
return stack
return stack, service
}

// dumpConfig is the dumpconfig command.
Expand Down
10 changes: 6 additions & 4 deletions cmd/geth/consolecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ import (
"strings"
"syscall"

"github.com/urfave/cli"

"github.com/ledgerwatch/turbo-geth/cmd/rpcdaemon/service"
"github.com/ledgerwatch/turbo-geth/cmd/utils"
"github.com/ledgerwatch/turbo-geth/console"
"github.com/ledgerwatch/turbo-geth/node"
"github.com/ledgerwatch/turbo-geth/rpc"
"github.com/urfave/cli"
)

var (
Expand Down Expand Up @@ -79,15 +80,16 @@ JavaScript API. See https://github.com/ledgerwatch/turbo-geth/wiki/JavaScript-Co
func localConsole(ctx *cli.Context) error {
// Create and start the node based on the CLI flags
prepare(ctx)
stack := makeFullNode(ctx)
stack, ethService := makeFullNode(ctx)

err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
diskdb, err := ctx.OpenDatabaseWithFreezer("chaindata", "")
if err != nil {
return nil, err
}
return service.New(diskdb), nil
return service.New(diskdb, ethService.TxPool()), nil
})

if err != nil {
panic(err)
}
Expand Down Expand Up @@ -204,7 +206,7 @@ func dialRPC(endpoint string) (*rpc.Client, error) {
// everything down.
func ephemeralConsole(ctx *cli.Context) error {
// Create and start the node based on the CLI flags
node := makeFullNode(ctx)
node, _ := makeFullNode(ctx)
startNode(ctx, node)
defer node.Close()

Expand Down
2 changes: 1 addition & 1 deletion cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func geth(ctx *cli.Context) error {
return fmt.Errorf("invalid command: %q", args[0])
}
prepare(ctx)
node := makeFullNode(ctx)
node, _ := makeFullNode(ctx)
defer node.Close()
startNode(ctx, node)
node.Wait()
Expand Down
1 change: 1 addition & 0 deletions cmd/restapi/apis/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var ErrEntityNotFound = errors.New("entity not found")
type Env struct {
KV ethdb.KV
DB ethdb.Getter
Back ethdb.Backend
Chaindata string
RemoteDBAddress string
}
3 changes: 2 additions & 1 deletion cmd/restapi/apis/remote_db_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (e *Env) GetDB(c *gin.Context) {

func (e *Env) PostDB(c *gin.Context) {
newAddr := c.Query("host") + ":" + c.Query("port")
kv, err := ethdb.NewRemote2().Path(newAddr).Open()
kv, back, err := ethdb.NewRemote2().Path(newAddr).Open()
if err != nil {
c.Error(err) //nolint:errcheck
return
Expand All @@ -38,5 +38,6 @@ func (e *Env) PostDB(c *gin.Context) {
e.KV = kv
db := ethdb.NewObjectDatabase(kv)
e.DB = db
e.Back = back
c.Status(http.StatusOK)
}
4 changes: 3 additions & 1 deletion cmd/restapi/rest/serve_rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ func ServeREST(ctx context.Context, restHost, rpcHost string, chaindata string)

var kv ethdb.KV
var db ethdb.Database
var back ethdb.Backend
var err error
if rpcHost != "" {
kv, err = ethdb.NewRemote2().Path(rpcHost).Open()
kv, back, err = ethdb.NewRemote2().Path(rpcHost).Open()
db = ethdb.NewObjectDatabase(kv)
} else if chaindata != "" {
database, errOpen := ethdb.Open(chaindata)
Expand All @@ -53,6 +54,7 @@ func ServeREST(ctx context.Context, restHost, rpcHost string, chaindata string)
e := &apis.Env{
KV: kv,
DB: db,
Back: back,
RemoteDBAddress: rpcHost,
Chaindata: chaindata,
}
Expand Down
14 changes: 8 additions & 6 deletions cmd/rpcdaemon/commands/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"math/big"
"strings"

"github.com/spf13/cobra"

"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/consensus"
"github.com/ledgerwatch/turbo-geth/core/rawdb"
Expand All @@ -17,7 +19,6 @@ import (
"github.com/ledgerwatch/turbo-geth/node"
"github.com/ledgerwatch/turbo-geth/params"
"github.com/ledgerwatch/turbo-geth/rpc"
"github.com/spf13/cobra"
)

// splitAndTrim splits input separated by a comma
Expand Down Expand Up @@ -132,12 +133,12 @@ func (api *APIImpl) rpcMarshalBlock(b *types.Block, inclTx bool, fullTx bool, ad
return fields, err
}

func GetAPI(db ethdb.KV, enabledApis []string) []rpc.API {
var rpcAPI = []rpc.API{}
func GetAPI(db ethdb.KV, txpool ethdb.Backend, enabledApis []string) []rpc.API {
var rpcAPI []rpc.API

dbReader := ethdb.NewObjectDatabase(db)
chainContext := NewChainContext(dbReader)
apiImpl := NewAPI(db, dbReader, chainContext)
apiImpl := NewAPI(db, dbReader, chainContext, txpool)
dbgAPIImpl := NewPrivateDebugAPI(db, dbReader, chainContext)

for _, enabledAPI := range enabledApis {
Expand Down Expand Up @@ -170,9 +171,10 @@ func daemon(cmd *cobra.Command, cfg Config) {
enabledApis := splitAndTrim(cfg.API)

var db ethdb.KV
var txPool ethdb.Backend
var err error
if cfg.privateApiAddr != "" {
db, err = ethdb.NewRemote2().Path(cfg.privateApiAddr).Open()
db, txPool, err = ethdb.NewRemote2().Path(cfg.privateApiAddr).Open()
if err != nil {
log.Error("Could not connect to remoteDb", "error", err)
return
Expand All @@ -192,7 +194,7 @@ func daemon(cmd *cobra.Command, cfg Config) {
return
}

var rpcAPI = GetAPI(db, enabledApis)
var rpcAPI = GetAPI(db, txPool, enabledApis)

httpEndpoint := fmt.Sprintf("%s:%d", cfg.httpListenAddress, cfg.httpPort)

Expand Down
7 changes: 5 additions & 2 deletions cmd/rpcdaemon/commands/eth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,32 @@ import (

// EthAPI is a collection of functions that are exposed in the
type EthAPI interface {
Coinbase(ctx context.Context) (common.Address, error)
Coinbase(context.Context) (common.Address, error)
BlockNumber(ctx context.Context) (hexutil.Uint64, error)
GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, fullTx bool) (map[string]interface{}, error)
GetBalance(_ context.Context, address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (*hexutil.Big, error)
GetTransactionReceipt(ctx context.Context, hash common.Hash) (map[string]interface{}, error)
GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error)
Call(ctx context.Context, args ethapi.CallArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides map[common.Address]ethapi.Account) (hexutil.Bytes, error)
EstimateGas(ctx context.Context, args ethapi.CallArgs) (hexutil.Uint64, error)
SendRawTransaction(ctx context.Context, encodedTx hexutil.Bytes) (common.Hash, error)
}

// APIImpl is implementation of the EthAPI interface based on remote Db access
type APIImpl struct {
db ethdb.KV
txpool ethdb.Backend
dbReader ethdb.Getter
chainContext core.ChainContext
}

// NewAPI returns APIImpl instance
func NewAPI(db ethdb.KV, dbReader ethdb.Getter, chainContext core.ChainContext) *APIImpl {
func NewAPI(db ethdb.KV, dbReader ethdb.Getter, chainContext core.ChainContext, txpool ethdb.Backend) *APIImpl {
return &APIImpl{
db: db,
dbReader: dbReader,
chainContext: chainContext,
txpool: txpool,
}
}

Expand Down
13 changes: 13 additions & 0 deletions cmd/rpcdaemon/commands/send_transaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package commands

import (
"context"

"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/common/hexutil"
)

func (api *APIImpl) SendRawTransaction(_ context.Context, encodedTx hexutil.Bytes) (common.Hash, error) {
res, err := api.txpool.AddLocal(encodedTx)
return common.BytesToHash(res), err
}
12 changes: 7 additions & 5 deletions cmd/rpcdaemon/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"github.com/ledgerwatch/turbo-geth/cmd/rpcdaemon/commands"
"github.com/ledgerwatch/turbo-geth/core"
"github.com/ledgerwatch/turbo-geth/ethdb"
"github.com/ledgerwatch/turbo-geth/node"
"github.com/ledgerwatch/turbo-geth/p2p"
Expand All @@ -12,16 +13,17 @@ var _ node.Service = &RPCDaemonService{}

// RPCDaemonService is used in js console and console tests
type RPCDaemonService struct {
api []rpc.API
db *ethdb.ObjectDatabase
api []rpc.API
db *ethdb.ObjectDatabase
txpool *core.TxPool
}

func (*RPCDaemonService) Protocols() []p2p.Protocol {
return []p2p.Protocol{}
}

func (r *RPCDaemonService) Start(server *p2p.Server) error {
r.api = commands.GetAPI(r.db.KV(), []string{"eth", "debug"})
r.api = commands.GetAPI(r.db.KV(), core.RawFromTxPool(r.txpool), []string{"eth", "debug"})
return nil
}

Expand All @@ -33,6 +35,6 @@ func (r *RPCDaemonService) APIs() []rpc.API {
return r.api
}

func New(db *ethdb.ObjectDatabase) *RPCDaemonService {
return &RPCDaemonService{[]rpc.API{}, db}
func New(db *ethdb.ObjectDatabase, txpool *core.TxPool) *RPCDaemonService {
return &RPCDaemonService{[]rpc.API{}, db, txpool}
}
10 changes: 6 additions & 4 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1726,15 +1726,17 @@ func setDNSDiscoveryDefaults(cfg *eth.Config, genesis common.Hash) {
}

// RegisterEthService adds an Ethereum client to the stack.
func RegisterEthService(stack *node.Node, cfg *eth.Config) {
var err error
err = stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
fullNode, err := eth.New(ctx, cfg)
func RegisterEthService(stack *node.Node, cfg *eth.Config) *eth.Ethereum {
fullNode := new(eth.Ethereum)
err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
fullNodeInst, err := eth.New(ctx, cfg)
*fullNode = *fullNodeInst //nolint:govet
return fullNode, err
})
if err != nil {
Fatalf("Failed to register the Ethereum service: %v", err)
}
return fullNode
}

// RegisterEthStatsService configures the Ethereum Stats daemon and adds it to
Expand Down
26 changes: 26 additions & 0 deletions core/raw_tx_pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package core

import (
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/core/types"
"github.com/ledgerwatch/turbo-geth/rlp"
)

type RawTxPool TxPool

func RawFromTxPool(pool *TxPool) *RawTxPool {
return (*RawTxPool)(pool)
}

func TxPoolFromRaw(pool *RawTxPool) *TxPool {
return (*TxPool)(pool)
}

func (pool *RawTxPool) AddLocal(signedtx []byte) ([]byte, error) {
tx := new(types.Transaction)
if err := rlp.DecodeBytes(signedtx, tx); err != nil {
return common.Hash{}.Bytes(), err
}

return tx.Hash().Bytes(), TxPoolFromRaw(pool).AddLocal(tx)
}
8 changes: 4 additions & 4 deletions core/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (config *TxPoolConfig) sanitize() TxPoolConfig {
return conf
}

// TxPool contains all currently known transactions. Transactions
// Backend contains all currently known transactions. Transactions
// enter the pool when they are received from the network or submitted
// locally. They exit the pool when they are included in the blockchain.
//
Expand Down Expand Up @@ -1502,15 +1502,15 @@ func (as *accountSet) merge(other *accountSet) {
as.cache = nil
}

// txLookup is used internally by TxPool to track transactions while allowing lookup without
// txLookup is used internally by Backend to track transactions while allowing lookup without
// mutex contention.
//
// Note, although this type is properly protected against concurrent access, it
// is **not** a type that should ever be mutated or even exposed outside of the
// transaction pool, since its internal state is tightly coupled with the pools
// internal mechanisms. The sole purpose of the type is to permit out-of-bound
// peeking into the pool in TxPool.Get without having to acquire the widely scoped
// TxPool.mu mutex.
// peeking into the pool in Backend.Get without having to acquire the widely scoped
// Backend.mu mutex.
type txLookup struct {
all map[common.Hash]*types.Transaction
slots int
Expand Down
Loading