Skip to content

Commit

Permalink
test(validator): modify e2e tests related to V2
Browse files Browse the repository at this point in the history
  • Loading branch information
seolaoh committed Jul 30, 2024
1 parent 1bf31fa commit 32e2f26
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"validatorManagerMaxFinalizations": 10,
"validatorManagerBaseReward": "0x1",
"assetManagerKgh": "0xff000000000000000000000000000000000000ff",
"assetManagerVault": "0x14dC79964da2C08b23698B3D3cc7Ca32193d9955",
"assetManagerVault": "0x14dc79964da2c08b23698b3d3cc7ca32193d9955",
"assetManagerMinDelegationPeriod": 2,
"assetManagerBondAmount": "0xa",
"securityCouncilOwners": [
Expand Down
5 changes: 3 additions & 2 deletions kroma-validator/challenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,16 @@ func (c *Challenger) InitConfig(ctx context.Context) error {
if err != nil {
if errors.Is(err, errors.New("method 'BOND_AMOUNT' not found")) {
requiredBondAmountV2 = big.NewInt(0)
} else {
return fmt.Errorf("failed to get required bond amount: %w", err)
}
return fmt.Errorf("failed to get required bond amount: %w", err)
}
c.requiredBondAmountV2 = requiredBondAmountV2

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

return nil
Expand Down
4 changes: 2 additions & 2 deletions op-e2e/actions/l2_challenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func (v *L2Validator) ActCreateChallenge(t Testing, outputIndex *big.Int) common
}, "challenge is already in progress")

canCreateChallenge, err := v.challenger.CanCreateChallenge(t.Ctx(), outputIndex)
require.NoError(t, err, "unable to check if challenger is in the status that can create challenge")
require.True(t, canCreateChallenge, "challenger is not in the status that can create challenge")
require.NoError(t, err, "unable to check if challenger can create challenge")
require.True(t, canCreateChallenge, "challenger cannot create challenge")

tx, err := v.challenger.CreateChallenge(t.Ctx(), outputRange)
require.NoError(t, err, "unable to create create challenge tx")
Expand Down
158 changes: 52 additions & 106 deletions op-e2e/actions/l2_challenger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,9 @@ func ChallengeBasic(t *testing.T, deltaTimeOffset *hexutil.Uint64, version uint8
// create challenge
rt.setupChallenge(rt.challenger1, version)

var beforeAsset *big.Int
var slashingAmount *big.Int
var taxAmount *big.Int
var beforeAsset, beforeAssetBonded *big.Int
if version == valhelper.ValidatorV2 {
beforeAsset, slashingAmount, taxAmount = rt.fetchChallengeAssets(rt.validator.address)
_, _, beforeAsset, beforeAssetBonded, _ = rt.fetchValidatorStatus(rt.validator)
}

interaction:
Expand Down Expand Up @@ -143,22 +141,11 @@ interaction:
require.Equal(rt.t, big.NewInt(2*rt.dp.DeployConfig.ValidatorPoolRequiredBondAmount.ToInt().Int64()), bond.Amount)
} else if version == valhelper.ValidatorV2 {
// check asserter has been slashed
valStatus, err := rt.validator.getValidatorStatus(rt.t)
require.NoError(rt.t, err)
require.Equal(rt.t, val.StatusRegistered, valStatus)

afterAsset, err := rt.assetMgrContract.TotalKroAssets(nil, rt.validator.address)
require.NoError(rt.t, err)
require.Equal(rt.t, beforeAsset.Uint64()-slashingAmount.Uint64(), afterAsset.Uint64())

inJail, err := rt.validator.isInJail(rt.t)
require.NoError(rt.t, err)
valStatus, inJail, afterAsset, afterAssetBonded, slashingAmount := rt.fetchValidatorStatus(rt.validator)
require.Equal(rt.t, val.StatusReady, valStatus)
require.True(rt.t, inJail)

// check security council has received tax
bal, err := rt.assetTokenContract.BalanceOf(nil, rt.sd.DeploymentsL1.SecurityCouncilProxy)
require.NoError(rt.t, err)
require.Equal(t, taxAmount.Uint64(), bal.Uint64())
require.Equal(rt.t, beforeAsset.Uint64()-slashingAmount.Uint64(), afterAsset.Uint64())
require.Equal(rt.t, beforeAssetBonded.Uint64()-slashingAmount.Uint64(), afterAssetBonded.Uint64())
}
}

