Skip to content

Commit

Permalink
Merge pull request #6398 from filecoin-project/feat/checkpoint-f3-fin…
Browse files Browse the repository at this point in the history
…alized-tipsets

feat: Checkpoint tipsets that are finalized by F3
  • Loading branch information
simlecode authored Oct 10, 2024
2 parents 102a6b1 + f07430e commit c8ec50f
Show file tree
Hide file tree
Showing 36 changed files with 328 additions and 110 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ commands:
executors:
golang:
docker:
- image: cimg/go:1.21.7
- image: cimg/go:1.22.8

jobs:
test_all:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/baisc_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: '1.22'
cache: true

- name: install deps
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/common_build_upload.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: '1.22'
cache: true

- name: install deps
Expand Down Expand Up @@ -237,7 +237,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: '1.22'
cache: true

- name: install more deps
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/common_go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: '1.22'
cache: true

- name: vars
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: '1.22'
cache: true

- name: install deps
Expand Down
2 changes: 1 addition & 1 deletion app/node/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (b *Builder) build(ctx context.Context) (*Node, error) {
return nil, errors.Wrap(err, "failed to build node.wallet")
}

nd.f3, err = f3.NewF3Submodule(ctx, nd.repo, nd.chain, nd.network, nd.wallet.GetWalletSign())
nd.f3, err = f3.NewF3Submodule(ctx, nd.repo, nd.chain, nd.network, nd.wallet.GetWalletSign(), nd.syncer)
if err != nil {
return nil, errors.Wrap(err, "failed to build node.f3")
}
Expand Down
3 changes: 3 additions & 0 deletions app/submodule/f3/f3_submodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/filecoin-project/venus/app/submodule/chain"
"github.com/filecoin-project/venus/app/submodule/network"
"github.com/filecoin-project/venus/app/submodule/syncer"
"github.com/filecoin-project/venus/pkg/repo"
"github.com/filecoin-project/venus/pkg/vf3"
"github.com/filecoin-project/venus/pkg/wallet"
Expand All @@ -23,6 +24,7 @@ func NewF3Submodule(ctx context.Context,
chain *chain.ChainSubmodule,
network *network.NetworkSubmodule,
walletSign wallet.WalletSignFunc,
syncer *syncer.SyncerSubmodule,
) (*F3Submodule, error) {
netConf := repo.Config().NetworkParams
if !netConf.F3Enabled {
Expand All @@ -39,6 +41,7 @@ func NewF3Submodule(ctx context.Context,
Datastore: repo.MetaDatastore(),
WalletSign: walletSign,
ManifestProvider: vf3.NewManifestProvider(network.NetworkName, repo.MetaDatastore(), network.Pubsub, netConf),
SyncerAPI: syncer.API(),
})
if err != nil {
return nil, err
Expand Down
5 changes: 5 additions & 0 deletions app/submodule/syncer/chain_sync.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package syncer

import (
"context"
"time"

"github.com/filecoin-project/venus/pkg/chainsync"
Expand Down Expand Up @@ -31,6 +32,10 @@ func (chs *ChainSyncProvider) HandleNewTipSet(ci *types.ChainInfo) error {
return chs.sync.BlockProposer().SendOwnBlock(ci)
}

func (chs *ChainSyncProvider) SyncCheckpoint(ctx context.Context, tsk types.TipSetKey) error {
return chs.sync.BlockProposer().SyncCheckpoint(ctx, tsk)
}

const (
incomeBlockLargeDelayDuration = time.Second * 5
slowFetchMessageDuration = time.Second * 3
Expand Down
5 changes: 5 additions & 0 deletions app/submodule/syncer/syncer_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,8 @@ func (sa *syncerAPI) SyncState(ctx context.Context) (*types.SyncState, error) {
func (sa *syncerAPI) SyncIncomingBlocks(ctx context.Context) (<-chan *types.BlockHeader, error) {
return sa.syncer.ChainSyncManager.BlockProposer().IncomingBlocks(ctx)
}

func (sa *syncerAPI) SyncCheckpoint(ctx context.Context, tsk types.TipSetKey) error {
log.Warnf("Marking tipset %s as checkpoint", tsk)
return sa.syncer.SyncProvider.SyncCheckpoint(ctx, tsk)
}
1 change: 1 addition & 0 deletions fixtures/networks/butterfly.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func ButterflySnapNet() *NetworkConf {
F3Enabled: true,
F3BootstrapEpoch: 1000,
ManifestServerID: "12D3KooWJr9jy4ngtJNR7JC1xgLFra3DjEtyxskRYWvBK9TC3Yn6",
F3Consensus: true,
},
}

Expand Down
2 changes: 1 addition & 1 deletion fixtures/networks/calibration.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func Calibration() *NetworkConf {
AllowableClockDriftSecs: 1,
Eip155ChainID: 314159,
ActorDebugging: false,
F3Enabled: true,
F3Consensus: true,
},
}

Expand Down
1 change: 1 addition & 0 deletions fixtures/networks/forcenet.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func ForceNet() *NetworkConf {
F3Enabled: true,
F3BootstrapEpoch: 100,
ManifestServerID: "12D3KooWHcNBkqXEBrsjoveQvj6zDF3vK5S9tAfqyYaQF1LGSJwG",
F3Consensus: true,
},
}

Expand Down
1 change: 1 addition & 0 deletions fixtures/networks/integrationtestnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func IntegrationNet() *NetworkConf {
F3Enabled: true,
F3BootstrapEpoch: -1,
ManifestServerID: "12D3KooWENMwUF9YxvQxar7uBWJtZkA6amvK4xWmKXfSiHUo2Qq7",
F3Consensus: true,
},
}

Expand Down
1 change: 1 addition & 0 deletions fixtures/networks/interopnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func InteropNet() *NetworkConf {
F3Enabled: true,
F3BootstrapEpoch: 1000,
ManifestServerID: "12D3KooWQJ2rdVnG4okDUB6yHQhAjNutGNemcM7XzqC9Eo4z9Jce",
F3Consensus: true,
},
}

