2019-12-06 14:06:42 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
2020-02-26 22:54:34 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2020-01-13 20:47:27 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-02-18 07:15:30 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
mh "github.com/multiformats/go-multihash"
|
|
|
|
"golang.org/x/xerrors"
|
2020-03-26 19:34:38 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/crypto"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/runtime"
|
|
|
|
|
2020-03-27 23:00:21 +00:00
|
|
|
"github.com/filecoin-project/sector-storage/ffiwrapper"
|
2019-12-06 14:06:42 +00:00
|
|
|
)
|
|
|
|
|
2020-02-18 07:15:30 +00:00
|
|
|
func init() {
|
|
|
|
mh.Codes[0xf104] = "filecoin"
|
|
|
|
}
|
|
|
|
|
2019-12-06 14:06:42 +00:00
|
|
|
// Actual type is defined in chain/types/vmcontext.go because the VMContext interface is there
|
|
|
|
|
2020-03-26 02:50:56 +00:00
|
|
|
func Syscalls(verifier ffiwrapper.Verifier) runtime.Syscalls {
|
2020-02-18 07:15:30 +00:00
|
|
|
return &syscallShim{verifier}
|
|
|
|
}
|
|
|
|
|
|
|
|
type syscallShim struct {
|
2020-03-26 02:50:56 +00:00
|
|
|
verifier ffiwrapper.Verifier
|
2020-02-18 07:15:30 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 00:42:39 +00:00
|
|
|
func (ss *syscallShim) ComputeUnsealedSectorCID(st abi.RegisteredProof, pieces []abi.PieceInfo) (cid.Cid, error) {
|
2020-02-23 00:47:47 +00:00
|
|
|
var sum abi.PaddedPieceSize
|
2020-02-18 07:15:30 +00:00
|
|
|
for _, p := range pieces {
|
2020-02-23 00:47:47 +00:00
|
|
|
sum += p.Size
|
2020-02-18 07:15:30 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 02:50:56 +00:00
|
|
|
commd, err := ffiwrapper.GenerateUnsealedCID(st, pieces)
|
2020-02-18 07:15:30 +00:00
|
|
|
if err != nil {
|
2020-02-23 00:47:47 +00:00
|
|
|
log.Errorf("generate data commitment failed: %s", err)
|
2020-02-18 07:15:30 +00:00
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
2020-02-27 00:42:39 +00:00
|
|
|
return commd, nil
|
2020-02-18 07:15:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *syscallShim) HashBlake2b(data []byte) [32]byte {
|
|
|
|
panic("NYI")
|
|
|
|
}
|
|
|
|
|
2020-03-09 04:21:46 +00:00
|
|
|
func (ss *syscallShim) VerifyConsensusFault(a, b, extra []byte, epoch abi.ChainEpoch) (*runtime.ConsensusFault, error) {
|
2020-02-18 07:15:30 +00:00
|
|
|
panic("NYI")
|
|
|
|
}
|
|
|
|
|
2020-02-26 22:54:34 +00:00
|
|
|
func (ss *syscallShim) VerifyPoSt(proof abi.PoStVerifyInfo) error {
|
|
|
|
ok, err := ss.verifier.VerifyFallbackPost(context.TODO(), proof)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("proof was invalid")
|
|
|
|
}
|
|
|
|
return nil
|
2020-02-18 07:15:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func cidToCommD(c cid.Cid) [32]byte {
|
|
|
|
b := c.Bytes()
|
|
|
|
var out [32]byte
|
|
|
|
copy(out[:], b[len(b)-32:])
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func cidToCommR(c cid.Cid) [32]byte {
|
|
|
|
b := c.Bytes()
|
|
|
|
var out [32]byte
|
|
|
|
copy(out[:], b[len(b)-32:])
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-02-26 22:54:34 +00:00
|
|
|
func (ss *syscallShim) VerifySeal(info abi.SealVerifyInfo) error {
|
2020-02-18 07:15:30 +00:00
|
|
|
//_, span := trace.StartSpan(ctx, "ValidatePoRep")
|
|
|
|
//defer span.End()
|
|
|
|
|
|
|
|
miner, err := address.NewIDAddress(uint64(info.Miner))
|
|
|
|
if err != nil {
|
2020-02-27 00:42:39 +00:00
|
|
|
return xerrors.Errorf("weirdly failed to construct address: %w", err)
|
2020-02-18 07:15:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ticket := []byte(info.Randomness)
|
|
|
|
proof := []byte(info.OnChain.Proof)
|
|
|
|
seed := []byte(info.InteractiveRandomness)
|
|
|
|
|
2020-02-28 20:52:14 +00:00
|
|
|
log.Infof("Verif r:%x; d:%x; m:%s; t:%x; s:%x; N:%d; p:%x", info.OnChain.SealedCID, info.UnsealedCID, miner, ticket, seed, info.SectorID.Number, proof)
|
2020-02-23 20:32:14 +00:00
|
|
|
|
2020-02-18 07:15:30 +00:00
|
|
|
//func(ctx context.Context, maddr address.Address, ssize abi.SectorSize, commD, commR, ticket, proof, seed []byte, sectorID abi.SectorNumber)
|
2020-02-27 00:42:39 +00:00
|
|
|
ok, err := ss.verifier.VerifySeal(info)
|
2020-02-18 07:15:30 +00:00
|
|
|
if err != nil {
|
2020-02-26 22:54:34 +00:00
|
|
|
return xerrors.Errorf("failed to validate PoRep: %w", err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid proof")
|
2020-02-18 07:15:30 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 22:54:34 +00:00
|
|
|
return nil
|
2020-02-18 07:15:30 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 22:54:34 +00:00
|
|
|
func (ss *syscallShim) VerifySignature(sig crypto.Signature, addr address.Address, input []byte) error {
|
|
|
|
return nil
|
2020-02-18 07:15:30 +00:00
|
|
|
/* // TODO: in genesis setup, we are currently faking signatures
|
|
|
|
if err := ss.rt.vmctx.VerifySignature(&sig, addr, input); err != nil {
|
|
|
|
return false
|
2019-12-06 14:06:42 +00:00
|
|
|
}
|
2020-02-18 07:15:30 +00:00
|
|
|
return true
|
|
|
|
*/
|
2019-12-06 14:06:42 +00:00
|
|
|
}
|