Skip to content

Commit

Permalink
Rename cbe (Combined Block Event) to BlockData
Browse files Browse the repository at this point in the history
  • Loading branch information
dkeysil committed Mar 12, 2024
1 parent d12aca9 commit c82476b
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package r2cbe
package blocksdata

import (
"encoding/json"
Expand All @@ -13,15 +13,15 @@ import (
"google.golang.org/protobuf/proto"
)

type combinedBlockEventsClient struct {
type blocksDataClient struct {
dispatcherURL *url.URL
dispatcherPath string
}

func NewCombinedBlockEventsClient(dispatcherURL string) *combinedBlockEventsClient {
func NewCombinedBlockEventsClient(dispatcherURL string) *blocksDataClient {
u, _ := url.Parse(dispatcherURL)

return &combinedBlockEventsClient{
return &blocksDataClient{
dispatcherURL: u,
dispatcherPath: u.Path,
}
Expand All @@ -33,7 +33,7 @@ type PresignedURLItem struct {
ExpiresAt int64 `json:"expiresAt"`
}

func (c *combinedBlockEventsClient) GetCombinedBlockEvents(bucket int64) (_ *protocol.CombinedBlockEvents, err error) {
func (c *blocksDataClient) GetBlocksData(bucket int64) (_ *protocol.BlocksData, err error) {
c.dispatcherURL.Path, err = url.JoinPath(c.dispatcherPath, fmt.Sprintf("%d", bucket))
if err != nil {
return nil, err
Expand Down Expand Up @@ -66,12 +66,12 @@ func (c *combinedBlockEventsClient) GetCombinedBlockEvents(bucket int64) (_ *pro
return nil, err
}

var events protocol.CombinedBlockEvents
var blocks protocol.BlocksData

err = proto.Unmarshal(b, &events)
err = proto.Unmarshal(b, &blocks)
if err != nil {
return nil, err
}

return &events, nil
return &blocks, nil
}
4 changes: 2 additions & 2 deletions clients/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ type IPAuthenticator interface {
FindAgentByContainerName(containerName string) (*config.AgentConfig, error)
}

type CombinedBlockEventsClient interface {
GetCombinedBlockEvents(bucket int64) (*protocol.CombinedBlockEvents, error)
type BlocksDataClient interface {
GetBlocksData(bucket int64) (*protocol.BlocksData, error)
}
36 changes: 18 additions & 18 deletions clients/mocks/mock_clients.go

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

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ require (
github.com/andybalholm/brotli v1.1.0
github.com/docker/docker v1.6.2
github.com/docker/go-connections v0.4.0
github.com/forta-network/forta-core-go v0.0.0-20240311161131-92809f963403
github.com/forta-network/forta-core-go v0.0.0-20240312150426-832d1633b86b
github.com/prometheus/client_golang v1.14.0
github.com/prometheus/client_model v0.3.0
github.com/prometheus/common v0.39.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ github.com/flynn/noise v1.0.0/go.mod h1:xbMo+0i6+IGbYdJhF31t2eR1BIU0CYc12+BNAKwU
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/forta-network/forta-core-go v0.0.0-20240311161131-92809f963403 h1:VM2aEAz6qcXOPgEx+yw7hZ8fg/zETn1oaXg+6/4gwc8=
github.com/forta-network/forta-core-go v0.0.0-20240311161131-92809f963403/go.mod h1:iNehCWOypwVeO8b1GKmsrEWReHTvO5qw8SsGvZsBINo=
github.com/forta-network/forta-core-go v0.0.0-20240312150138-fc6cb6ef61e9 h1:4zxIApnQO2sdLq9GsTBUo2LbNwmikVjLykE68Mri6iY=
github.com/forta-network/forta-core-go v0.0.0-20240312150138-fc6cb6ef61e9/go.mod h1:iNehCWOypwVeO8b1GKmsrEWReHTvO5qw8SsGvZsBINo=
github.com/forta-network/forta-core-go v0.0.0-20240312150426-832d1633b86b h1:Y4XVUlyBLrhO+C3MfQYt3Rcl5JrY6WtQTytDDnute3A=
github.com/forta-network/forta-core-go v0.0.0-20240312150426-832d1633b86b/go.mod h1:iNehCWOypwVeO8b1GKmsrEWReHTvO5qw8SsGvZsBINo=
github.com/forta-network/go-multicall v0.0.0-20230609185354-1436386c6707 h1:f6I7K43i2m6AwHSsDxh0Mf3qFzYt8BKnabSl/zGFmh0=
github.com/forta-network/go-multicall v0.0.0-20230609185354-1436386c6707/go.mod h1:nqTUF1REklpWLZ/M5HfzqhSHNz4dPVKzJvbLziqTZpw=
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
Expand Down
6 changes: 3 additions & 3 deletions services/json-rpc/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func NewCache(expire time.Duration) *inMemory {
}
}

func (c *inMemory) Append(events *protocol.CombinedBlockEvents) {
for _, event := range events.Events {
func (c *inMemory) Append(blocksData *protocol.BlocksData) {
for _, event := range blocksData.Blocks {
chainID := event.ChainID

// eth_blockNumber
Expand All @@ -44,7 +44,7 @@ func (c *inMemory) Append(events *protocol.CombinedBlockEvents) {
method = "eth_getBlockByNumber"
params = fmt.Sprintf(`["%s", "true"]`, event.Block.Number)

block := domain.BlockFromCombinedBlockEvent(event)
block := domain.BlockFromBlockData(event)
c.cache.SetDefault(cacheKey(chainID, method, params), block)

// eth_getLogs
Expand Down
12 changes: 6 additions & 6 deletions services/json-rpc/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func TestCache(t *testing.T) {
cache := NewCache(time.Millisecond * 500)

cache.Append(events)
cache.Append(blocks)

blockNumber, ok := cache.Get(1, "eth_blockNumber", "[]")
assert.True(t, ok)
Expand All @@ -28,11 +28,11 @@ func TestCache(t *testing.T) {
assert.Empty(t, blockNumber)
}

var events = &protocol.CombinedBlockEvents{
Events: []*protocol.CombinedBlockEvent{
var blocks = &protocol.BlocksData{
Blocks: []*protocol.BlockData{
{
ChainID: 1,
Block: &protocol.CombinedBlock{
Block: &protocol.BlockWithTransactions{
Hash: "0xaaaa",
Number: "1",
Transactions: []*protocol.Transaction{
Expand Down Expand Up @@ -63,7 +63,7 @@ var events = &protocol.CombinedBlockEvents{
},
{
ChainID: 2,
Block: &protocol.CombinedBlock{
Block: &protocol.BlockWithTransactions{
Hash: "0xffff",
Number: "100",
Transactions: []*protocol.Transaction{
Expand All @@ -83,7 +83,7 @@ var events = &protocol.CombinedBlockEvents{
},
{
ChainID: 2,
Block: &protocol.CombinedBlock{
Block: &protocol.BlockWithTransactions{
Hash: "0xfffd",
Number: "101",
Transactions: []*protocol.Transaction{
Expand Down
10 changes: 5 additions & 5 deletions services/json-rpc/cache/json_rpc_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/forta-network/forta-core-go/utils"
"github.com/forta-network/forta-node/clients"
"github.com/forta-network/forta-node/clients/r2cbe"
"github.com/forta-network/forta-node/clients/blocksdata"
"github.com/forta-network/forta-node/config"
log "github.com/sirupsen/logrus"
)
Expand All @@ -28,7 +28,7 @@ type JsonRpcCache struct {

cache *inMemory

cbeClient clients.CombinedBlockEventsClient
blocksDataClient clients.BlocksDataClient
}

func NewJsonRpcCache(ctx context.Context, cfg config.JsonRpcCacheConfig) (*JsonRpcCache, error) {
Expand All @@ -52,7 +52,7 @@ func (c *JsonRpcCache) Start() error {
Handler: c.Handler(),
}

c.cbeClient = r2cbe.NewCombinedBlockEventsClient(c.cfg.DispatcherURL)
c.blocksDataClient = blocksdata.NewCombinedBlockEventsClient(c.cfg.DispatcherURL)

utils.GoListenAndServe(c.server)

Expand Down Expand Up @@ -133,14 +133,14 @@ func (c *JsonRpcCache) pollEvents() {
time.Sleep(1 * time.Second)
log.Info("Polling for combined block events", "bucket", bucket)

events, err := c.cbeClient.GetCombinedBlockEvents(bucket)
events, err := c.blocksDataClient.GetBlocksData(bucket)
if err != nil {
log.WithError(err).Error("Failed to get combined block events", "bucket", bucket)
bucket = time.Now().Truncate(time.Second * 10).Unix()
continue
}

log.Info("Added combined block events to local cache", "bucket", bucket, "events", len(events.Events))
log.Info("Added combined block events to local cache", "bucket", bucket, "events", len(events.Blocks))
c.cache.Append(events)
bucket += 10 // 10 seconds
}
Expand Down
6 changes: 3 additions & 3 deletions services/json-rpc/cache/json_rpc_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ var (

func TestJsonRpcCache(t *testing.T) {
ctrl := gomock.NewController(t)
eventsClient := mock_clients.NewMockCombinedBlockEventsClient(ctrl)
blocksDataClient := mock_clients.NewMockBlocksDataClient(ctrl)
authenticator := mock_clients.NewMockIPAuthenticator(ctrl)

count := 0
appended := make(chan struct{})
eventsClient.EXPECT().GetCombinedBlockEvents(gomock.Any()).Return(events, nil).Do(func(any) {
blocksDataClient.EXPECT().GetBlocksData(gomock.Any()).Return(blocks, nil).Do(func(any) {
count++
if count == 2 {
close(appended)
Expand All @@ -40,7 +40,7 @@ func TestJsonRpcCache(t *testing.T) {
jrpCache := JsonRpcCache{
ctx: context.TODO(),
botAuthenticator: authenticator,
cbeClient: eventsClient,
blocksDataClient: blocksDataClient,
cfg: config.JsonRpcCacheConfig{
CacheExpirePeriodSeconds: 300,
},
Expand Down

0 comments on commit c82476b

Please sign in to comment.