2019-08-16 05:34:25 +02:00
|
|
|
package manager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/lbryio/lbry.go/extras/errors"
|
|
|
|
"github.com/lbryio/lbry.go/extras/jsonrpc"
|
|
|
|
"github.com/lbryio/lbry.go/extras/util"
|
2019-08-20 07:06:51 +02:00
|
|
|
"github.com/lbryio/ytsync/sdk"
|
2019-08-23 01:28:51 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-08-28 21:08:25 +02:00
|
|
|
"strconv"
|
2019-08-16 05:34:25 +02:00
|
|
|
)
|
|
|
|
|
2019-08-23 01:28:51 +02:00
|
|
|
func waitConfirmations(s *Sync) error {
|
|
|
|
allConfirmed := false
|
|
|
|
waiting:
|
|
|
|
for !allConfirmed {
|
|
|
|
utxolist, err := s.daemon.UTXOList(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if utxolist == nil {
|
|
|
|
return errors.Err("no response")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, utxo := range *utxolist {
|
|
|
|
if utxo.Confirmations <= 0 {
|
|
|
|
err = s.waitForNewBlock()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue waiting
|
|
|
|
}
|
|
|
|
}
|
|
|
|
allConfirmed = true
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-28 21:08:25 +02:00
|
|
|
func abandonSupports(s *Sync) (float64, error) {
|
2019-08-27 00:31:27 +02:00
|
|
|
totalPages := uint64(1)
|
|
|
|
var allSupports []jsonrpc.Claim
|
|
|
|
for page := uint64(1); page <= totalPages; page++ {
|
|
|
|
supports, err := s.daemon.SupportList(nil, page, 50)
|
|
|
|
if err != nil {
|
2019-08-28 21:08:25 +02:00
|
|
|
return 0, errors.Prefix("cannot list claims", err)
|
2019-08-27 00:31:27 +02:00
|
|
|
}
|
|
|
|
allSupports = append(allSupports, (*supports).Items...)
|
|
|
|
totalPages = (*supports).TotalPages
|
|
|
|
}
|
2019-08-27 19:49:51 +02:00
|
|
|
alreadyAbandoned := make(map[string]bool, len(allSupports))
|
2019-08-28 21:08:25 +02:00
|
|
|
totalAbandoned := 0.0
|
2019-08-27 19:49:51 +02:00
|
|
|
for _, support := range allSupports {
|
|
|
|
_, ok := alreadyAbandoned[support.ClaimID]
|
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
alreadyAbandoned[support.ClaimID] = true
|
|
|
|
summary, err := s.daemon.SupportAbandon(&support.ClaimID, nil, nil, nil, nil)
|
|
|
|
if err != nil {
|
2019-08-28 21:08:25 +02:00
|
|
|
return totalAbandoned, errors.Err(err)
|
|
|
|
}
|
|
|
|
if len(summary.Outputs) < 1 {
|
|
|
|
return totalAbandoned, errors.Err("error abandoning supports: no outputs while abandoning %s", support.ClaimID)
|
2019-08-27 19:49:51 +02:00
|
|
|
}
|
2019-08-28 21:08:25 +02:00
|
|
|
outputAmount, err := strconv.ParseFloat(summary.Outputs[0].Amount, 64)
|
|
|
|
if err != nil {
|
|
|
|
return totalAbandoned, errors.Err(err)
|
|
|
|
}
|
|
|
|
totalAbandoned += outputAmount
|
|
|
|
log.Infof("Abandoned supports of %.4f LBC for claim %s", outputAmount, support.ClaimID)
|
2019-08-27 19:49:51 +02:00
|
|
|
}
|
2019-08-28 21:08:25 +02:00
|
|
|
return totalAbandoned, nil
|
2019-08-27 00:31:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func transferVideos(s *Sync) error {
|
2019-08-21 19:40:35 +02:00
|
|
|
cleanTransfer := true
|
2019-08-27 00:31:27 +02:00
|
|
|
for _, video := range s.syncedVideos {
|
2019-08-23 01:28:51 +02:00
|
|
|
if !video.Published || video.Transferred || video.MetadataVersion != LatestMetadataVersion {
|
|
|
|
log.Debugf("skipping video: %s", video.VideoID)
|
2019-08-21 19:40:35 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-16 05:34:25 +02:00
|
|
|
//Todo - Wait for prior sync to see that the publish is confirmed in lbrycrd?
|
2019-08-27 00:31:27 +02:00
|
|
|
c, err := s.daemon.ClaimSearch(nil, &video.ClaimID, nil, nil)
|
2019-08-16 05:34:25 +02:00
|
|
|
if err != nil {
|
2019-08-21 19:40:35 +02:00
|
|
|
return errors.Err(err)
|
2019-08-16 05:34:25 +02:00
|
|
|
}
|
|
|
|
if len(c.Claims) == 0 {
|
2019-08-21 19:40:35 +02:00
|
|
|
return errors.Err("cannot transfer: no claim found for this video")
|
2019-08-16 05:34:25 +02:00
|
|
|
} else if len(c.Claims) > 1 {
|
2019-08-21 19:40:35 +02:00
|
|
|
return errors.Err("cannot transfer: too many claims. claimID: %s", video.ClaimID)
|
2019-08-16 05:34:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
streamUpdateOptions := jsonrpc.StreamUpdateOptions{
|
|
|
|
StreamCreateOptions: &jsonrpc.StreamCreateOptions{
|
2019-08-27 00:31:27 +02:00
|
|
|
ClaimCreateOptions: jsonrpc.ClaimCreateOptions{ClaimAddress: &s.publishAddress},
|
2019-08-16 05:34:25 +02:00
|
|
|
},
|
|
|
|
Bid: util.PtrToString("0.009"), // Todo - Dont hardcode
|
|
|
|
}
|
|
|
|
|
2019-08-20 07:06:51 +02:00
|
|
|
videoStatus := sdk.VideoStatus{
|
2019-08-27 00:31:27 +02:00
|
|
|
ChannelID: s.YoutubeChannelID,
|
2019-08-20 07:06:51 +02:00
|
|
|
VideoID: video.VideoID,
|
|
|
|
ClaimID: video.ClaimID,
|
|
|
|
ClaimName: video.ClaimName,
|
|
|
|
Status: VideoStatusPublished,
|
|
|
|
IsTransferred: util.PtrToBool(true),
|
|
|
|
}
|
|
|
|
|
2019-08-27 00:31:27 +02:00
|
|
|
result, updateError := s.daemon.StreamUpdate(video.ClaimID, streamUpdateOptions)
|
2019-08-23 01:28:51 +02:00
|
|
|
if updateError != nil {
|
2019-08-21 19:40:35 +02:00
|
|
|
cleanTransfer = false
|
2019-08-23 01:28:51 +02:00
|
|
|
videoStatus.FailureReason = updateError.Error()
|
2019-08-20 07:06:51 +02:00
|
|
|
videoStatus.Status = VideoStatusTranferFailed
|
|
|
|
videoStatus.IsTransferred = util.PtrToBool(false)
|
|
|
|
}
|
2019-08-28 06:10:54 +02:00
|
|
|
log.Infof("TRANSFERRED %t", len(result.Outputs) != 0)
|
2019-08-27 00:31:27 +02:00
|
|
|
statusErr := s.APIConfig.MarkVideoStatus(videoStatus)
|
2019-08-20 07:06:51 +02:00
|
|
|
if statusErr != nil {
|
2019-08-23 01:28:51 +02:00
|
|
|
return errors.Err(statusErr)
|
2019-08-20 07:06:51 +02:00
|
|
|
}
|
2019-08-23 01:28:51 +02:00
|
|
|
if updateError != nil {
|
2019-08-20 07:06:51 +02:00
|
|
|
return errors.Err(err)
|
2019-08-16 05:34:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Todo - Transfer Channel as last step and post back to remote db that channel is transferred.
|
2019-08-21 05:38:36 +02:00
|
|
|
//Transfer channel
|
2019-08-21 19:40:35 +02:00
|
|
|
if !cleanTransfer {
|
2019-08-21 05:38:36 +02:00
|
|
|
return errors.Err("A video has failed to transfer for the channel...skipping channel transfer")
|
|
|
|
}
|
2019-08-27 00:31:27 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func transferChannel(s *Sync) error {
|
|
|
|
channelClaim, err := s.daemon.ClaimSearch(nil, &s.lbryChannelID, nil, nil)
|
2019-08-21 05:38:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
2019-08-23 01:28:51 +02:00
|
|
|
if channelClaim == nil || len(channelClaim.Claims) == 0 {
|
2019-08-27 00:31:27 +02:00
|
|
|
return errors.Err("There is no channel claim for channel %s", s.LbryChannelName)
|
2019-08-21 05:38:36 +02:00
|
|
|
}
|
|
|
|
updateOptions := jsonrpc.ChannelUpdateOptions{
|
|
|
|
ChannelCreateOptions: jsonrpc.ChannelCreateOptions{
|
|
|
|
ClaimCreateOptions: jsonrpc.ClaimCreateOptions{
|
2019-08-27 00:31:27 +02:00
|
|
|
ClaimAddress: &s.publishAddress,
|
2019-08-21 05:38:36 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2019-08-27 00:31:27 +02:00
|
|
|
result, err := s.daemon.ChannelUpdate(s.lbryChannelID, updateOptions)
|
2019-08-28 06:10:54 +02:00
|
|
|
log.Infof("TRANSFERRED %t", len(result.Outputs) != 0)
|
2019-08-23 01:28:51 +02:00
|
|
|
|
2019-08-21 05:38:36 +02:00
|
|
|
return errors.Err(err)
|
2019-08-16 05:34:25 +02:00
|
|
|
}
|