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

Remove receipts from the publisher #861

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
12 changes: 4 additions & 8 deletions clients/alertapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type client struct {
apiUrl string
}

func (c *client) post(path string, body interface{}, headers map[string]string, target interface{}) error {
func (c *client) post(path string, body interface{}, headers map[string]string) error {
jsonVal, err := json.Marshal(body)
if err != nil {
return err
Expand Down Expand Up @@ -47,20 +47,16 @@ func (c *client) post(path string, body interface{}, headers map[string]string,
}).Error("alert api error")
return fmt.Errorf("%d error: %s", resp.StatusCode, string(b))
}
return json.Unmarshal(b, target)
return nil
}

func (c *client) PostBatch(batch *domain.AlertBatchRequest, token string) (*domain.AlertBatchResponse, error) {
func (c *client) PostBatch(batch *domain.AlertBatchRequest, token string) error {
path := fmt.Sprintf("/batch/%s", batch.Ref)
headers := map[string]string{
"content-type": "application/json",
"Authorization": fmt.Sprintf("Bearer %s", token),
}
var resp domain.AlertBatchResponse
if err := c.post(path, batch, headers, &resp); err != nil {
return nil, err
}
return &resp, nil
return c.post(path, batch, headers)
}

func NewClient(apiUrl string) *client {
Expand Down
2 changes: 1 addition & 1 deletion clients/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type MessageClient interface {

// AlertAPIClient calls an http api on the analyzer to store alerts
type AlertAPIClient interface {
PostBatch(batch *domain.AlertBatchRequest, token string) (*domain.AlertBatchResponse, error)
PostBatch(batch *domain.AlertBatchRequest, token string) error
}

type IPAuthenticator interface {
Expand Down
75 changes: 4 additions & 71 deletions services/publisher/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ import (
"github.com/forta-network/forta-node/clients"
"github.com/forta-network/forta-node/clients/alertapi"
"github.com/forta-network/forta-node/clients/messaging"
"github.com/forta-network/forta-node/clients/storagegrpc"
"github.com/forta-network/forta-node/config"
"github.com/forta-network/forta-node/services/components/estimation"
"github.com/forta-network/forta-node/services/components/metrics"
"github.com/forta-network/forta-node/services/publisher/webhooklog"
"github.com/forta-network/forta-node/services/storage"
"github.com/forta-network/forta-node/store"
ipfsapi "github.com/ipfs/go-ipfs-api"
log "github.com/sirupsen/logrus"
Expand All @@ -59,16 +57,14 @@ type Publisher struct {
cfg PublisherConfig
contract AlertsContract
ipfs ipfs.Client
storage protocol.StorageClient
metricsAggregator *AgentMetricsAggregator
messageClient clients.MessageClient
alertClient clients.AlertAPIClient
localAlertClient LocalAlertClient

lifecycleMetrics metrics.Lifecycle

batchRefStore store.StringStore
lastReceiptStore store.StringStore
batchRefStore store.StringStore

blockTimeline estimation.BlockTimeline

Expand Down Expand Up @@ -109,9 +105,6 @@ type Publisher struct {
// LocalAlertClient sends the local alerts.
type LocalAlertClient webhook.AlertWebhookClient

// StorageClient stores content.
type StorageClient protocol.StorageClient

// EthClient interacts with the Ethereum API.
type EthClient interface {
BlockNumber(ctx context.Context) (uint64, error)
Expand Down Expand Up @@ -276,12 +269,6 @@ func (pub *Publisher) publishNextBatch(batch *protocol.AlertBatch) (published bo
},
)

var lastReceipt string
lr, err := pub.lastReceiptStore.Get()
if err == nil {
lastReceipt = lr
}

signedBatchSummary, err := security.SignBatchSummary(
pub.cfg.Key, &protocol.BatchSummary{
Batch: cid,
Expand All @@ -290,7 +277,6 @@ func (pub *Publisher) publishNextBatch(batch *protocol.AlertBatch) (published bo
BlockEnd: batch.BlockEnd,
AlertCount: batch.AlertCount,
ScannerVersion: batch.ScannerVersion,
PreviousReceipt: lastReceipt,
LatestBlockInput: batch.LatestBlockInput,
Timestamp: time.Now().UTC().Format(time.RFC3339),
},
Expand All @@ -302,7 +288,6 @@ func (pub *Publisher) publishNextBatch(batch *protocol.AlertBatch) (published bo

scannerAddr := pub.cfg.Key.Address.Hex()

var resp *domain.AlertBatchResponse
for i := 0; i < defaultBatchSendRetryTimes; i++ {
var scannerJwt string
scannerJwt, err = security.CreateScannerJWT(
Expand All @@ -314,7 +299,7 @@ func (pub *Publisher) publishNextBatch(batch *protocol.AlertBatch) (published bo
logger.WithError(err).Error("failed to create batch jwt")
return false, err
}
resp, err = pub.alertClient.PostBatch(&domain.AlertBatchRequest{
err = pub.alertClient.PostBatch(&domain.AlertBatchRequest{
Scanner: scannerAddr,
ChainID: int64(batch.ChainId),
BlockStart: int64(batch.BlockStart),
Expand All @@ -340,47 +325,6 @@ func (pub *Publisher) publishNextBatch(batch *protocol.AlertBatch) (published bo
return false, fmt.Errorf("failed to send the alert tx: %v", err)
}

if resp.SignedReceipt != nil {
// store off receipt id
if err := pub.lastReceiptStore.Put(resp.ReceiptID); err != nil {
logger.WithError(err).Error("failed to marshal receipt")
return true, err
}
logger = logger.WithFields(
log.Fields{
"receiptId": resp.ReceiptID,
},
)

// if for some reason receipt can't marshal, log and move on
b, err := json.Marshal(resp.SignedReceipt)
if err != nil {
logger.WithError(err).Error("failed to marshal receipt (not saving receipt)")
return true, nil
}
logger = logger.WithFields(log.Fields{
"receipt": string(b),
})

if pub.cfg.Config.AdvancedConfig.IPFSExperiment {
ctx, cancel := context.WithTimeout(pub.ctx, time.Second*10)
defer cancel()
putResp, err := pub.storage.Put(ctx, &protocol.PutRequest{
User: scannerAddr,
Kind: storage.KindBatchReceipt,
Bytes: b,
})
if err != nil {
logger.WithError(err).Warn("failed to store batch receipt")
} else {
logger = logger.WithFields(log.Fields{
"storedReceiptRef": putResp.ContentId,
"storedReceiptPath": putResp.ContentPath,
})
}
}
}

logger.Info("alert batch")

return true, nil
Expand Down Expand Up @@ -864,15 +808,7 @@ func NewPublisher(ctx context.Context, blockTimeline estimation.BlockTimeline, c

apiClient := alertapi.NewClient(cfg.Publish.APIURL)

var storageClient protocol.StorageClient
if !cfg.LocalModeConfig.Enable && cfg.AdvancedConfig.IPFSExperiment {
storageClient, err = storagegrpc.DialContext(ctx, fmt.Sprintf("%s:%s", config.DockerStorageContainerName, config.DefaultStoragePort))
if err != nil {
return nil, fmt.Errorf("failed to dial the storage client: %v", err)
}
}

return initPublisher(ctx, msgClient, lifecycleMetrics, apiClient, storageClient, blockTimeline,
return initPublisher(ctx, msgClient, lifecycleMetrics, apiClient, blockTimeline,
PublisherConfig{
ChainID: cfg.ChainID,
Key: key,
Expand All @@ -886,8 +822,7 @@ func NewPublisher(ctx context.Context, blockTimeline estimation.BlockTimeline, c
func initPublisher(
ctx context.Context, mc clients.MessageClient,
lifecycleMetrics metrics.Lifecycle, alertClient clients.AlertAPIClient,
storageClient StorageClient, blockTimeline estimation.BlockTimeline,
cfg PublisherConfig,
blockTimeline estimation.BlockTimeline, cfg PublisherConfig,
) (*Publisher, error) {
ipfsClient, err := ipfs.NewClient(fmt.Sprintf("http://%s:5001", config.DockerIpfsContainerName))
if err != nil {
Expand Down Expand Up @@ -929,14 +864,12 @@ func initPublisher(
ctx: ctx,
cfg: cfg,
ipfs: ipfsClient,
storage: storageClient,
metricsAggregator: NewMetricsAggregator(time.Duration(*cfg.PublisherConfig.Batch.MetricsBucketIntervalSeconds)*time.Second, int64(cfg.ChainID)),
messageClient: mc,
alertClient: alertClient,
localAlertClient: localAlertClient,
lifecycleMetrics: lifecycleMetrics,
batchRefStore: store.NewFileStringStore(path.Join(cfg.Config.FortaDir, ".last-batch")),
lastReceiptStore: store.NewFileStringStore(path.Join(cfg.Config.FortaDir, ".last-receipt")),
blockTimeline: blockTimeline,

skipEmpty: cfg.PublisherConfig.Batch.SkipEmpty,
Expand Down
Loading