From 6dc7b938f9b4027db747b10f331828cb4b5eacd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sun, 26 Nov 2023 10:19:45 +0000 Subject: [PATCH] vochain/indexer: invert "live results" option This matches IndexerCfg.IgnoreLiveResults and Indexer.ignoreLiveResults, so the logic is now overall simpler and we don't negate twice. Moreover, the vast majority of calls do want to count live results, such as all the calls to newTestIndexer, so those can default to false. --- service/indexer.go | 2 +- test/testcommon/vochain.go | 2 +- vochain/indexer/archive_test.go | 2 +- vochain/indexer/bench_test.go | 6 ++--- vochain/indexer/indexer.go | 6 ++--- vochain/indexer/indexer_test.go | 42 ++++++++++++++++----------------- vocone/vocone.go | 2 +- 7 files changed, 31 insertions(+), 31 deletions(-) diff --git a/service/indexer.go b/service/indexer.go index 4b2c7fcdb..4eb77c6da 100644 --- a/service/indexer.go +++ b/service/indexer.go @@ -12,7 +12,7 @@ func (vs *VocdoniService) VochainIndexer() error { log.Info("creating vochain indexer service") var err error vs.Indexer, err = indexer.New(filepath.Join(vs.Config.DataDir, "indexer"), vs.App, - indexer.Options{CountLiveResults: !vs.Config.Indexer.IgnoreLiveResults}, + indexer.Options{IgnoreLiveResults: vs.Config.Indexer.IgnoreLiveResults}, ) if err != nil { return err diff --git a/test/testcommon/vochain.go b/test/testcommon/vochain.go index 6f4f3d0f0..d73eb349f 100644 --- a/test/testcommon/vochain.go +++ b/test/testcommon/vochain.go @@ -105,7 +105,7 @@ func NewVochainStateWithProcess(tb testing.TB) *state.State { func NewMockIndexer(tb testing.TB, vnode *vochain.BaseApplication) *indexer.Indexer { tb.Log("starting vochain indexer") - sc, err := indexer.New(tb.TempDir(), vnode, indexer.Options{CountLiveResults: true}) + sc, err := indexer.New(tb.TempDir(), vnode, indexer.Options{}) if err != nil { tb.Fatal(err) } diff --git a/vochain/indexer/archive_test.go b/vochain/indexer/archive_test.go index f37559d21..083c9887e 100644 --- a/vochain/indexer/archive_test.go +++ b/vochain/indexer/archive_test.go @@ -10,7 +10,7 @@ import ( func TestImportArchive(t *testing.T) { app := vochain.TestBaseApplication(t) - idx := newTestIndexer(t, app, true) + idx := newTestIndexer(t, app) archive := []*ArchiveProcess{} archiveProcess1 := &ArchiveProcess{} diff --git a/vochain/indexer/bench_test.go b/vochain/indexer/bench_test.go index 4303b5594..7043c8cb8 100644 --- a/vochain/indexer/bench_test.go +++ b/vochain/indexer/bench_test.go @@ -21,7 +21,7 @@ import ( func BenchmarkIndexer(b *testing.B) { app := vochain.TestBaseApplication(b) - idx := newTestIndexer(b, app, true) + idx := newTestIndexer(b, app) pid := util.RandomBytes(32) if err := app.State.AddProcess(&models.Process{ ProcessId: pid, @@ -131,7 +131,7 @@ func BenchmarkFetchTx(b *testing.B) { numTxs := 1000 app := vochain.TestBaseApplication(b) - idx := newTestIndexer(b, app, true) + idx := newTestIndexer(b, app) b.ReportAllocs() b.ResetTimer() @@ -164,7 +164,7 @@ func BenchmarkFetchTx(b *testing.B) { func BenchmarkNewProcess(b *testing.B) { app := vochain.TestBaseApplication(b) - idx := newTestIndexer(b, app, true) + idx := newTestIndexer(b, app) _ = idx // used via the callbacks; we want to benchmark it too startTime := time.Now() numProcesses := b.N diff --git a/vochain/indexer/indexer.go b/vochain/indexer/indexer.go index 9b5e9423c..687937d99 100644 --- a/vochain/indexer/indexer.go +++ b/vochain/indexer/indexer.go @@ -95,7 +95,7 @@ type Indexer struct { } type Options struct { - CountLiveResults bool + IgnoreLiveResults bool } // New returns an instance of the Indexer @@ -103,7 +103,7 @@ type Options struct { func New(dataDir string, app *vochain.BaseApplication, opts Options) (*Indexer, error) { idx := &Indexer{ App: app, - ignoreLiveResults: !opts.CountLiveResults, + ignoreLiveResults: opts.IgnoreLiveResults, // TODO(mvdan): these three maps are all keyed by process ID, // and each of them needs to query existing data from the DB. @@ -113,7 +113,7 @@ func New(dataDir string, app *vochain.BaseApplication, opts Options) (*Indexer, blockUpdateProcs: make(map[string]bool), blockUpdateProcVoteCounts: make(map[string]bool), } - log.Infow("indexer initialization", "dataDir", dataDir, "liveResults", opts.CountLiveResults) + log.Infow("indexer initialization", "dataDir", dataDir, "liveResults", !opts.IgnoreLiveResults) // The DB itself is opened in "rwc" mode, so it is created if it does not yet exist. // Create the parent directory as well if it doesn't exist. diff --git a/vochain/indexer/indexer_test.go b/vochain/indexer/indexer_test.go index 7845d39f7..2a7ca8e7a 100644 --- a/vochain/indexer/indexer_test.go +++ b/vochain/indexer/indexer_test.go @@ -30,8 +30,8 @@ func init() { goose.SetLogger(stdlog.New(io.Discard, "", 0)) } -func newTestIndexer(tb testing.TB, app *vochain.BaseApplication, countLiveResults bool) *Indexer { - idx, err := New(tb.TempDir(), app, Options{CountLiveResults: countLiveResults}) +func newTestIndexer(tb testing.TB, app *vochain.BaseApplication) *Indexer { + idx, err := New(tb.TempDir(), app, Options{}) if err != nil { tb.Fatal(err) } @@ -53,7 +53,7 @@ func TestEntityList(t *testing.T) { func testEntityList(t *testing.T, entityCount int) { app := vochain.TestBaseApplication(t) - idx := newTestIndexer(t, app, true) + idx := newTestIndexer(t, app) baseProcess := &models.Process{ BlockCount: 10, VoteOptions: &models.ProcessVoteOptions{MaxCount: 8, MaxValue: 3}, @@ -110,7 +110,7 @@ func testEntityList(t *testing.T, entityCount int) { func TestEntitySearch(t *testing.T) { app := vochain.TestBaseApplication(t) - idx := newTestIndexer(t, app, true) + idx := newTestIndexer(t, app) entityIds := []string{ "1011d50537fa164b6fef261141797bbe4014526e", @@ -204,7 +204,7 @@ func TestProcessList(t *testing.T) { func testProcessList(t *testing.T, procsCount int) { app := vochain.TestBaseApplication(t) - idx := newTestIndexer(t, app, true) + idx := newTestIndexer(t, app) // Add 10 entities and process for storing random content var eidOneProcess []byte // entity ID with one process @@ -285,7 +285,7 @@ func testProcessList(t *testing.T, procsCount int) { func TestProcessSearch(t *testing.T) { app := vochain.TestBaseApplication(t) - idx := newTestIndexer(t, app, true) + idx := newTestIndexer(t, app) // Add 10 entities and process for storing random content for i := 0; i < 10; i++ { @@ -436,7 +436,7 @@ func TestProcessSearch(t *testing.T) { func TestProcessListWithNamespaceAndStatus(t *testing.T) { app := vochain.TestBaseApplication(t) - idx := newTestIndexer(t, app, true) + idx := newTestIndexer(t, app) // Add 10 processes with different namespaces (from 10 to 20) and status ENDED for i := 0; i < 10; i++ { @@ -503,7 +503,7 @@ func TestProcessListWithNamespaceAndStatus(t *testing.T) { func TestResults(t *testing.T) { app := vochain.TestBaseApplication(t) - idx := newTestIndexer(t, app, true) + idx := newTestIndexer(t, app) keys, root, proofs := testvoteproof.CreateKeysAndBuildCensus(t, 30) pid := util.RandomBytes(32) @@ -668,7 +668,7 @@ func TestResults(t *testing.T) { func TestLiveResults(t *testing.T) { app := vochain.TestBaseApplication(t) - idx := newTestIndexer(t, app, true) + idx := newTestIndexer(t, app) pid := util.RandomBytes(32) if err := app.State.AddProcess(&models.Process{ @@ -720,7 +720,7 @@ func TestLiveResults(t *testing.T) { func TestAddVote(t *testing.T) { app := vochain.TestBaseApplication(t) - idx := newTestIndexer(t, app, true) + idx := newTestIndexer(t, app) options := &models.ProcessVoteOptions{ MaxCount: 3, @@ -810,7 +810,7 @@ func TestBallotProtocolRateProduct(t *testing.T) { // Rate a product from 0 to 4 app := vochain.TestBaseApplication(t) - idx := newTestIndexer(t, app, true) + idx := newTestIndexer(t, app) // Rate 2 products from 0 to 4 pid := util.RandomBytes(32) @@ -848,7 +848,7 @@ func TestBallotProtocolQuadratic(t *testing.T) { // Rate a product from 0 to 4 app := vochain.TestBaseApplication(t) - idx := newTestIndexer(t, app, true) + idx := newTestIndexer(t, app) // Rate 2 products from 0 to 4 pid := util.RandomBytes(32) @@ -898,7 +898,7 @@ func TestBallotProtocolMultiChoice(t *testing.T) { app := vochain.TestBaseApplication(t) - idx := newTestIndexer(t, app, true) + idx := newTestIndexer(t, app) // Rate 2 products from 0 to 4 pid := util.RandomBytes(32) @@ -943,7 +943,7 @@ func TestBallotProtocolMultiChoice(t *testing.T) { func TestAfterSyncBootStrap(t *testing.T) { app := vochain.TestBaseApplication(t) - idx := newTestIndexer(t, app, true) + idx := newTestIndexer(t, app) pid := util.RandomBytes(32) qt.Assert(t, app.IsSynchronizing(), qt.Equals, false) @@ -1011,7 +1011,7 @@ func TestAfterSyncBootStrap(t *testing.T) { func TestCountVotes(t *testing.T) { app := vochain.TestBaseApplication(t) - idx := newTestIndexer(t, app, true) + idx := newTestIndexer(t, app) pid := util.RandomBytes(32) err := app.State.AddProcess(&models.Process{ @@ -1069,7 +1069,7 @@ func TestCountVotes(t *testing.T) { func TestOverwriteVotes(t *testing.T) { app := vochain.TestBaseApplication(t) - idx := newTestIndexer(t, app, true) + idx := newTestIndexer(t, app) pid := util.RandomBytes(32) keys, root, proofs := testvoteproof.CreateKeysAndBuildCensus(t, 10) @@ -1257,7 +1257,7 @@ func TestOverwriteVotes(t *testing.T) { func TestTxIndexer(t *testing.T) { app := vochain.TestBaseApplication(t) - idx := newTestIndexer(t, app, true) + idx := newTestIndexer(t, app) getTxID := func(i, j int) [32]byte { return [32]byte{byte(i), byte(j)} @@ -1316,7 +1316,7 @@ func TestTxIndexer(t *testing.T) { func TestCensusUpdate(t *testing.T) { app := vochain.TestBaseApplication(t) - idx := newTestIndexer(t, app, true) + idx := newTestIndexer(t, app) originalCensusRoot := util.RandomBytes(32) originalCensusURI := new(string) @@ -1368,7 +1368,7 @@ func TestCensusUpdate(t *testing.T) { func TestEndBlock(t *testing.T) { app := vochain.TestBaseApplication(t) - idx := newTestIndexer(t, app, true) + idx := newTestIndexer(t, app) pid := util.RandomBytes(32) @@ -1418,7 +1418,7 @@ func TestEndBlock(t *testing.T) { func TestAccountsList(t *testing.T) { app := vochain.TestBaseApplication(t) - idx := newTestIndexer(t, app, true) + idx := newTestIndexer(t, app) keys := make([]*ethereum.SignKeys, 0) for i := 0; i < 25; i++ { @@ -1479,7 +1479,7 @@ func TestAccountsList(t *testing.T) { func TestTokenTransfers(t *testing.T) { app := vochain.TestBaseApplication(t) - idx := newTestIndexer(t, app, true) + idx := newTestIndexer(t, app) keys := make([]*ethereum.SignKeys, 0) // create 3 accounts diff --git a/vocone/vocone.go b/vocone/vocone.go index 0958c41a3..33c41bf59 100644 --- a/vocone/vocone.go +++ b/vocone/vocone.go @@ -89,7 +89,7 @@ func NewVocone(dataDir string, keymanager *ethereum.SignKeys, disableIPFS bool, // Create indexer if vc.Indexer, err = indexer.New(filepath.Join(dataDir, "indexer"), vc.App, - indexer.Options{CountLiveResults: true}, + indexer.Options{}, ); err != nil { return nil, err }