Skip to content

Commit

Permalink
l1: add EthSubscriber test
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Jun 27, 2024
1 parent 22794c2 commit 303c6e5
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions l1/l1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"errors"
"math/big"
"net"
"net/http"
"testing"
"time"

Expand All @@ -15,6 +17,9 @@ 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/core/types"
"github.com/ethereum/go-ethereum/rpc"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)
Expand Down Expand Up @@ -154,3 +159,85 @@ 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) {
blockHeight := big.NewInt(100)
return types.Header{
ParentHash: common.Hash{},
UncleHash: common.Hash{},
Root: common.Hash{},
TxHash: common.Hash{},
ReceiptHash: common.Hash{},
Bloom: types.Bloom{},
Difficulty: big.NewInt(0),
Number: blockHeight,
GasLimit: 0,
GasUsed: 0,
Time: 0,
Extra: []byte{},
}, nil
}

type testEmptyService struct{}

func (testEmptyService) GetBlockByNumber(ctx context.Context, number string, fullTx bool) (interface{}, error) {
return nil, nil
}

func TestEthSubscriber_FinalisedHeight(t *testing.T) {
tests := map[string]struct {
service service
expected uint64
}{
"testService": {
service: testService{},
expected: 100,
},
"testEmptyService": {
service: testEmptyService{},
expected: 0,
},
}

for name, test := range tests {
t.Run(name, 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", test.service)
defer server.Stop()

subscriber, err := l1.NewEthSubscriber("ws://"+listener.Addr().String(), common.Address{})
require.NoError(t, err)

height, _ := subscriber.FinalisedHeight(ctx)
require.Equal(t, test.expected, height)
})
}
}

0 comments on commit 303c6e5

Please sign in to comment.