Skip to content

Commit

Permalink
feat: add api StateComputeDataCID and refactor api `StateSectorPreC…
Browse files Browse the repository at this point in the history
…ommitInfo` (#5318)
  • Loading branch information
simlecode committed Oct 21, 2022
1 parent 8e4d2b6 commit 1682eca
Show file tree
Hide file tree
Showing 22 changed files with 636 additions and 500 deletions.
27 changes: 27 additions & 0 deletions app/submodule/chain/chaininfo_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,3 +719,30 @@ func (cia *chainInfoAPI) StateActorManifestCID(ctx context.Context, nv network.V

return c, nil
}

// StateCall runs the given message and returns its result without any persisted changes.
//
// StateCall applies the message to the tipset's parent state. The
// message is not applied on-top-of the messages in the passed-in
// tipset.
func (cia *chainInfoAPI) StateCall(ctx context.Context, msg *types.Message, tsk types.TipSetKey) (*types.InvocResult, error) {
start := time.Now()
ts, err := cia.chain.ChainReader.GetTipSet(ctx, tsk)
if err != nil {
return nil, fmt.Errorf("loading tipset %s: %v", tsk, err)
}
ret, err := cia.chain.Stmgr.Call(ctx, msg, ts)
if err != nil {
return nil, err
}
duration := time.Since(start)

mcid := msg.Cid()
return &types.InvocResult{
MsgCid: mcid,
Msg: msg,
MsgRct: &ret.Receipt,
ExecutionTrace: types.ExecutionTrace{},
Duration: duration,
}, nil
}
80 changes: 71 additions & 9 deletions app/submodule/chain/miner_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ import (
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/cbor"
"github.com/filecoin-project/go-state-types/dline"
"github.com/filecoin-project/go-state-types/network"
"github.com/ipfs/go-cid"
cbornode "github.com/ipfs/go-ipld-cbor"
"github.com/libp2p/go-libp2p-core/peer"
cbg "github.com/whyrusleeping/cbor-gen"

"github.com/filecoin-project/go-state-types/builtin/v8/miner"
market2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/market"
market5 "github.com/filecoin-project/specs-actors/v5/actors/builtin/market"
"github.com/filecoin-project/venus/pkg/state/tree"
"github.com/filecoin-project/venus/pkg/vm/register"
"github.com/filecoin-project/venus/venus-shared/actors"
"github.com/filecoin-project/venus/venus-shared/actors/adt"
"github.com/filecoin-project/venus/venus-shared/actors/builtin"
_init "github.com/filecoin-project/venus/venus-shared/actors/builtin/init"
Expand Down Expand Up @@ -60,19 +64,13 @@ func (msa *minerStateAPI) StateMinerSectorAllocated(ctx context.Context, maddr a
}

// StateSectorPreCommitInfo returns the PreCommit info for the specified miner's sector
func (msa *minerStateAPI) StateSectorPreCommitInfo(ctx context.Context, maddr address.Address, n abi.SectorNumber, tsk types.TipSetKey) (miner.SectorPreCommitOnChainInfo, error) {
func (msa *minerStateAPI) StateSectorPreCommitInfo(ctx context.Context, maddr address.Address, n abi.SectorNumber, tsk types.TipSetKey) (*miner.SectorPreCommitOnChainInfo, error) {
_, view, err := msa.Stmgr.ParentStateViewTsk(ctx, tsk)
if err != nil {
return miner.SectorPreCommitOnChainInfo{}, fmt.Errorf("loading tipset:%s parent state view: %v", tsk, err)
return nil, fmt.Errorf("loading tipset:%s parent state view: %v", tsk, err)
}

pci, err := view.SectorPreCommitInfo(ctx, maddr, n)
if err != nil {
return miner.SectorPreCommitOnChainInfo{}, err
} else if pci == nil {
return miner.SectorPreCommitOnChainInfo{}, fmt.Errorf("precommit info is not exists")
}
return *pci, nil
return view.SectorPreCommitInfo(ctx, maddr, n)
}

// StateSectorGetInfo returns the on-chain info for the specified miner's sector. Returns null in case the sector info isn't found
Expand Down Expand Up @@ -373,6 +371,70 @@ func (msa *minerStateAPI) StateMarketStorageDeal(ctx context.Context, dealID abi
}, nil
}

// StateComputeDataCID computes DataCID from a set of on-chain deals
func (msa *minerStateAPI) StateComputeDataCID(ctx context.Context, maddr address.Address, sectorType abi.RegisteredSealProof, deals []abi.DealID, tsk types.TipSetKey) (cid.Cid, error) {
nv, err := msa.API().StateNetworkVersion(ctx, tsk)
if err != nil {
return cid.Cid{}, err
}

var ccparams []byte
if nv < network.Version13 {
ccparams, err = actors.SerializeParams(&market2.ComputeDataCommitmentParams{
DealIDs: deals,
SectorType: sectorType,
})
} else {
ccparams, err = actors.SerializeParams(&market5.ComputeDataCommitmentParams{
Inputs: []*market5.SectorDataSpec{
{
DealIDs: deals,
SectorType: sectorType,
},
},
})
}

if err != nil {
return cid.Undef, fmt.Errorf("computing params for ComputeDataCommitment: %w", err)
}

ccmt := &types.Message{
To: market.Address,
From: maddr,
Value: types.NewInt(0),
Method: market.Methods.ComputeDataCommitment,
Params: ccparams,
}
r, err := msa.API().StateCall(ctx, ccmt, tsk)
if err != nil {
return cid.Undef, fmt.Errorf("calling ComputeDataCommitment: %w", err)
}
if r.MsgRct.ExitCode != 0 {
return cid.Undef, fmt.Errorf("receipt for ComputeDataCommitment had exit code %d", r.MsgRct.ExitCode)
}

if nv < network.Version13 {
var c cbg.CborCid
if err := c.UnmarshalCBOR(bytes.NewReader(r.MsgRct.Return)); err != nil {
return cid.Undef, fmt.Errorf("failed to unmarshal CBOR to CborCid: %w", err)
}

return cid.Cid(c), nil
}

var cr market5.ComputeDataCommitmentReturn
if err := cr.UnmarshalCBOR(bytes.NewReader(r.MsgRct.Return)); err != nil {
return cid.Undef, fmt.Errorf("failed to unmarshal CBOR to CborCid: %w", err)
}

if len(cr.CommDs) != 1 {
return cid.Undef, fmt.Errorf("CommD output must have 1 entry")
}

return cid.Cid(cr.CommDs[0]), nil
}

var initialPledgeNum = big.NewInt(110)
var initialPledgeDen = big.NewInt(100)

Expand Down
15 changes: 15 additions & 0 deletions app/submodule/chain/v0api/chaininfo_v0api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package v0api

import (
"context"
"fmt"

"github.com/filecoin-project/go-address"
v0api "github.com/filecoin-project/venus/venus-shared/api/chain/v0"
v1api "github.com/filecoin-project/venus/venus-shared/api/chain/v1"

"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/builtin/v8/miner"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/ipfs/go-cid"

Expand Down Expand Up @@ -60,3 +63,15 @@ func (a *WrapperV1IChain) ChainGetRandomnessFromBeacon(ctx context.Context, key
func (a *WrapperV1IChain) ChainGetRandomnessFromTickets(ctx context.Context, key types.TipSetKey, personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) (abi.Randomness, error) {
return a.StateGetRandomnessFromTickets(ctx, personalization, randEpoch, entropy, key)
}

func (a *WrapperV1IChain) StateSectorPreCommitInfo(ctx context.Context, maddr address.Address, n abi.SectorNumber, tsk types.TipSetKey) (miner.SectorPreCommitOnChainInfo, error) {
pi, err := a.IChain.StateSectorPreCommitInfo(ctx, maddr, n, tsk)
if err != nil {
return miner.SectorPreCommitOnChainInfo{}, err
}
if pi == nil {
return miner.SectorPreCommitOnChainInfo{}, fmt.Errorf("precommit info does not exist")
}

return *pi, nil
}
34 changes: 1 addition & 33 deletions app/submodule/syncer/syncer_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,39 +155,7 @@ func (sa *syncerAPI) SyncSubmitBlock(ctx context.Context, blk *types.BlockMsg) e
return nil
}

// MethodGroup: State
// The State methods are used to query, inspect, and interact with chain state.
// Most methods take a TipSetKey as a parameter. The state looked up is the parent state of the tipset.
// A nil TipSetKey can be provided as a param, this will cause the heaviest tipset in the chain to be used.

// StateCall runs the given message and returns its result without any persisted changes.
//
// StateCall applies the message to the tipset's parent state. The
// message is not applied on-top-of the messages in the passed-in
// tipset.
func (sa *syncerAPI) StateCall(ctx context.Context, msg *types.Message, tsk types.TipSetKey) (*types.InvocResult, error) {
start := time.Now()
ts, err := sa.syncer.ChainModule.ChainReader.GetTipSet(ctx, tsk)
if err != nil {
return nil, fmt.Errorf("loading tipset %s: %v", tsk, err)
}
ret, err := sa.syncer.Stmgr.Call(ctx, msg, ts)
if err != nil {
return nil, err
}
duration := time.Since(start)

mcid := msg.Cid()
return &types.InvocResult{
MsgCid: mcid,
Msg: msg,
MsgRct: &ret.Receipt,
ExecutionTrace: types.ExecutionTrace{},
Duration: duration,
}, nil
}

//SyncState just compatible code lotus
// SyncState just compatible code lotus
func (sa *syncerAPI) SyncState(ctx context.Context) (*types.SyncState, error) {
tracker := sa.syncer.ChainSyncManager.BlockProposer().SyncTracker()
tracker.History()
Expand Down
6 changes: 3 additions & 3 deletions cmd/dispute.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ var disputerMsgCmd = &cmds.Command{
Params: dpp,
}

rslt, err := env.(*node.Env).SyncerAPI.StateCall(req.Context, dmsg, types.EmptyTSK)
rslt, err := env.(*node.Env).ChainAPI.StateCall(req.Context, dmsg, types.EmptyTSK)
if err != nil {
return fmt.Errorf("failed to simulate dispute: %w", err)
}
Expand Down Expand Up @@ -239,7 +239,7 @@ var disputerStartCmd = &cmds.Command{
disputableProofs := fullDeadlines[dl.index].DisputableProofCount
proofsChecked += disputableProofs

ms, err := makeDisputeWindowedPosts(ctx, env.(*node.Env).SyncerAPI, dl, disputableProofs, fromAddr)
ms, err := makeDisputeWindowedPosts(ctx, env.(*node.Env).ChainAPI, dl, disputableProofs, fromAddr)
if err != nil {
return fmt.Errorf("failed to check for disputes: %w", err)
}
Expand Down Expand Up @@ -349,7 +349,7 @@ var disputerStartCmd = &cmds.Command{

// for a given miner, index, and maxPostIndex, tries to dispute posts from 0...postsSnapshotted-1
// returns a list of DisputeWindowedPoSt msgs that are expected to succeed if sent
func makeDisputeWindowedPosts(ctx context.Context, api v1api.ISyncer, dl minerDeadline, postsSnapshotted uint64, sender address.Address) ([]*types.Message, error) {
func makeDisputeWindowedPosts(ctx context.Context, api v1api.IChain, dl minerDeadline, postsSnapshotted uint64, sender address.Address) ([]*types.Message, error) {
disputes := make([]*types.Message, 0)

for i := uint64(0); i < postsSnapshotted; i++ {
Expand Down
3 changes: 2 additions & 1 deletion venus-shared/api/chain/v0/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ type IChainInfo interface {
// ChainGetGenesis returns the genesis tipset.
ChainGetGenesis(context.Context) (*types.TipSet, error) //perm:read
// StateActorManifestCID returns the CID of the builtin actors manifest for the given network version
StateActorManifestCID(context.Context, network.Version) (cid.Cid, error) //perm:read
StateActorManifestCID(context.Context, network.Version) (cid.Cid, error) //perm:read
StateCall(ctx context.Context, msg *types.Message, tsk types.TipSetKey) (*types.InvocResult, error) //perm:read
}

type IMinerState interface {
Expand Down
Loading

0 comments on commit 1682eca

Please sign in to comment.