2021-06-12 01:39:15 +00:00
|
|
|
package mock
|
2021-05-19 00:01:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
|
2021-06-12 01:39:15 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2021-06-16 07:13:15 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2021-05-19 00:01:30 +00:00
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
prooftypes "github.com/filecoin-project/go-state-types/proof"
|
2023-11-13 23:59:34 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/storiface"
|
2021-06-12 01:39:15 +00:00
|
|
|
miner5 "github.com/filecoin-project/specs-actors/v5/actors/builtin/miner"
|
|
|
|
tutils "github.com/filecoin-project/specs-actors/v5/support/testing"
|
2021-05-19 00:01:30 +00:00
|
|
|
)
|
|
|
|
|
2022-06-14 18:03:38 +00:00
|
|
|
// Ideally, we'd use extern/sealer/mock. Unfortunately, those mocks are a bit _too_ accurate
|
2021-05-19 00:01:30 +00:00
|
|
|
// and would force us to load sector info for window post proofs.
|
|
|
|
|
|
|
|
const (
|
|
|
|
mockSealProofPrefix = "valid seal proof:"
|
|
|
|
mockAggregateSealProofPrefix = "valid aggregate seal proof:"
|
|
|
|
mockPoStProofPrefix = "valid post proof:"
|
|
|
|
)
|
|
|
|
|
2021-06-16 07:13:15 +00:00
|
|
|
var log = logging.Logger("simulation-mock")
|
|
|
|
|
2021-06-08 00:45:53 +00:00
|
|
|
// mockVerifier is a simple mock for verifying "fake" proofs.
|
|
|
|
type mockVerifier struct{}
|
|
|
|
|
2022-06-17 11:52:19 +00:00
|
|
|
var Verifier storiface.Verifier = mockVerifier{}
|
2021-06-08 00:45:53 +00:00
|
|
|
|
2022-04-20 21:34:28 +00:00
|
|
|
func (mockVerifier) VerifySeal(proof prooftypes.SealVerifyInfo) (bool, error) {
|
2021-06-08 00:45:53 +00:00
|
|
|
addr, err := address.NewIDAddress(uint64(proof.Miner))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-06-12 01:39:15 +00:00
|
|
|
mockProof, err := MockSealProof(proof.SealProof, addr)
|
2021-06-08 00:45:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-06-16 07:13:15 +00:00
|
|
|
if bytes.Equal(proof.Proof, mockProof) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
log.Debugw("invalid seal proof", "expected", mockProof, "actual", proof.Proof, "miner", addr)
|
|
|
|
return false, nil
|
2021-06-08 00:45:53 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 21:34:28 +00:00
|
|
|
func (mockVerifier) VerifyAggregateSeals(aggregate prooftypes.AggregateSealVerifyProofAndInfos) (bool, error) {
|
2021-06-08 00:45:53 +00:00
|
|
|
addr, err := address.NewIDAddress(uint64(aggregate.Miner))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-06-12 01:39:15 +00:00
|
|
|
mockProof, err := MockAggregateSealProof(aggregate.SealProof, addr, len(aggregate.Infos))
|
2021-06-08 00:45:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-06-16 07:13:15 +00:00
|
|
|
if bytes.Equal(aggregate.Proof, mockProof) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
log.Debugw("invalid aggregate seal proof",
|
|
|
|
"expected", mockProof,
|
|
|
|
"actual", aggregate.Proof,
|
|
|
|
"count", len(aggregate.Infos),
|
|
|
|
"miner", addr,
|
|
|
|
)
|
|
|
|
return false, nil
|
2021-06-08 00:45:53 +00:00
|
|
|
}
|
2021-11-04 15:59:29 +00:00
|
|
|
|
|
|
|
// TODO: do the thing
|
2022-04-20 21:34:28 +00:00
|
|
|
func (mockVerifier) VerifyReplicaUpdate(update prooftypes.ReplicaUpdateInfo) (bool, error) {
|
2021-11-04 15:59:29 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:34:28 +00:00
|
|
|
func (mockVerifier) VerifyWinningPoSt(ctx context.Context, info prooftypes.WinningPoStVerifyInfo) (bool, error) {
|
2021-06-08 00:45:53 +00:00
|
|
|
panic("should not be called")
|
|
|
|
}
|
2022-04-20 21:34:28 +00:00
|
|
|
func (mockVerifier) VerifyWindowPoSt(ctx context.Context, info prooftypes.WindowPoStVerifyInfo) (bool, error) {
|
2021-06-08 00:45:53 +00:00
|
|
|
if len(info.Proofs) != 1 {
|
|
|
|
return false, fmt.Errorf("expected exactly one proof")
|
|
|
|
}
|
|
|
|
proof := info.Proofs[0]
|
|
|
|
addr, err := address.NewIDAddress(uint64(info.Prover))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-06-12 01:39:15 +00:00
|
|
|
mockProof, err := MockWindowPoStProof(proof.PoStProof, addr)
|
2021-06-08 00:45:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-06-16 07:13:15 +00:00
|
|
|
if bytes.Equal(proof.ProofBytes, mockProof) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugw("invalid window post proof",
|
|
|
|
"expected", mockProof,
|
|
|
|
"actual", info.Proofs[0],
|
|
|
|
"miner", addr,
|
|
|
|
)
|
|
|
|
return false, nil
|
2021-06-08 00:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mockVerifier) GenerateWinningPoStSectorChallenge(context.Context, abi.RegisteredPoStProof, abi.ActorID, abi.PoStRandomness, uint64) ([]uint64, error) {
|
|
|
|
panic("should not be called")
|
|
|
|
}
|
|
|
|
|
2021-06-12 01:39:15 +00:00
|
|
|
// MockSealProof generates a mock "seal" proof tied to the specified proof type and the given miner.
|
|
|
|
func MockSealProof(proofType abi.RegisteredSealProof, minerAddr address.Address) ([]byte, error) {
|
2021-05-19 00:01:30 +00:00
|
|
|
plen, err := proofType.ProofSize()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
proof := make([]byte, plen)
|
|
|
|
i := copy(proof, mockSealProofPrefix)
|
|
|
|
binary.BigEndian.PutUint64(proof[i:], uint64(proofType))
|
|
|
|
i += 8
|
|
|
|
i += copy(proof[i:], minerAddr.Bytes())
|
|
|
|
return proof, nil
|
|
|
|
}
|
|
|
|
|
2021-06-12 01:39:15 +00:00
|
|
|
// MockAggregateSealProof generates a mock "seal" aggregate proof tied to the specified proof type,
|
2021-06-08 00:45:53 +00:00
|
|
|
// the given miner, and the number of proven sectors.
|
2021-06-12 01:39:15 +00:00
|
|
|
func MockAggregateSealProof(proofType abi.RegisteredSealProof, minerAddr address.Address, count int) ([]byte, error) {
|
2021-05-19 00:01:30 +00:00
|
|
|
proof := make([]byte, aggProofLen(count))
|
|
|
|
i := copy(proof, mockAggregateSealProofPrefix)
|
|
|
|
binary.BigEndian.PutUint64(proof[i:], uint64(proofType))
|
|
|
|
i += 8
|
|
|
|
binary.BigEndian.PutUint64(proof[i:], uint64(count))
|
|
|
|
i += 8
|
|
|
|
i += copy(proof[i:], minerAddr.Bytes())
|
|
|
|
|
|
|
|
return proof, nil
|
|
|
|
}
|
|
|
|
|
2021-06-12 01:39:15 +00:00
|
|
|
// MockWindowPoStProof generates a mock "window post" proof tied to the specified proof type, and the
|
2021-06-08 00:45:53 +00:00
|
|
|
// given miner.
|
2021-06-12 01:39:15 +00:00
|
|
|
func MockWindowPoStProof(proofType abi.RegisteredPoStProof, minerAddr address.Address) ([]byte, error) {
|
2021-05-19 00:01:30 +00:00
|
|
|
plen, err := proofType.ProofSize()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
proof := make([]byte, plen)
|
|
|
|
i := copy(proof, mockPoStProofPrefix)
|
|
|
|
i += copy(proof[i:], minerAddr.Bytes())
|
|
|
|
return proof, nil
|
|
|
|
}
|
|
|
|
|
2021-06-12 01:39:15 +00:00
|
|
|
// makeCommR generates a "fake" but valid CommR for a sector. It is unique for the given sector/miner.
|
|
|
|
func MockCommR(minerAddr address.Address, sno abi.SectorNumber) cid.Cid {
|
|
|
|
return tutils.MakeCID(fmt.Sprintf("%s:%d", minerAddr, sno), &miner5.SealedCIDPrefix)
|
|
|
|
}
|
|
|
|
|
2021-05-19 00:01:30 +00:00
|
|
|
// TODO: dedup
|
|
|
|
func aggProofLen(nproofs int) int {
|
|
|
|
switch {
|
|
|
|
case nproofs <= 8:
|
|
|
|
return 11220
|
|
|
|
case nproofs <= 16:
|
|
|
|
return 14196
|
|
|
|
case nproofs <= 32:
|
|
|
|
return 17172
|
|
|
|
case nproofs <= 64:
|
|
|
|
return 20148
|
|
|
|
case nproofs <= 128:
|
|
|
|
return 23124
|
|
|
|
case nproofs <= 256:
|
|
|
|
return 26100
|
|
|
|
case nproofs <= 512:
|
|
|
|
return 29076
|
|
|
|
case nproofs <= 1024:
|
|
|
|
return 32052
|
|
|
|
case nproofs <= 2048:
|
|
|
|
return 35028
|
|
|
|
case nproofs <= 4096:
|
|
|
|
return 38004
|
|
|
|
case nproofs <= 8192:
|
|
|
|
return 40980
|
|
|
|
default:
|
|
|
|
panic("too many proofs")
|
|
|
|
}
|
|
|
|
}
|