Skip to content

Commit

Permalink
add client test
Browse files Browse the repository at this point in the history
  • Loading branch information
devbugging committed May 23, 2024
1 parent 9572c3d commit 05d9c56
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions services/requester/cross-spork_client_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package requester

import (
"context"
"testing"

"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/access"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"

"github.com/onflow/flow-evm-gateway/services/testutils"
Expand Down Expand Up @@ -62,3 +66,62 @@ func Test_CrossSporkClients(t *testing.T) {
require.False(t, clients.continuous())
})
}

func Test_CrossSpork(t *testing.T) {
t.Run("client", func(t *testing.T) {
past1Last := uint64(300)
past2Last := uint64(500)
currentLast := uint64(1000)
current := testutils.SetupClientForRange(501, currentLast)
past1 := testutils.SetupClientForRange(100, past1Last)
past2 := testutils.SetupClientForRange(301, past2Last)

client, err := NewCrossSporkClient(current, []access.Client{past2, past1}, zerolog.Nop())
require.NoError(t, err)

c, err := client.getClientForHeight(150)
require.NoError(t, err)
require.Equal(t, past1, c)

c, err = client.getClientForHeight(past2Last - 1)
require.NoError(t, err)
require.Equal(t, past2, c)

c, err = client.getClientForHeight(600)
require.NoError(t, err)
require.Equal(t, current, c)

c, err = client.getClientForHeight(10)
require.Nil(t, c)
require.ErrorIs(t, err, ErrOutOfRange)

require.True(t, client.IsPastSpork(200))
require.True(t, client.IsPastSpork(past1Last))
require.False(t, client.IsPastSpork(past2Last+1))
require.False(t, client.IsPastSpork(600))

_, err = client.ExecuteScriptAtBlockHeight(context.Background(), 20, []byte{}, nil)
require.ErrorIs(t, err, ErrOutOfRange)

_, err = client.GetBlockHeaderByHeight(context.Background(), 20)
require.ErrorIs(t, err, ErrOutOfRange)

_, _, err = client.SubscribeEventsByBlockHeight(context.Background(), 20, flow.EventFilter{}, nil)
require.ErrorIs(t, err, ErrOutOfRange)

height, err := client.GetLatestHeightForSpork(context.Background(), past2Last-10)
require.NoError(t, err)
require.Equal(t, past2Last, height)

height, err = client.GetLatestHeightForSpork(context.Background(), past1Last-10)
require.NoError(t, err)
require.Equal(t, past1Last, height)

height, err = client.GetLatestHeightForSpork(context.Background(), currentLast-10)
require.NoError(t, err)
require.Equal(t, currentLast, height)

_, err = client.GetLatestHeightForSpork(context.Background(), 10)
require.ErrorIs(t, err, ErrOutOfRange)
})
}

0 comments on commit 05d9c56

Please sign in to comment.