Skip to content

Commit

Permalink
vochain/indexer: invert "live results" option
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mvdan authored and p4u committed Dec 11, 2023
1 parent 272b93d commit 6dc7b93
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion service/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/testcommon/vochain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion vochain/indexer/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
6 changes: 3 additions & 3 deletions vochain/indexer/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions vochain/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ type Indexer struct {
}

type Options struct {
CountLiveResults bool
IgnoreLiveResults bool
}

// New returns an instance of the Indexer
// using the local storage database in dataDir and integrated into the state vochain instance
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.
Expand All @@ -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.
Expand Down
42 changes: 21 additions & 21 deletions vochain/indexer/indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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},
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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++ {
Expand Down Expand Up @@ -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++ {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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++ {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion vocone/vocone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 6dc7b93

Please sign in to comment.