Skip to content

Commit

Permalink
feat: Add get latest to BlockByNumber (#583)
Browse files Browse the repository at this point in the history
Co-authored-by: Rob De Feo <robdefeo@gmail.com>
  • Loading branch information
rickynyairo and robdefeo committed Mar 8, 2020
1 parent e49f9cd commit 78364a4
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 44 deletions.
7 changes: 4 additions & 3 deletions cmd/indexer/internal/clients/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import "context"

//go:generate mockgen -source=block.go -package=clientstest -destination=./clientstest/block_mock.go

// BlockByNumber is a client that gets the blocks by number
type BlockByNumber interface {
Get(ctx context.Context, blockNo uint64) (blk interface{}, err error)
// Block is a client that gets the blocks by number and the latest block
type Block interface {
BlockByNumber(ctx context.Context, blockNo uint64) (blk interface{}, err error)
LatestBlockNumber(ctx context.Context) (blockNo uint64, err error)
}
49 changes: 32 additions & 17 deletions cmd/indexer/internal/clients/clientstest/block_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion cmd/indexer/internal/ethereum/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,23 @@ type Client struct {
client *ethclient.Client
}

func (c *Client) Get(ctx context.Context, blockNo uint64) (blk interface{}, err error) {
func (c *Client) BlockByNumber(ctx context.Context, blockNo uint64) (blk interface{}, err error) {
return c.client.BlockByNumber(ctx, big.NewInt(int64(blockNo)))
}

func (c *Client) NetworkID(ctx context.Context) (*big.Int, error) {
return c.client.NetworkID(ctx)
}

func (c *Client) LatestBlockNumber(ctx context.Context) (blockNo uint64, err error) {
hdr, err := c.client.HeaderByNumber(ctx, nil)
if err != nil {
return 0, err
}

return hdr.Number.Uint64(), nil
}

func (c *Client) GetLatest(ctx context.Context) (blk interface{}, err error) {
return c.client.BlockByNumber(ctx, nil)
}
104 changes: 100 additions & 4 deletions cmd/indexer/internal/ethereum/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestNewRPC(t *testing.T) {
}
}

func TestClient_Get(t *testing.T) {
func TestClient_BlockByNumber(t *testing.T) {
server := httptest.NewServer(nil)
defer server.Close()
type fields struct {
Expand Down Expand Up @@ -91,13 +91,13 @@ func TestClient_Get(t *testing.T) {
c := &Client{
client: tt.fields.client,
}
gotBlk, err := c.Get(tt.args.ctx, tt.args.blockNo)
gotBlk, err := c.BlockByNumber(tt.args.ctx, tt.args.blockNo)
if (err != nil) != tt.wantErr {
t.Errorf("Client.Get() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("Client.BlockByNumber() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !assert.Equal(t, tt.wantBlk, gotBlk) {
t.Errorf("Client.Get() = %v, want %v", gotBlk, tt.wantBlk)
t.Errorf("Client.BlockByNumber() = %v, want %v", gotBlk, tt.wantBlk)
}
})
}
Expand Down Expand Up @@ -150,3 +150,99 @@ func TestClient_NetworkID(t *testing.T) {
})
}
}

func TestClient_LatestBlockNumber(t *testing.T) {
server := httptest.NewServer(nil)
defer server.Close()
type fields struct {
client *ethclient.Client
}
type args struct {
ctx context.Context
}
tests := []struct {
name string
fields fields
args args
wantNo uint64
wantErr bool
}{
{
"err",
fields{
func() *ethclient.Client {
c, _ := ethclient.Dial(server.URL)
return c
}(),
},
args{
context.Background(),
},
(uint64)(0),
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Client{
client: tt.fields.client,
}
gotNo, err := c.LatestBlockNumber(tt.args.ctx)
if (err != nil) != tt.wantErr {
t.Errorf("Client.LatestBlockNumber() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !assert.Equal(t, tt.wantNo, gotNo) {
t.Errorf("Client.LatestBlockNumber() = %v, want %v", gotNo, tt.wantNo)
}
})
}
}

func TestClient_GetLatest(t *testing.T) {
server := httptest.NewServer(nil)
defer server.Close()
type fields struct {
client *ethclient.Client
}
type args struct {
ctx context.Context
}
tests := []struct {
name string
fields fields
args args
wantBlk interface{}
wantErr bool
}{
{
"err",
fields{
func() *ethclient.Client {
c, _ := ethclient.Dial(server.URL)
return c
}(),
},
args{
context.Background(),
},
(*types.Block)(nil),
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Client{
client: tt.fields.client,
}
gotBlk, err := c.GetLatest(tt.args.ctx)
if (err != nil) != tt.wantErr {
t.Errorf("Client.GetLatest() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !assert.Equal(t, tt.wantBlk, gotBlk) {
t.Errorf("Client.GetLatest() = %v, want %v", gotBlk, tt.wantBlk)
}
})
}
}
6 changes: 3 additions & 3 deletions cmd/indexer/internal/processor/sequential.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ type Sequential struct {

syncStore datastore.SyncStore
blockProcessor actions.Block
blockClient clients.BlockByNumber
blockClient clients.Block
}

func NewSequential(protocol, network string, store datastore.SyncStore, proc actions.Block, client clients.BlockByNumber) *Sequential {
func NewSequential(protocol, network string, store datastore.SyncStore, proc actions.Block, client clients.Block) *Sequential {
return &Sequential{
protocol: protocol,
network: network,
Expand All @@ -36,7 +36,7 @@ func (s *Sequential) NextBlock(ctx context.Context) error {

fmt.Println("block number: ", blkNo)

blk, err := s.blockClient.Get(ctx, blkNo)
blk, err := s.blockClient.BlockByNumber(ctx, blkNo)
if err != nil {
return err
}
Expand Down
32 changes: 16 additions & 16 deletions cmd/indexer/internal/processor/sequential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestSequential_NextBlock(t *testing.T) {
network string
store datastore.SyncStore
proc actions.Block
client clients.BlockByNumber
client clients.Block
}
tests := []struct {
name string
Expand All @@ -48,9 +48,9 @@ func TestSequential_NextBlock(t *testing.T) {
proc.EXPECT().Run(context.Background(), protocols.Ethereum, ethereum.Mainnet, "block").Return(nil).Times(1)
return proc
}(),
func() clients.BlockByNumber {
client := clientstest.NewMockBlockByNumber(mockCtrl)
client.EXPECT().Get(context.Background(), uint64(1)).Return("block", nil).Times(1)
func() clients.Block {
client := clientstest.NewMockBlock(mockCtrl)
client.EXPECT().BlockByNumber(context.Background(), uint64(1)).Return("block", nil).Times(1)
return client
}(),
},
Expand All @@ -73,9 +73,9 @@ func TestSequential_NextBlock(t *testing.T) {
proc.EXPECT().Run(context.Background(), protocols.Ethereum, ethereum.Mainnet, "block").Return(nil).Times(0)
return proc
}(),
func() clients.BlockByNumber {
client := clientstest.NewMockBlockByNumber(mockCtrl)
client.EXPECT().Get(context.Background(), uint64(2)).Return("block", nil).Times(0)
func() clients.Block {
client := clientstest.NewMockBlock(mockCtrl)
client.EXPECT().BlockByNumber(context.Background(), uint64(2)).Return("block", nil).Times(0)
return client
}(),
},
Expand All @@ -98,9 +98,9 @@ func TestSequential_NextBlock(t *testing.T) {
proc.EXPECT().Run(context.Background(), protocols.Ethereum, ethereum.Mainnet, "block").Return(nil).Times(0)
return proc
}(),
func() clients.BlockByNumber {
client := clientstest.NewMockBlockByNumber(mockCtrl)
client.EXPECT().Get(context.Background(), uint64(1)).Return(nil, errors.Errorf("error getting block")).Times(1)
func() clients.Block {
client := clientstest.NewMockBlock(mockCtrl)
client.EXPECT().BlockByNumber(context.Background(), uint64(1)).Return(nil, errors.Errorf("error getting block")).Times(1)
return client
}(),
},
Expand All @@ -123,9 +123,9 @@ func TestSequential_NextBlock(t *testing.T) {
proc.EXPECT().Run(context.Background(), protocols.Ethereum, ethereum.Mainnet, "block").Return(errors.Errorf("error running processor")).Times(1)
return proc
}(),
func() clients.BlockByNumber {
client := clientstest.NewMockBlockByNumber(mockCtrl)
client.EXPECT().Get(context.Background(), uint64(1)).Return("block", nil).Times(1)
func() clients.Block {
client := clientstest.NewMockBlock(mockCtrl)
client.EXPECT().BlockByNumber(context.Background(), uint64(1)).Return("block", nil).Times(1)
return client
}(),
},
Expand All @@ -148,9 +148,9 @@ func TestSequential_NextBlock(t *testing.T) {
proc.EXPECT().Run(context.Background(), protocols.Ethereum, ethereum.Mainnet, "block").Return(nil).Times(1)
return proc
}(),
func() clients.BlockByNumber {
client := clientstest.NewMockBlockByNumber(mockCtrl)
client.EXPECT().Get(context.Background(), uint64(1)).Return("block", nil).Times(1)
func() clients.Block {
client := clientstest.NewMockBlock(mockCtrl)
client.EXPECT().BlockByNumber(context.Background(), uint64(1)).Return("block", nil).Times(1)
return client
}(),
},
Expand Down

0 comments on commit 78364a4

Please sign in to comment.