Skip to content

Commit

Permalink
feat(f3): manifest retrieval from smart contract
Browse files Browse the repository at this point in the history
  • Loading branch information
hanabi1224 committed Feb 21, 2025
1 parent de02701 commit f27db8d
Show file tree
Hide file tree
Showing 17 changed files with 325 additions and 206 deletions.
1 change: 0 additions & 1 deletion docs/docs/users/reference/env_variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ process.
| `FOREST_F3_SIDECAR_RPC_ENDPOINT` | string | 127.0.0.1:23456 | `127.0.0.1:23456` | An RPC endpoint of F3 sidecar. |
| `FOREST_F3_SIDECAR_FFI_ENABLED` | 1 or true | hard-coded per chain | 1 | Whether or not to start the F3 sidecar via FFI |
| `FOREST_F3_CONSENSUS_ENABLED` | 1 or true | hard-coded per chain | 1 | Whether or not to apply the F3 consensus to the node |
| `FOREST_F3_MANIFEST_SERVER` | string | empty | `12D3KooWENMwUF9YxvQxar7uBWJtZkA6amvK4xWmKXfSiHUo2Qq7` | Set dynamic F3 manifest server |
| `FOREST_F3_FINALITY` | integer | inherited from chain configuration | 900 | Set the chain finality epochs in F3 manifest |
| `FOREST_F3_PERMANENT_PARTICIPATING_MINER_ADDRESSES` | comma delimited strings | empty | `t0100,t0101` | Set the miner addresses that participate in F3 permanently |
| `FOREST_F3_INITIAL_POWER_TABLE` | string | empty | `bafyreicmaj5hhoy5mgqvamfhgexxyergw7hdeshizghodwkjg6qmpoco7i` | Set the F3 initial power table CID |
Expand Down
3 changes: 0 additions & 3 deletions f3-sidecar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,3 @@ environment variable `FOREST_F3_SIDECAR_FFI_BUILD_OPT_OUT=1` is set.

F3 sidecar is not started by default, set `FOREST_F3_SIDECAR_FFI_ENABLED=1` to
opt in.

Set dynamic manifest server via `FOREST_F3_MANIFEST_SERVER`, e.g.
`FOREST_F3_MANIFEST_SERVER=12D3KooWENMwUF9YxvQxar7uBWJtZkA6amvK4xWmKXfSiHUo2Qq7`
10 changes: 10 additions & 0 deletions f3-sidecar/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"

