Skip to content

Commit

Permalink
feat(validator): apply PR Reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
sm-stack committed Apr 25, 2024
1 parent 756f49c commit b0db29f
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 317 deletions.
103 changes: 50 additions & 53 deletions kroma-validator/challenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/kroma-network/kroma/kroma-bindings/bindings"
chal "github.com/kroma-network/kroma/kroma-validator/challenge"
"github.com/kroma-network/kroma/kroma-validator/metrics"
val "github.com/kroma-network/kroma/kroma-validator/validator"
)

var deletedOutputRoot = [32]byte{}
Expand Down Expand Up @@ -56,9 +55,7 @@ type Challenger struct {
l2BlockTime *big.Int
checkpoint *big.Int
requiredBondAmount *big.Int

isValManagerEnabled bool
terminationIndex *big.Int
poolTerminationIndex *big.Int

l2OutputSubmittedSub ethereum.Subscription
challengeCreatedSub ethereum.Subscription
Expand Down Expand Up @@ -160,38 +157,20 @@ func (c *Challenger) InitConfig(ctx context.Context) error {
}
c.requiredBondAmount = requiredBondAmount

cCtx, cCancel = context.WithTimeout(ctx, c.cfg.NetworkTimeout)
defer cCancel()
nextOutputIndex, err := c.l2ooContract.NextOutputIndex(optsutils.NewSimpleCallOpts(cCtx))
if err != nil {
return fmt.Errorf("failed to get latest output index: %w", err)
}

cCtx, cCancel = context.WithTimeout(ctx, c.cfg.NetworkTimeout)
defer cCancel()
isTerminated, err := c.valpoolContract.IsTerminated(
optsutils.NewSimpleCallOpts(cCtx),
nextOutputIndex,
)
if err != nil {
return fmt.Errorf("failed to whether valpool is terminated or not: %w", err)
}
c.isValManagerEnabled = isTerminated

cCtx, cCancel = context.WithTimeout(ctx, c.cfg.NetworkTimeout)
defer cCancel()
terminationIndex, err := c.valpoolContract.TERMINATEOUTPUTINDEX(optsutils.NewSimpleCallOpts(cCtx))
if err != nil {
return fmt.Errorf("failed to get termination index: %w", err)
}
c.terminationIndex = terminationIndex

return nil
})
if err != nil {
return fmt.Errorf("failed to initiate valpool config: %w", err)
}

cCtx, cCancel := context.WithTimeout(ctx, c.cfg.NetworkTimeout)
defer cCancel()
poolTerminationIndex, err := c.valpoolContract.TERMINATEOUTPUTINDEX(optsutils.NewSimpleCallOpts(cCtx))
if err != nil {
return fmt.Errorf("failed to get termination index: %w", err)
}
c.poolTerminationIndex = poolTerminationIndex

return nil
}

