diff --git a/l1/l1_test.go b/l1/l1_test.go index 84a4d208ea..e9dc635bb1 100644 --- a/l1/l1_test.go +++ b/l1/l1_test.go @@ -4,6 +4,8 @@ import ( "context" "errors" "math/big" + "net" + "net/http" "testing" "time" @@ -15,6 +17,8 @@ import ( "github.com/NethermindEth/juno/l1/contract" "github.com/NethermindEth/juno/mocks" "github.com/NethermindEth/juno/utils" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/rpc" "github.com/stretchr/testify/require" "go.uber.org/mock/gomock" ) @@ -154,3 +158,63 @@ func TestEventListener(t *testing.T) { StateRoot: new(felt.Felt), }, got) } + +func newTestL1Client(service service) *rpc.Server { + server := rpc.NewServer() + if err := server.RegisterName("eth", service); err != nil { + panic(err) + } + return server +} + +type service interface { + GetBlockByNumber(ctx context.Context, number string, fullTx bool) (interface{}, error) +} + +type testService struct{} + +func (testService) GetBlockByNumber(ctx context.Context, number string, fullTx bool) (interface{}, error) { + return 100, nil +} + +type testEmptyService struct{} + +func (testEmptyService) GetBlockByNumber(ctx context.Context, number string, fullTx bool) (interface{}, error) { + return nil, nil +} + +func TestEthSubscriber_EmptyFinalisedHeight(t *testing.T) { + tc := []service{ + testService{}, + testEmptyService{}, + } + + for _, sv := range tc { + t.Run("", func(t *testing.T) { + startServer := func(addr string, service service) (*rpc.Server, net.Listener) { + srv := newTestL1Client(service) + l, err := net.Listen("tcp", addr) + if err != nil { + t.Fatal("can't listen:", err) + } + go func() { + _ = http.Serve(l, srv.WebsocketHandler([]string{"*"})) + }() + return srv, l + } + + ctx, cancel := context.WithTimeout(context.Background(), 12*time.Second) + defer cancel() + + server, listener := startServer("127.0.0.1:0", sv) + defer server.Stop() + + subscriber, err := l1.NewEthSubscriber("ws://"+listener.Addr().String(), common.Address{}) + require.NoError(t, err) + + height, err := subscriber.FinalisedHeight(ctx) + require.Equal(t, uint64(0), height) + require.Error(t, err) + }) + } +}