Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that genesis-time is not used for jailing and slashing #148

Merged
merged 2 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions x/slashing/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k Keeper) {
blockTimes := k.getBlockTimes()
blockTimes = append(blockTimes, ctx.BlockTime())
slashable := false
slashable, blockTimes = truncateByWindow(ctx.BlockTime(), blockTimes, signedBlocksWindow)
k.setBlockTimes(batch, blockTimes)

if ctx.BlockHeight() != 1 {
// Ignore first blocktime, which comes from the genesis file and may not match actual time
slashable, blockTimes = truncateByWindow(ctx.BlockTime(), blockTimes, signedBlocksWindow)
k.setBlockTimes(batch, blockTimes)
}

// Iterate over all the validators which *should* have signed this block
// store whether or not they have actually signed it and slash/unbond any
Expand Down
82 changes: 82 additions & 0 deletions x/slashing/keeper/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,88 @@ func TestBeginBlocker(t *testing.T) {

}

func TestOldGenesisTime(t *testing.T) {
// The launch of emoney-3 revealed that the first block contains the genesis time from genesis.json. In the case of emoney-3, this was inherited directly from
// emoney-2 and was thus some time in the past, leading to immediate jailing and slashing of validators that were not ready.
// Block 1 should be treated differently to avoid this in future upgrades.
genesisTime, _ := time.Parse(time.RFC3339, "2020-11-04T13:00:00Z") // Genesis time from emoney-2 and emoney-3

ctx, keeper, _, _, stakingKeeper, database := createTestComponents(t)

var power1, power2 int64 = 70, 30
addr1, pk1 := addrs[2], pks[2]
addr2, pk2 := addrs[1], pks[1]

// bond the validators
_, err := staking.NewHandler(stakingKeeper)(ctx, NewTestMsgCreateValidator(addr1, pk1, sdk.TokensFromConsensusPower(power1)))
require.NoError(t, err)
_, err = staking.NewHandler(stakingKeeper)(ctx, NewTestMsgCreateValidator(addr2, pk2, sdk.TokensFromConsensusPower(power2)))
require.NoError(t, err)

val1 := abci.Validator{pk1.Address(), power1}
blewater marked this conversation as resolved.
Show resolved Hide resolved
val2 := abci.Validator{pk2.Address(), power2}

// Val2 fails to sign
req := abci.RequestBeginBlock{
LastCommitInfo: abci.LastCommitInfo{
Votes: []abci.VoteInfo{
{
Validator: val1,
SignedLastBlock: true,
},
{
Validator: val2,
SignedLastBlock: false,
},
},
},
}

// Start processing blocks
now := time.Now()
ctx = ctx.WithBlockHeight(1).WithBlockTime(genesisTime)

// block 1
{
batch := database.NewBatch()
ctx = apptypes.WithCurrentBatch(ctx, batch)
BeginBlocker(ctx, req, keeper)
staking.EndBlocker(ctx, stakingKeeper)
batch.Write()
}

ctx = ctx.WithBlockHeight(2).WithBlockTime(now)
// block 2
{
batch := database.NewBatch()
ctx = apptypes.WithCurrentBatch(ctx, batch)
BeginBlocker(ctx, req, keeper)
staking.EndBlocker(ctx, stakingKeeper)
batch.Write()
}

// Validator 2 must remain bonded.
validator, found := stakingKeeper.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(pk2))
require.True(t, found)
require.Equal(t, stakingtypes.Bonded, validator.GetStatus())

// create a third block 25 hours later and ensure jailing
ctx = ctx.WithBlockHeight(3).WithBlockTime(now.Add(61 * time.Minute))
// block 3
{
batch := database.NewBatch()
ctx = apptypes.WithCurrentBatch(ctx, batch)
BeginBlocker(ctx, req, keeper)
staking.EndBlocker(ctx, stakingKeeper)
batch.Write()
}

// Validator 2 must be unbonding.
validator, found = stakingKeeper.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(pk2))
require.True(t, found)
require.Equal(t, stakingtypes.Unbonding.String(), validator.GetStatus().String())
}

func createTestComponents(t *testing.T) (sdk.Context, Keeper, banktypes.AccountKeeper, bankkeeper.Keeper, stakingkeeper.Keeper, *dbm.MemDB) {
t.Helper()
encConfig := MakeTestEncodingConfig()
Expand Down