Skip to content

Commit

Permalink
Advance next needed voting only after shouldVote contract method retu…
Browse files Browse the repository at this point in the history
…rns false
  • Loading branch information
mboben committed Sep 29, 2023
1 parent a8a4e2c commit f29268a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
1 change: 1 addition & 0 deletions indexer/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type MirrorConfig struct {
type VotingConfig struct {
CronjobConfig
config.EpochConfig
GasLimit uint64 `toml:"gas_limit" envconfig:"VOTING_GAS_LIMIT"`
}

type UptimeConfig struct {
Expand Down
19 changes: 6 additions & 13 deletions indexer/cronjob/voting.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type votingCronjob struct {
type votingDB interface {
FetchState(name string) (database.State, error)
FetchPChainVotingData(start, end time.Time) ([]database.PChainTxData, error)
UpdateState(state *database.State) error
UpdateState(epoch int64, force bool) error
}

type votingContract interface {
Expand Down Expand Up @@ -121,12 +121,6 @@ func (c *votingCronjob) Call() error {
return err
}
logger.Info("Submitted votes for epoch %d", e)

// Update state
state.NextDBIndex = uint64(e + 1)
if err := c.db.UpdateState(&state); err != nil {
return err
}
}
return nil
}
Expand All @@ -139,6 +133,10 @@ func (c *votingCronjob) submitVotes(e int64, votingData []database.PChainTxData)
return err
}
if !shouldVote {
// Update state
if err := c.db.UpdateState(e+1, false); err != nil {
return err
}
return nil
}

Expand All @@ -161,12 +159,7 @@ func (c *votingCronjob) reset(firstEpoch int64) error {
}

logger.Info("Resetting voting cronjob state to epoch %d", firstEpoch)
state, err := c.db.FetchState(votingStateName)
if err != nil {
return err
}
state.NextDBIndex = uint64(firstEpoch)
err = c.db.UpdateState(&state)
err := c.db.UpdateState(firstEpoch, true)
if err != nil {
return err
}
Expand Down
18 changes: 16 additions & 2 deletions indexer/cronjob/voting_stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package cronjob
import (
"flare-indexer/database"
"flare-indexer/indexer/config"
"flare-indexer/logger"
"flare-indexer/utils/contracts/voting"
"flare-indexer/utils/staking"
"math/big"
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/pkg/errors"
"gorm.io/gorm"
)

Expand All @@ -25,8 +27,19 @@ func (db *votingDBGorm) FetchPChainVotingData(start, end time.Time) ([]database.
return database.FetchPChainVotingData(db.g, start, end)
}

func (db *votingDBGorm) UpdateState(state *database.State) error {
return database.UpdateState(db.g, state)
func (db *votingDBGorm) UpdateState(epoch int64, force bool) error {
return db.g.Transaction(func(tx *gorm.DB) error {
state, err := database.FetchState(tx, votingStateName)
if err != nil {
return errors.Wrap(err, "database.FetchState")
}
if !force && state.NextDBIndex >= uint64(epoch) {
logger.Debug("state already up to date")
return nil
}
state.NextDBIndex = uint64(epoch)
return database.UpdateState(tx, &state)
})
}

type votingContractCChain struct {
Expand All @@ -50,6 +63,7 @@ func newVotingContractCChain(cfg *config.Config) (votingContract, error) {
if err != nil {
return nil, err
}
txOpts.GasLimit = cfg.VotingCronjob.GasLimit

callOpts := &bind.CallOpts{From: txOpts.From}

Expand Down
13 changes: 11 additions & 2 deletions indexer/cronjob/voting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,16 @@ func (db *votingDBTest) FetchPChainVotingData(start, end time.Time) ([]database.
return db.votingData[timeRange{start, end}], nil
}

func (db *votingDBTest) UpdateState(state *database.State) error {
db.states[state.Name] = *state
func (db *votingDBTest) UpdateState(epoch int64, force bool) error {
if state, ok := db.states[votingStateName]; ok {
if !force && state.NextDBIndex >= uint64(epoch) {
return nil
}
}
db.states[votingStateName] = database.State{
Name: votingStateName,
NextDBIndex: uint64(epoch),
}
return nil
}

Expand All @@ -59,6 +67,7 @@ func (c *votingContractTest) SubmitVote(epoch *big.Int, merkleRoot [32]byte) err
}

c.submittedVotes[epochInt] = merkleRoot
c.shouldVote[epochInt] = false
return nil
}

Expand Down

0 comments on commit f29268a

Please sign in to comment.