Skip to content

Commit

Permalink
ffi: s/verifier/proofs, incorporate GenerateUnsealedCID
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Apr 30, 2024
1 parent c95be8a commit ba0c691
Show file tree
Hide file tree
Showing 24 changed files with 258 additions and 69 deletions.
12 changes: 6 additions & 6 deletions chain/consensus/filcns/filecoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ import (
"github.com/filecoin-project/lotus/chain/actors/builtin/reward"
"github.com/filecoin-project/lotus/chain/beacon"
"github.com/filecoin-project/lotus/chain/consensus"
"github.com/filecoin-project/lotus/chain/proofs"
proofsffi "github.com/filecoin-project/lotus/chain/proofs/ffi"
"github.com/filecoin-project/lotus/chain/rand"
"github.com/filecoin-project/lotus/chain/stmgr"
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/verifier"
verifierffi "github.com/filecoin-project/lotus/chain/verifier/ffi"
"github.com/filecoin-project/lotus/chain/vm"
"github.com/filecoin-project/lotus/lib/async"
"github.com/filecoin-project/lotus/lib/sigs"
Expand All @@ -50,7 +50,7 @@ type FilecoinEC struct {
// the state manager handles making state queries
sm *stmgr.StateManager

verifier verifier.Verifier
verifier proofs.Verifier

genesis *types.TipSet
}
Expand Down Expand Up @@ -94,7 +94,7 @@ var RewardFunc = func(ctx context.Context, vmi vm.Interface, em stmgr.ExecMonito
return nil
}

func NewFilecoinExpectedConsensus(sm *stmgr.StateManager, beacon beacon.Schedule, verifier verifier.Verifier, genesis chain.Genesis) consensus.Consensus {
func NewFilecoinExpectedConsensus(sm *stmgr.StateManager, beacon beacon.Schedule, verifier proofs.Verifier, genesis chain.Genesis) consensus.Consensus {
if build.InsecurePoStValidation {
log.Warn("*********************************************************************************************")
log.Warn(" [INSECURE-POST-VALIDATION] Insecure test validation is enabled. If you see this outside of a test, it is a severe bug! ")
Expand Down Expand Up @@ -367,8 +367,8 @@ func (filec *FilecoinEC) VerifyWinningPoStProof(ctx context.Context, nv network.
}
}

// TODO(rvagg): why is this using verifierffi.ProofVerifier.VerifyWinningPoSt directly and not filec.verifier.VerifyWinningPoSt?
ok, err := verifierffi.ProofVerifier.VerifyWinningPoSt(ctx, proof.WinningPoStVerifyInfo{
// TODO(rvagg): why is this using proofsffi.ProofVerifier.VerifyWinningPoSt directly and not filec.verifier.VerifyWinningPoSt?
ok, err := proofsffi.ProofVerifier.VerifyWinningPoSt(ctx, proof.WinningPoStVerifyInfo{
Randomness: rand,
Proofs: h.WinPoStProof,
ChallengedSectors: sectors,
Expand Down
10 changes: 5 additions & 5 deletions chain/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ import (
"github.com/filecoin-project/lotus/chain/consensus/filcns"
genesis2 "github.com/filecoin-project/lotus/chain/gen/genesis"
"github.com/filecoin-project/lotus/chain/index"
"github.com/filecoin-project/lotus/chain/proofs"
proofsffi "github.com/filecoin-project/lotus/chain/proofs/ffi"
"github.com/filecoin-project/lotus/chain/rand"
"github.com/filecoin-project/lotus/chain/stmgr"
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/verifier"
verifierffi "github.com/filecoin-project/lotus/chain/verifier/ffi"
"github.com/filecoin-project/lotus/chain/vm"
"github.com/filecoin-project/lotus/chain/wallet"
"github.com/filecoin-project/lotus/cmd/lotus-seed/seed"
Expand Down Expand Up @@ -349,7 +349,7 @@ func CarWalkFunc(nd format.Node) (out []*format.Link, err error) {
}

func (cg *ChainGen) nextBlockProof(ctx context.Context, pts *types.TipSet, m address.Address, round abi.ChainEpoch) ([]types.BeaconEntry, *types.ElectionProof, *types.Ticket, error) {
mc := &mca{w: cg.w, sm: cg.sm, pv: verifierffi.ProofVerifier, bcn: cg.beacon}
mc := &mca{w: cg.w, sm: cg.sm, pv: proofsffi.ProofVerifier, bcn: cg.beacon}

mbi, err := mc.MinerGetBaseInfo(ctx, m, round, pts.Key())
if err != nil {
Expand Down Expand Up @@ -602,7 +602,7 @@ type MiningCheckAPI interface {
type mca struct {
w *wallet.LocalWallet
sm *stmgr.StateManager
pv verifier.Verifier
pv proofs.Verifier
bcn beacon.Schedule
}

Expand Down Expand Up @@ -684,7 +684,7 @@ func ComputeVRF(ctx context.Context, sign SignFunc, worker address.Address, sigI

type genFakeVerifier struct{}

var _ verifier.Verifier = (*genFakeVerifier)(nil)
var _ proofs.Verifier = (*genFakeVerifier)(nil)

func (m genFakeVerifier) VerifySeal(svi proof7.SealVerifyInfo) (bool, error) {
return true, nil
Expand Down
72 changes: 72 additions & 0 deletions chain/proofs/ffi/proofs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package proofsffi

import (
"math/bits"

"github.com/ipfs/go-cid"

ffi "github.com/filecoin-project/filecoin-ffi"
"github.com/filecoin-project/go-commp-utils/zerocomm"
"github.com/filecoin-project/go-state-types/abi"
)

func GenerateUnsealedCID(proofType abi.RegisteredSealProof, pieces []abi.PieceInfo) (cid.Cid, error) {
ssize, err := proofType.SectorSize()
if err != nil {
return cid.Undef, err
}

pssize := abi.PaddedPieceSize(ssize)
allPieces := make([]abi.PieceInfo, 0, len(pieces))
if len(pieces) == 0 {
allPieces = append(allPieces, abi.PieceInfo{
Size: pssize,
PieceCID: zerocomm.ZeroPieceCommitment(pssize.Unpadded()),
})
} else {
var sum abi.PaddedPieceSize

padTo := func(pads []abi.PaddedPieceSize) {
for _, p := range pads {
allPieces = append(allPieces, abi.PieceInfo{
Size: p,
PieceCID: zerocomm.ZeroPieceCommitment(p.Unpadded()),
})

sum += p
}
}

for _, p := range pieces {
ps, _ := GetRequiredPadding(sum, p.Size)
padTo(ps)

allPieces = append(allPieces, p)
sum += p.Size
}

ps, _ := GetRequiredPadding(sum, pssize)
padTo(ps)
}

return ffi.GenerateUnsealedCID(proofType, allPieces)
}

func GetRequiredPadding(oldLength abi.PaddedPieceSize, newPieceLength abi.PaddedPieceSize) ([]abi.PaddedPieceSize, abi.PaddedPieceSize) {
padPieces := make([]abi.PaddedPieceSize, 0)
toFill := uint64(-oldLength % newPieceLength)

n := bits.OnesCount64(toFill)
var sum abi.PaddedPieceSize
for i := 0; i < n; i++ {
next := bits.TrailingZeros64(toFill)
psize := uint64(1) << uint(next)
toFill ^= psize

padded := abi.PaddedPieceSize(psize)
padPieces = append(padPieces, padded)
sum += padded
}

return padPieces, sum
}
117 changes: 117 additions & 0 deletions chain/proofs/ffi/proofs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package proofsffi_test

import (
"bytes"
"testing"

"github.com/ipfs/go-cid"
"github.com/stretchr/testify/require"

ffi "github.com/filecoin-project/filecoin-ffi"
commpffi "github.com/filecoin-project/go-commp-utils/ffiwrapper"
"github.com/filecoin-project/go-state-types/abi"

proofsffi "github.com/filecoin-project/lotus/chain/proofs/ffi"
)

func TestGenerateUnsealedCID(t *testing.T) {
pt := abi.RegisteredSealProof_StackedDrg2KiBV1
ups := int(abi.PaddedPieceSize(2048).Unpadded())

commP := func(b []byte) cid.Cid {
pf, werr, err := commpffi.ToReadableFile(bytes.NewReader(b), int64(len(b)))
require.NoError(t, err)

c, err := ffi.GeneratePieceCIDFromFile(pt, pf, abi.UnpaddedPieceSize(len(b)))
require.NoError(t, err)

require.NoError(t, werr())

return c
}

testCommEq := func(name string, in [][]byte, expect [][]byte) {
t.Run(name, func(t *testing.T) {
upi := make([]abi.PieceInfo, len(in))
for i, b := range in {
upi[i] = abi.PieceInfo{
Size: abi.UnpaddedPieceSize(len(b)).Padded(),
PieceCID: commP(b),
}
}

sectorPi := []abi.PieceInfo{
{
Size: 2048,
PieceCID: commP(bytes.Join(expect, nil)),
},
}

expectCid, err := proofsffi.GenerateUnsealedCID(pt, sectorPi)
require.NoError(t, err)

actualCid, err := proofsffi.GenerateUnsealedCID(pt, upi)
require.NoError(t, err)

require.Equal(t, expectCid, actualCid)
})
}

barr := func(b byte, den int) []byte {
return bytes.Repeat([]byte{b}, ups/den)
}

// 0000
testCommEq("zero",
nil,
[][]byte{barr(0, 1)},
)

// 1111
testCommEq("one",
[][]byte{barr(1, 1)},
[][]byte{barr(1, 1)},
)

// 11 00
testCommEq("one|2",
[][]byte{barr(1, 2)},
[][]byte{barr(1, 2), barr(0, 2)},
)

// 1 0 00
testCommEq("one|4",
[][]byte{barr(1, 4)},
[][]byte{barr(1, 4), barr(0, 4), barr(0, 2)},
)

// 11 2 0
testCommEq("one|2-two|4",
[][]byte{barr(1, 2), barr(2, 4)},
[][]byte{barr(1, 2), barr(2, 4), barr(0, 4)},
)

// 1 0 22
testCommEq("one|4-two|2",
[][]byte{barr(1, 4), barr(2, 2)},
[][]byte{barr(1, 4), barr(0, 4), barr(2, 2)},
)

// 1 0 22 0000
testCommEq("one|8-two|4",
[][]byte{barr(1, 8), barr(2, 4)},
[][]byte{barr(1, 8), barr(0, 8), barr(2, 4), barr(0, 2)},
)

// 11 2 0 0000
testCommEq("one|4-two|8",
[][]byte{barr(1, 4), barr(2, 8)},
[][]byte{barr(1, 4), barr(2, 8), barr(0, 8), barr(0, 2)},
)

// 1 0 22 3 0 00 4444 5 0 00
testCommEq("one|16-two|8-three|16-four|4-five|16",
[][]byte{barr(1, 16), barr(2, 8), barr(3, 16), barr(4, 4), barr(5, 16)},
[][]byte{barr(1, 16), barr(0, 16), barr(2, 8), barr(3, 16), barr(0, 16), barr(0, 8), barr(4, 4), barr(5, 16), barr(0, 16), barr(0, 8)},
)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build cgo
// +build cgo

package verifierffi
package proofsffi

import (
"context"
Expand All @@ -12,10 +12,10 @@ import (
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/proof"

"github.com/filecoin-project/lotus/chain/verifier"
"github.com/filecoin-project/lotus/chain/proofs"
)

var _ verifier.Verifier = ProofVerifier
var _ proofs.Verifier = ProofVerifier

type proofVerifier struct{}

Expand Down
4 changes: 2 additions & 2 deletions chain/verifier/mock/mock.go → chain/proofs/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"github.com/filecoin-project/go-state-types/abi"
prooftypes "github.com/filecoin-project/go-state-types/proof"

"github.com/filecoin-project/lotus/chain/verifier"
"github.com/filecoin-project/lotus/chain/proofs"
)

var _ verifier.Verifier = MockVerifier
var _ proofs.Verifier = MockVerifier

type mockVerifier struct{}

Expand Down
2 changes: 1 addition & 1 deletion chain/verifier/verifier.go → chain/proofs/verifier.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package verifier
package proofs

import (
"context"
Expand Down
6 changes: 3 additions & 3 deletions chain/stmgr/actors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
"github.com/filecoin-project/lotus/chain/actors/builtin/power"
"github.com/filecoin-project/lotus/chain/actors/builtin/verifreg"
"github.com/filecoin-project/lotus/chain/beacon"
"github.com/filecoin-project/lotus/chain/proofs"
"github.com/filecoin-project/lotus/chain/rand"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/verifier"
"github.com/filecoin-project/lotus/chain/vm"
)

Expand Down Expand Up @@ -118,7 +118,7 @@ func MinerSectorInfo(ctx context.Context, sm *StateManager, maddr address.Addres
return mas.GetSector(sid)
}

func GetSectorsForWinningPoSt(ctx context.Context, nv network.Version, pv verifier.Verifier, sm *StateManager, st cid.Cid, maddr address.Address, rand abi.PoStRandomness) ([]builtin.ExtendedSectorInfo, error) {
func GetSectorsForWinningPoSt(ctx context.Context, nv network.Version, pv proofs.Verifier, sm *StateManager, st cid.Cid, maddr address.Address, rand abi.PoStRandomness) ([]builtin.ExtendedSectorInfo, error) {
act, err := sm.LoadActorRaw(ctx, maddr, st)
if err != nil {
return nil, xerrors.Errorf("failed to load miner actor: %w", err)
Expand Down Expand Up @@ -302,7 +302,7 @@ func ListMinerActors(ctx context.Context, sm *StateManager, ts *types.TipSet) ([
return powState.ListAllMiners()
}

func MinerGetBaseInfo(ctx context.Context, sm *StateManager, bcs beacon.Schedule, tsk types.TipSetKey, round abi.ChainEpoch, maddr address.Address, pv verifier.Verifier) (*api.MiningBaseInfo, error) {
func MinerGetBaseInfo(ctx context.Context, sm *StateManager, bcs beacon.Schedule, tsk types.TipSetKey, round abi.ChainEpoch, maddr address.Address, pv proofs.Verifier) (*api.MiningBaseInfo, error) {
ts, err := sm.ChainStore().LoadTipSet(ctx, tsk)
if err != nil {
return nil, xerrors.Errorf("failed to load tipset for mining base: %w", err)
Expand Down
10 changes: 5 additions & 5 deletions chain/vm/syscalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import (
"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
"github.com/filecoin-project/lotus/chain/actors/policy"
"github.com/filecoin-project/lotus/chain/proofs"
proofsffi "github.com/filecoin-project/lotus/chain/proofs/ffi"
"github.com/filecoin-project/lotus/chain/state"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/verifier"
"github.com/filecoin-project/lotus/lib/sigs"
"github.com/filecoin-project/lotus/storage/sealer/ffiwrapper"
)

func init() {
Expand All @@ -39,7 +39,7 @@ func init() {

type SyscallBuilder func(ctx context.Context, rt *Runtime) runtime7.Syscalls

func Syscalls(verifier verifier.Verifier) SyscallBuilder {
func Syscalls(verifier proofs.Verifier) SyscallBuilder {
return func(ctx context.Context, rt *Runtime) runtime7.Syscalls {

return &syscallShim{
Expand All @@ -66,11 +66,11 @@ type syscallShim struct {
actor address.Address
cstate *state.StateTree
cst cbor.IpldStore
verifier verifier.Verifier
verifier proofs.Verifier
}

func (ss *syscallShim) ComputeUnsealedSectorCID(st abi.RegisteredSealProof, pieces []abi.PieceInfo) (cid.Cid, error) {
commd, err := ffiwrapper.GenerateUnsealedCID(st, pieces)
commd, err := proofsffi.GenerateUnsealedCID(st, pieces)
if err != nil {
log.Errorf("generate data commitment failed: %s", err)
return cid.Undef, err
Expand Down
Loading

0 comments on commit ba0c691

Please sign in to comment.