From 34986dbfcc81e21422d6d3ac7b880a7d0666e1d6 Mon Sep 17 00:00:00 2001 From: Eric Warehime Date: Fri, 12 Aug 2022 08:19:09 -0700 Subject: [PATCH] Use config to control noop exporter round start (#1176) * Use config to control noop exporter round start --- exporters/noop/noop_exporter.go | 14 ++++++++++---- exporters/noop/noop_exporter_config.go | 7 +++++++ exporters/noop/noop_exporter_test.go | 22 ++++++++++++++++++---- 3 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 exporters/noop/noop_exporter_config.go diff --git a/exporters/noop/noop_exporter.go b/exporters/noop/noop_exporter.go index a06018a13..d09afbfdb 100644 --- a/exporters/noop/noop_exporter.go +++ b/exporters/noop/noop_exporter.go @@ -1,11 +1,13 @@ package noop import ( + "fmt" "github.com/algorand/go-algorand/data/bookkeeping" "github.com/algorand/indexer/data" "github.com/algorand/indexer/exporters" "github.com/algorand/indexer/plugins" "github.com/sirupsen/logrus" + "gopkg.in/yaml.v3" ) // `noopExporter`s will function without ever erroring. This means they will also process out of order blocks @@ -14,7 +16,7 @@ import ( // The `noopExporter` will maintain `Round` state according to the round of the last block it processed. type noopExporter struct { round uint64 - cfg plugins.PluginConfig + cfg ExporterConfig } var noopExporterMetadata exporters.ExporterMetadata = exporters.ExporterMetadata{ @@ -30,7 +32,6 @@ type Constructor struct{} func (c *Constructor) New() exporters.Exporter { return &noopExporter{ round: 0, - cfg: "", } } @@ -38,12 +39,17 @@ func (exp *noopExporter) Metadata() exporters.ExporterMetadata { return noopExporterMetadata } -func (exp *noopExporter) Init(_ plugins.PluginConfig, _ *logrus.Logger) error { +func (exp *noopExporter) Init(cfg plugins.PluginConfig, _ *logrus.Logger) error { + if err := yaml.Unmarshal([]byte(cfg), &exp.cfg); err != nil { + return fmt.Errorf("init failure in unmarshalConfig: %v", err) + } + exp.round = exp.cfg.Round return nil } func (exp *noopExporter) Config() plugins.PluginConfig { - return exp.cfg + ret, _ := yaml.Marshal(exp.cfg) + return plugins.PluginConfig(ret) } func (exp *noopExporter) Close() error { diff --git a/exporters/noop/noop_exporter_config.go b/exporters/noop/noop_exporter_config.go new file mode 100644 index 000000000..5a39b2533 --- /dev/null +++ b/exporters/noop/noop_exporter_config.go @@ -0,0 +1,7 @@ +package noop + +// ExporterConfig specific to the noop exporter +type ExporterConfig struct { + // Optionally specify the round to start on + Round uint64 `yaml:"round"` +} diff --git a/exporters/noop/noop_exporter_test.go b/exporters/noop/noop_exporter_test.go index 5095759d6..296695a75 100644 --- a/exporters/noop/noop_exporter_test.go +++ b/exporters/noop/noop_exporter_test.go @@ -7,6 +7,7 @@ import ( "github.com/algorand/indexer/plugins" "github.com/sirupsen/logrus/hooks/test" "github.com/stretchr/testify/assert" + "gopkg.in/yaml.v3" "testing" ) @@ -30,15 +31,21 @@ func TestExporterMetadata(t *testing.T) { assert.Equal(t, noopExporterMetadata.ExpDeprecated, meta.Deprecated()) } -func TestExporterConnect(t *testing.T) { +func TestExporterInit(t *testing.T) { assert.NoError(t, ne.Init("", nil)) } func TestExporterConfig(t *testing.T) { - assert.Equal(t, plugins.PluginConfig(""), ne.Config()) + defaultConfig := &ExporterConfig{} + expected, err := yaml.Marshal(defaultConfig) + if err != nil { + t.Fatalf("unable to Marshal default noop.ExporterConfig: %v", err) + } + assert.NoError(t, ne.Init("", nil)) + assert.Equal(t, plugins.PluginConfig(expected), ne.Config()) } -func TestExporterDisconnect(t *testing.T) { +func TestExporterClose(t *testing.T) { assert.NoError(t, ne.Close()) } @@ -46,13 +53,20 @@ func TestExporterHandleGenesis(t *testing.T) { assert.NoError(t, ne.HandleGenesis(bookkeeping.Genesis{})) } +func TestExporterStartRound(t *testing.T) { + assert.NoError(t, ne.Init("", nil)) + assert.Equal(t, uint64(0), ne.Round()) + assert.NoError(t, ne.Init("round: 55", nil)) + assert.Equal(t, uint64(55), ne.Round()) + +} + func TestExporterRoundReceive(t *testing.T) { eData := data.BlockData{ BlockHeader: bookkeeping.BlockHeader{ Round: 5, }, } - assert.Equal(t, uint64(0), ne.Round()) assert.NoError(t, ne.Receive(eData)) assert.Equal(t, uint64(6), ne.Round()) }