Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a timeline of highest blocks #136

Merged
merged 28 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b324762
implement a timeline of highest blocks
canercidam Nov 20, 2022
6362833
add ability to disable logs
canercidam Nov 21, 2022
c84a886
Merge branch 'master' into caner/block-timeline
canercidam Oct 16, 2023
1ab11b7
add functionality to calculate lag in a given minute
canercidam Oct 16, 2023
34a40d6
add experiment as optional test
canercidam Oct 16, 2023
9f891ea
add distance number per chain
canercidam Oct 16, 2023
151b354
reduce optimism threshold
canercidam Oct 16, 2023
ff76c57
add method to check timeline size
canercidam Oct 16, 2023
a321998
log delay
canercidam Oct 17, 2023
56cab57
return delay
canercidam Oct 17, 2023
1f88ce4
fix the timeline construction
canercidam Oct 17, 2023
81372b6
remove distance stuff
canercidam Oct 17, 2023
3ed77f8
add sorting and fix the experiment test
canercidam Oct 18, 2023
d3a368a
remove the size method
canercidam Oct 18, 2023
44281ae
incorporate the score estimation
canercidam Oct 18, 2023
a75a2c5
remove redundant method and increase coverage
canercidam Oct 18, 2023
bf10332
fix tests
canercidam Oct 18, 2023
1105118
fix issues
canercidam Oct 18, 2023
70dfdf5
shorten the tests
canercidam Oct 18, 2023
ba668cb
fix typo
canercidam Oct 18, 2023
4ae9aa5
one more short test
canercidam Oct 18, 2023
9946960
add one more short test
canercidam Oct 19, 2023
55b69f6
try changing cache key
canercidam Oct 19, 2023
e77a27a
reset cache key
canercidam Oct 19, 2023
7ab4acc
add skip to timeline experiment
canercidam Oct 19, 2023
cbfba0f
Merge branch 'master' into caner/block-timeline
canercidam Oct 19, 2023
12c6c82
fix tests
canercidam Oct 19, 2023
3dfb95f
remove some debug lines
canercidam Oct 19, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,10 @@ mocks:
$(MOCKGEN) -source manifest/client.go -destination manifest/mocks/mock_client.go
$(MOCKGEN) -source clients/graphql/client.go -destination clients/mocks/mock_graphql_client.go
$(MOCKGEN) -source utils/ethutils/iterator.go -destination utils/ethutils/mocks/mock_iterator.go
$(MOCKGEN) -source inspect/proxy_api.go -destination inspect/mocks/mock_proxy_api.go

.PHONY: test
test:
go test -v -count=1 -coverprofile=coverage.out ./...
go test -v -count=1 -short -coverprofile=coverage.out ./...

.PHONY: coverage
coverage:
Expand Down
26 changes: 22 additions & 4 deletions ethereum/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,28 @@
log "github.com/sirupsen/logrus"
)

// EthClient is the original interface from go-ethereum.
type EthClient interface {
ethereum.ChainReader
ethereum.ChainStateReader
ethereum.TransactionReader
ethereum.LogFilterer
ethereum.ContractCaller
BlockNumber(ctx context.Context) (uint64, error)
BlockByNumber(ctx context.Context, blockNum *big.Int) (*types.Block, error)
}

// RPCClient is a wrapper implementation of the RPC client.
type RPCClient interface {
Close()
Call(result interface{}, method string, args ...interface{}) error
CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error
Subscribe(ctx context.Context, channel interface{}, args ...interface{}) (domain.ClientSubscription, error)
}

// Subscriber subscribes to Ethereum namespaces.
type Subscriber interface {
RPCClient
Subscribe(ctx context.Context, namespace string, channel interface{}, args ...interface{}) (domain.ClientSubscription, error)
}

// Client is an interface encompassing all ethereum actions
Expand Down Expand Up @@ -82,6 +99,7 @@
type streamEthClient struct {
apiName string
rpcClient RPCClient
subscriber Subscriber
retryInterval time.Duration
isWebsocket bool

Expand Down Expand Up @@ -331,7 +349,7 @@
log.Debug("subscribing to blockchain head")
recvCh := make(chan *types.Header)
sendCh := make(chan *types.Header)
sub, err := e.rpcClient.Subscribe(ctx, recvCh, "newHeads")
sub, err := e.subscriber.Subscribe(ctx, "eth", recvCh, "newHeads")
if err != nil {
return nil, fmt.Errorf("failed to subscribe: %v", err)
}
Expand Down Expand Up @@ -384,8 +402,8 @@
*rpc.Client
}

func (rc *rpcClient) Subscribe(ctx context.Context, channel interface{}, args ...interface{}) (domain.ClientSubscription, error) {
sub, err := rc.EthSubscribe(ctx, channel, args...)
func (rc *rpcClient) Subscribe(ctx context.Context, namespace string, channel interface{}, args ...interface{}) (domain.ClientSubscription, error) {
sub, err := rc.Subscribe(ctx, namespace, channel, args...)

Check failure on line 406 in ethereum/client.go

View workflow job for this annotation

GitHub Actions / Validate Go code

SA5007: infinite recursive call (staticcheck)
return sub, err
}

Expand Down
14 changes: 9 additions & 5 deletions ethereum/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@ const testBlockHash = "0x4fc0862e76691f5312964883954d5c2db35e2b8f7a4f191775a4f50

var testErr = errors.New("test err")

func initClient(t *testing.T) (*streamEthClient, *mocks.MockRPCClient, context.Context) {
func initClient(t *testing.T) (*streamEthClient, *mocks.MockRPCClient, *mocks.MockSubscriber, context.Context) {
minBackoff = 1 * time.Millisecond
maxBackoff = 1 * time.Millisecond
ctx := context.Background()
ctrl := gomock.NewController(t)
client := mocks.NewMockRPCClient(ctrl)
subscriber := mocks.NewMockSubscriber(ctrl)

return &streamEthClient{rpcClient: client}, client, ctx
return &streamEthClient{rpcClient: client, subscriber: subscriber}, client, subscriber, ctx
}

func TestEthClient_BlockByHash(t *testing.T) {
r := require.New(t)

ethClient, client, ctx := initClient(t)
ethClient, client, _, ctx := initClient(t)
hash := testBlockHash
// verify retry
client.EXPECT().CallContext(gomock.Any(), gomock.Any(), blocksByHash, testBlockHash).Return(testErr).Times(1)
Expand All @@ -50,10 +51,10 @@ func TestEthClient_BlockByHash(t *testing.T) {
func TestEthClient_SubscribeToHeader_Err(t *testing.T) {
r := require.New(t)

ethClient, client, ctx := initClient(t)
ethClient, _, subscriber, ctx := initClient(t)
sub := mock_domain.NewMockClientSubscription(gomock.NewController(t))

client.EXPECT().Subscribe(gomock.Any(), gomock.Any(), "newHeads").Return(sub, nil).Times(2)
subscriber.EXPECT().Subscribe(gomock.Any(), "eth", gomock.Any(), "newHeads").Return(sub, nil).Times(2)
errCh := make(chan error, 1)
errCh <- errors.New("subscription encountered some error")
sub.EXPECT().Err().Return(errCh).Times(2)
Expand All @@ -64,9 +65,12 @@ func TestEthClient_SubscribeToHeader_Err(t *testing.T) {

headerCh, err = ethClient.SubscribeToHead(ctx)
r.NoError(err)
var blocked bool
select {
case <-time.After(time.Second):
// should continue from here
blocked = true
case <-headerCh: // should block
}
r.True(blocked)
}
Loading
Loading