2019-08-16 05:34:25 +02:00
|
|
|
package manager
|
|
|
|
|
|
|
|
import (
|
2019-09-27 16:10:57 +02:00
|
|
|
"fmt"
|
2019-10-11 19:41:55 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-10-10 16:50:33 +02:00
|
|
|
"github.com/lbryio/lbry.go/v2/extras/errors"
|
|
|
|
"github.com/lbryio/lbry.go/v2/extras/jsonrpc"
|
2019-10-11 00:57:42 +02:00
|
|
|
"github.com/lbryio/lbry.go/v2/extras/stop"
|
2019-10-10 16:50:33 +02:00
|
|
|
"github.com/lbryio/lbry.go/v2/extras/util"
|
2020-08-08 01:12:55 +02:00
|
|
|
"github.com/lbryio/ytsync/v5/shared"
|
2020-06-11 18:45:56 +02:00
|
|
|
"github.com/lbryio/ytsync/v5/timing"
|
2019-10-11 19:41:55 +02:00
|
|
|
|
2019-08-23 01:28:51 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-08-16 05:34:25 +02:00
|
|
|
)
|
|
|
|
|
2019-08-23 01:28:51 +02:00
|
|
|
func waitConfirmations(s *Sync) error {
|
2020-05-19 23:13:01 +02:00
|
|
|
start := time.Now()
|
|
|
|
defer func(start time.Time) {
|
|
|
|
timing.TimedComponent("waitConfirmations").Add(time.Since(start))
|
|
|
|
}(start)
|
2019-10-08 01:38:39 +02:00
|
|
|
defaultAccount, err := s.getDefaultAccount()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-23 01:28:51 +02:00
|
|
|
allConfirmed := false
|
2020-05-08 16:33:29 +02:00
|
|
|
waitCount := 0
|
2019-08-23 01:28:51 +02:00
|
|
|
waiting:
|
2020-05-14 17:28:15 +02:00
|
|
|
for !allConfirmed && waitCount < 2 {
|
2019-12-08 17:48:24 +01:00
|
|
|
utxolist, err := s.daemon.UTXOList(&defaultAccount, 1, 10000)
|
2019-08-23 01:28:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if utxolist == nil {
|
|
|
|
return errors.Err("no response")
|
|
|
|
}
|
|
|
|
|
2019-12-08 17:48:24 +01:00
|
|
|
for _, utxo := range utxolist.Items {
|
2019-08-23 01:28:51 +02:00
|
|
|
if utxo.Confirmations <= 0 {
|
|
|
|
err = s.waitForNewBlock()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-08 16:33:29 +02:00
|
|
|
waitCount++
|
2019-08-23 01:28:51 +02:00
|
|
|
continue waiting
|
|
|
|
}
|
|
|
|
}
|
|
|
|
allConfirmed = true
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-11 19:41:55 +02:00
|
|
|
type abandonResponse struct {
|
|
|
|
ClaimID string
|
|
|
|
Error error
|
|
|
|
Amount float64
|
|
|
|
}
|
|
|
|
|
2019-08-28 21:08:25 +02:00
|
|
|
func abandonSupports(s *Sync) (float64, error) {
|
2020-05-19 23:13:01 +02:00
|
|
|
start := time.Now()
|
|
|
|
defer func(start time.Time) {
|
|
|
|
timing.TimedComponent("abandonSupports").Add(time.Since(start))
|
|
|
|
}(start)
|
2019-08-27 00:31:27 +02:00
|
|
|
totalPages := uint64(1)
|
|
|
|
var allSupports []jsonrpc.Claim
|
2019-10-01 16:55:43 +02:00
|
|
|
defaultAccount, err := s.getDefaultAccount()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2019-08-27 00:31:27 +02:00
|
|
|
for page := uint64(1); page <= totalPages; page++ {
|
2019-10-01 16:55:43 +02:00
|
|
|
supports, err := s.daemon.SupportList(&defaultAccount, page, 50)
|
2019-08-27 00:31:27 +02:00
|
|
|
if err != nil {
|
2020-08-18 01:09:53 +02:00
|
|
|
supports, err = s.daemon.SupportList(&defaultAccount, page, 50)
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Prefix("cannot list supports", err)
|
|
|
|
}
|
2019-08-27 00:31:27 +02:00
|
|
|
}
|
|
|
|
allSupports = append(allSupports, (*supports).Items...)
|
|
|
|
totalPages = (*supports).TotalPages
|
|
|
|
}
|
2019-10-11 19:41:55 +02:00
|
|
|
producerWG := &stop.Group{}
|
|
|
|
|
|
|
|
claimIDChan := make(chan string, len(allSupports))
|
|
|
|
abandonRspChan := make(chan abandonResponse, len(allSupports))
|
2019-08-27 19:49:51 +02:00
|
|
|
alreadyAbandoned := make(map[string]bool, len(allSupports))
|
2019-10-11 19:41:55 +02:00
|
|
|
producerWG.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer producerWG.Done()
|
|
|
|
for _, support := range allSupports {
|
|
|
|
_, ok := alreadyAbandoned[support.ClaimID]
|
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
alreadyAbandoned[support.ClaimID] = true
|
|
|
|
claimIDChan <- support.ClaimID
|
2019-08-27 19:49:51 +02:00
|
|
|
}
|
2019-10-11 19:41:55 +02:00
|
|
|
}()
|
|
|
|
consumerWG := &stop.Group{}
|
|
|
|
//TODO: remove this once the SDK team fixes their RPC bugs....
|
2020-05-20 04:28:29 +02:00
|
|
|
s.daemon.SetRPCTimeout(60 * time.Second)
|
|
|
|
defer s.daemon.SetRPCTimeout(5 * time.Minute)
|
2020-08-08 01:12:55 +02:00
|
|
|
for i := 0; i < s.Manager.CliFlags.ConcurrentJobs; i++ {
|
2019-10-11 19:41:55 +02:00
|
|
|
consumerWG.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer consumerWG.Done()
|
2020-03-31 04:30:02 +02:00
|
|
|
outer:
|
2019-10-11 19:41:55 +02:00
|
|
|
for {
|
|
|
|
claimID, more := <-claimIDChan
|
|
|
|
if !more {
|
|
|
|
return
|
|
|
|
} else {
|
2020-03-31 04:30:02 +02:00
|
|
|
summary, err := s.daemon.TxoSpend(util.PtrToString("support"), &claimID, nil, nil, nil, &defaultAccount)
|
2019-10-11 19:41:55 +02:00
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "Client.Timeout exceeded while awaiting headers") {
|
|
|
|
log.Errorf("Support abandon for %s timed out, retrying...", claimID)
|
2020-03-31 04:30:02 +02:00
|
|
|
summary, err = s.daemon.TxoSpend(util.PtrToString("support"), &claimID, nil, nil, nil, &defaultAccount)
|
2019-10-11 19:41:55 +02:00
|
|
|
if err != nil {
|
|
|
|
//TODO GUESS HOW MUCH LBC WAS RELEASED THAT WE DON'T KNOW ABOUT, because screw you SDK
|
|
|
|
abandonRspChan <- abandonResponse{
|
|
|
|
ClaimID: claimID,
|
|
|
|
Error: err,
|
|
|
|
Amount: 0, // this is likely wrong, but oh well... there is literally nothing I can do about it
|
|
|
|
}
|
2019-10-12 09:49:49 +02:00
|
|
|
continue
|
2019-10-11 19:41:55 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
abandonRspChan <- abandonResponse{
|
|
|
|
ClaimID: claimID,
|
|
|
|
Error: err,
|
|
|
|
Amount: 0,
|
|
|
|
}
|
2019-10-12 09:49:49 +02:00
|
|
|
continue
|
2019-10-11 19:41:55 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-31 04:30:02 +02:00
|
|
|
if summary == nil || len(*summary) < 1 {
|
2019-10-11 19:41:55 +02:00
|
|
|
abandonRspChan <- abandonResponse{
|
|
|
|
ClaimID: claimID,
|
|
|
|
Error: errors.Err("error abandoning supports: no outputs while abandoning %s", claimID),
|
|
|
|
Amount: 0,
|
|
|
|
}
|
2019-10-12 09:49:49 +02:00
|
|
|
continue
|
2019-10-11 19:41:55 +02:00
|
|
|
}
|
2020-03-31 04:30:02 +02:00
|
|
|
var outputAmount float64
|
|
|
|
for _, tx := range *summary {
|
|
|
|
amount, err := strconv.ParseFloat(tx.Outputs[0].Amount, 64)
|
|
|
|
if err != nil {
|
|
|
|
abandonRspChan <- abandonResponse{
|
|
|
|
ClaimID: claimID,
|
|
|
|
Error: errors.Err(err),
|
|
|
|
Amount: 0,
|
|
|
|
}
|
|
|
|
continue outer
|
|
|
|
}
|
|
|
|
outputAmount += amount
|
|
|
|
}
|
2019-10-11 19:41:55 +02:00
|
|
|
if err != nil {
|
|
|
|
abandonRspChan <- abandonResponse{
|
|
|
|
ClaimID: claimID,
|
|
|
|
Error: errors.Err(err),
|
|
|
|
Amount: 0,
|
|
|
|
}
|
2019-10-12 09:49:49 +02:00
|
|
|
continue
|
2019-10-11 19:41:55 +02:00
|
|
|
}
|
|
|
|
log.Infof("Abandoned supports of %.4f LBC for claim %s", outputAmount, claimID)
|
|
|
|
abandonRspChan <- abandonResponse{
|
|
|
|
ClaimID: claimID,
|
|
|
|
Error: nil,
|
|
|
|
Amount: outputAmount,
|
|
|
|
}
|
2019-10-12 09:49:49 +02:00
|
|
|
continue
|
2019-10-11 19:41:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
producerWG.Wait()
|
|
|
|
close(claimIDChan)
|
|
|
|
consumerWG.Wait()
|
|
|
|
close(abandonRspChan)
|
|
|
|
|
|
|
|
totalAbandoned := 0.0
|
|
|
|
for r := range abandonRspChan {
|
|
|
|
if r.Error != nil {
|
|
|
|
log.Errorf("Failed abandoning supports for %s: %s", r.ClaimID, r.Error.Error())
|
2019-09-25 04:38:49 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-10-11 19:41:55 +02:00
|
|
|
totalAbandoned += r.Amount
|
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
|
|
|
}
|
|
|
|
|
2019-10-11 00:57:42 +02:00
|
|
|
type updateInfo struct {
|
|
|
|
ClaimID string
|
|
|
|
streamUpdateOptions *jsonrpc.StreamUpdateOptions
|
2020-08-08 01:12:55 +02:00
|
|
|
videoStatus *shared.VideoStatus
|
2019-10-11 00:57:42 +02:00
|
|
|
}
|
|
|
|
|
2019-08-27 00:31:27 +02:00
|
|
|
func transferVideos(s *Sync) error {
|
2020-05-19 23:13:01 +02:00
|
|
|
start := time.Now()
|
|
|
|
defer func(start time.Time) {
|
|
|
|
timing.TimedComponent("transferVideos").Add(time.Since(start))
|
|
|
|
}(start)
|
2019-08-21 19:40:35 +02:00
|
|
|
cleanTransfer := true
|
|
|
|
|
2020-08-08 01:12:55 +02:00
|
|
|
streamChan := make(chan updateInfo, s.Manager.CliFlags.ConcurrentJobs)
|
2019-10-11 00:57:42 +02:00
|
|
|
account, err := s.getDefaultAccount()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-13 00:55:50 +02:00
|
|
|
streams, err := s.daemon.StreamList(&account, 1, 30000)
|
2019-10-11 00:57:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
producerWG := &stop.Group{}
|
|
|
|
producerWG.Add(1)
|
|
|
|
go func() {
|
2019-10-11 19:41:55 +02:00
|
|
|
defer producerWG.Done()
|
2019-10-11 00:57:42 +02:00
|
|
|
for _, video := range s.syncedVideos {
|
2020-08-08 01:12:55 +02:00
|
|
|
if !video.Published || video.Transferred || video.MetadataVersion != shared.LatestMetadataVersion {
|
2019-10-10 16:50:33 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-10-11 00:57:42 +02:00
|
|
|
var stream *jsonrpc.Claim = nil
|
2019-12-08 17:48:24 +01:00
|
|
|
for _, c := range streams.Items {
|
2020-08-08 01:12:55 +02:00
|
|
|
if c.ClaimID != video.ClaimID || (c.SigningChannel != nil && c.SigningChannel.ClaimID != s.DbChannelData.ChannelClaimID) {
|
2019-10-11 00:57:42 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
stream = &c
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if stream == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-08-16 05:34:25 +02:00
|
|
|
|
2019-10-11 00:57:42 +02:00
|
|
|
streamUpdateOptions := jsonrpc.StreamUpdateOptions{
|
|
|
|
StreamCreateOptions: &jsonrpc.StreamCreateOptions{
|
2020-01-12 04:01:40 +01:00
|
|
|
ClaimCreateOptions: jsonrpc.ClaimCreateOptions{
|
2021-03-23 01:18:26 +01:00
|
|
|
ClaimAddress: &s.DbChannelData.PublishAddress.Address,
|
2020-01-12 04:01:40 +01:00
|
|
|
FundingAccountIDs: []string{
|
|
|
|
account,
|
|
|
|
},
|
|
|
|
},
|
2019-10-11 00:57:42 +02:00
|
|
|
},
|
2022-01-26 07:11:26 +01:00
|
|
|
Bid: util.PtrToString(fmt.Sprintf("%.5f", publishAmount/2.)),
|
2019-10-11 00:57:42 +02:00
|
|
|
}
|
2020-08-08 01:12:55 +02:00
|
|
|
videoStatus := shared.VideoStatus{
|
|
|
|
ChannelID: s.DbChannelData.ChannelId,
|
2019-10-11 00:57:42 +02:00
|
|
|
VideoID: video.VideoID,
|
|
|
|
ClaimID: video.ClaimID,
|
|
|
|
ClaimName: video.ClaimName,
|
2020-08-08 01:12:55 +02:00
|
|
|
Status: shared.VideoStatusPublished,
|
2019-10-11 00:57:42 +02:00
|
|
|
IsTransferred: util.PtrToBool(true),
|
|
|
|
}
|
|
|
|
streamChan <- updateInfo{
|
|
|
|
ClaimID: video.ClaimID,
|
|
|
|
streamUpdateOptions: &streamUpdateOptions,
|
|
|
|
videoStatus: &videoStatus,
|
|
|
|
}
|
2019-08-20 07:06:51 +02:00
|
|
|
}
|
2019-10-11 00:57:42 +02:00
|
|
|
}()
|
2019-08-20 07:06:51 +02:00
|
|
|
|
2019-10-11 00:57:42 +02:00
|
|
|
consumerWG := &stop.Group{}
|
2020-08-08 01:12:55 +02:00
|
|
|
for i := 0; i < s.Manager.CliFlags.ConcurrentJobs; i++ {
|
2019-10-11 00:57:42 +02:00
|
|
|
consumerWG.Add(1)
|
|
|
|
go func(worker int) {
|
|
|
|
defer consumerWG.Done()
|
|
|
|
for {
|
|
|
|
ui, more := <-streamChan
|
|
|
|
if !more {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
err := s.streamUpdate(&ui)
|
|
|
|
if err != nil {
|
|
|
|
cleanTransfer = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(i)
|
2019-08-16 05:34:25 +02:00
|
|
|
}
|
2019-10-11 00:57:42 +02:00
|
|
|
producerWG.Wait()
|
|
|
|
close(streamChan)
|
|
|
|
consumerWG.Wait()
|
|
|
|
|
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
|
|
|
|
}
|
2019-09-27 19:24:05 +02:00
|
|
|
|
2019-10-11 00:57:42 +02:00
|
|
|
func (s *Sync) streamUpdate(ui *updateInfo) error {
|
2020-05-19 23:13:01 +02:00
|
|
|
start := time.Now()
|
2019-10-11 00:57:42 +02:00
|
|
|
result, updateError := s.daemon.StreamUpdate(ui.ClaimID, *ui.streamUpdateOptions)
|
2020-05-19 23:13:01 +02:00
|
|
|
timing.TimedComponent("transferStreamUpdate").Add(time.Since(start))
|
2019-10-11 00:57:42 +02:00
|
|
|
if updateError != nil {
|
|
|
|
ui.videoStatus.FailureReason = updateError.Error()
|
2022-02-07 19:17:41 +01:00
|
|
|
ui.videoStatus.Status = shared.VideoStatusTransferFailed
|
2019-10-11 00:57:42 +02:00
|
|
|
ui.videoStatus.IsTransferred = util.PtrToBool(false)
|
|
|
|
} else {
|
|
|
|
ui.videoStatus.IsTransferred = util.PtrToBool(len(result.Outputs) != 0)
|
|
|
|
}
|
|
|
|
log.Infof("TRANSFERRED %t", *ui.videoStatus.IsTransferred)
|
2020-08-08 01:12:55 +02:00
|
|
|
statusErr := s.Manager.ApiConfig.MarkVideoStatus(*ui.videoStatus)
|
2019-10-11 00:57:42 +02:00
|
|
|
if statusErr != nil {
|
|
|
|
return errors.Prefix(statusErr.Error(), updateError)
|
|
|
|
}
|
|
|
|
return errors.Err(updateError)
|
|
|
|
}
|
|
|
|
|
2019-08-27 00:31:27 +02:00
|
|
|
func transferChannel(s *Sync) error {
|
2020-05-19 23:13:01 +02:00
|
|
|
start := time.Now()
|
|
|
|
defer func(start time.Time) {
|
|
|
|
timing.TimedComponent("transferChannel").Add(time.Since(start))
|
|
|
|
}(start)
|
2019-10-08 14:42:57 +02:00
|
|
|
account, err := s.getDefaultAccount()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-13 00:55:50 +02:00
|
|
|
channelClaims, err := s.daemon.ChannelList(&account, 1, 50, nil)
|
2019-08-21 05:38:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
2019-10-08 14:42:57 +02:00
|
|
|
var channelClaim *jsonrpc.Transaction = nil
|
|
|
|
for _, c := range channelClaims.Items {
|
2020-08-08 01:12:55 +02:00
|
|
|
if c.ClaimID != s.DbChannelData.ChannelClaimID {
|
2019-10-08 14:42:57 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
channelClaim = &c
|
|
|
|
break
|
2019-08-21 05:38:36 +02:00
|
|
|
}
|
2019-10-08 14:42:57 +02:00
|
|
|
if channelClaim == nil {
|
2019-09-25 04:38:49 +02:00
|
|
|
return nil
|
|
|
|
}
|
2019-10-08 14:42:57 +02:00
|
|
|
|
2019-08-21 05:38:36 +02:00
|
|
|
updateOptions := jsonrpc.ChannelUpdateOptions{
|
2019-10-03 21:06:47 +02:00
|
|
|
Bid: util.PtrToString(fmt.Sprintf("%.6f", channelClaimAmount-0.005)),
|
2019-08-21 05:38:36 +02:00
|
|
|
ChannelCreateOptions: jsonrpc.ChannelCreateOptions{
|
|
|
|
ClaimCreateOptions: jsonrpc.ClaimCreateOptions{
|
2021-03-23 01:18:26 +01:00
|
|
|
ClaimAddress: &s.DbChannelData.PublishAddress.Address,
|
2019-08-21 05:38:36 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-08-08 01:12:55 +02:00
|
|
|
result, err := s.daemon.ChannelUpdate(s.DbChannelData.ChannelClaimID, updateOptions)
|
2019-09-27 16:10:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
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-10-08 01:38:39 +02:00
|
|
|
return nil
|
2019-08-16 05:34:25 +02:00
|
|
|
}
|