From 47d1356323f79674d36d0bb82e622353fac4fdc6 Mon Sep 17 00:00:00 2001 From: Erin Swenson-Healey Date: Tue, 17 Mar 2020 09:39:59 -0700 Subject: [PATCH] send state identifiery when getting miner worker address (#153) * GetMinerWorker to take chain state identifier * pass TipSetToken for chain head to GetMinerWorkerAddress * reorganize imports --- retrievalmarket/impl/provider.go | 10 +++++++++- .../impl/testnodes/test_retrieval_provider_node.go | 2 +- retrievalmarket/types.go | 6 +++--- storagemarket/impl/provider.go | 7 ++++++- storagemarket/impl/providerstates/provider_states.go | 7 ++++++- storagemarket/impl/providerutils/providerutils.go | 7 ++++--- .../impl/providerutils/providerutils_test.go | 7 ++++--- storagemarket/impl/storedask/storedask.go | 11 +++++++++-- storagemarket/testnodes/testnodes.go | 5 ++--- storagemarket/types.go | 2 +- 10 files changed, 45 insertions(+), 19 deletions(-) diff --git a/retrievalmarket/impl/provider.go b/retrievalmarket/impl/provider.go index 77dec9b2..03678a2d 100644 --- a/retrievalmarket/impl/provider.go +++ b/retrievalmarket/impl/provider.go @@ -171,7 +171,15 @@ func (p *provider) HandleQueryStream(stream rmnet.RetrievalQueryStream) { MaxPaymentIntervalIncrease: p.paymentIntervalIncrease, } - paymentAddress, err := p.node.GetMinerWorker(context.TODO(), p.minerAddress) + ctx := context.TODO() + + tok, _, err := p.node.GetChainHead(ctx) + if err != nil { + log.Errorf("Retrieval query: GetChainHead: %s", err) + return + } + + paymentAddress, err := p.node.GetMinerWorkerAddress(ctx, p.minerAddress, tok) if err != nil { log.Errorf("Retrieval query: Lookup Payment Address: %s", err) answer.Status = retrievalmarket.QueryResponseError diff --git a/retrievalmarket/impl/testnodes/test_retrieval_provider_node.go b/retrievalmarket/impl/testnodes/test_retrieval_provider_node.go index a97857f4..fac91c83 100644 --- a/retrievalmarket/impl/testnodes/test_retrieval_provider_node.go +++ b/retrievalmarket/impl/testnodes/test_retrieval_provider_node.go @@ -115,7 +115,7 @@ func (trpn *TestRetrievalProviderNode) SavePaymentVoucher( } // GetMinerWorker translates an address -func (trpn *TestRetrievalProviderNode) GetMinerWorker(ctx context.Context, addr address.Address) (address.Address, error) { +func (trpn *TestRetrievalProviderNode) GetMinerWorkerAddress(ctx context.Context, addr address.Address, tok shared.TipSetToken) (address.Address, error) { return addr, nil } diff --git a/retrievalmarket/types.go b/retrievalmarket/types.go index 10c1de18..46f84882 100644 --- a/retrievalmarket/types.go +++ b/retrievalmarket/types.go @@ -6,14 +6,14 @@ import ( "fmt" "io" - "github.com/filecoin-project/go-fil-markets/shared" - "github.com/filecoin-project/go-address" "github.com/filecoin-project/specs-actors/actors/abi" "github.com/filecoin-project/specs-actors/actors/abi/big" "github.com/filecoin-project/specs-actors/actors/builtin/paych" "github.com/ipfs/go-cid" "github.com/libp2p/go-libp2p-core/peer" + + "github.com/filecoin-project/go-fil-markets/shared" ) //go:generate cbor-gen-for Query QueryResponse DealProposal DealResponse Params QueryParams DealPayment Block ClientDealState ProviderDealState PaymentInfo @@ -316,7 +316,7 @@ type RetrievalProviderNode interface { GetChainHead(ctx context.Context) (shared.TipSetToken, abi.ChainEpoch, error) // returns the worker address associated with a miner - GetMinerWorker(ctx context.Context, miner address.Address) (address.Address, error) + GetMinerWorkerAddress(ctx context.Context, miner address.Address, tok shared.TipSetToken) (address.Address, error) UnsealSector(ctx context.Context, sectorID uint64, offset uint64, length uint64) (io.ReadCloser, error) SavePaymentVoucher(ctx context.Context, paymentChannel address.Address, voucher *paych.SignedVoucher, proof []byte, expectedAmount abi.TokenAmount) (abi.TokenAmount, error) } diff --git a/storagemarket/impl/provider.go b/storagemarket/impl/provider.go index 88f789da..cb42f0c0 100644 --- a/storagemarket/impl/provider.go +++ b/storagemarket/impl/provider.go @@ -289,7 +289,12 @@ func (p *providerDealEnvironment) SendSignedResponse(ctx context.Context, resp * return xerrors.Errorf("couldn't send response: %w", err) } - sig, err := providerutils.SignMinerData(ctx, resp, p.p.actor, p.Node().GetMinerWorker, p.Node().SignBytes) + tok, _, err := p.p.spn.GetChainHead(ctx) + if err != nil { + return xerrors.Errorf("couldn't get chain head: %w", err) + } + + sig, err := providerutils.SignMinerData(ctx, resp, p.p.actor, tok, p.Node().GetMinerWorkerAddress, p.Node().SignBytes) if err != nil { return xerrors.Errorf("failed to sign response message: %w", err) } diff --git a/storagemarket/impl/providerstates/provider_states.go b/storagemarket/impl/providerstates/provider_states.go index d7e28b55..9384cd53 100644 --- a/storagemarket/impl/providerstates/provider_states.go +++ b/storagemarket/impl/providerstates/provider_states.go @@ -154,7 +154,12 @@ func VerifyData(ctx fsm.Context, environment ProviderDealEnvironment, deal stora // PublishDeal publishes a deal on chain and sends the deal id back to the client func PublishDeal(ctx fsm.Context, environment ProviderDealEnvironment, deal storagemarket.MinerDeal) error { - waddr, err := environment.Node().GetMinerWorker(ctx.Context(), deal.Proposal.Provider) + tok, _, err := environment.Node().GetChainHead(ctx.Context()) + if err != nil { + return ctx.Trigger(storagemarket.ProviderEventNodeErrored, xerrors.Errorf("acquiring chain head: %w", err)) + } + + waddr, err := environment.Node().GetMinerWorkerAddress(ctx.Context(), deal.Proposal.Provider, tok) if err != nil { return ctx.Trigger(storagemarket.ProviderEventNodeErrored, xerrors.Errorf("looking up miner worker: %w", err)) } diff --git a/storagemarket/impl/providerutils/providerutils.go b/storagemarket/impl/providerutils/providerutils.go index 41ac9cf2..88814ea9 100644 --- a/storagemarket/impl/providerutils/providerutils.go +++ b/storagemarket/impl/providerutils/providerutils.go @@ -13,6 +13,7 @@ import ( logging "github.com/ipfs/go-log/v2" "golang.org/x/xerrors" + "github.com/filecoin-project/go-fil-markets/shared" "github.com/filecoin-project/go-fil-markets/storagemarket" "github.com/filecoin-project/go-fil-markets/storagemarket/impl/requestvalidation" ) @@ -41,19 +42,19 @@ func VerifyProposal(sdp market.ClientDealProposal, verifier VerifyFunc) error { } // WorkerLookupFunc is a function that can lookup a miner worker address from a storage miner actor -type WorkerLookupFunc func(context.Context, address.Address) (address.Address, error) +type WorkerLookupFunc func(context.Context, address.Address, shared.TipSetToken) (address.Address, error) // SignFunc is a function that can sign a set of bytes with a given address type SignFunc func(context.Context, address.Address, []byte) (*crypto.Signature, error) // SignMinerData signs the given data structure with a signature for the given address -func SignMinerData(ctx context.Context, data interface{}, address address.Address, workerLookup WorkerLookupFunc, sign SignFunc) (*crypto.Signature, error) { +func SignMinerData(ctx context.Context, data interface{}, address address.Address, tok shared.TipSetToken, workerLookup WorkerLookupFunc, sign SignFunc) (*crypto.Signature, error) { msg, err := cborutil.Dump(data) if err != nil { return nil, xerrors.Errorf("serializing: %w", err) } - worker, err := workerLookup(ctx, address) + worker, err := workerLookup(ctx, address, tok) if err != nil { return nil, err } diff --git a/storagemarket/impl/providerutils/providerutils_test.go b/storagemarket/impl/providerutils/providerutils_test.go index 0ec40de1..e6c893e4 100644 --- a/storagemarket/impl/providerutils/providerutils_test.go +++ b/storagemarket/impl/providerutils/providerutils_test.go @@ -14,6 +14,7 @@ import ( "github.com/libp2p/go-libp2p-core/peer" "github.com/stretchr/testify/require" + "github.com/filecoin-project/go-fil-markets/shared" "github.com/filecoin-project/go-fil-markets/shared_testutil" "github.com/filecoin-project/go-fil-markets/storagemarket" "github.com/filecoin-project/go-fil-markets/storagemarket/impl/providerutils" @@ -56,7 +57,7 @@ func TestVerifyProposal(t *testing.T) { func TestSignMinerData(t *testing.T) { ctx := context.Background() - successLookup := func(context.Context, address.Address) (address.Address, error) { + successLookup := func(context.Context, address.Address, shared.TipSetToken) (address.Address, error) { return address.TestAddress2, nil } successSign := func(context.Context, address.Address, []byte) (*crypto.Signature, error) { @@ -82,7 +83,7 @@ func TestSignMinerData(t *testing.T) { }, "worker lookup errors": { data: shared_testutil.MakeTestStorageAsk(), - workerLookup: func(context.Context, address.Address) (address.Address, error) { + workerLookup: func(context.Context, address.Address, shared.TipSetToken) (address.Address, error) { return address.Undef, errors.New("Something went wrong") }, signBytes: successSign, @@ -99,7 +100,7 @@ func TestSignMinerData(t *testing.T) { } for name, data := range tests { t.Run(name, func(t *testing.T) { - _, err := providerutils.SignMinerData(ctx, data.data, address.TestAddress, data.workerLookup, data.signBytes) + _, err := providerutils.SignMinerData(ctx, data.data, address.TestAddress, shared.TipSetToken{}, data.workerLookup, data.signBytes) require.Equal(t, err != nil, data.shouldErr) }) } diff --git a/storagemarket/impl/storedask/storedask.go b/storagemarket/impl/storedask/storedask.go index 6c62faf5..b249cd6f 100644 --- a/storagemarket/impl/storedask/storedask.go +++ b/storagemarket/impl/storedask/storedask.go @@ -60,7 +60,9 @@ func (s *StoredAsk) AddAsk(price abi.TokenAmount, duration abi.ChainEpoch) error seqno = s.ask.Ask.SeqNo + 1 } - _, height, err := s.spn.GetChainHead(context.TODO()) + ctx := context.TODO() + + _, height, err := s.spn.GetChainHead(ctx) if err != nil { return err } @@ -73,7 +75,12 @@ func (s *StoredAsk) AddAsk(price abi.TokenAmount, duration abi.ChainEpoch) error MinPieceSize: defaultMinPieceSize, } - sig, err := providerutils.SignMinerData(context.TODO(), ask, s.actor, s.spn.GetMinerWorker, s.spn.SignBytes) + tok, _, err := s.spn.GetChainHead(ctx) + if err != nil { + return err + } + + sig, err := providerutils.SignMinerData(ctx, ask, s.actor, tok, s.spn.GetMinerWorkerAddress, s.spn.SignBytes) if err != nil { return err } diff --git a/storagemarket/testnodes/testnodes.go b/storagemarket/testnodes/testnodes.go index 623157b3..3986d536 100644 --- a/storagemarket/testnodes/testnodes.go +++ b/storagemarket/testnodes/testnodes.go @@ -4,8 +4,6 @@ import ( "context" "io" - "github.com/filecoin-project/go-fil-markets/shared" - "github.com/filecoin-project/go-address" "github.com/filecoin-project/specs-actors/actors/abi" "github.com/filecoin-project/specs-actors/actors/abi/big" @@ -13,6 +11,7 @@ import ( "github.com/filecoin-project/specs-actors/actors/crypto" "github.com/ipfs/go-cid" + "github.com/filecoin-project/go-fil-markets/shared" "github.com/filecoin-project/go-fil-markets/shared_testutil" "github.com/filecoin-project/go-fil-markets/storagemarket" ) @@ -229,7 +228,7 @@ func (n *FakeProviderNode) OnDealComplete(ctx context.Context, deal storagemarke } // GetMinerWorker returns the address specified by MinerAddr -func (n *FakeProviderNode) GetMinerWorker(ctx context.Context, miner address.Address) (address.Address, error) { +func (n *FakeProviderNode) GetMinerWorkerAddress(ctx context.Context, miner address.Address, tok shared.TipSetToken) (address.Address, error) { if n.MinerWorkerError == nil { return n.MinerAddr, nil } diff --git a/storagemarket/types.go b/storagemarket/types.go index e979bdc7..61f70ae2 100644 --- a/storagemarket/types.go +++ b/storagemarket/types.go @@ -309,7 +309,7 @@ type StorageProviderNode interface { OnDealComplete(ctx context.Context, deal MinerDeal, pieceSize abi.UnpaddedPieceSize, pieceReader io.Reader) error // returns the worker address associated with a miner - GetMinerWorker(ctx context.Context, miner address.Address) (address.Address, error) + GetMinerWorkerAddress(ctx context.Context, addr address.Address, tok shared.TipSetToken) (address.Address, error) // Signs bytes SignBytes(ctx context.Context, signer address.Address, b []byte) (*crypto.Signature, error)