Expand All @@ -183,11 +170,9 @@ func ChallengeAsserterBisectTimeout(t *testing.T, deltaTimeOffset *hexutil.Uint6
// create challenge
rt.setupChallenge(rt.challenger1, version)

var beforeAsset *big.Int
var slashingAmount *big.Int
var taxAmount *big.Int
var beforeAsset, beforeAssetBonded *big.Int
if version == valhelper.ValidatorV2 {
beforeAsset, slashingAmount, taxAmount = rt.fetchChallengeAssets(rt.validator.address)
_, _, beforeAsset, beforeAssetBonded, _ = rt.fetchValidatorStatus(rt.validator)
}

interaction:
Expand Down Expand Up @@ -239,22 +224,11 @@ interaction:
require.Equal(rt.t, big.NewInt(2*rt.dp.DeployConfig.ValidatorPoolRequiredBondAmount.ToInt().Int64()), bond.Amount)
} else if version == valhelper.ValidatorV2 {
// check asserter has been slashed
valStatus, err := rt.validator.getValidatorStatus(rt.t)
require.NoError(rt.t, err)
require.Equal(rt.t, val.StatusRegistered, valStatus)

afterAsset, err := rt.assetMgrContract.TotalKroAssets(nil, rt.validator.address)
require.NoError(rt.t, err)
require.Equal(rt.t, beforeAsset.Uint64()-slashingAmount.Uint64(), afterAsset.Uint64())

inJail, err := rt.validator.isInJail(rt.t)
require.NoError(rt.t, err)
valStatus, inJail, afterAsset, afterAssetBonded, slashingAmount := rt.fetchValidatorStatus(rt.validator)
require.Equal(rt.t, val.StatusReady, valStatus)
require.True(rt.t, inJail)

// check security council has received tax
bal, err := rt.assetTokenContract.BalanceOf(nil, rt.sd.DeploymentsL1.SecurityCouncilProxy)
require.NoError(rt.t, err)
require.Equal(t, taxAmount.Uint64(), bal.Uint64())
require.Equal(rt.t, beforeAsset.Uint64()-slashingAmount.Uint64(), afterAsset.Uint64())
require.Equal(rt.t, beforeAssetBonded.Uint64()-slashingAmount.Uint64(), afterAssetBonded.Uint64())
}
}

Expand All @@ -278,11 +252,9 @@ func ChallengeChallengerBisectTimeout(t *testing.T, deltaTimeOffset *hexutil.Uin
// create challenge
rt.setupChallenge(rt.challenger1, version)

var beforeAsset *big.Int
var slashingAmount *big.Int
var taxAmount *big.Int
var beforeAsset, beforeAssetBonded *big.Int
if version == valhelper.ValidatorV2 {
beforeAsset, slashingAmount, taxAmount = rt.fetchChallengeAssets(rt.challenger1.address)
_, _, beforeAsset, beforeAssetBonded, _ = rt.fetchValidatorStatus(rt.challenger1)
}

interaction:
Expand Down Expand Up @@ -333,22 +305,11 @@ interaction:
require.Equal(rt.t, big.NewInt(2*rt.dp.DeployConfig.ValidatorPoolRequiredBondAmount.ToInt().Int64()), bond.Amount)
} else if version == valhelper.ValidatorV2 {
// check challenger has been slashed
valStatus, err := rt.challenger1.getValidatorStatus(rt.t)
require.NoError(rt.t, err)
require.Equal(rt.t, val.StatusRegistered, valStatus)

afterAsset, err := rt.assetMgrContract.TotalKroAssets(nil, rt.challenger1.address)
require.NoError(rt.t, err)
require.Equal(rt.t, beforeAsset.Uint64()-slashingAmount.Uint64(), afterAsset.Uint64())

inJail, err := rt.challenger1.isInJail(rt.t)
require.NoError(rt.t, err)
valStatus, inJail, afterAsset, afterAssetBonded, slashingAmount := rt.fetchValidatorStatus(rt.challenger1)
require.Equal(rt.t, val.StatusReady, valStatus)
require.True(rt.t, inJail)

// check security council has received tax
bal, err := rt.assetTokenContract.BalanceOf(nil, rt.sd.DeploymentsL1.SecurityCouncilProxy)
require.NoError(rt.t, err)
require.Equal(t, taxAmount.Uint64(), bal.Uint64())
require.Equal(rt.t, beforeAsset.Uint64()-slashingAmount.Uint64(), afterAsset.Uint64())
require.Equal(rt.t, beforeAssetBonded.Uint64()-slashingAmount.Uint64(), afterAssetBonded.Uint64())
}
}

