From 9612e7ad63f1c89e9d67f78e9c79cf6aed72d6ca Mon Sep 17 00:00:00 2001 From: IronGauntlets Date: Thu, 7 Nov 2024 20:28:32 +0000 Subject: [PATCH] Add l1Client interface in rpc package --- node/node.go | 2 +- rpc/handlers.go | 18 ++++++++++++------ rpc/l1.go | 4 ++-- rpc/l1_test.go | 2 +- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/node/node.go b/node/node.go index 3ad0929794..503cbaa9bc 100644 --- a/node/node.go +++ b/node/node.go @@ -285,7 +285,7 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen return nil, fmt.Errorf("create L1 client: %w", err) } n.services = append(n.services, l1Client) - rpcHandler.WithETHClient(l1Client.L1()) + rpcHandler.WithL1Client(l1Client.L1()) } else { n.log.Warnw("L1 client not found, cannot serve starknet_getMessage RPC endpoint") } diff --git a/rpc/handlers.go b/rpc/handlers.go index 094ad849d3..4d4d35b508 100644 --- a/rpc/handlers.go +++ b/rpc/handlers.go @@ -16,13 +16,14 @@ import ( "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/feed" "github.com/NethermindEth/juno/jsonrpc" - "github.com/NethermindEth/juno/l1" "github.com/NethermindEth/juno/l1/contract" "github.com/NethermindEth/juno/sync" "github.com/NethermindEth/juno/utils" "github.com/NethermindEth/juno/vm" "github.com/ethereum/go-ethereum/accounts/abi" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/lru" + "github.com/ethereum/go-ethereum/core/types" "github.com/sourcegraph/conc" ) @@ -31,6 +32,10 @@ type Gateway interface { AddTransaction(context.Context, json.RawMessage) (json.RawMessage, error) } +type l1Client interface { + TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) +} + var ( ErrContractNotFound = &jsonrpc.Error{Code: 20, Message: "Contract not found"} ErrBlockNotFound = &jsonrpc.Error{Code: 24, Message: "Block not found"} @@ -83,7 +88,6 @@ type Handler struct { feederClient *feeder.Client vm vm.VM log utils.Logger - ethSubscriber *l1.Subscriber version string newHeads *feed.Feed[*core.Header] @@ -94,8 +98,10 @@ type Handler struct { blockTraceCache *lru.Cache[traceCacheKey, []TracedBlockTransaction] - filterLimit uint - callMaxSteps uint64 + filterLimit uint + callMaxSteps uint64 + + l1Client l1Client coreContractABI abi.ABI } @@ -139,8 +145,8 @@ func (h *Handler) WithFilterLimit(limit uint) *Handler { return h } -func (h *Handler) WithETHClient(ethSubscriber l1.Subscriber) *Handler { - h.ethSubscriber = ðSubscriber +func (h *Handler) WithL1Client(l1Client l1Client) *Handler { + h.l1Client = l1Client return h } diff --git a/rpc/l1.go b/rpc/l1.go index d5bef184d5..216237bd08 100644 --- a/rpc/l1.go +++ b/rpc/l1.go @@ -83,11 +83,11 @@ func (h *Handler) GetMessageStatus(ctx context.Context, l1TxnHash *common.Hash) } func (h *Handler) messageToL2Logs(ctx context.Context, txHash *common.Hash) ([]*common.Hash, *jsonrpc.Error) { - if h.ethSubscriber == nil { + if h.l1Client == nil { return nil, ErrL1ClientNotFound } - receipt, err := (*h.ethSubscriber).TransactionReceipt(ctx, *txHash) + receipt, err := h.l1Client.TransactionReceipt(ctx, *txHash) if err != nil { return nil, ErrTxnHashNotFound } diff --git a/rpc/l1_test.go b/rpc/l1_test.go index 81a08edb3c..67036efb88 100644 --- a/rpc/l1_test.go +++ b/rpc/l1_test.go @@ -23,7 +23,7 @@ func TestGetMessageStatus(t *testing.T) { mockReader := mocks.NewMockReader(mockCtrl) mockSubscriber := mocks.NewMockSubscriber(mockCtrl) - handler := rpc.New(mockReader, nil, nil, "", nil).WithETHClient(mockSubscriber) + handler := rpc.New(mockReader, nil, nil, "", nil).WithL1Client(mockSubscriber) l1Receipt := `{"blockHash":"0x42b045a05a24a1585aa3f2102e238e782e4ec3220a25358c74a29fe5f5a52f47","blockNumber":"0x13e6075","contractAddress":null,"cumulativeGasUsed":"0x83cba1","effectiveGasPrice":"0x42dba7811","from":"0xc3b49b03a6d9d71f8d3fa6582437374e650f3c46","gasUsed":"0x15070","logs":[{"address":"0xc662c410c0ecf747543f5ba90660f6abebd9c8c4","blockHash":"0x42b045a05a24a1585aa3f2102e238e782e4ec3220a25358c74a29fe5f5a52f47","blockNumber":"0x13e6075","data":"0x00000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000195c3c0000000000000000000000000000000000000000000000000000048c273950000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000c3b49b03a6d9d71f8d3fa6582437374e650f3c4603a1bf949fa7424b4bd48661a62ded82bc6f6e3c5f5c6d5904c07e6143187d1b0000000000000000000000000000000000000000000000000000000000000061","logIndex":"0x11e","removed":false,"topics":["0xdb80dd488acf86d17c747445b0eabb5d57c541d3bd7b6b87af987858e5066b2b","0x0000000000000000000000007ad94e71308bb65c6bc9df35cc69cc9f953d69e5","0x038862e1b15526eda31ed6fd26805c40748458db8e420cb3be3bc65c332c023b","0x03593216f3a8b22f4cf375e5486e3d13bfde9d0f26976d20ac6f653c73f7e507"],"transactionHash":"0x5780c6fe46f958a7ebf9308e6db16d819ff9e06b1e88f9e718c50cde10898f38","transactionIndex":"0x42"},{"address":"0x7ad94e71308bb65c6bc9df35cc69cc9f953d69e5","blockHash":"0x42b045a05a24a1585aa3f2102e238e782e4ec3220a25358c74a29fe5f5a52f47","blockNumber":"0x13e6075","data":"0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000c3b49b03a6d9d71f8d3fa6582437374e650f3c4603a1bf949fa7424b4bd48661a62ded82bc6f6e3c5f5c6d5904c07e6143187d1b0000000000000000000000000000000000000000000000000000000000000061","logIndex":"0x11f","removed":false,"topics":["0x6956d5f0b9182eedf6e4d4cde0f4c961c33d12daa74e00ed363bf9ab1123bb0a","0x000000000000000000000000c3b49b03a6d9d71f8d3fa6582437374e650f3c46","0x03a1bf949fa7424b4bd48661a62ded82bc6f6e3c5f5c6d5904c07e6143187d1b"],"transactionHash":"0x5780c6fe46f958a7ebf9308e6db16d819ff9e06b1e88f9e718c50cde10898f38","transactionIndex":"0x42"}],"logsBloom":"0x00000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000010000000180000000000000000000002000000000000000000000001000000000000000000000100000000000100000000080001000000020008000000000000000000000020000000010000001000000000000000100000000000000000000000000000000000000000000000020000000000000100000000000000000002000000000000000000000000000000000000100000000000000000000040000000000000000000000000100000000000000000000000100010000000000000100000000","status":"0x1","to":"0x7ad94e71308bb65c6bc9df35cc69cc9f953d69e5","transactionHash":"0x5780c6fe46f958a7ebf9308e6db16d819ff9e06b1e88f9e718c50cde10898f38","transactionIndex":"0x42","type":"0x2"}` var l1TxnReceipt types.Receipt