"github.com/filecoin-project/go-f3"
"github.com/filecoin-project/go-f3/certs"
Expand Down Expand Up @@ -62,3 +63,12 @@ func (h *F3ServerHandler) F3GetProgress(_ context.Context) gpbft.Instant {
func (h *F3ServerHandler) F3GetManifest(_ context.Context) *manifest.Manifest {
return h.f3.Manifest()
}

func (h *F3ServerHandler) F3SetManifest(_ context.Context, m *manifest.Manifest) error {
if ManifestProvider != nil {
ManifestProvider.Update(m)
return nil
} else {
return errors.New("forest manifest provider is not properly initialized")
}
}
7 changes: 3 additions & 4 deletions f3-sidecar/ffi_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,19 @@ import (
var GoF3NodeImpl GoF3Node

type GoF3Node interface {
run(rpc_endpoint *string, jwt *string, f3_rpc_endpoint *string, initial_power_table *string, bootstrap_epoch *int64, finality *int64, f3_root *string, manifest_server *string) bool
run(rpc_endpoint *string, jwt *string, f3_rpc_endpoint *string, initial_power_table *string, bootstrap_epoch *int64, finality *int64, f3_root *string) bool
}

//export CGoF3Node_run
func CGoF3Node_run(rpc_endpoint C.StringRef, jwt C.StringRef, f3_rpc_endpoint C.StringRef, initial_power_table C.StringRef, bootstrap_epoch C.int64_t, finality C.int64_t, f3_root C.StringRef, manifest_server C.StringRef, slot *C.void, cb *C.void) {
func CGoF3Node_run(rpc_endpoint C.StringRef, jwt C.StringRef, f3_rpc_endpoint C.StringRef, initial_power_table C.StringRef, bootstrap_epoch C.int64_t, finality C.int64_t, f3_root C.StringRef, slot *C.void, cb *C.void) {
_new_rpc_endpoint := newString(rpc_endpoint)
_new_jwt := newString(jwt)
_new_f3_rpc_endpoint := newString(f3_rpc_endpoint)
_new_initial_power_table := newString(initial_power_table)
_new_bootstrap_epoch := newC_int64_t(bootstrap_epoch)
_new_finality := newC_int64_t(finality)
_new_f3_root := newString(f3_root)
_new_manifest_server := newString(manifest_server)
resp := GoF3NodeImpl.run(&_new_rpc_endpoint, &_new_jwt, &_new_f3_rpc_endpoint, &_new_initial_power_table, &_new_bootstrap_epoch, &_new_finality, &_new_f3_root, &_new_manifest_server)
resp := GoF3NodeImpl.run(&_new_rpc_endpoint, &_new_jwt, &_new_f3_rpc_endpoint, &_new_initial_power_table, &_new_bootstrap_epoch, &_new_finality, &_new_f3_root)
resp_ref, buffer := cvt_ref(cntC_bool, refC_bool)(&resp)
asmcall.CallFuncG0P2(unsafe.Pointer(cb), unsafe.Pointer(&resp_ref), unsafe.Pointer(slot))
runtime.KeepAlive(resp_ref)
Expand Down
4 changes: 2 additions & 2 deletions f3-sidecar/ffi_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ type f3Impl struct {
ctx context.Context
}

func (f3 *f3Impl) run(rpc_endpoint *string, jwt *string, f3_rpc_endpoint *string, initial_power_table *string, bootstrap_epoch *int64, finality *int64, db *string, manifest_server *string) bool {
func (f3 *f3Impl) run(rpc_endpoint *string, jwt *string, f3_rpc_endpoint *string, initial_power_table *string, bootstrap_epoch *int64, finality *int64, db *string) bool {
var err error = nil
const MAX_RETRY int = 5
nRetry := 0
for nRetry <= MAX_RETRY {
err = run(f3.ctx, *rpc_endpoint, *jwt, *f3_rpc_endpoint, *initial_power_table, *bootstrap_epoch, *finality, *db, *manifest_server)
err = run(f3.ctx, *rpc_endpoint, *jwt, *f3_rpc_endpoint, *initial_power_table, *bootstrap_epoch, *finality, *db)
if err != nil {
nRetry += 1
logger.Errorf("Unexpected F3 failure, retrying(%d) in 10s... error=%s", nRetry, err)
Expand Down
5 changes: 2 additions & 3 deletions f3-sidecar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ func main() {
flag.Int64Var(&finality, "finality", 900, "chain finality epochs")
var root string
flag.StringVar(&root, "root", "f3-data", "path to the f3 data directory")
var manifestServer string
flag.StringVar(&manifestServer, "manifest-server", "", "the peer id of the dynamic manifest server")

flag.Parse()

ctx := context.Background()

err := run(ctx, rpcEndpoint, jwt, f3RpcEndpoint, initialPowerTable, bootstrapEpoch, finality, root, manifestServer)
err := run(ctx, rpcEndpoint, jwt, f3RpcEndpoint, initialPowerTable, bootstrapEpoch, finality, root)
if err != nil {
panic(err)
}
Expand Down
28 changes: 28 additions & 0 deletions f3-sidecar/manifest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"context"

"github.com/filecoin-project/go-f3/manifest"
)

type ForestManifestProvider struct {
ch chan *manifest.Manifest
}

func NewForestManifestProvider(initialValue *manifest.Manifest) (*ForestManifestProvider, error) {
if err := initialValue.Validate(); err != nil {
return nil, err
}
p := ForestManifestProvider{ch: make(chan *manifest.Manifest)}
p.Update(initialValue)
return &p, nil
}

func (p *ForestManifestProvider) Update(m *manifest.Manifest) {
p.ch <- m
}

func (p *ForestManifestProvider) Start(context.Context) error { return nil }
func (p *ForestManifestProvider) Stop(context.Context) error { return nil }
func (p *ForestManifestProvider) ManifestUpdates() <-chan *manifest.Manifest { return p.ch }
43 changes: 7 additions & 36 deletions f3-sidecar/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ import (
"github.com/filecoin-project/go-f3/manifest"
"github.com/filecoin-project/go-jsonrpc"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
leveldb "github.com/ipfs/go-ds-leveldb"
"github.com/libp2p/go-libp2p/core/peer"
)

func run(ctx context.Context, rpcEndpoint string, jwt string, f3RpcEndpoint string, initialPowerTable string, bootstrapEpoch int64, finality int64, f3Root string, manifestServer string) error {
var ManifestProvider *ForestManifestProvider

func run(ctx context.Context, rpcEndpoint string, jwt string, f3RpcEndpoint string, initialPowerTable string, bootstrapEpoch int64, finality int64, f3Root string) error {
api := FilecoinApi{}
isJwtProvided := len(jwt) > 0
closer, err := jsonrpc.NewClient(context.Background(), rpcEndpoint, "Filecoin", &api, nil)
Expand Down Expand Up @@ -95,40 +94,12 @@ func run(ctx context.Context, rpcEndpoint string, jwt string, f3RpcEndpoint stri
}
m.CommitteeLookback = manifest.DefaultCommitteeLookback

var manifestProvider manifest.ManifestProvider
switch manifestServerID, err := peer.Decode(manifestServer); {
case err != nil:
logger.Info("Using static manifest provider")
if manifestProvider, err = manifest.NewStaticManifestProvider(m); err != nil {
return err
}
default:
logger.Infof("Using dynamic manifest provider at %s", manifestServerID)
manifestDS := namespace.Wrap(ds, datastore.NewKey("/f3-dynamic-manifest"))
primaryNetworkName := m.NetworkName
filter := func(m *manifest.Manifest) error {
if m.EC.Finalize {
return fmt.Errorf("refusing dynamic manifest that finalizes tipsets")
}
if m.NetworkName == primaryNetworkName {
return fmt.Errorf(
"refusing dynamic manifest with network name %q that clashes with initial manifest",
primaryNetworkName,
)
}
return nil
}
if manifestProvider, err = manifest.NewDynamicManifestProvider(
p2p.PubSub,
manifestServerID,
manifest.DynamicManifestProviderWithInitialManifest(m),
manifest.DynamicManifestProviderWithDatastore(manifestDS),
manifest.DynamicManifestProviderWithFilter(filter)); err != nil {
return err
}
ManifestProvider, err = NewForestManifestProvider(m)
if err != nil {
return err
}

f3Module, err := f3.New(ctx, manifestProvider, ds,
f3Module, err := f3.New(ctx, ManifestProvider, ds,
p2p.Host, p2p.PubSub, verif, &ec, f3Root)
if err != nil {
return err
Expand Down
2 changes: 0 additions & 2 deletions src/daemon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,6 @@ fn maybe_start_f3_service(
chain_finality,
bootstrap_epoch,
initial_power_table,
manifest_server,
} = crate::f3::get_f3_sidecar_params(&chain_config);
move || {
crate::f3::run_f3_sidecar_if_enabled(
Expand All @@ -609,7 +608,6 @@ fn maybe_start_f3_service(
chain_finality,
std::env::var("FOREST_F3_ROOT")
.unwrap_or(default_f3_root.display().to_string()),
manifest_server.map(|i| i.to_string()).unwrap_or_default(),
);
Ok(())
}
Expand Down
1 change: 0 additions & 1 deletion src/f3/go_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ pub trait GoF3Node {
bootstrap_epoch: i64,
finality: i64,
f3_root: String,
manifest_server: String,
) -> bool;
}
52 changes: 0 additions & 52 deletions src/f3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ mod go_ffi;
use go_ffi::*;

use cid::Cid;
use libp2p::PeerId;

use crate::{networks::ChainConfig, utils::misc::env::is_env_set_and_truthy};

Expand All @@ -18,7 +17,6 @@ pub struct F3Options {
pub chain_finality: i64,
pub bootstrap_epoch: i64,
pub initial_power_table: Cid,
pub manifest_server: Option<PeerId>,
}

pub fn get_f3_sidecar_params(chain_config: &ChainConfig) -> F3Options {
Expand Down Expand Up @@ -55,34 +53,11 @@ pub fn get_f3_sidecar_params(chain_config: &ChainConfig) -> F3Options {
tracing::info!("Using F3 bootstrap epoch {i} set by FOREST_F3_BOOTSTRAP_EPOCH")
})
.unwrap_or(chain_config.f3_bootstrap_epoch);
let manifest_server = match std::env::var("FOREST_F3_MANIFEST_SERVER") {
Ok(v) => {
if v.is_empty() {
None
} else {
match v.parse() {
Ok(i) => Some(i),
_ => {
tracing::warn!(
"Invalid libp2p peer id {v} set by FOREST_F3_MANIFEST_SERVER"
);
None
}
}
.inspect(|i| {
tracing::info!("Using F3 manifest server {i} set by FOREST_F3_MANIFEST_SERVER")
})
.or(chain_config.f3_manifest_server)
}
}
_ => chain_config.f3_manifest_server,
};

F3Options {
chain_finality,
bootstrap_epoch,
initial_power_table,
manifest_server,
}
}

Expand All @@ -95,7 +70,6 @@ pub fn run_f3_sidecar_if_enabled(
_bootstrap_epoch: i64,
_finality: i64,
_f3_root: String,
_manifest_server: String,
) {
if is_sidecar_ffi_enabled(chain_config) {
#[cfg(all(f3sidecar, not(feature = "no-f3-sidecar")))]
Expand All @@ -109,7 +83,6 @@ pub fn run_f3_sidecar_if_enabled(
_bootstrap_epoch,
_finality,
_f3_root,
_manifest_server,
);
}
}
Expand Down Expand Up @@ -147,7 +120,6 @@ mod tests {
chain_finality: chain_config.policy.chain_finality,
bootstrap_epoch: chain_config.f3_bootstrap_epoch,
initial_power_table: chain_config.f3_initial_power_table,
manifest_server: chain_config.f3_manifest_server,
}
);

Expand All @@ -158,29 +130,6 @@ mod tests {
"bafyreicmaj5hhoy5mgqvamfhgexxyergw7hdeshizghodwkjg6qmpoco7i",
);
std::env::set_var("FOREST_F3_BOOTSTRAP_EPOCH", "100");
// mainnet server
std::env::set_var(
"FOREST_F3_MANIFEST_SERVER",
"12D3KooWENMwUF9YxvQxar7uBWJtZkA6amvK4xWmKXfSiHUo2Qq7",
);
assert_eq!(
get_f3_sidecar_params(&chain_config),
F3Options {
chain_finality: 100,
bootstrap_epoch: 100,
initial_power_table: "bafyreicmaj5hhoy5mgqvamfhgexxyergw7hdeshizghodwkjg6qmpoco7i"
.parse()
.unwrap(),
manifest_server: Some(
"12D3KooWENMwUF9YxvQxar7uBWJtZkA6amvK4xWmKXfSiHUo2Qq7"
.parse()
.unwrap()
),
}
);

// Unset FOREST_F3_MANIFEST_SERVER
std::env::set_var("FOREST_F3_MANIFEST_SERVER", "");
assert_eq!(
get_f3_sidecar_params(&chain_config),
F3Options {
Expand All @@ -189,7 +138,6 @@ mod tests {
initial_power_table: "bafyreicmaj5hhoy5mgqvamfhgexxyergw7hdeshizghodwkjg6qmpoco7i"
.parse()
.unwrap(),
manifest_server: None,
}
);
}
Expand Down
Loading

0 comments on commit f27db8d

Please sign in to comment.