Expand Down Expand Up @@ -380,10 +359,6 @@ func (c *Challenger) subscribeL2OutputSubmitted() {
select {
case ev := <-c.l2OutputSubmittedEventChan:
c.log.Info("watched output submitted event", "l2BlockNumber", ev.L2BlockNumber, "outputRoot", ev.OutputRoot, "outputIndex", ev.L2OutputIndex)
// if the emitted output index is greater than the termination output index, set the config to use the ValidatorManager
if ev.L2OutputIndex.Cmp(c.terminationIndex) > 0 {
c.isValManagerEnabled = true
}
// if the emitted output index is less than or equal to the checkpoint, it is considered reorg occurred.
if ev.L2OutputIndex.Cmp(c.checkpoint) <= 0 {
c.wg.Add(1)
Expand Down Expand Up @@ -479,7 +454,7 @@ func (c *Challenger) handleOutput(outputIndex *big.Int) {
return
}

canCreateChallenge, err := c.CanCreateChallenge(c.ctx)
canCreateChallenge, err := c.CanCreateChallenge(c.ctx, outputIndex)
if err != nil {
c.log.Error(err.Error())
continue
Expand Down Expand Up @@ -586,17 +561,22 @@ func (c *Challenger) handleChallenge(outputIndex *big.Int, asserter common.Addre

// if challenger
if isChallenger && c.cfg.ChallengerEnabled {
// if output has been already deleted, cancel challenge to refund pending bond
if isOutputDeleted && status != chal.StatusChallengerTimeout {
tx, err := c.CancelChallenge(c.ctx, outputIndex)
if err != nil {
c.log.Error("failed to create cancel challenge tx", "err", err, "outputIndex", outputIndex)
continue
}
if err := c.submitChallengeTx(tx); err != nil {
c.log.Error("failed to submit cancel challenge tx", "err", err, "outputIndex", outputIndex)
continue
if isOutputDeleted {
// if output has been already deleted, cancel challenge to refund pending bond in ValidatorPool
if outputIndex.Cmp(c.poolTerminationIndex) <= 0 && status != chal.StatusChallengerTimeout {
tx, err := c.CancelChallenge(c.ctx, outputIndex)
if err != nil {
c.log.Error("failed to create cancel challenge tx", "err", err, "outputIndex", outputIndex)
continue
}
if err := c.submitChallengeTx(tx); err != nil {
c.log.Error("failed to submit cancel challenge tx", "err", err, "outputIndex", outputIndex)
continue
}
}
// if output has been already deleted, terminate handling
c.log.Info("output is already deleted when handling challenge", "outputIndex", outputIndex)
return
}

// if output is already finalized, terminate handling
Expand Down Expand Up @@ -640,22 +620,27 @@ func (c *Challenger) submitChallengeTx(tx *types.Transaction) error {
}

// CanCreateChallenge checks if challenger is in the status that can make challenge.
func (c *Challenger) CanCreateChallenge(ctx context.Context) (bool, error) {
func (c *Challenger) CanCreateChallenge(ctx context.Context, outputIndex *big.Int) (bool, error) {
cCtx, cCancel := context.WithTimeout(ctx, c.cfg.NetworkTimeout)
defer cCancel()
from := c.cfg.TxManager.From()
if c.isValManagerEnabled {
vaultStatus, err := c.valManagerContract.GetStatus(optsutils.NewSimpleCallOpts(cCtx), from)
if c.poolTerminationIndex.Cmp(outputIndex) < 0 {
validatorStatus, err := c.valManagerContract.GetStatus(optsutils.NewSimpleCallOpts(cCtx), from)
if err != nil {
return false, fmt.Errorf("failed to fetch the vault status: %w", err)
return false, fmt.Errorf("failed to fetch the validator status: %w", err)
}

if vaultStatus != val.StatusCanSubmitOutput {
c.log.Warn("vault is not in the status to make a challenge", "status", vaultStatus)
if isInJail, err := c.IsInJail(ctx); err != nil {
return false, err
} else if isInJail {
c.log.Warn("validator is in jail")
return false, nil
}

c.log.Info("vault status", "status", vaultStatus)
if validatorStatus != StatusActive {
c.log.Warn("validator is not in the status that can create a challenge", "status", validatorStatus)
return false, nil
}
} else {
balance, err := c.valpoolContract.BalanceOf(optsutils.NewSimpleCallOpts(cCtx), from)
if err != nil {
Expand All @@ -672,12 +657,24 @@ func (c *Challenger) CanCreateChallenge(ctx context.Context) (bool, error) {
return false, nil
}

c.log.Info("deposit amount", "deposit", balance)
c.log.Info("deposit amount and bond amount", "deposit", balance, "bond", c.requiredBondAmount)
}

return true, nil
}

func (c *Challenger) IsInJail(ctx context.Context) (bool, error) {
cCtx, cCancel := context.WithTimeout(ctx, c.cfg.NetworkTimeout)
defer cCancel()
from := c.cfg.TxManager.From()
isInJail, err := c.valManagerContract.InJail(optsutils.NewSimpleCallOpts(cCtx), from)
if err != nil {
return false, fmt.Errorf("failed to fetch the jail status: %w", err)
}

return isInJail, nil
}

func (c *Challenger) IsInChallengeCreationPeriod(ctx context.Context, outputIndex *big.Int) (bool, error) {
cCtx, cCancel := context.WithTimeout(ctx, c.cfg.NetworkTimeout)
defer cCancel()
Expand Down
1 change: 0 additions & 1 deletion kroma-validator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ type Config struct {
ValidatorPoolAddr common.Address
ValidatorManagerAddr common.Address
AssetManagerAddr common.Address
GovTokenAddr common.Address
ChallengerPollInterval time.Duration
NetworkTimeout time.Duration
TxManager *txmgr.BufferedTxManager
Expand Down
Loading

0 comments on commit b0db29f

Please sign in to comment.