Merge pull request #7210 from filecoin-project/fix/fork-check
fix: correctly handle null blocks when detecting an expensive fork
This commit is contained in:
commit
6a02237f6f
@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/filecoin-project/go-address"
|
"github.com/filecoin-project/go-address"
|
||||||
|
"github.com/filecoin-project/go-state-types/abi"
|
||||||
"github.com/filecoin-project/go-state-types/crypto"
|
"github.com/filecoin-project/go-state-types/crypto"
|
||||||
"github.com/ipfs/go-cid"
|
"github.com/ipfs/go-cid"
|
||||||
"go.opencensus.io/trace"
|
"go.opencensus.io/trace"
|
||||||
@ -20,41 +21,52 @@ import (
|
|||||||
|
|
||||||
var ErrExpensiveFork = errors.New("refusing explicit call due to state fork at epoch")
|
var ErrExpensiveFork = errors.New("refusing explicit call due to state fork at epoch")
|
||||||
|
|
||||||
|
// Call applies the given message to the given tipset's parent state, at the epoch following the
|
||||||
|
// tipset's parent. In the presence of null blocks, the height at which the message is invoked may
|
||||||
|
// be less than the specified tipset.
|
||||||
|
//
|
||||||
|
// - If no tipset is specified, the first tipset without an expensive migration is used.
|
||||||
|
// - If executing a message at a given tipset would trigger an expensive migration, the call will
|
||||||
|
// fail with ErrExpensiveFork.
|
||||||
func (sm *StateManager) Call(ctx context.Context, msg *types.Message, ts *types.TipSet) (*api.InvocResult, error) {
|
func (sm *StateManager) Call(ctx context.Context, msg *types.Message, ts *types.TipSet) (*api.InvocResult, error) {
|
||||||
ctx, span := trace.StartSpan(ctx, "statemanager.Call")
|
ctx, span := trace.StartSpan(ctx, "statemanager.Call")
|
||||||
defer span.End()
|
defer span.End()
|
||||||
|
|
||||||
|
var pheight abi.ChainEpoch = -1
|
||||||
|
|
||||||
// If no tipset is provided, try to find one without a fork.
|
// If no tipset is provided, try to find one without a fork.
|
||||||
if ts == nil {
|
if ts == nil {
|
||||||
ts = sm.cs.GetHeaviestTipSet()
|
ts = sm.cs.GetHeaviestTipSet()
|
||||||
|
|
||||||
// Search back till we find a height with no fork, or we reach the beginning.
|
// Search back till we find a height with no fork, or we reach the beginning.
|
||||||
for ts.Height() > 0 && sm.hasExpensiveFork(ctx, ts.Height()-1) {
|
for ts.Height() > 0 {
|
||||||
var err error
|
pts, err := sm.cs.GetTipSetFromKey(ts.Parents())
|
||||||
ts, err = sm.cs.GetTipSetFromKey(ts.Parents())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, xerrors.Errorf("failed to find a non-forking epoch: %w", err)
|
return nil, xerrors.Errorf("failed to find a non-forking epoch: %w", err)
|
||||||
}
|
}
|
||||||
|
if !sm.hasExpensiveFork(pts.Height()) {
|
||||||
|
pheight = pts.Height()
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
ts = pts
|
||||||
}
|
}
|
||||||
|
} else if ts.Height() > 0 {
|
||||||
bstate := ts.ParentState()
|
|
||||||
pts, err := sm.cs.LoadTipSet(ts.Parents())
|
pts, err := sm.cs.LoadTipSet(ts.Parents())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, xerrors.Errorf("failed to load parent tipset: %w", err)
|
return nil, xerrors.Errorf("failed to load parent tipset: %w", err)
|
||||||
}
|
}
|
||||||
pheight := pts.Height()
|
pheight = pts.Height()
|
||||||
|
if sm.hasExpensiveFork(pheight) {
|
||||||
// If we have to run an expensive migration, and we're not at genesis,
|
|
||||||
// return an error because the migration will take too long.
|
|
||||||
//
|
|
||||||
// We allow this at height 0 for at-genesis migrations (for testing).
|
|
||||||
if pheight > 0 && sm.hasExpensiveFork(ctx, pheight) {
|
|
||||||
return nil, ErrExpensiveFork
|
return nil, ErrExpensiveFork
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// We can't get the parent tipset in this case.
|
||||||
|
pheight = ts.Height() - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
bstate := ts.ParentState()
|
||||||
|
|
||||||
// Run the (not expensive) migration.
|
// Run the (not expensive) migration.
|
||||||
bstate, err = sm.handleStateForks(ctx, bstate, pheight, nil, ts)
|
bstate, err := sm.handleStateForks(ctx, bstate, pheight, nil, ts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to handle fork: %w", err)
|
return nil, fmt.Errorf("failed to handle fork: %w", err)
|
||||||
}
|
}
|
||||||
@ -140,25 +152,33 @@ func (sm *StateManager) CallWithGas(ctx context.Context, msg *types.Message, pri
|
|||||||
// run the fork logic in `sm.TipSetState`. We need the _current_
|
// run the fork logic in `sm.TipSetState`. We need the _current_
|
||||||
// height to have no fork, because we'll run it inside this
|
// height to have no fork, because we'll run it inside this
|
||||||
// function before executing the given message.
|
// function before executing the given message.
|
||||||
for ts.Height() > 0 && (sm.hasExpensiveFork(ctx, ts.Height()) || sm.hasExpensiveFork(ctx, ts.Height()-1)) {
|
for ts.Height() > 0 {
|
||||||
var err error
|
pts, err := sm.cs.GetTipSetFromKey(ts.Parents())
|
||||||
ts, err = sm.cs.GetTipSetFromKey(ts.Parents())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, xerrors.Errorf("failed to find a non-forking epoch: %w", err)
|
return nil, xerrors.Errorf("failed to find a non-forking epoch: %w", err)
|
||||||
}
|
}
|
||||||
}
|
if !sm.hasExpensiveForkBetween(pts.Height(), ts.Height()+1) {
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
// When we're not at the genesis block, make sure we don't have an expensive migration.
|
ts = pts
|
||||||
if ts.Height() > 0 && (sm.hasExpensiveFork(ctx, ts.Height()) || sm.hasExpensiveFork(ctx, ts.Height()-1)) {
|
}
|
||||||
|
} else if ts.Height() > 0 {
|
||||||
|
pts, err := sm.cs.GetTipSetFromKey(ts.Parents())
|
||||||
|
if err != nil {
|
||||||
|
return nil, xerrors.Errorf("failed to find a non-forking epoch: %w", err)
|
||||||
|
}
|
||||||
|
if sm.hasExpensiveForkBetween(pts.Height(), ts.Height()+1) {
|
||||||
return nil, ErrExpensiveFork
|
return nil, ErrExpensiveFork
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
state, _, err := sm.TipSetState(ctx, ts)
|
state, _, err := sm.TipSetState(ctx, ts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, xerrors.Errorf("computing tipset state: %w", err)
|
return nil, xerrors.Errorf("computing tipset state: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Technically, the tipset we're passing in here should be ts+1, but that may not exist.
|
||||||
state, err = sm.handleStateForks(ctx, state, ts.Height(), nil, ts)
|
state, err = sm.handleStateForks(ctx, state, ts.Height(), nil, ts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to handle fork: %w", err)
|
return nil, fmt.Errorf("failed to handle fork: %w", err)
|
||||||
|
@ -99,7 +99,6 @@ func (sm *StateManager) ApplyBlocks(ctx context.Context, parentEpoch abi.ChainEp
|
|||||||
}
|
}
|
||||||
|
|
||||||
// handle state forks
|
// handle state forks
|
||||||
// XXX: The state tree
|
|
||||||
newState, err := sm.handleStateForks(ctx, pstate, i, em, ts)
|
newState, err := sm.handleStateForks(ctx, pstate, i, em, ts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cid.Undef, cid.Undef, xerrors.Errorf("error handling state forks: %w", err)
|
return cid.Undef, cid.Undef, xerrors.Errorf("error handling state forks: %w", err)
|
||||||
|
@ -41,8 +41,11 @@ type MigrationCache interface {
|
|||||||
// - The oldState is the state produced by the upgrade epoch.
|
// - The oldState is the state produced by the upgrade epoch.
|
||||||
// - The returned newState is the new state that will be used by the next epoch.
|
// - The returned newState is the new state that will be used by the next epoch.
|
||||||
// - The height is the upgrade epoch height (already executed).
|
// - The height is the upgrade epoch height (already executed).
|
||||||
// - The tipset is the tipset for the last non-null block before the upgrade. Do
|
// - The tipset is the first non-null tipset after the upgrade height (the tipset in
|
||||||
// not assume that ts.Height() is the upgrade height.
|
// which the upgrade is executed). Do not assume that ts.Height() is the upgrade height.
|
||||||
|
//
|
||||||
|
// NOTE: In StateCompute and CallWithGas, the passed tipset is actually the tipset _before_ the
|
||||||
|
// upgrade. The tipset should really only be used for referencing the "current chain".
|
||||||
type MigrationFunc func(
|
type MigrationFunc func(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
sm *StateManager, cache MigrationCache,
|
sm *StateManager, cache MigrationCache,
|
||||||
@ -208,7 +211,19 @@ func (sm *StateManager) handleStateForks(ctx context.Context, root cid.Cid, heig
|
|||||||
return retCid, nil
|
return retCid, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sm *StateManager) hasExpensiveFork(ctx context.Context, height abi.ChainEpoch) bool {
|
// Returns true executing tipsets between the specified heights would trigger an expensive
|
||||||
|
// migration. NOTE: migrations occurring _at_ the target height are not included, as they're
|
||||||
|
// executed _after_ the target height.
|
||||||
|
func (sm *StateManager) hasExpensiveForkBetween(parent, height abi.ChainEpoch) bool {
|
||||||
|
for h := parent; h < height; h++ {
|
||||||
|
if _, ok := sm.expensiveUpgrades[h]; ok {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sm *StateManager) hasExpensiveFork(height abi.ChainEpoch) bool {
|
||||||
_, ok := sm.expensiveUpgrades[height]
|
_, ok := sm.expensiveUpgrades[height]
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
@ -242,6 +242,19 @@ func TestForkHeightTriggers(t *testing.T) {
|
|||||||
func TestForkRefuseCall(t *testing.T) {
|
func TestForkRefuseCall(t *testing.T) {
|
||||||
logging.SetAllLoggers(logging.LevelInfo)
|
logging.SetAllLoggers(logging.LevelInfo)
|
||||||
|
|
||||||
|
for after := 0; after < 3; after++ {
|
||||||
|
for before := 0; before < 3; before++ {
|
||||||
|
// Makes the lints happy...
|
||||||
|
after := after
|
||||||
|
before := before
|
||||||
|
t.Run(fmt.Sprintf("after:%d,before:%d", after, before), func(t *testing.T) {
|
||||||
|
testForkRefuseCall(t, before, after)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
func testForkRefuseCall(t *testing.T, nullsBefore, nullsAfter int) {
|
||||||
ctx := context.TODO()
|
ctx := context.TODO()
|
||||||
|
|
||||||
cg, err := gen.NewGenerator()
|
cg, err := gen.NewGenerator()
|
||||||
@ -249,6 +262,7 @@ func TestForkRefuseCall(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var migrationCount int
|
||||||
sm, err := NewStateManagerWithUpgradeSchedule(
|
sm, err := NewStateManagerWithUpgradeSchedule(
|
||||||
cg.ChainStore(), cg.StateManager().VMSys(), UpgradeSchedule{{
|
cg.ChainStore(), cg.StateManager().VMSys(), UpgradeSchedule{{
|
||||||
Network: network.Version1,
|
Network: network.Version1,
|
||||||
@ -256,6 +270,7 @@ func TestForkRefuseCall(t *testing.T) {
|
|||||||
Height: testForkHeight,
|
Height: testForkHeight,
|
||||||
Migration: func(ctx context.Context, sm *StateManager, cache MigrationCache, cb ExecMonitor,
|
Migration: func(ctx context.Context, sm *StateManager, cache MigrationCache, cb ExecMonitor,
|
||||||
root cid.Cid, height abi.ChainEpoch, ts *types.TipSet) (cid.Cid, error) {
|
root cid.Cid, height abi.ChainEpoch, ts *types.TipSet) (cid.Cid, error) {
|
||||||
|
migrationCount++
|
||||||
return root, nil
|
return root, nil
|
||||||
}}})
|
}}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -292,14 +307,20 @@ func TestForkRefuseCall(t *testing.T) {
|
|||||||
GasFeeCap: types.NewInt(0),
|
GasFeeCap: types.NewInt(0),
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < 50; i++ {
|
nullStart := abi.ChainEpoch(testForkHeight - nullsBefore)
|
||||||
ts, err := cg.NextTipSet()
|
nullLength := abi.ChainEpoch(nullsBefore + nullsAfter)
|
||||||
|
|
||||||
|
for i := 0; i < testForkHeight*2; i++ {
|
||||||
|
pts := cg.CurTipset.TipSet()
|
||||||
|
skip := abi.ChainEpoch(0)
|
||||||
|
if pts.Height() == nullStart {
|
||||||
|
skip = nullLength
|
||||||
|
}
|
||||||
|
ts, err := cg.NextTipSetFromMiners(pts, cg.Miners, skip)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
pts, err := cg.ChainStore().LoadTipSet(ts.TipSet.TipSet().Parents())
|
|
||||||
require.NoError(t, err)
|
|
||||||
parentHeight := pts.Height()
|
parentHeight := pts.Height()
|
||||||
currentHeight := ts.TipSet.TipSet().Height()
|
currentHeight := ts.TipSet.TipSet().Height()
|
||||||
|
|
||||||
@ -321,7 +342,20 @@ func TestForkRefuseCall(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.True(t, ret.MsgRct.ExitCode.IsSuccess())
|
require.True(t, ret.MsgRct.ExitCode.IsSuccess())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Calls without a tipset should walk back to the last non-fork tipset.
|
||||||
|
// We _verify_ that the migration wasn't run multiple times at the end of the
|
||||||
|
// test.
|
||||||
|
ret, err = sm.CallWithGas(ctx, m, nil, nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.True(t, ret.MsgRct.ExitCode.IsSuccess())
|
||||||
|
|
||||||
|
ret, err = sm.Call(ctx, m, nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.True(t, ret.MsgRct.ExitCode.IsSuccess())
|
||||||
}
|
}
|
||||||
|
// Make sure we didn't execute the migration multiple times.
|
||||||
|
require.Equal(t, migrationCount, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestForkPreMigration(t *testing.T) {
|
func TestForkPreMigration(t *testing.T) {
|
||||||
|
@ -143,13 +143,14 @@ func ComputeState(ctx context.Context, sm *StateManager, height abi.ChainEpoch,
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i := ts.Height(); i < height; i++ {
|
for i := ts.Height(); i < height; i++ {
|
||||||
// handle state forks
|
// Technically, the tipset we're passing in here should be ts+1, but that may not exist.
|
||||||
base, err = sm.handleStateForks(ctx, base, i, &InvocationTracer{trace: &trace}, ts)
|
base, err = sm.handleStateForks(ctx, base, i, &InvocationTracer{trace: &trace}, ts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cid.Undef, nil, xerrors.Errorf("error handling state forks: %w", err)
|
return cid.Undef, nil, xerrors.Errorf("error handling state forks: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: should we also run cron here?
|
// We intentionally don't run cron here, as we may be trying to look into the
|
||||||
|
// future. It's not guaranteed to be accurate... but that's fine.
|
||||||
}
|
}
|
||||||
|
|
||||||
r := store.NewChainRand(sm.cs, ts.Cids())
|
r := store.NewChainRand(sm.cs, ts.Cids())
|
||||||
|
Loading…
Reference in New Issue
Block a user