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

Relay jRPC methods to TrustedSequencer if pool info is needed #1767

Merged
merged 5 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
relay eth_getTransactionCount to trusted seq when block pending
  • Loading branch information
tclemos committed Mar 7, 2023
commit 2d259cc7009df17826a8ea7fe2636d74a1896771
14 changes: 14 additions & 0 deletions jsonrpc/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/0xPolygonHermez/zkevm-node/encoding"
"github.com/0xPolygonHermez/zkevm-node/hex"
"github.com/jackc/pgx/v4"
)

Expand Down Expand Up @@ -167,6 +168,19 @@ func (b *BlockNumber) getNumericBlockNumber(ctx context.Context, s stateInterfac
}
}

func (b *BlockNumber) StringOrHex() string {
switch *b {
case EarliestBlockNumber:
return Earliest
case PendingBlockNumber:
return Pending
case LatestBlockNumber:
return Latest
default:
return hex.EncodeUint64(uint64(*b))
}
}

func stringToBlockNumber(str string) (BlockNumber, error) {
str = strings.Trim(str, "\"")
switch str {
Expand Down
18 changes: 18 additions & 0 deletions jsonrpc/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,24 @@ func TestIndexUnmarshalJSON(t *testing.T) {
}
}

func TestBlockNumberStringOrHex(t *testing.T) {
testCases := []struct {
bn *BlockNumber
expectedResult string
}{
{bn: bnPtr(BlockNumber(-3)), expectedResult: "pending"},
{bn: bnPtr(BlockNumber(-2)), expectedResult: "latest"},
{bn: bnPtr(BlockNumber(-1)), expectedResult: "earliest"},
{bn: bnPtr(BlockNumber(0)), expectedResult: "0x0"},
{bn: bnPtr(BlockNumber(100)), expectedResult: "0x64"},
}

for _, testCase := range testCases {
result := testCase.bn.StringOrHex()
assert.Equal(t, testCase.expectedResult, result)
}
}

func bnPtr(bn BlockNumber) *BlockNumber {
return &bn
}
24 changes: 23 additions & 1 deletion jsonrpc/endpoints_eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,11 @@ func (e *EthEndpoints) GetTransactionCount(address common.Address, number *Block
var nonce uint64
var err error
if number != nil && *number == PendingBlockNumber {
pendingNonce, err = e.pool.GetNonce(ctx, address)
if e.cfg.SequencerNodeURI != "" {
return e.getTransactionCountFromSequencerNode(address, number)
} else {
pendingNonce, err = e.pool.GetNonce(ctx, address)
}
if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to count pending transactions", err)
}
Expand All @@ -491,6 +495,24 @@ func (e *EthEndpoints) GetTransactionCount(address common.Address, number *Block
})
}

func (e *EthEndpoints) getTransactionCountFromSequencerNode(address common.Address, number *BlockNumber) (interface{}, rpcError) {
res, err := JSONRPCCall(e.cfg.SequencerNodeURI, "eth_getTransactionCount", address.String(), number.StringOrHex())
if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to get nonce from sequencer node", err)
}

if res.Error != nil {
return rpcErrorResponse(res.Error.Code, res.Error.Message, nil)
}

var nonce argUint64
err = json.Unmarshal(res.Result, &nonce)
if err != nil {
return rpcErrorResponse(defaultErrorCode, "failed to read nonce from sequencer node", err)
}
return nonce, nil
}

// GetBlockTransactionCountByHash returns the number of transactions in a
// block from a block matching the given block hash.
func (e *EthEndpoints) GetBlockTransactionCountByHash(hash common.Hash) (interface{}, rpcError) {
Expand Down