|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "code.riba.cloud/go/toolbox-interplanetary/fil" |
| 9 | + "code.riba.cloud/go/toolbox/cmn" |
| 10 | + "github.com/filecoin-project/go-address" |
| 11 | + "github.com/filecoin-project/go-state-types/abi" |
| 12 | + filabi "github.com/filecoin-project/go-state-types/abi" |
| 13 | + filbig "github.com/filecoin-project/go-state-types/big" |
| 14 | + filbuiltin "github.com/filecoin-project/go-state-types/builtin" |
| 15 | + "github.com/filecoin-project/lotus/api" |
| 16 | + "github.com/filecoin-project/lotus/chain/types" |
| 17 | + lru "github.com/hashicorp/golang-lru/v2" |
| 18 | + "github.com/storacha/spade/internal/app" |
| 19 | +) |
| 20 | + |
| 21 | +// AuthorizationLotusClient defines the minimal Filecoin client interface |
| 22 | +// required to support SP authorization. |
| 23 | +type AuthorizationLotusClient interface { |
| 24 | + // ChainGetTipSetByHeight looks back for a tipset at the specified epoch. |
| 25 | + ChainGetTipSetByHeight(context.Context, abi.ChainEpoch, types.TipSetKey) (*types.TipSet, error) |
| 26 | + // StateAccountKey retrieves the key address for an account at a given tipset. |
| 27 | + StateAccountKey(context.Context, address.Address, types.TipSetKey) (address.Address, error) |
| 28 | + // StateMinerInfo retrieves miner info at a given tipset. |
| 29 | + StateMinerInfo(context.Context, address.Address, types.TipSetKey) (api.MinerInfo, error) |
| 30 | +} |
| 31 | + |
| 32 | +// LookbackLotusClient defines the minimal Filecoin client interface required to |
| 33 | +// support lookback tipset retrieval. |
| 34 | +type LookbackLotusClient interface { |
| 35 | + // ChainHead returns the current head of the chain. |
| 36 | + ChainHead(context.Context) (*types.TipSet, error) |
| 37 | + // ChainGetTipSetByHeight looks back for a tipset at the specified epoch. |
| 38 | + ChainGetTipSetByHeight(context.Context, abi.ChainEpoch, types.TipSetKey) (*types.TipSet, error) |
| 39 | +} |
| 40 | + |
| 41 | +// ReservationLotusClient defines the minimal Filecoin client interface required |
| 42 | +// to support reservation eligibility checks and related operations. |
| 43 | +type ReservationLotusClient interface { |
| 44 | + LookbackLotusClient |
| 45 | + // MinerGetBaseInfo retrieves mining base info for a miner at a given tipset. |
| 46 | + MinerGetBaseInfo(context.Context, address.Address, abi.ChainEpoch, types.TipSetKey) (*api.MiningBaseInfo, error) |
| 47 | +} |
| 48 | + |
| 49 | +// SpadeLotusClient defines the minimal Lotus client interface required to |
| 50 | +// support all Spade service operations. |
| 51 | +type SpadeLotusClient interface { |
| 52 | + AuthorizationLotusClient |
| 53 | + ReservationLotusClient |
| 54 | +} |
| 55 | + |
| 56 | +var collateralCache, _ = lru.New[filabi.ChainEpoch, filbig.Int](128) |
| 57 | + |
| 58 | +func ProviderCollateralEstimateGiB(ctx context.Context, sourceEpoch filabi.ChainEpoch) (filbig.Int, error) { |
| 59 | + if pc, didFind := collateralCache.Get(sourceEpoch); didFind { |
| 60 | + return pc, nil |
| 61 | + } |
| 62 | + |
| 63 | + collateralGiB, err := app.EpochMinProviderCollateralEstimateGiB(ctx, sourceEpoch) |
| 64 | + if err != nil { |
| 65 | + return collateralGiB, cmn.WrErr(err) |
| 66 | + } |
| 67 | + |
| 68 | + // make it 1.7 times larger, so that fluctuations in the state won't prevent the deal from being proposed/published later |
| 69 | + // capped by https://github.com/filecoin-project/lotus/blob/v1.13.2-rc2/markets/storageadapter/provider.go#L267 |
| 70 | + // and https://github.com/filecoin-project/lotus/blob/v1.13.2-rc2/markets/storageadapter/provider.go#L41 |
| 71 | + inflatedCollateralGiB := filbig.Div( |
| 72 | + filbig.Product( |
| 73 | + collateralGiB, |
| 74 | + filbig.NewInt(17), |
| 75 | + ), |
| 76 | + filbig.NewInt(10), |
| 77 | + ) |
| 78 | + |
| 79 | + collateralCache.Add(sourceEpoch, inflatedCollateralGiB) |
| 80 | + return inflatedCollateralGiB, nil |
| 81 | +} |
| 82 | + |
| 83 | +// GetTipset retrieves the tipset at the specified lookback epoch. It is a |
| 84 | +// copy of [fil.GetTipset] adjusted to use the minimal interface |
| 85 | +// [LookbackLotusClient]. |
| 86 | +func GetTipset(ctx context.Context, lapi LookbackLotusClient, lookback uint) (*fil.LotusTS, error) { |
| 87 | + latestHead, err := lapi.ChainHead(ctx) |
| 88 | + if err != nil { |
| 89 | + return nil, fmt.Errorf("failed getting chain head: %w", err) |
| 90 | + } |
| 91 | + |
| 92 | + wallUnix := time.Now().Unix() |
| 93 | + filUnix := int64(latestHead.Blocks()[0].Timestamp) |
| 94 | + |
| 95 | + if wallUnix < filUnix-3 || // allow few seconds clock-drift tolerance |
| 96 | + wallUnix > filUnix+int64( |
| 97 | + fil.PropagationDelaySecs+(fil.APIMaxTipsetsBehind*filbuiltin.EpochDurationSeconds), |
| 98 | + ) { |
| 99 | + return nil, fmt.Errorf( |
| 100 | + "lotus API out of sync: chainHead reports unixtime %d (height: %d) while walltime is %d (delta: %s)", |
| 101 | + filUnix, |
| 102 | + latestHead.Height(), |
| 103 | + wallUnix, |
| 104 | + time.Second*time.Duration(wallUnix-filUnix), |
| 105 | + ) |
| 106 | + } |
| 107 | + |
| 108 | + if lookback == 0 { |
| 109 | + return latestHead, nil |
| 110 | + } |
| 111 | + |
| 112 | + latestHeight := latestHead.Height() |
| 113 | + tipsetAtLookback, err := lapi.ChainGetTipSetByHeight(ctx, latestHeight-filabi.ChainEpoch(lookback), latestHead.Key()) |
| 114 | + if err != nil { |
| 115 | + return nil, fmt.Errorf("determining target tipset %d epochs ago failed: %w", lookback, err) |
| 116 | + } |
| 117 | + |
| 118 | + return tipsetAtLookback, nil |
| 119 | +} |
0 commit comments