Expand All @@ -372,11 +333,9 @@ func ChallengeChallengerProvingTimeout(t *testing.T, deltaTimeOffset *hexutil.Ui
// create challenge
rt.setupChallenge(rt.challenger1, version)

var beforeAsset *big.Int
var slashingAmount *big.Int
var taxAmount *big.Int
var beforeAsset, beforeAssetBonded *big.Int
if version == valhelper.ValidatorV2 {
beforeAsset, slashingAmount, taxAmount = rt.fetchChallengeAssets(rt.challenger1.address)
_, _, beforeAsset, beforeAssetBonded, _ = rt.fetchValidatorStatus(rt.challenger1)
}

interaction:
Expand Down Expand Up @@ -431,22 +390,11 @@ interaction:
require.Equal(rt.t, big.NewInt(2*rt.dp.DeployConfig.ValidatorPoolRequiredBondAmount.ToInt().Int64()), bond.Amount)
} else if version == valhelper.ValidatorV2 {
// check challenger has been slashed
valStatus, err := rt.challenger1.getValidatorStatus(rt.t)
require.NoError(rt.t, err)
require.Equal(rt.t, val.StatusRegistered, valStatus)

afterAsset, err := rt.assetMgrContract.TotalKroAssets(nil, rt.challenger1.address)
require.NoError(rt.t, err)
require.Equal(rt.t, beforeAsset.Uint64()-slashingAmount.Uint64(), afterAsset.Uint64())

inJail, err := rt.challenger1.isInJail(rt.t)
require.NoError(rt.t, err)
valStatus, inJail, afterAsset, afterAssetBonded, slashingAmount := rt.fetchValidatorStatus(rt.challenger1)
require.Equal(rt.t, val.StatusReady, valStatus)
require.True(rt.t, inJail)

// check security council has received tax
bal, err := rt.assetTokenContract.BalanceOf(nil, rt.sd.DeploymentsL1.SecurityCouncilProxy)
require.NoError(rt.t, err)
require.Equal(t, taxAmount.Uint64(), bal.Uint64())
require.Equal(rt.t, beforeAsset.Uint64()-slashingAmount.Uint64(), afterAsset.Uint64())
require.Equal(rt.t, beforeAssetBonded.Uint64()-slashingAmount.Uint64(), afterAssetBonded.Uint64())
}
}

Expand All @@ -471,11 +419,10 @@ func ChallengeInvalidProofFail(t *testing.T, deltaTimeOffset *hexutil.Uint64, ve
// create challenge
rt.setupChallenge(rt.challenger1, version)

var taxAmount *big.Int
var beforeAssetChal, beforeAssetVal, beforeAssetBondedChal, beforeAssetBondedVal *big.Int
if version == valhelper.ValidatorV2 {
// if the challenger proves fault with invalid proof, asserter will be slashed
// after security council dismisses the challenge, the slashed asset should be handled manually
_, _, taxAmount = rt.fetchChallengeAssets(rt.validator.address)
_, _, beforeAssetChal, beforeAssetBondedChal, _ = rt.fetchValidatorStatus(rt.challenger1)
_, _, beforeAssetVal, beforeAssetBondedVal, _ = rt.fetchValidatorStatus(rt.validator)
}

interaction:
Expand Down Expand Up @@ -551,15 +498,19 @@ interaction:
require.NoError(rt.t, err)
require.Equal(rt.t, big.NewInt(2*rt.dp.DeployConfig.ValidatorPoolRequiredBondAmount.ToInt().Int64()), bond.Amount)
} else if version == valhelper.ValidatorV2 {
// check asserter has been unjailed by guardian
inJail, err := rt.validator.isInJail(rt.t)
require.NoError(rt.t, err)
require.False(rt.t, inJail)
// check challenger has been slashed
valStatus, inJail, afterAsset, afterAssetBonded, slashingAmount := rt.fetchValidatorStatus(rt.challenger1)
require.Equal(rt.t, val.StatusReady, valStatus)
require.True(rt.t, inJail)
require.Equal(rt.t, beforeAssetChal.Uint64()-slashingAmount.Uint64(), afterAsset.Uint64())
require.Equal(rt.t, beforeAssetBondedChal.Uint64()-slashingAmount.Uint64(), afterAssetBonded.Uint64())

// check security council has received tax
bal, err := rt.assetTokenContract.BalanceOf(nil, rt.sd.DeploymentsL1.SecurityCouncilProxy)
require.NoError(rt.t, err)
require.Equal(t, taxAmount.Uint64(), bal.Uint64())
// check asserter has been reverted slash by guardian
valStatus, inJail, afterAsset, afterAssetBonded, slashingAmount = rt.fetchValidatorStatus(rt.validator)
require.Equal(rt.t, val.StatusActive, valStatus)
require.False(rt.t, inJail)
require.Equal(rt.t, beforeAssetVal.Uint64(), afterAsset.Uint64())
require.Equal(rt.t, beforeAssetBondedVal.Uint64(), afterAssetBonded.Uint64())
}
}

