Skip to content

Commit

Permalink
Merge pull request #13 from flare-foundation/mirror-client
Browse files Browse the repository at this point in the history
Mirror client updates
  • Loading branch information
mboben authored Sep 7, 2023
2 parents 7da7f6c + 61c85a6 commit 616a029
Show file tree
Hide file tree
Showing 6 changed files with 1,006 additions and 756 deletions.
4 changes: 2 additions & 2 deletions indexer/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ type VotingConfig struct {
}

type EpochConfig struct {
Period time.Duration `toml:"epoch_period" envconfig:"EPOCH_PERIOD"`
Start utils.Timestamp `toml:"epoch_time" envconfig:"EPOCH_TIME"`
Period time.Duration `toml:"period" envconfig:"EPOCH_PERIOD"`
Start utils.Timestamp `toml:"start" envconfig:"EPOCH_TIME"`
}

type UptimeConfig struct {
Expand Down
82 changes: 39 additions & 43 deletions indexer/cronjob/mirror.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cronjob

import (
"context"
"flare-indexer/database"
"flare-indexer/indexer/config"
indexerctx "flare-indexer/indexer/context"
Expand All @@ -16,7 +15,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -179,47 +177,23 @@ type epochRange struct {

var errNoEpochsToMirror = errors.New("no epochs to mirror")

func (e *epochRange) validate() error {
if e.start > e.end {
return errNoEpochsToMirror
}

return nil
}

func (c *mirrorCronJob) getEpochRange() (*epochRange, error) {
epochRange := new(epochRange)
eg, ctx := errgroup.WithContext(context.Background())

eg.Go(func() error {
startEpoch, err := c.getStartEpoch()
if err != nil {
return err
}

epochRange.start = startEpoch
return nil
})

eg.Go(func() error {
endEpoch, err := c.getEndEpoch(ctx)
if err != nil {
return err
}

epochRange.end = endEpoch
return nil
})

if err := eg.Wait(); err != nil {
startEpoch, err := c.getStartEpoch()
if err != nil {
return nil, err
}

if err := epochRange.validate(); err != nil {
logger.Debug("start epoch: %d", startEpoch)

endEpoch, err := c.getEndEpoch(startEpoch)
if err != nil {
return nil, err
}

return epochRange, nil
return &epochRange{
start: startEpoch,
end: endEpoch,
}, nil
}

func (c *mirrorCronJob) getStartEpoch() (int64, error) {
Expand All @@ -231,11 +205,12 @@ func (c *mirrorCronJob) getStartEpoch() (int64, error) {
return int64(jobState.NextDBIndex), nil
}

func (c *mirrorCronJob) getEndEpoch(ctx context.Context) (int64, error) {
func (c *mirrorCronJob) getEndEpoch(startEpoch int64) (int64, error) {
currEpoch := c.epochs.getCurrentEpoch()
logger.Debug("current epoch: %d", currEpoch)

for epoch := currEpoch; epoch > 0; epoch-- {
confirmed, err := c.isEpochConfirmed(ctx, epoch)
for epoch := currEpoch; epoch > startEpoch; epoch-- {
confirmed, err := c.isEpochConfirmed(epoch)
if err != nil {
return 0, err
}
Expand All @@ -248,9 +223,8 @@ func (c *mirrorCronJob) getEndEpoch(ctx context.Context) (int64, error) {
return 0, errNoEpochsToMirror
}

func (c *mirrorCronJob) isEpochConfirmed(ctx context.Context, epoch int64) (bool, error) {
opts := &bind.CallOpts{Context: ctx}
merkleRoot, err := c.votingContract.GetMerkleRoot(opts, big.NewInt(epoch))
func (c *mirrorCronJob) isEpochConfirmed(epoch int64) (bool, error) {
merkleRoot, err := c.votingContract.GetMerkleRoot(new(bind.CallOpts), big.NewInt(epoch))
if err != nil {
return false, errors.Wrap(err, "votingContract.GetMerkleRoot")
}
Expand Down Expand Up @@ -299,6 +273,10 @@ func (c *mirrorCronJob) mirrorTxs(txs []database.PChainTxData, epochID int64) er
return err
}

if err := c.checkMerkleRoot(merkleTree, epochID); err != nil {
return err
}

for i := range txs {
in := mirrorTxInput{
epochID: big.NewInt(epochID),
Expand All @@ -314,6 +292,24 @@ func (c *mirrorCronJob) mirrorTxs(txs []database.PChainTxData, epochID int64) er
return nil
}

func (c *mirrorCronJob) checkMerkleRoot(tree merkle.Tree, epoch int64) error {
root, err := tree.Root()
if err != nil {
return err
}

contractRoot, err := c.votingContract.GetMerkleRoot(new(bind.CallOpts), big.NewInt(epoch))
if err != nil {
return errors.Wrap(err, "votingContract.GetMerkleRoot")
}

if root != contractRoot {
return errors.Errorf("merkle root mismatch: got %x, expected %x", root, contractRoot)
}

return nil
}

type mirrorTxInput struct {
epochID *big.Int
merkleTree merkle.Tree
Expand All @@ -331,7 +327,7 @@ func (c *mirrorCronJob) mirrorTx(in *mirrorTxInput) error {
return err
}

_, err = c.mirroringContract.VerifyStake(c.txOpts, *stakeData, merkleProof)
_, err = c.mirroringContract.MirrorStake(c.txOpts, *stakeData, merkleProof)
if err != nil {
return errors.Wrap(err, "mirroringContract.VerifyStake")
}
Expand Down
Loading

0 comments on commit 616a029

Please sign in to comment.