Skip to content

Commit

Permalink
fix panic when claim.Value.GetStream() is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
lyoshenka committed May 28, 2020
1 parent b194539 commit 169edc3
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions app/query/processors.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func preflightHookGet(caller *Caller, query *Query) (*jsonrpc.RPCResponse, error
}
stream := claim.Value.GetStream()

if stream.Fee != nil && stream.Fee.Amount > 0 {
feeAmount := stream.GetFee().GetAmount()
if feeAmount > 0 {
isPaidStream = true

purchaseQuery, err := NewQuery(jsonrpc.NewRequest(
Expand Down Expand Up @@ -73,8 +74,8 @@ func preflightHookGet(caller *Caller, query *Query) (*jsonrpc.RPCResponse, error
} else {
// Assuming the stream is of a paid variety and we have just paid for the stream
metrics.LbrytvPurchases.Inc()
metrics.LbrytvPurchaseAmounts.Observe(float64(stream.Fee.Amount))
log.Infof("made a purchase for %.2f LBC", float64(stream.Fee.Amount))
metrics.LbrytvPurchaseAmounts.Observe(float64(feeAmount))
log.Infof("made a purchase for %d LBC", feeAmount)
// This is needed so changes can propagate for the subsequent resolve
time.Sleep(1 * time.Second)
claim, err = resolve(caller, query, url)
Expand All @@ -91,20 +92,15 @@ func preflightHookGet(caller *Caller, query *Query) (*jsonrpc.RPCResponse, error
}
metrics.LbrytvStreamRequests.WithLabelValues(metricLabel).Inc()

size := stream.GetSource().Size

if err != nil {
return nil, fmt.Errorf("error getting size by magic: %v", err)
}

if isPaidStream {
size := stream.GetSource().GetSize()
if claim.PurchaseReceipt == nil {
log.Errorf("stream was paid for but receipt not found in the resolve response")
return nil, fmt.Errorf("couldn't find purchase receipt for paid stream")
}

log.Debugf("creating stream token with stream id=%s, txid=%s, size=%v", claim.Name+"/"+claim.ClaimID, claim.PurchaseReceipt.Txid, uint64(size))
token, err := paid.CreateToken(claim.Name+"/"+claim.ClaimID, claim.PurchaseReceipt.Txid, uint64(size), paid.ExpTenSecPer100MB)
log.Debugf("creating stream token with stream id=%s, txid=%s, size=%v", claim.Name+"/"+claim.ClaimID, claim.PurchaseReceipt.Txid, size)
token, err := paid.CreateToken(claim.Name+"/"+claim.ClaimID, claim.PurchaseReceipt.Txid, size, paid.ExpTenSecPer100MB)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -159,12 +155,6 @@ func resolve(c *Caller, q *Query, url string) (*ljsonrpc.Claim, error) {
return &claim, err
}

func checkIsPaidStream(s interface{}) bool {
f := s.(ljsonrpc.File)
stream := f.Metadata.GetStream()
return stream.Fee != nil && stream.Fee.Amount > 0
}

func getStatusResponse(c *Caller, q *Query) (*jsonrpc.RPCResponse, error) {
var response map[string]interface{}

Expand Down

0 comments on commit 169edc3

Please sign in to comment.