Expand All @@ -584,11 +535,9 @@ func ChallengeForceDeleteOutputBySecurityCouncil(t *testing.T, deltaTimeOffset *
// create challenge
rt.setupChallenge(rt.challenger1, version)

var beforeAsset *big.Int
var slashingAmount *big.Int
var taxAmount *big.Int
var beforeAsset, beforeAssetBonded *big.Int
if version == valhelper.ValidatorV2 {
beforeAsset, slashingAmount, taxAmount = rt.fetchChallengeAssets(rt.validator.address)
_, _, beforeAsset, beforeAssetBonded, _ = rt.fetchValidatorStatus(rt.validator)
}

interaction:
Expand Down Expand Up @@ -651,22 +600,11 @@ interaction:
require.Equal(rt.t, big.NewInt(2*rt.dp.DeployConfig.ValidatorPoolRequiredBondAmount.ToInt().Int64()), bond.Amount)
} else if version == valhelper.ValidatorV2 {
// check asserter has been slashed
valStatus, err := rt.validator.getValidatorStatus(rt.t)
require.NoError(rt.t, err)
require.Equal(rt.t, val.StatusRegistered, valStatus)

inJail, err := rt.validator.isInJail(rt.t)
require.NoError(rt.t, err)
valStatus, inJail, afterAsset, afterAssetBonded, slashingAmount := rt.fetchValidatorStatus(rt.validator)
require.Equal(rt.t, val.StatusReady, valStatus)
require.True(rt.t, inJail)

afterAsset, err := rt.assetMgrContract.TotalKroAssets(nil, rt.validator.address)
require.NoError(rt.t, err)
require.Equal(rt.t, beforeAsset.Uint64()-slashingAmount.Uint64(), afterAsset.Uint64())

// check security council has received tax (in this case, tax is double: challenger timeout and force delete output)
bal, err := rt.assetTokenContract.BalanceOf(nil, rt.sd.DeploymentsL1.SecurityCouncilProxy)
require.NoError(rt.t, err)
require.Equal(t, taxAmount.Uint64()*2, bal.Uint64())
require.Equal(rt.t, beforeAssetBonded.Uint64()-slashingAmount.Uint64(), afterAssetBonded.Uint64())
}
}

Expand Down Expand Up @@ -741,6 +679,10 @@ interaction1:
balance, err := rt.valPoolContract.BalanceOf(nil, rt.challenger2.address)
require.NoError(rt.t, err)
require.Equal(rt.t, balance.Int64(), defaultDepositAmount-rt.dp.DeployConfig.ValidatorPoolRequiredBondAmount.ToInt().Int64())
} else if version == valhelper.ValidatorV2 {
// check bond amount before challenge is canceled
_, _, _, bond, slashingAmount := rt.fetchValidatorStatus(rt.challenger2)
require.Equal(t, slashingAmount.Uint64(), bond.Uint64())
}

// progress challenge by challenger 2
Expand Down Expand Up @@ -777,5 +719,9 @@ interaction2:
balance, err := rt.valPoolContract.BalanceOf(nil, rt.challenger2.address)
require.NoError(rt.t, err)
require.Equal(rt.t, balance.Int64(), int64(defaultDepositAmount))
} else if version == valhelper.ValidatorV2 {
// check bond amount released after challenge canceled
_, _, _, bond, _ := rt.fetchValidatorStatus(rt.challenger2)
require.Equal(t, uint64(0), bond.Uint64())
}
}
37 changes: 19 additions & 18 deletions op-e2e/actions/l2_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/ethereum-optimism/optimism/op-service/testlog"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -260,6 +259,11 @@ func (rt *Runtime) setupChallenge(challenger *L2Validator, version uint8) {
require.Equal(rt.t, rt.dp.DeployConfig.ValidatorPoolRequiredBondAmount.ToInt(), bond.Amount)
} else if version == valhelper.ValidatorV2 {
rt.registerToValMgr(challenger)

// check bond amount before create challenge
bond, err := rt.assetMgrContract.TotalValidatorKroBonded(nil, challenger.address)
require.NoError(rt.t, err)
require.Equal(rt.t, uint64(0), bond.Uint64())
}

