Skip to content

Commit

Permalink
Initial approach to implement eth_sendTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Dec 26, 2023
1 parent ce54945 commit 7e1a11b
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 19 deletions.
2 changes: 1 addition & 1 deletion jsonrpc/debug_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (d *Debug) TraceCall(
return nil, ErrHeaderNotFound
}

tx, err := DecodeTxn(arg, header.Number, d.store, true)
tx, err := DecodeTxn(arg, d.store, true)
if err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions jsonrpc/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"
"unicode"

"github.com/0xPolygon/polygon-edge/crypto"
"github.com/0xPolygon/polygon-edge/secrets"
"github.com/armon/go-metrics"
"github.com/hashicorp/go-hclog"
Expand Down Expand Up @@ -68,6 +69,7 @@ type dispatcherParams struct {
concurrentRequestsDebug uint64

secretsManager secrets.SecretsManager
txSigner crypto.TxSigner
}

func (dp dispatcherParams) isExceedingBatchLengthLimit(value uint64) bool {
Expand Down Expand Up @@ -106,6 +108,7 @@ func (d *Dispatcher) registerEndpoints(store JSONRPCStore) error {
d.params.secretsManager,
d.params.chainID,
d.params.priceLimit,
d.params.txSigner,
)
if err != nil {
return err
Expand Down
34 changes: 28 additions & 6 deletions jsonrpc/eth_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/0xPolygon/polygon-edge/chain"
"github.com/0xPolygon/polygon-edge/consensus/polybft/wallet"
"github.com/0xPolygon/polygon-edge/crypto"
"github.com/0xPolygon/polygon-edge/gasprice"
"github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/helper/progress"
Expand Down Expand Up @@ -101,6 +102,7 @@ type Eth struct {
filterManager *FilterManager
priceLimit uint64
account *wallet.Account
txSigner crypto.TxSigner
}

func NewEth(
Expand All @@ -109,7 +111,8 @@ func NewEth(
filterManager *FilterManager,
secretsManager secrets.SecretsManager,
chainID uint64,
priceLimit uint64) (*Eth, error) {
priceLimit uint64,
txSigner crypto.TxSigner) (*Eth, error) {
account, err := wallet.NewAccountFromSecret(secretsManager)
if err != nil {
return nil, fmt.Errorf("failed to read account data: %w", err)
Expand All @@ -122,6 +125,7 @@ func NewEth(
account: account,
priceLimit: priceLimit,
filterManager: filterManager,
txSigner: txSigner,
}, nil
}

Expand Down Expand Up @@ -246,9 +250,27 @@ func (e *Eth) SendRawTransaction(buf argBytes) (interface{}, error) {
}

// SendTransaction rejects eth_sendTransaction json-rpc call as we don't support wallet management
func (e *Eth) SendTransaction(_ *txnArgs) (interface{}, error) {
return nil, fmt.Errorf("request calls to eth_sendTransaction method are not supported," +
" use eth_sendRawTransaction instead")
func (e *Eth) SendTransaction(args *txnArgs) (interface{}, error) {
tx, err := DecodeTxn(args, e.store, true)
if err != nil {
return nil, err
}

ecdsaPrivKey, err := e.account.GetEcdsaPrivateKey()
if err != nil {
return nil, err
}

signedTx, err := e.txSigner.SignTx(tx, ecdsaPrivKey)
if err != nil {
return nil, err
}

if err := e.store.AddTx(signedTx); err != nil {
return nil, err
}

return signedTx.Hash.String(), nil
}

// GetTransactionByHash returns a transaction by its hash.
Expand Down Expand Up @@ -490,7 +512,7 @@ func (e *Eth) Call(arg *txnArgs, filter BlockNumberOrHash, apiOverride *stateOve
return nil, err
}

transaction, err := DecodeTxn(arg, header.Number, e.store, true)
transaction, err := DecodeTxn(arg, e.store, true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -545,7 +567,7 @@ func (e *Eth) EstimateGas(arg *txnArgs, rawNum *BlockNumber) (interface{}, error
}

// testTransaction should execute tx with nonce always set to the current expected nonce for the account
transaction, err := DecodeTxn(arg, header.Number, e.store, true)
transaction, err := DecodeTxn(arg, e.store, true)
if err != nil {
return nil, err
}
Expand Down
18 changes: 14 additions & 4 deletions jsonrpc/eth_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func TestEth_DecodeTxn(t *testing.T) {
store.SetAccount(addr, acc)
}

res, err := DecodeTxn(tt.arg, 1, store, false)
res, err := DecodeTxn(tt.arg, store, false)
assert.Equal(t, tt.res, res)
assert.Equal(t, tt.err, err)
})
Expand Down Expand Up @@ -290,7 +290,7 @@ func TestEth_TxnType(t *testing.T) {
Nonce: 0,
Type: types.DynamicFeeTx,
}
res, err := DecodeTxn(args, 1, store, false)
res, err := DecodeTxn(args, store, false)

expectedRes.ComputeHash()
assert.NoError(t, err)
Expand All @@ -299,13 +299,23 @@ func TestEth_TxnType(t *testing.T) {

func newTestEthEndpoint(store testStore) *Eth {
return &Eth{
hclog.NewNullLogger(), store, 100, nil, 0, nil,
logger: hclog.NewNullLogger(),
store: store,
chainID: 100,
filterManager: nil,
priceLimit: 0,
account: nil,
}
}

func newTestEthEndpointWithPriceLimit(store testStore, priceLimit uint64) *Eth {
return &Eth{
hclog.NewNullLogger(), store, 100, nil, priceLimit, nil,
logger: hclog.NewNullLogger(),
store: store,
chainID: 100,
filterManager: nil,
priceLimit: priceLimit,
account: nil,
}
}

Expand Down
2 changes: 1 addition & 1 deletion jsonrpc/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func GetNextNonce(address types.Address, number BlockNumber, store nonceGetter)
return acc.Nonce, nil
}

func DecodeTxn(arg *txnArgs, blockNumber uint64, store nonceGetter, forceSetNonce bool) (*types.Transaction, error) {
func DecodeTxn(arg *txnArgs, store nonceGetter, forceSetNonce bool) (*types.Transaction, error) {
if arg == nil {
return nil, errors.New("missing value for required argument 0")
}
Expand Down
2 changes: 1 addition & 1 deletion jsonrpc/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ func TestDecodeTxn(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
t.Parallel()

tx, err := DecodeTxn(test.arg, 1, test.store, false)
tx, err := DecodeTxn(test.arg, test.store, false)

// DecodeTxn computes hash of tx
if !test.err {
Expand Down
3 changes: 3 additions & 0 deletions jsonrpc/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync"
"time"

"github.com/0xPolygon/polygon-edge/crypto"
"github.com/0xPolygon/polygon-edge/secrets"
"github.com/0xPolygon/polygon-edge/versioning"
"github.com/gorilla/websocket"
Expand Down Expand Up @@ -74,6 +75,7 @@ type Config struct {
WebSocketReadLimit uint64

SecretsManager secrets.SecretsManager
TxSigner crypto.TxSigner
}

// NewJSONRPC returns the JSONRPC http server
Expand All @@ -89,6 +91,7 @@ func NewJSONRPC(logger hclog.Logger, config *Config) (*JSONRPC, error) {
blockRangeLimit: config.BlockRangeLimit,
concurrentRequestsDebug: config.ConcurrentRequestsDebug,
secretsManager: config.SecretsManager,
txSigner: config.TxSigner,
},
)

Expand Down
13 changes: 7 additions & 6 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ func NewServer(config *Config) (*Server, error) {
// compute the genesis root state
config.Chain.Genesis.StateRoot = genesisRoot

// Use the london signer with eip-155 as a fallback one
var signer crypto.TxSigner = crypto.NewLondonSigner(
// Use the london txSigner with eip-155 as a fallback one
var txSigner crypto.TxSigner = crypto.NewLondonSigner(
uint64(m.config.Chain.Params.ChainID),
config.Chain.Params.Forks.IsActive(chain.Homestead, 0),
crypto.NewEIP155Signer(
Expand Down Expand Up @@ -318,7 +318,7 @@ func NewServer(config *Config) (*Server, error) {
config.Chain,
nil,
m.executor,
signer,
txSigner,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -356,7 +356,7 @@ func NewServer(config *Config) (*Server, error) {
return nil, err
}

m.txpool.SetSigner(signer)
m.txpool.SetSigner(txSigner)
}

{
Expand Down Expand Up @@ -389,7 +389,7 @@ func NewServer(config *Config) (*Server, error) {
}

// setup and start jsonrpc server
if err := m.setupJSONRPC(); err != nil {
if err := m.setupJSONRPC(txSigner); err != nil {
return nil, err
}

Expand Down Expand Up @@ -850,7 +850,7 @@ func (j *jsonRPCHub) GetSyncProgression() *progress.Progression {
// SETUP //

// setupJSONRCP sets up the JSONRPC server, using the set configuration
func (s *Server) setupJSONRPC() error {
func (s *Server) setupJSONRPC(txSigner crypto.TxSigner) error {
hub := &jsonRPCHub{
state: s.state,
restoreProgression: s.restoreProgression,
Expand All @@ -875,6 +875,7 @@ func (s *Server) setupJSONRPC() error {
ConcurrentRequestsDebug: s.config.JSONRPC.ConcurrentRequestsDebug,
WebSocketReadLimit: s.config.JSONRPC.WebSocketReadLimit,
SecretsManager: s.secretsManager,
TxSigner: txSigner,
}

srv, err := jsonrpc.NewJSONRPC(s.logger, conf)
Expand Down

0 comments on commit 7e1a11b

Please sign in to comment.