2020-09-16 14:34:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-09-16 15:54:22 +00:00
|
|
|
"bufio"
|
2020-09-16 14:34:54 +00:00
|
|
|
"context"
|
2020-09-16 15:54:22 +00:00
|
|
|
"errors"
|
2020-09-16 14:34:54 +00:00
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
"github.com/minio/blake2b-simd"
|
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
|
|
|
|
2020-09-16 14:34:54 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-04-20 21:34:28 +00:00
|
|
|
prooftypes "github.com/filecoin-project/go-state-types/proof"
|
|
|
|
|
2022-06-17 11:52:19 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/storiface"
|
2020-09-16 14:34:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type cachingVerifier struct {
|
|
|
|
ds datastore.Datastore
|
2022-06-17 11:52:19 +00:00
|
|
|
backend storiface.Verifier
|
2020-09-16 14:34:54 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 15:54:22 +00:00
|
|
|
const bufsize = 128
|
|
|
|
|
|
|
|
func (cv cachingVerifier) withCache(execute func() (bool, error), param cbg.CBORMarshaler) (bool, error) {
|
|
|
|
hasher := blake2b.New256()
|
|
|
|
wr := bufio.NewWriterSize(hasher, bufsize)
|
|
|
|
err := param.MarshalCBOR(wr)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("could not marshal call info: %+v", err)
|
|
|
|
return execute()
|
|
|
|
}
|
|
|
|
err = wr.Flush()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("could not flush: %+v", err)
|
|
|
|
return execute()
|
|
|
|
}
|
|
|
|
hash := hasher.Sum(nil)
|
|
|
|
key := datastore.NewKey(string(hash))
|
2021-12-13 12:46:49 +00:00
|
|
|
fromDs, err := cv.ds.Get(context.Background(), key)
|
2020-09-16 15:54:22 +00:00
|
|
|
if err == nil {
|
|
|
|
switch fromDs[0] {
|
|
|
|
case 's':
|
|
|
|
return true, nil
|
|
|
|
case 'f':
|
|
|
|
return false, nil
|
|
|
|
case 'e':
|
|
|
|
return false, errors.New(string(fromDs[1:]))
|
|
|
|
default:
|
|
|
|
log.Errorf("bad cached result in cache %s(%x)", fromDs[0], fromDs[0])
|
|
|
|
return execute()
|
|
|
|
}
|
|
|
|
} else if errors.Is(err, datastore.ErrNotFound) {
|
|
|
|
// recalc
|
|
|
|
ok, err := execute()
|
|
|
|
var save []byte
|
|
|
|
if err != nil {
|
|
|
|
if ok {
|
2020-09-16 22:06:20 +00:00
|
|
|
log.Errorf("success with an error: %+v", err)
|
2020-09-16 15:54:22 +00:00
|
|
|
} else {
|
|
|
|
save = append([]byte{'e'}, []byte(err.Error())...)
|
|
|
|
}
|
|
|
|
} else if ok {
|
|
|
|
save = []byte{'s'}
|
|
|
|
} else {
|
|
|
|
save = []byte{'f'}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(save) != 0 {
|
2021-12-13 12:46:49 +00:00
|
|
|
errSave := cv.ds.Put(context.Background(), key, save)
|
2020-09-16 15:54:22 +00:00
|
|
|
if errSave != nil {
|
|
|
|
log.Errorf("error saving result: %+v", errSave)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok, err
|
|
|
|
} else {
|
|
|
|
log.Errorf("could not get data from cache: %+v", err)
|
|
|
|
return execute()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:34:28 +00:00
|
|
|
func (cv *cachingVerifier) VerifySeal(svi prooftypes.SealVerifyInfo) (bool, error) {
|
2020-09-16 15:54:22 +00:00
|
|
|
return cv.withCache(func() (bool, error) {
|
|
|
|
return cv.backend.VerifySeal(svi)
|
|
|
|
}, &svi)
|
2020-09-16 14:34:54 +00:00
|
|
|
}
|
2020-10-08 01:09:33 +00:00
|
|
|
|
2022-04-20 21:34:28 +00:00
|
|
|
func (cv *cachingVerifier) VerifyWinningPoSt(ctx context.Context, info prooftypes.WinningPoStVerifyInfo) (bool, error) {
|
2022-01-11 16:53:14 +00:00
|
|
|
return cv.backend.VerifyWinningPoSt(ctx, info)
|
2020-09-16 14:34:54 +00:00
|
|
|
}
|
2022-01-11 16:53:14 +00:00
|
|
|
|
2022-04-20 21:34:28 +00:00
|
|
|
func (cv *cachingVerifier) VerifyWindowPoSt(ctx context.Context, info prooftypes.WindowPoStVerifyInfo) (bool, error) {
|
2020-09-16 15:54:22 +00:00
|
|
|
return cv.withCache(func() (bool, error) {
|
|
|
|
return cv.backend.VerifyWindowPoSt(ctx, info)
|
|
|
|
}, &info)
|
2020-09-16 14:34:54 +00:00
|
|
|
}
|
|
|
|
func (cv *cachingVerifier) GenerateWinningPoStSectorChallenge(ctx context.Context, proofType abi.RegisteredPoStProof, a abi.ActorID, rnd abi.PoStRandomness, u uint64) ([]uint64, error) {
|
|
|
|
return cv.backend.GenerateWinningPoStSectorChallenge(ctx, proofType, a, rnd, u)
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:34:28 +00:00
|
|
|
func (cv cachingVerifier) VerifyAggregateSeals(aggregate prooftypes.AggregateSealVerifyProofAndInfos) (bool, error) {
|
2021-05-17 18:47:41 +00:00
|
|
|
return cv.backend.VerifyAggregateSeals(aggregate)
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:34:28 +00:00
|
|
|
func (cv cachingVerifier) VerifyReplicaUpdate(update prooftypes.ReplicaUpdateInfo) (bool, error) {
|
2021-11-04 15:59:29 +00:00
|
|
|
return cv.backend.VerifyReplicaUpdate(update)
|
|
|
|
}
|
|
|
|
|
2022-06-17 11:52:19 +00:00
|
|
|
var _ storiface.Verifier = (*cachingVerifier)(nil)
|