// submit create challenge tx
Expand Down Expand Up @@ -288,6 +292,11 @@ func (rt *Runtime) setupChallenge(challenger *L2Validator, version uint8) {
cBal, err := rt.valPoolContract.BalanceOf(nil, challenger.address)
require.NoError(rt.t, err)
require.Equal(rt.t, new(big.Int).Sub(new(big.Int).SetInt64(defaultDepositAmount), rt.dp.DeployConfig.ValidatorPoolRequiredBondAmount.ToInt()), cBal)
} else if version == valhelper.ValidatorV2 {
// check bond amount after create challenge
bond, err := rt.assetMgrContract.TotalValidatorKroBonded(nil, challenger.address)
require.NoError(rt.t, err)
require.Equal(rt.t, rt.dp.DeployConfig.AssetManagerBondAmount.ToInt().Uint64(), bond.Uint64())
}
}

Expand All @@ -304,6 +313,7 @@ func (rt *Runtime) depositToValPool(validator *L2Validator) {

func (rt *Runtime) registerToValMgr(validator *L2Validator) {
minActivateAmount := rt.dp.DeployConfig.ValidatorManagerMinActivateAmount.ToInt()
minActivateAmount = new(big.Int).Mul(minActivateAmount, common.Big256)

// approve governance token
validator.ActApprove(rt.t, minActivateAmount)
Expand All @@ -314,8 +324,7 @@ func (rt *Runtime) registerToValMgr(validator *L2Validator) {
rt.includeL1BlockBySender(validator.address)

// check validator status is active
status, err := validator.getValidatorStatus(rt.t)
require.NoError(rt.t, err)
status := validator.getValidatorStatus(rt.t)
require.Equal(rt.t, val.StatusActive, status)
}

Expand Down Expand Up @@ -354,25 +363,17 @@ func (rt *Runtime) submitL2Output() {
require.Equal(rt.t, types.ReceiptStatusSuccessful, receipt.Status, "submission failed")
}

func (rt *Runtime) fetchChallengeAssets(loser common.Address) (*big.Int, *big.Int, *big.Int) {
slashingRate, err := rt.assetMgrContract.SLASHINGRATE(nil)
require.NoError(rt.t, err)
slashingRateDenom, err := rt.assetMgrContract.SLASHINGRATEDENOM(nil)
require.NoError(rt.t, err)
taxRate, err := rt.assetMgrContract.TAXNUMERATOR(nil)
func (rt *Runtime) fetchValidatorStatus(validator *L2Validator) (uint8, bool, *big.Int, *big.Int, *big.Int) {
valStatus := validator.getValidatorStatus(rt.t)
inJail := validator.isInJail(rt.t)
slashingAmount, err := rt.assetMgrContract.BONDAMOUNT(nil)
require.NoError(rt.t, err)
taxDenom, err := rt.assetMgrContract.TAXDENOMINATOR(nil)
validatorAsset, err := rt.assetMgrContract.TotalValidatorKro(nil, validator.address)
require.NoError(rt.t, err)
minSlashingAmount, err := rt.assetMgrContract.MINSLASHINGAMOUNT(nil)
validatorAssetBonded, err := rt.assetMgrContract.TotalValidatorKroBonded(nil, validator.address)
require.NoError(rt.t, err)
totalAsset, err := rt.assetMgrContract.TotalKroAssets(nil, loser)
require.NoError(rt.t, err)

slashingAmount := new(big.Int).Div(new(big.Int).Mul(totalAsset, slashingRate), slashingRateDenom)
slashingAmount = math.BigMax(slashingAmount, minSlashingAmount)
taxAmount := new(big.Int).Div(new(big.Int).Mul(slashingAmount, taxRate), taxDenom)

return totalAsset, slashingAmount, taxAmount
return valStatus, inJail, validatorAsset, validatorAssetBonded, slashingAmount
}

func (rt *Runtime) includeL1BlockBySender(from common.Address) {
Expand Down
Loading

0 comments on commit 32e2f26

Please sign in to comment.