Skip to content

Commit

Permalink
Add l1Client interface in rpc package
Browse files Browse the repository at this point in the history
  • Loading branch information
IronGauntlets committed Nov 7, 2024
1 parent fba5243 commit 9612e7a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
18 changes: 12 additions & 6 deletions rpc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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"}
Expand Down Expand Up @@ -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]
Expand All @@ -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
}

Expand Down Expand Up @@ -139,8 +145,8 @@ func (h *Handler) WithFilterLimit(limit uint) *Handler {
return h
}

func (h *Handler) WithETHClient(ethSubscriber l1.Subscriber) *Handler {
h.ethSubscriber = &ethSubscriber
func (h *Handler) WithL1Client(l1Client l1Client) *Handler {
h.l1Client = l1Client
return h
}

Expand Down
4 changes: 2 additions & 2 deletions rpc/l1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion rpc/l1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9612e7a

Please sign in to comment.