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

test(chain): test forking before/after finality #12652

Merged
merged 1 commit into from
Oct 28, 2024
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
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
Stebalien marked this conversation as resolved.
Show resolved Hide resolved
}

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()
}
Stebalien marked this conversation as resolved.
Show resolved Hide resolved

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)
}
Loading