Skip to content

Commit

Permalink
test(chain): test forking before/after finality (#12652)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien authored Oct 28, 2024
1 parent c402b5b commit 2b7f595
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
21 changes: 16 additions & 5 deletions chain/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ type ChainGen struct {

GetMessages func(*ChainGen) ([]*types.SignedMessage, error)

// Set to false to allow the chain to advance without updating the state-tree (e.g., this
// allows one to keep the power without having to post).
AdvanceState bool

w *wallet.LocalWallet

eppProvs map[address.Address]WinningPoStProver
Expand Down Expand Up @@ -272,11 +276,12 @@ func NewGeneratorWithSectorsAndUpgradeSchedule(numSectors int, us stmgr.UpgradeS
beacon: beac,
w: w,

GetMessages: getRandomMessages,
Miners: miners,
eppProvs: mgen,
banker: banker,
receivers: receievers,
GetMessages: getRandomMessages,
AdvanceState: true,
Miners: miners,
eppProvs: mgen,
banker: banker,
receivers: receievers,

CurTipset: gents,

Expand Down Expand Up @@ -355,6 +360,8 @@ func (cg *ChainGen) nextBlockProof(ctx context.Context, pts *types.TipSet, m add
mbi, err := mc.MinerGetBaseInfo(ctx, m, round, pts.Key())
if err != nil {
return nil, nil, nil, xerrors.Errorf("get miner base info: %w", err)
} else if mbi == nil {
return nil, nil, nil, nil
}

entries := mbi.BeaconEntries
Expand Down Expand Up @@ -523,6 +530,10 @@ func (cg *ChainGen) makeBlock(parents *types.TipSet, m address.Address, vrfticke
return nil, err
}

if !cg.AdvanceState {
fblk.Header.ParentStateRoot = parents.ParentState()
}

return fblk, err
}

Expand Down
39 changes: 39 additions & 0 deletions chain/store/checkpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/stretchr/testify/require"

"github.com/filecoin-project/lotus/chain/actors/policy"
"github.com/filecoin-project/lotus/chain/gen"
)

Expand All @@ -20,6 +21,7 @@ func TestChainCheckpoint(t *testing.T) {
if err != nil {
t.Fatal(err)
}
cg.AdvanceState = false

// Let the first miner mine some blocks.
last := cg.CurTipset.TipSet()
Expand Down Expand Up @@ -83,4 +85,41 @@ func TestChainCheckpoint(t *testing.T) {

head = cs.GetHeaviestTipSet()
require.True(t, head.Equals(checkpoint))

// Now extend the fork 900 epochs into the future.
for i := 0; i < int(policy.ChainFinality)+10; i++ {
ts, err := cg.NextTipSetFromMiners(last, cg.Miners[1:], 0)
require.NoError(t, err)

last = ts.TipSet.TipSet()
}

// Try to re-checkpoint to the long fork. This will work because we only have to revert a
// single epoch.
err = cs.SetCheckpoint(ctx, last)
require.NoError(t, err)

head = cs.GetHeaviestTipSet()
require.True(t, head.Equals(last))

// Now try to go back to the checkpoint. This should fail because it's too far in the past
// on the wrong fork.
err = cs.SetCheckpoint(ctx, checkpoint)
require.Error(t, err)

// Now extend the checkpoint chain to the same tipset.
for checkpoint.Height() < last.Height() {
ts, err := cg.NextTipSetFromMiners(checkpoint, cg.Miners[1:], 0)
require.NoError(t, err)

checkpoint = ts.TipSet.TipSet()
}

// We should still refuse to switch.
err = cs.SetCheckpoint(ctx, checkpoint)
require.Error(t, err)

// But it should be possible to set a checkpoint on a common chain
err = cs.SetCheckpoint(ctx, checkpointParents)
require.NoError(t, err)
}

0 comments on commit 2b7f595

Please sign in to comment.