Expand Down
1 change: 1 addition & 0 deletions fixtures/networks/mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func Mainnet() *NetworkConf {
F3Enabled: true,
F3BootstrapEpoch: -1,
ManifestServerID: "12D3KooWENMwUF9YxvQxar7uBWJtZkA6amvK4xWmKXfSiHUo2Qq7",
F3Consensus: false,
},
}

Expand Down
1 change: 1 addition & 0 deletions fixtures/networks/net_2k.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func Net2k() *NetworkConf {
F3Enabled: true,
F3BootstrapEpoch: 1000,
ManifestServerID: "12D3KooWHcNBkqXEBrsjoveQvj6zDF3vK5S9tAfqyYaQF1LGSJwG",
F3Consensus: true,
},
}

Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/filecoin-project/venus

go 1.21
go 1.22

require (
contrib.go.opencensus.io/exporter/prometheus v0.4.2
Expand All @@ -27,7 +27,7 @@ require (
github.com/filecoin-project/go-commp-utils v0.1.3
github.com/filecoin-project/go-crypto v0.0.1
github.com/filecoin-project/go-data-transfer/v2 v2.0.0-rc6
github.com/filecoin-project/go-f3 v0.2.0
github.com/filecoin-project/go-f3 v0.3.0
github.com/filecoin-project/go-fil-commcid v0.1.0
github.com/filecoin-project/go-fil-markets v1.28.2
github.com/filecoin-project/go-jsonrpc v0.1.5
Expand Down Expand Up @@ -117,7 +117,6 @@ require (
)

require (
github.com/Kubuxu/go-broadcast v0.0.0-20240621161059-1a8c90734cd6 // indirect
github.com/filecoin-project/go-clock v0.1.0 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/ipfs/go-blockservice v0.5.2 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q
github.com/DataDog/zstd v1.4.1/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
github.com/Gurpartap/async v0.0.0-20180927173644-4f7f499dd9ee h1:8doiS7ib3zi6/K172oDhSKU0dJ/miJramo9NITOMyZQ=
github.com/Gurpartap/async v0.0.0-20180927173644-4f7f499dd9ee/go.mod h1:W0GbEAA4uFNYOGG2cJpmFJ04E6SD1NLELPYZB57/7AY=
github.com/Kubuxu/go-broadcast v0.0.0-20240621161059-1a8c90734cd6 h1:yh2/1fz3ajTaeKskSWxtSBNScdRZfQ/A5nyd9+64T6M=
github.com/Kubuxu/go-broadcast v0.0.0-20240621161059-1a8c90734cd6/go.mod h1:5LOj/fF3Oc/cvJqzDiyfx4XwtBPRWUYEz+V+b13sH5U=
github.com/Kubuxu/go-os-helper v0.0.1 h1:EJiD2VUQyh5A9hWJLmc6iWg6yIcJ7jpBcwC8GMGXfDk=
github.com/Kubuxu/go-os-helper v0.0.1/go.mod h1:N8B+I7vPCT80IcP58r50u4+gEEcsZETFUpAzWW2ep1Y=
github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE=
Expand Down Expand Up @@ -277,8 +275,8 @@ github.com/filecoin-project/go-data-transfer/v2 v2.0.0-rc6 h1:EsbXTWsBKT764qtX4M
github.com/filecoin-project/go-data-transfer/v2 v2.0.0-rc6/go.mod h1:cX1acvFVWC5EXnnmFPWEFXbO7nLUdSZa+nqgi1QpTpw=
github.com/filecoin-project/go-ds-versioning v0.1.2 h1:to4pTadv3IeV1wvgbCbN6Vqd+fu+7tveXgv/rCEZy6w=
github.com/filecoin-project/go-ds-versioning v0.1.2/go.mod h1:C9/l9PnB1+mwPa26BBVpCjG/XQCB0yj/q5CK2J8X1I4=
github.com/filecoin-project/go-f3 v0.2.0 h1:Gis44+hOrDjSUEw3IDmU7CudNILi5e+bb1pgZgp680k=
github.com/filecoin-project/go-f3 v0.2.0/go.mod h1:43fBLX0iX0+Nnw4Z91wSrdfDYAd6YEDexy7GcLnIJtk=
github.com/filecoin-project/go-f3 v0.3.0 h1:GUY7+QSyWqX4MEuFtQAYkYLRM1t3GleZ0U2I87oOStM=
github.com/filecoin-project/go-f3 v0.3.0/go.mod h1:MVf4ynbRdLMnZZWK8GJCex0VH1lQvh9AwFTMEu7jX1Y=
github.com/filecoin-project/go-fil-commcid v0.0.0-20201016201715-d41df56b4f6a/go.mod h1:Eaox7Hvus1JgPrL5+M3+h7aSPHc0cVqpSxA+TxIEpZQ=
github.com/filecoin-project/go-fil-commcid v0.1.0 h1:3R4ds1A9r6cr8mvZBfMYxTS88OqLYEo6roi+GiIeOh8=
github.com/filecoin-project/go-fil-commcid v0.1.0/go.mod h1:Eaox7Hvus1JgPrL5+M3+h7aSPHc0cVqpSxA+TxIEpZQ=
Expand Down
69 changes: 59 additions & 10 deletions pkg/chain/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1096,24 +1096,73 @@ func (store *Store) Import(ctx context.Context, r io.Reader) (*types.TipSet, *ty
return root, &tailBlock, nil
}

// SetCheckPoint set current checkpoint
func (store *Store) SetCheckPoint(checkPoint types.TipSetKey) {
store.checkPoint = checkPoint
}

// WriteCheckPoint writes the given cids to disk.
func (store *Store) WriteCheckPoint(ctx context.Context, cids types.TipSetKey) error {
log.Infof("WriteCheckPoint %v", cids)
// SetCheckpoint will set a checkpoint past which the chainstore will not allow forks. If the new
// checkpoint is not an ancestor of the current head, head will be set to the new checkpoint.
//
// NOTE: Checkpoints cannot be set beyond ForkLengthThreshold epochs in the past, but can be set
// arbitrarily far into the future.
// NOTE: The new checkpoint must already be synced.
func (store *Store) SetCheckpoint(ctx context.Context, ts *types.TipSet) error {
log.Infof("SetCheckPoint at %d: %v", ts.Height(), ts.Key())
buf := new(bytes.Buffer)
err := cids.MarshalCBOR(buf)
err := ts.Key().MarshalCBOR(buf)
if err != nil {
return err
}
return store.ds.Put(ctx, CheckPoint, buf.Bytes())

store.mu.RLock()
defer store.mu.RUnlock()

// Otherwise, this operation could get _very_ expensive.
if store.head.Height()-ts.Height() > policy.ChainFinality {
return fmt.Errorf("cannot set a checkpoint before the fork threshold")
}

if !ts.Equals(store.head) {
anc, err := store.IsAncestorOf(ctx, ts, store.head)
if err != nil {
return fmt.Errorf("cannot determine whether checkpoint tipset is in main-chain: %w", err)
}

if !anc {
if err := store.setHead(ctx, ts); err != nil {
return fmt.Errorf("failed to switch chains when setting checkpoint: %w", err)
}
}
}

if err := store.ds.Put(ctx, CheckPoint, buf.Bytes()); err != nil {
return err
}
store.checkPoint = ts.Key()

return nil
}

// IsAncestorOf returns true if 'a' is an ancestor of 'b'
func (store *Store) IsAncestorOf(ctx context.Context, a, b *types.TipSet) (bool, error) {
if b.Height() <= a.Height() {
return false, nil
}

cur := b
for !a.Equals(cur) && cur.Height() > a.Height() {
next, err := store.GetTipSet(ctx, cur.Parents())
if err != nil {
return false, err
}

cur = next
}

return cur.Equals(a), nil
}

// GetCheckPoint get the check point from store or disk.
func (store *Store) GetCheckPoint() types.TipSetKey {
store.mu.RLock()
defer store.mu.RUnlock()

return store.checkPoint
}

Expand Down
1 change: 1 addition & 0 deletions pkg/chainsync/chainsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type BlockProposer interface {
SendOwnBlock(ci *types2.ChainInfo) error
SendGossipBlock(ci *types2.ChainInfo) error
IncomingBlocks(ctx context.Context) (<-chan *types2.BlockHeader, error)
SyncCheckpoint(ctx context.Context, tsk types2.TipSetKey) error
}

var _ = (BlockProposer)((*dispatcher.Dispatcher)(nil))
Expand Down
5 changes: 5 additions & 0 deletions pkg/chainsync/dispatcher/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type dispatchSyncer interface {
Head() *types2.TipSet
HandleNewTipSet(context.Context, *types.Target) error
ValidateMsgMeta(ctx context.Context, fblk *types2.FullBlock) error
SyncCheckpoint(ctx context.Context, tsk types2.TipSetKey) error
}

// NewDispatcher creates a new syncing dispatcher with default queue sizes.
Expand Down Expand Up @@ -335,3 +336,7 @@ func (d *Dispatcher) processCtrl(ctrlMsg interface{}) {
log.Info("dispatcher control can not handle type %T", typedMsg)
}
}

func (d *Dispatcher) SyncCheckpoint(ctx context.Context, tsk types2.TipSetKey) error {
return d.syncer.SyncCheckpoint(ctx, tsk)
}
4 changes: 4 additions & 0 deletions pkg/chainsync/dispatcher/dispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func (fs *mockSyncer) ValidateMsgMeta(ctx context.Context, fblk *types.FullBlock
return nil
}

func (fs *mockSyncer) SyncCheckpoint(ctx context.Context, tsk types.TipSetKey) error {
return nil
}

func TestDispatchStartHappy(t *testing.T) {
tf.UnitTest(t)
s := &mockSyncer{
Expand Down
Loading

0 comments on commit c8ec50f

Please sign in to comment.