Skip to content

Commit

Permalink
feat: apply PR reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
sm-stack committed Jul 25, 2024
1 parent a3d2c5f commit 44752b8
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 35 deletions.
19 changes: 10 additions & 9 deletions kroma-validator/challenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ func (c *Challenger) handleChallenge(outputIndex *big.Int, asserter common.Addre
continue
}
case chal.StatusChallengerTimeout:
// call challenger timeout to increase bond from pending bond
// call challenger timeout to take challenger's bond away
tx, err := c.ChallengerTimeout(c.ctx, outputIndex, challenger)
if err != nil {
c.log.Error("failed to create challenger timeout tx", "err", err, "outputIndex", outputIndex, "challenger", challenger)
Expand All @@ -601,8 +601,8 @@ func (c *Challenger) handleChallenge(outputIndex *big.Int, asserter common.Addre
// if challenger
if isChallenger && c.cfg.ChallengerEnabled {
if isOutputDeleted {
// if output has been already deleted, cancel challenge to refund pending bond in ValidatorPool
if !c.IsValPoolTerminated(outputIndex) && status != chal.StatusChallengerTimeout {
// if output has been already deleted, cancel challenge to refund pending bond
if 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)
Expand Down Expand Up @@ -663,6 +663,7 @@ func (c *Challenger) CanCreateChallenge(ctx context.Context, outputIndex *big.In
cCtx, cCancel := context.WithTimeout(ctx, c.cfg.NetworkTimeout)
defer cCancel()
from := c.cfg.TxManager.From()

var balance, requiredBondAmount *big.Int
if c.IsValPoolTerminated(outputIndex) {
if isInJail, err := c.isInJail(ctx); err != nil {
Expand All @@ -683,7 +684,7 @@ func (c *Challenger) CanCreateChallenge(ctx context.Context, outputIndex *big.In
return false, nil
}

balance, err = c.assetMgrContract.TotalValidatorBalance(optsutils.NewSimpleCallOpts(cCtx), from)
balance, err = c.assetMgrContract.TotalValidatorKroNotBonded(optsutils.NewSimpleCallOpts(cCtx), from)
if err != nil {
return false, fmt.Errorf("failed to fetch balance: %w", err)
}
Expand All @@ -697,19 +698,19 @@ func (c *Challenger) CanCreateChallenge(ctx context.Context, outputIndex *big.In
requiredBondAmount = c.requiredBondAmountV1
}

c.metr.RecordDepositAmount(balance)
c.metr.RecordUnbondedDepositAmount(balance)

// Check if the deposit amount is less than the required bond amount
// Check if the unbonded deposit amount is less than the required bond amount
if balance.Cmp(requiredBondAmount) == -1 {
c.log.Warn(
"deposit is less than bond attempt amount",
"unbonded deposit is less than bond attempt amount",
"requiredBondAmount", requiredBondAmount,
"deposit", balance,
"unbonded_deposit", balance,
)
return false, nil
}

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

return true, nil
}
Expand Down
13 changes: 7 additions & 6 deletions kroma-validator/l2_output_submitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ func (l *L2OutputSubmitter) assertCanSubmitOutput(ctx context.Context, outputInd
cCtx, cCancel := context.WithTimeout(ctx, l.cfg.NetworkTimeout)
defer cCancel()
from := l.cfg.TxManager.From()

var balance, requiredBondAmount *big.Int
if l.IsValPoolTerminated(outputIndex) {
if isInJail, err := l.IsInJail(ctx); err != nil {
Expand All @@ -346,7 +347,7 @@ func (l *L2OutputSubmitter) assertCanSubmitOutput(ctx context.Context, outputInd
return nil
}

balance, err = l.assetMgrContract.TotalValidatorBalance(optsutils.NewSimpleCallOpts(cCtx), from)
balance, err = l.assetMgrContract.TotalValidatorKroNotBonded(optsutils.NewSimpleCallOpts(cCtx), from)
if err != nil {
return fmt.Errorf("failed to fetch balance: %w", err)
}
Expand All @@ -360,19 +361,19 @@ func (l *L2OutputSubmitter) assertCanSubmitOutput(ctx context.Context, outputInd
requiredBondAmount = l.requiredBondAmountV1
}

l.metr.RecordDepositAmount(balance)
l.metr.RecordUnbondedDepositAmount(balance)

// Check if the deposit amount is less than the required bond amount
// Check if the unbonded deposit amount is less than the required bond amount
if balance.Cmp(requiredBondAmount) == -1 {
l.log.Warn(
"deposit is less than bond attempt amount",
"unbonded deposit is less than bond attempt amount",
"requiredBondAmount", requiredBondAmount,
"deposit", balance,
"unbonded_deposit", balance,
)
return nil
}

l.log.Info("deposit amount and bond amount", "deposit", balance, "bond", requiredBondAmount)
l.log.Info("unbonded deposit amount and bond amount", "unbonded_deposit", balance, "bond", requiredBondAmount)

return nil
}
Expand Down
26 changes: 13 additions & 13 deletions kroma-validator/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Metricer interface {
txmetrics.TxMetricer

RecordL2OutputSubmitted(l2ref eth.L2BlockRef)
RecordDepositAmount(amount *big.Int)
RecordUnbondedDepositAmount(amount *big.Int)
RecordValidatorStatus(status uint8)
RecordNextValidator(address common.Address)
RecordChallengeCheckpoint(outputIndex *big.Int)
Expand All @@ -45,12 +45,12 @@ type Metrics struct {
txmetrics.TxMetrics
opmetrics.RPCMetrics

Info prometheus.GaugeVec
Up prometheus.Gauge
DepositAmount prometheus.Gauge
ValidatorStatus prometheus.Gauge
NextValidator prometheus.GaugeVec
ChallengeCheckpoint prometheus.Gauge
Info prometheus.GaugeVec
Up prometheus.Gauge
UnbondedDepositAmount prometheus.Gauge
ValidatorStatus prometheus.Gauge
NextValidator prometheus.GaugeVec
ChallengeCheckpoint prometheus.Gauge
}

var _ Metricer = (*Metrics)(nil)
Expand Down Expand Up @@ -85,10 +85,10 @@ func NewMetrics(procName string) *Metrics {
Name: "up",
Help: "1 if the kroma-validator has finished starting up",
}),
DepositAmount: factory.NewGauge(prometheus.GaugeOpts{
UnbondedDepositAmount: factory.NewGauge(prometheus.GaugeOpts{
Namespace: ns,
Name: "deposit_amount",
Help: "The amount deposited into the ValidatorPool contract",
Name: "unbonded_deposit_amount",
Help: "The amount of Validator balance excluding the bonded amount",
}),
ValidatorStatus: factory.NewGauge(prometheus.GaugeOpts{
Namespace: ns,
Expand Down Expand Up @@ -142,9 +142,9 @@ func (m *Metrics) RecordL2OutputSubmitted(l2ref eth.L2BlockRef) {
m.RecordL2Ref(L2OutputSubmitted, l2ref)
}

// RecordDepositAmount sets the amount deposited into the ValidatorPool contract.
func (m *Metrics) RecordDepositAmount(amount *big.Int) {
m.DepositAmount.Set(opmetrics.WeiToEther(amount))
// RecordUnbondedDepositAmount sets the amount deposited into the ValidatorPool contract.
func (m *Metrics) RecordUnbondedDepositAmount(amount *big.Int) {
m.UnbondedDepositAmount.Set(opmetrics.WeiToEther(amount))
}

// RecordValidatorStatus sets the status of validator in the ValidatorManager contract.
Expand Down
2 changes: 1 addition & 1 deletion kroma-validator/metrics/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (*noopMetrics) RecordInfo(version string) {}
func (*noopMetrics) RecordUp() {}

func (*noopMetrics) RecordL2OutputSubmitted(l2ref eth.L2BlockRef) {}
func (*noopMetrics) RecordDepositAmount(amount *big.Int) {}
func (*noopMetrics) RecordUnbondedDepositAmount(amount *big.Int) {}
func (*noopMetrics) RecordValidatorStatus(status uint8) {}
func (*noopMetrics) RecordNextValidator(address common.Address) {}
func (*noopMetrics) RecordChallengeCheckpoint(outputIndex *big.Int) {}
8 changes: 2 additions & 6 deletions packages/contracts/contracts/L1/AssetManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,9 @@ contract AssetManager is ISemver, IERC721Receiver, IAssetManager {
}

/**
* @notice Returns the total amount of validator balance excluding the bond amount.
*
* @param validator Address of the validator.
*
* @return The total amount of validator balance excluding the bond amount.
* @inheritdoc IAssetManager
*/
function totalValidatorBalance(address validator) external view returns (uint128) {
function totalValidatorKroNotBonded(address validator) external view returns (uint128) {
return _vaults[validator].asset.validatorKro - _vaults[validator].asset.validatorKroBonded;
}

Expand Down
9 changes: 9 additions & 0 deletions packages/contracts/contracts/L1/interfaces/IAssetManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,15 @@ interface IAssetManager {
*/
function totalValidatorKroBonded(address validator) external view returns (uint128);

/**
* @notice Returns the total amount of validator balance excluding the bond amount.
*
* @param validator Address of the validator.
*
* @return The total amount of validator balance excluding the bond amount.
*/
function totalValidatorKroNotBonded(address validator) external view returns (uint128);

/**
* @notice Returns the total amount of KRO that delegated by the delegators and accumulated as
* KRO delegation reward.
Expand Down

0 comments on commit 44752b8

Please sign in to comment.