Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
16 changes: 16 additions & 0 deletions cgo/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ package cgo
// #cgo darwin LDFLAGS: ${SRCDIR}/../libfilcrypto.a -Wl,-undefined,dynamic_lookup
// #cgo pkg-config: ${SRCDIR}/../filcrypto.pc
// #include "../filcrypto.h"
// // Provide fallbacks when FVM is not compiled into filcrypto
// #ifndef FVM_ERROR_INVALID_HANDLE
// #define FVM_ERROR_INVALID_HANDLE -1
// #endif
// #ifndef FVM_ERROR_NOT_FOUND
// #define FVM_ERROR_NOT_FOUND -2
// #endif
// #ifndef FVM_ERROR_IO
// #define FVM_ERROR_IO -3
// #endif
// #ifndef FVM_ERROR_INVALID_ARGUMENT
// #define FVM_ERROR_INVALID_ARGUMENT -4
// #endif
// #ifndef FVM_ERROR_PANIC
// #define FVM_ERROR_PANIC -5
// #endif
import "C"
import (
"fmt"
Expand Down
2 changes: 2 additions & 0 deletions cgo/fvm.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build fvm

package cgo

/*
Expand Down
69 changes: 7 additions & 62 deletions cgo/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type RegisteredAggregationProof C.RegisteredAggregationProof_t
type RegisteredPoStProof C.RegisteredPoStProof_t
type RegisteredUpdateProof C.RegisteredUpdateProof_t

type FvmRegisteredVersion C.FvmRegisteredVersion_t
// FVM types moved to types_fvm.go behind build tag

type AggregationInputs C.AggregationInputs_t

Expand Down Expand Up @@ -49,8 +49,7 @@ type ByteArray32 C.uint8_32_array_t
type ByteArray48 C.uint8_48_array_t
type ByteArray96 C.uint8_96_array_t

type FvmMachine C.InnerFvmMachine_t
type FvmMachineExecuteResponse C.FvmMachineExecuteResponse_t
// FVM types moved to types_fvm.go behind build tag

type resultBool C.Result_bool_t
type resultGeneratePieceCommitment C.Result_GeneratePieceCommitment_t
Expand All @@ -70,8 +69,7 @@ type resultGenerateFallbackSectorChallenges C.Result_GenerateFallbackSectorChall
type resultGenerateSingleWindowPoStWithVanilla C.Result_GenerateSingleWindowPoStWithVanilla_t
type resultPoStProof C.Result_PoStProof_t

type resultFvmMachine C.Result_InnerFvmMachine_ptr_t
type resultFvmMachineExecuteResponse C.Result_FvmMachineExecuteResponse_t
// FVM types moved to types_fvm.go behind build tag

type result interface {
statusCode() FCPResponseStatus
Expand Down Expand Up @@ -606,63 +604,10 @@ func (ptr *PoStProof) Destroy() {
}
}

func (ptr *resultFvmMachineExecuteResponse) statusCode() FCPResponseStatus {
return FCPResponseStatus(ptr.status_code)
}

func (ptr *resultFvmMachineExecuteResponse) errorMsg() *SliceBoxedUint8 {
return (*SliceBoxedUint8)(&ptr.error_msg)
}

func (ptr *resultFvmMachineExecuteResponse) destroy() {
if ptr != nil {
C.destroy_fvm_machine_execute_response((*C.Result_FvmMachineExecuteResponse_t)(ptr))
ptr = nil
}
}

func (ptr *resultFvmMachine) statusCode() FCPResponseStatus {
return FCPResponseStatus(ptr.status_code)
}
// FVM helpers moved to types_fvm.go

func (ptr *resultFvmMachine) errorMsg() *SliceBoxedUint8 {
return (*SliceBoxedUint8)(&ptr.error_msg)
}
// FVM helpers moved to types_fvm.go

func (ptr *resultFvmMachine) destroy() {
if ptr != nil {
C.destroy_create_fvm_machine_response((*C.Result_InnerFvmMachine_ptr_t)(ptr))
ptr = nil
}
}
// FVM helpers moved to types_fvm.go

func (ptr *FvmMachine) Destroy() {
if ptr != nil {
C.drop_fvm_machine((*C.InnerFvmMachine_t)(ptr))
ptr = nil
}
}

func (r FvmMachineExecuteResponse) copy() FvmMachineExecuteResponseGo {
return FvmMachineExecuteResponseGo{
ExitCode: uint64(r.exit_code),
ReturnVal: (*SliceBoxedUint8)(&r.return_val).copy(),
GasUsed: uint64(r.gas_used),
PenaltyHi: uint64(r.penalty_hi),
PenaltyLo: uint64(r.penalty_lo),
MinerTipHi: uint64(r.miner_tip_hi),
MinerTipLo: uint64(r.miner_tip_lo),
BaseFeeBurnHi: uint64(r.base_fee_burn_hi),
BaseFeeBurnLo: uint64(r.base_fee_burn_lo),
OverEstimationBurnHi: uint64(r.over_estimation_burn_hi),
OverEstimationBurnLo: uint64(r.over_estimation_burn_lo),
RefundHi: uint64(r.refund_hi),
RefundLo: uint64(r.refund_lo),
GasRefund: int64(r.gas_refund),
GasBurned: int64(r.gas_burned),
ExecTrace: (*SliceBoxedUint8)(&r.exec_trace).copy(),
FailureInfo: string((*SliceBoxedUint8)(&r.failure_info).slice()),
Events: (*SliceBoxedUint8)(&r.events).copy(),
EventsRoot: (*SliceBoxedUint8)(&r.events_root).copy(),
}
}
// FVM helpers moved to types_fvm.go
80 changes: 80 additions & 0 deletions cgo/types_fvm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//go:build fvm

package cgo

/*
#cgo LDFLAGS: -L${SRCDIR}/..
#cgo pkg-config: ${SRCDIR}/../filcrypto.pc
#include "../filcrypto.h"
#include <stdlib.h>
*/
import "C"

type FvmRegisteredVersion C.FvmRegisteredVersion_t

type FvmMachine C.InnerFvmMachine_t
type FvmMachineExecuteResponse C.FvmMachineExecuteResponse_t

type resultFvmMachine C.Result_InnerFvmMachine_ptr_t
type resultFvmMachineExecuteResponse C.Result_FvmMachineExecuteResponse_t

func (ptr *resultFvmMachineExecuteResponse) statusCode() FCPResponseStatus {
return FCPResponseStatus(ptr.status_code)
}

func (ptr *resultFvmMachineExecuteResponse) errorMsg() *SliceBoxedUint8 {
return (*SliceBoxedUint8)(&ptr.error_msg)
}

func (ptr *resultFvmMachineExecuteResponse) destroy() {
if ptr != nil {
C.destroy_fvm_machine_execute_response((*C.Result_FvmMachineExecuteResponse_t)(ptr))
ptr = nil
}
}

func (ptr *resultFvmMachine) statusCode() FCPResponseStatus {
return FCPResponseStatus(ptr.status_code)
}

func (ptr *resultFvmMachine) errorMsg() *SliceBoxedUint8 {
return (*SliceBoxedUint8)(&ptr.error_msg)
}

func (ptr *resultFvmMachine) destroy() {
if ptr != nil {
C.destroy_create_fvm_machine_response((*C.Result_InnerFvmMachine_ptr_t)(ptr))
ptr = nil
}
}

func (ptr *FvmMachine) Destroy() {
if ptr != nil {
C.drop_fvm_machine((*C.InnerFvmMachine_t)(ptr))
ptr = nil
}
}

func (r FvmMachineExecuteResponse) copy() FvmMachineExecuteResponseGo {
return FvmMachineExecuteResponseGo{
ExitCode: uint64(r.exit_code),
ReturnVal: (*SliceBoxedUint8)(&r.return_val).copy(),
GasUsed: uint64(r.gas_used),
PenaltyHi: uint64(r.penalty_hi),
PenaltyLo: uint64(r.penalty_lo),
MinerTipHi: uint64(r.miner_tip_hi),
MinerTipLo: uint64(r.miner_tip_lo),
BaseFeeBurnHi: uint64(r.base_fee_burn_hi),
BaseFeeBurnLo: uint64(r.base_fee_burn_lo),
OverEstimationBurnHi: uint64(r.over_estimation_burn_hi),
OverEstimationBurnLo: uint64(r.over_estimation_burn_lo),
RefundHi: uint64(r.refund_hi),
RefundLo: uint64(r.refund_lo),
GasRefund: int64(r.gas_refund),
GasBurned: int64(r.gas_burned),
ExecTrace: (*SliceBoxedUint8)(&r.exec_trace).copy(),
FailureInfo: string((*SliceBoxedUint8)(&r.failure_info).slice()),
Events: (*SliceBoxedUint8)(&r.events).copy(),
EventsRoot: (*SliceBoxedUint8)(&r.events_root).copy(),
}
}
3 changes: 2 additions & 1 deletion fvm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//go:build cgo && (amd64 || arm64 || riscv64)
//go:build cgo && (amd64 || arm64 || riscv64) && fvm
// +build cgo
// +build amd64 arm64 riscv64
// +build fvm

package ffi

Expand Down
92 changes: 92 additions & 0 deletions fvm_stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//go:build !fvm

package ffi

import (
"context"
"errors"

"github.com/filecoin-project/filecoin-ffi/cgo"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/network"
"github.com/ipfs/go-cid"
"golang.org/x/xerrors"
)

type FVM struct{}

type FVMOpts struct {
FVMVersion uint64
Externs cgo.Externs

Epoch abi.ChainEpoch
Timestamp uint64
ChainID uint64
BaseFee abi.TokenAmount
BaseCircSupply abi.TokenAmount
NetworkVersion network.Version
StateBase cid.Cid
Tracing bool
FlushAllBlocks bool

Debug bool
ActorRedirect cid.Cid
}

func CreateFVM(opts *FVMOpts) (*FVM, error) {
return nil, errors.New("FVM support not built in this binary")
}

func (f *FVM) ApplyMessage(_ []byte, _ uint) (*ApplyRet, error) {
return nil, errors.New("FVM support not built in this binary")
}

func (f *FVM) ApplyImplicitMessage(_ []byte) (*ApplyRet, error) {
return nil, errors.New("FVM support not built in this binary")
}

func (f *FVM) Flush() (cid.Cid, error) {
return cid.Undef, errors.New("FVM support not built in this binary")
}

// Minimal stubs to satisfy references
type ApplyRet struct {
Return []byte
ExitCode uint64
GasUsed int64
MinerPenalty abi.TokenAmount
MinerTip abi.TokenAmount
BaseFeeBurn abi.TokenAmount
OverEstimationBurn abi.TokenAmount
Refund abi.TokenAmount
GasRefund int64
GasBurned int64
ExecTraceBytes []byte
FailureInfo string
EventsRoot *cid.Cid
EventsBytes []byte
}

// Ensure cgo.Externs referenced to avoid unused import when stubbed
var _ = func() any { return context.TODO() }

// splitBigInt splits a big.Int into high and low uint64 values.
// This is used by tests and needs to be available even when FVM is not built.
func splitBigInt(i big.Int) (hi uint64, lo uint64, err error) {
if i.Sign() < 0 {
return 0, 0, xerrors.Errorf("negative number: %s", i)
}
words := i.Bits()
switch len(words) {
case 2:
hi = uint64(words[1])
fallthrough
case 1:
lo = uint64(words[0])
case 0:
default:
return 0, 0, xerrors.Errorf("exceeds max bigint size: %s", i)
}
return hi, lo, nil
}
17 changes: 12 additions & 5 deletions install-filcrypto
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,21 @@ build_from_source() {
use_fixed_rows_to_discard=",fixed-rows-to-discard"
fi

additional_flags=""
# Add feature specific rust flags as needed here.
# Collect features into an array, stripping leading commas as we go
features=()
[ -n "${use_multicore_sdr}" ] && features+=("${use_multicore_sdr}")
[ -n "${gpu_flags}" ] && features+=("$(echo "${gpu_flags}" | sed 's/^,//')")
[ "${FFI_USE_FVM}" == "1" ] && features+=("fvm")
[ -n "${use_fixed_rows_to_discard}" ] && features+=("$(echo "${use_fixed_rows_to_discard}" | sed 's/^,//')")
if [ "${FFI_USE_BLST_PORTABLE}" == "1" ] || [ "${FFI_PORTABLE}" == "1" ]; then
additional_flags="${additional_flags} --no-default-features --features ${use_multicore_sdr},blst-portable${gpu_flags}${use_fixed_rows_to_discard}"
else
additional_flags="${additional_flags} --no-default-features --features ${use_multicore_sdr}${gpu_flags}${use_fixed_rows_to_discard}"
features=("blst-portable" "${features[@]}")
fi

# Join features with commas
feature_list=$(IFS=','; echo "${features[*]}")

additional_flags="--no-default-features --features ${feature_list}"

echo "Using additional build flags: ${additional_flags}"
if [ -n "${__release_flags}" ]; then
RUSTFLAGS="-C target-feature=${__release_flags}" ./scripts/build-release.sh ${build} "${additional_flags}"
Expand Down
29 changes: 20 additions & 9 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ rayon = "1.10.0"
anyhow = "1.0.97"
serde_json = "1.0.140"
rust-gpu-tools = { version = "0.7", optional = true, default-features = false }
fvm4 = { package = "fvm", version = "~4.7.4", default-features = false, features = ["verify-signature", "nv28-dev"] }
fvm4_shared = { package = "fvm_shared", version = "~4.7.4" }
fvm3 = { package = "fvm", version = "~3.13.0", default-features = false }
fvm3_shared = { package = "fvm_shared", version = "~3.13.0" }
fvm2 = { package = "fvm", version = "~2.11.0", default-features = false }
fvm2_shared = { package = "fvm_shared", version = "~2.11.0" }
fvm_ipld_encoding = "0.5.3"
fvm_ipld_blockstore = "0.3.1"
fvm4 = { package = "fvm", version = "~4.7.4", default-features = false, features = ["verify-signature", "nv28-dev"], optional = true }
fvm4_shared = { package = "fvm_shared", version = "~4.7.4", optional = true }
fvm3 = { package = "fvm", version = "~3.13.0", default-features = false, optional = true }
fvm3_shared = { package = "fvm_shared", version = "~3.13.0", optional = true }
fvm2 = { package = "fvm", version = "~2.11.0", default-features = false, optional = true }
fvm2_shared = { package = "fvm_shared", version = "~2.11.0", optional = true }
fvm_ipld_encoding = { version = "0.5.3", optional = true }
fvm_ipld_blockstore = { version = "0.3.1", optional = true }
num-traits = "0.2.19"
cid = { version = "0.11.1", features = ["serde"], default-features = false }
lazy_static = "1.5.0"
Expand All @@ -54,7 +54,18 @@ memmap2 = "0.9"
tempfile = "3.19.1"

[features]
default = ["cuda", "multicore-sdr"]
default = ["cuda", "multicore-sdr", "fvm"]
fvm = [
"fvm2",
"fvm3",
"fvm4",
"fvm2_shared",
"fvm3_shared",
"fvm4_shared",
"fvm_ipld_encoding",
"fvm_ipld_blockstore",
]
curio = []
blst-portable = ["bls-signatures/blst-portable", "blstrs/portable"]
cuda = [
"filecoin-proofs-api/cuda",
Expand Down
9 changes: 6 additions & 3 deletions rust/scripts/build-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,13 @@ main() {
fi

# generate filcrypto.h
# The header files are the same even without having any features enables,
# this reduces the compile time and makes it work on more platforms.
# Check if FVM is in the build features - if so, include it in header generation
local __header_features="c-headers"
if echo "${@:2}" | grep -q "fvm"; then
__header_features="c-headers,fvm"
fi
RUSTFLAGS="${__rust_flags}" HEADER_DIR="." \
cargo test --no-default-features --locked build_headers --features c-headers
cargo test --no-default-features --locked build_headers --features ${__header_features}

# generate pkg-config
#
Expand Down
Loading
Loading