Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storage Client Statemachine Refactor #136

Merged
merged 5 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
Expand Down
138 changes: 138 additions & 0 deletions shared_testutil/test_network_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

rm "github.com/filecoin-project/go-fil-markets/retrievalmarket"
rmnet "github.com/filecoin-project/go-fil-markets/retrievalmarket/network"
smnet "github.com/filecoin-project/go-fil-markets/storagemarket/network"
)

// QueryReader is a function to mock reading queries.
Expand Down Expand Up @@ -501,3 +502,140 @@ func StubbedDealPaymentReader(payment rm.DealPayment) DealPaymentReader {
return payment, nil
}
}

// StorageDealProposalReader is a function to mock reading deal proposals.
type StorageDealProposalReader func() (smnet.Proposal, error)

// StorageDealResponseReader is a function to mock reading deal responses.
type StorageDealResponseReader func() (smnet.SignedResponse, error)

// StorageDealResponseWriter is a function to mock writing deal responses.
type StorageDealResponseWriter func(smnet.SignedResponse) error

// StorageDealProposalWriter is a function to mock writing deal proposals.
type StorageDealProposalWriter func(smnet.Proposal) error

// TestStorageDealStream is a retrieval deal stream with predefined
// stubbed behavior.
type TestStorageDealStream struct {
p peer.ID
proposalReader StorageDealProposalReader
proposalWriter StorageDealProposalWriter
responseReader StorageDealResponseReader
responseWriter StorageDealResponseWriter
}

// TestStorageDealStreamParams are parameters used to setup a TestStorageDealStream.
// All parameters except the peer ID are optional.
type TestStorageDealStreamParams struct {
PeerID peer.ID
ProposalReader StorageDealProposalReader
ProposalWriter StorageDealProposalWriter
ResponseReader StorageDealResponseReader
ResponseWriter StorageDealResponseWriter
}

// NewTestStorageDealStream returns a new TestStorageDealStream with the
// behavior specified by the paramaters, or default behaviors if not specified.
func NewTestStorageDealStream(params TestStorageDealStreamParams) smnet.StorageDealStream {
stream := TestStorageDealStream{
p: params.PeerID,
proposalReader: TrivialStorageDealProposalReader,
proposalWriter: TrivialStorageDealProposalWriter,
responseReader: TrivialStorageDealResponseReader,
responseWriter: TrivialStorageDealResponseWriter,
}
if params.ProposalReader != nil {
stream.proposalReader = params.ProposalReader
}
if params.ProposalWriter != nil {
stream.proposalWriter = params.ProposalWriter
}
if params.ResponseReader != nil {
stream.responseReader = params.ResponseReader
}
if params.ResponseWriter != nil {
stream.responseWriter = params.ResponseWriter
}
return &stream
}

// ReadDealProposal calls the mocked deal proposal reader function.
func (tsds *TestStorageDealStream) ReadDealProposal() (smnet.Proposal, error) {
return tsds.proposalReader()
}

// WriteDealProposal calls the mocked deal proposal writer function.
func (tsds *TestStorageDealStream) WriteDealProposal(dealProposal smnet.Proposal) error {
return tsds.proposalWriter(dealProposal)
}

// ReadDealResponse calls the mocked deal response reader function.
func (tsds *TestStorageDealStream) ReadDealResponse() (smnet.SignedResponse, error) {
return tsds.responseReader()
}

// WriteDealResponse calls the mocked deal response writer function.
func (tsds *TestStorageDealStream) WriteDealResponse(dealResponse smnet.SignedResponse) error {
return tsds.responseWriter(dealResponse)
}

// RemotePeer returns the other peer
func (tsds TestStorageDealStream) RemotePeer() peer.ID { return tsds.p }

// Close closes the stream (does nothing for mocked stream)
func (tsds TestStorageDealStream) Close() error { return nil }

// TrivialStorageDealProposalReader succeeds trivially, returning an empty proposal.
func TrivialStorageDealProposalReader() (smnet.Proposal, error) {
return smnet.Proposal{}, nil
}

// TrivialStorageDealResponseReader succeeds trivially, returning an empty deal response.
func TrivialStorageDealResponseReader() (smnet.SignedResponse, error) {
return smnet.SignedResponse{}, nil
}

// TrivialStorageDealProposalWriter succeeds trivially, returning no error.
func TrivialStorageDealProposalWriter(smnet.Proposal) error {
return nil
}

// TrivialStorageDealResponseWriter succeeds trivially, returning no error.
func TrivialStorageDealResponseWriter(smnet.SignedResponse) error {
return nil
}

// StubbedStorageProposalReader returns the given proposal when called
func StubbedStorageProposalReader(proposal smnet.Proposal) StorageDealProposalReader {
return func() (smnet.Proposal, error) {
return proposal, nil
}
}

// StubbedStorageResponseReader returns the given deal response when called
func StubbedStorageResponseReader(response smnet.SignedResponse) StorageDealResponseReader {
return func() (smnet.SignedResponse, error) {
return response, nil
}
}

// FailStorageProposalWriter always fails
func FailStorageProposalWriter(smnet.Proposal) error {
return errors.New("write proposal failed")
}

// FailStorageProposalReader always fails
func FailStorageProposalReader() (smnet.Proposal, error) {
return smnet.ProposalUndefined, errors.New("read proposal failed")
}

// FailStorageResponseWriter always fails
func FailStorageResponseWriter(smnet.SignedResponse) error {
return errors.New("write proposal failed")
}

// FailStorageResponseReader always fails
func FailStorageResponseReader() (smnet.SignedResponse, error) {
return smnet.SignedResponseUndefined, errors.New("read response failed")
}
30 changes: 30 additions & 0 deletions shared_testutil/test_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math/rand"
"testing"

cborutil "github.com/filecoin-project/go-cbor-util"
"github.com/filecoin-project/specs-actors/actors/builtin/market"

"github.com/filecoin-project/go-address"
Expand Down Expand Up @@ -141,6 +142,35 @@ func MakeTestClientDealProposal() *market.ClientDealProposal {
}
}

// MakeTestDataRef returns a storage market data ref
func MakeTestDataRef() *storagemarket.DataRef {
return &storagemarket.DataRef{
Root: GenerateCids(1)[0],
}
}

// MakeTestClientDeal returns a storage market client deal
func MakeTestClientDeal(state storagemarket.StorageDealStatus, clientDealProposal *market.ClientDealProposal) (*storagemarket.ClientDeal, error) {
proposalNd, err := cborutil.AsIpld(clientDealProposal)

if err != nil {
return nil, err
}

p, err := test.RandPeerID()
if err != nil {
return nil, err
}
return &storagemarket.ClientDeal{
ProposalCid: proposalNd.Cid(),
ClientDealProposal: *clientDealProposal,
State: state,
Miner: p,
MinerWorker: address.TestAddress2,
DataRef: MakeTestDataRef(),
}, nil
}

// MakeTestStorageAsk generates a storage ask
func MakeTestStorageAsk() *storagemarket.StorageAsk {
return &storagemarket.StorageAsk{
Expand Down
Loading