Skip to content

Commit

Permalink
refactor: simplify legacy deal response code
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkmc committed Jun 20, 2023
1 parent 45883c6 commit d44ec74
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 97 deletions.
2 changes: 1 addition & 1 deletion itests/disabled_markets_v1_deal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ func TestDisabledMarketsV1Deal(t *testing.T) {
log.Debugw(di.Message)

err = f.WaitDealSealed(ctx, dealProposalCid)
require.ErrorContains(t, err, "Legacy deals are deprecated in Boost. Please propose a boost deal")
require.ErrorContains(t, err, "protocol are deprecated")
}
156 changes: 60 additions & 96 deletions storagemarket/lp2pimpl/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/filecoin-project/boost/storagemarket/types"
"github.com/filecoin-project/go-address"
cborutil "github.com/filecoin-project/go-cbor-util"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/lotus/api/v1api"
chaintypes "github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/lib/sigs"
Expand All @@ -26,6 +27,7 @@ import (
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
typegen "github.com/whyrusleeping/cbor-gen"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -412,27 +414,16 @@ func (p *DealProvider) handleLegacyDealStream(s network.Stream) {
reqLog.Debugw("handled legacy deal status request", "duration", time.Since(start).String())
}()

_ = s.SetReadDeadline(time.Now().Add(providerReadDeadline))

ts, err := p.fullNode.ChainHead(p.ctx)
if err != nil {
reqLog.Errorf("could not get chain head: %s", err)
}

maddr, err := p.spApi.ActorAddress(p.ctx)
if err != nil {
reqLog.Errorf("could not get miner actor address: %s", err)
}

mi, err := p.fullNode.StateMinerInfo(p.ctx, maddr, ts.Key())
if err != nil {
reqLog.Errorf("could not get miner info: %s", err)
}
rejMsg := fmt.Sprintf("deal proposals made over the legacy %s protocol are deprecated"+
" - please use the %s deal proposal protocol.", s.Protocol(), DealProtocolv121ID)
const rejState = gfm_storagemarket.StorageDealProposalRejected
var signedResponse typegen.CBORUnmarshaler

_ = s.SetReadDeadline(time.Now().Add(providerReadDeadline))
switch s.Protocol() {
case gfm_storagemarket.DealProtocolID101:
var prop gfm_migration.Proposal0
err = prop.UnmarshalCBOR(s)
err := prop.UnmarshalCBOR(s)
_ = s.SetReadDeadline(time.Time{}) // Clear read deadline so conn doesn't get closed
if err != nil {
reqLog.Errorf("failed to unmarshal the proposal message: %s", err)
Expand All @@ -445,39 +436,18 @@ func (p *DealProvider) handleLegacyDealStream(s network.Stream) {
return
}

resp := gfm_migration.Response0{
State: gfm_storagemarket.StorageDealProposalRejected,
Message: "Legacy deals are deprecated in Boost. Please propose a boost deal",
Proposal: pcid,
PublishMessage: nil,
}

msg, err := cborutil.Dump(resp)
if err != nil {
reqLog.Errorf("could not convert response to bytes: %s", err)
}

localSignature, err := p.fullNode.WalletSign(p.ctx, mi.Worker, msg)
resp := gfm_migration.Response0{State: rejState, Message: rejMsg, Proposal: pcid}
sig, err := p.signLegacyResponse(&resp)
if err != nil {
reqLog.Errorf("failed to sign the message: %s", err)
}

signedResponse := gfm_migration.SignedResponse0{
Response: resp,
Signature: localSignature,
reqLog.Errorf("getting signed response: %s", err)
return
}

// Set a deadline on writing to the stream so it doesn't hang
_ = s.SetWriteDeadline(time.Now().Add(providerWriteDeadline))
defer s.SetWriteDeadline(time.Time{}) // nolint
signedResponse = &gfm_migration.SignedResponse0{Response: resp, Signature: sig}

err = signedResponse.UnmarshalCBOR(s)
if err != nil {
reqLog.Errorf("error writing response to the stream: %s", err)
}
case gfm_storagemarket.DealProtocolID110:
var prop gfm_migration.Proposal1
err = prop.UnmarshalCBOR(s)
err := prop.UnmarshalCBOR(s)
_ = s.SetReadDeadline(time.Time{}) // Clear read deadline so conn doesn't get closed
if err != nil {
reqLog.Errorf("failed to unmarshal the proposal message: %s", err)
Expand All @@ -490,40 +460,18 @@ func (p *DealProvider) handleLegacyDealStream(s network.Stream) {
return
}

resp := gfm_network.Response{
State: gfm_storagemarket.StorageDealProposalRejected,
Message: "Legacy deals are deprecated in Boost. Please propose a boost deal",
Proposal: pcid,
PublishMessage: nil,
}

msg, err := cborutil.Dump(&resp)
resp := gfm_network.Response{State: rejState, Message: rejMsg, Proposal: pcid}
sig, err := p.signLegacyResponse(&resp)
if err != nil {
reqLog.Errorf("could not convert response to bytes: %s", err)
}

localSignature, err := p.fullNode.WalletSign(p.ctx, mi.Worker, msg)
if err != nil {
reqLog.Errorf("failed to sign the message: %s", err)
}

signedResponse := gfm_network.SignedResponse{
Response: resp,
Signature: localSignature,
reqLog.Errorf("getting signed response: %s", err)
return
}

// Set a deadline on writing to the stream so it doesn't hang
_ = s.SetWriteDeadline(time.Now().Add(providerWriteDeadline))
defer s.SetWriteDeadline(time.Time{}) // nolint

err = signedResponse.UnmarshalCBOR(s)
if err != nil {
reqLog.Errorf("error writing response to the stream: %s", err)
}
signedResponse = &gfm_network.SignedResponse{Response: resp, Signature: sig}

case gfm_storagemarket.DealProtocolID111:
var prop gfm_network.Proposal
err = prop.UnmarshalCBOR(s)
err := prop.UnmarshalCBOR(s)
_ = s.SetReadDeadline(time.Time{}) // Clear read deadline so conn doesn't get closed
if err != nil {
reqLog.Errorf("failed to unmarshal the proposal message: %s", err)
Expand All @@ -536,35 +484,51 @@ func (p *DealProvider) handleLegacyDealStream(s network.Stream) {
return
}

resp := gfm_network.Response{
State: gfm_storagemarket.StorageDealProposalRejected,
Message: "Legacy deals are deprecated in Boost. Please propose a boost deal",
Proposal: pcid,
PublishMessage: nil,
}

msg, err := cborutil.Dump(&resp)
resp := gfm_network.Response{State: rejState, Message: rejMsg, Proposal: pcid}
sig, err := p.signLegacyResponse(&resp)
if err != nil {
reqLog.Errorf("could not convert response to bytes: %s", err)
reqLog.Errorf("getting signed response: %s", err)
return
}

localSignature, err := p.fullNode.WalletSign(p.ctx, mi.Worker, msg)
if err != nil {
reqLog.Errorf("failed to sign the message: %s", err)
}
signedResponse = &gfm_network.SignedResponse{Response: resp, Signature: sig}
}

signedResponse := gfm_network.SignedResponse{
Response: resp,
Signature: localSignature,
}
// Set a deadline on writing to the stream so it doesn't hang
_ = s.SetWriteDeadline(time.Now().Add(providerWriteDeadline))
defer s.SetWriteDeadline(time.Time{}) // nolint

// Set a deadline on writing to the stream so it doesn't hang
_ = s.SetWriteDeadline(time.Now().Add(providerWriteDeadline))
defer s.SetWriteDeadline(time.Time{}) // nolint
err := signedResponse.UnmarshalCBOR(s)
if err != nil {
reqLog.Errorf("error writing response to the stream: %s", err)
}
}

err = cborutil.WriteCborRPC(s, &signedResponse)
if err != nil {
reqLog.Errorf("error writing response to the stream: %s", err)
}
func (p *DealProvider) signLegacyResponse(resp typegen.CBORMarshaler) (*crypto.Signature, error) {
ts, err := p.fullNode.ChainHead(p.ctx)
if err != nil {
return nil, fmt.Errorf("getting chain head: %w", err)
}

maddr, err := p.spApi.ActorAddress(p.ctx)
if err != nil {
return nil, fmt.Errorf("getting miner actor address: %w", err)
}

mi, err := p.fullNode.StateMinerInfo(p.ctx, maddr, ts.Key())
if err != nil {
return nil, fmt.Errorf("getting miner info: %w", err)
}

msg, err := cborutil.Dump(&resp)
if err != nil {
return nil, fmt.Errorf("could not convert response to bytes: %w", err)
}

localSignature, err := p.fullNode.WalletSign(p.ctx, mi.Worker, msg)
if err != nil {
return nil, fmt.Errorf("failed to sign the message: %w", err)
}

return localSignature, err
}

0 comments on commit d44ec74

Please sign in to comment.