improve getDefaultAccount

use default account on more calls
improve API logging
fix memory leak
This commit is contained in:
Niko Storni 2019-10-16 19:38:45 +02:00
parent 682117a030
commit 291a105269
3 changed files with 43 additions and 32 deletions

View file

@ -141,7 +141,9 @@ func (s *Sync) walletSetup() error {
return nil return nil
} }
func (s *Sync) getDefaultAccount() (string, error) { func (s *Sync) getDefaultAccount() (string, error) {
if s.defaultAccountID == "" {
accounts, err := s.daemon.AccountList() accounts, err := s.daemon.AccountList()
if err != nil { if err != nil {
return "", errors.Err(err) return "", errors.Err(err)
@ -150,17 +152,17 @@ func (s *Sync) getDefaultAccount() (string, error) {
if logUtils.IsRegTest() { if logUtils.IsRegTest() {
accountsNet = (*accounts).LBCRegtest accountsNet = (*accounts).LBCRegtest
} }
defaultAccount := ""
for _, account := range accountsNet { for _, account := range accountsNet {
if account.IsDefault { if account.IsDefault {
defaultAccount = account.ID s.defaultAccountID = account.ID
break break
} }
} }
if defaultAccount == "" { if s.defaultAccountID == "" {
return "", errors.Err("No default account found") return "", errors.Err("No default account found")
} }
return defaultAccount, nil }
return s.defaultAccountID, nil
} }
func (s *Sync) ensureEnoughUTXOs() error { func (s *Sync) ensureEnoughUTXOs() error {

View file

@ -89,6 +89,7 @@ type Sync struct {
transferState int transferState int
clientPublishAddress string clientPublishAddress string
publicKey string publicKey string
defaultAccountID string
} }
func (s *Sync) AppendSyncedVideo(videoID string, published bool, failureReason string, claimName string, claimID string, metadataVersion int8, size int64) { func (s *Sync) AppendSyncedVideo(videoID string, published bool, failureReason string, claimName string, claimID string, metadataVersion int8, size int64) {
@ -342,7 +343,10 @@ func (s *Sync) processTransfers() (e error) {
if err != nil { if err != nil {
return err return err
} }
defaultAccount, err := s.getDefaultAccount()
if err != nil {
return err
}
reallocateSupports := supportAmount > 0.01 reallocateSupports := supportAmount > 0.01
if reallocateSupports { if reallocateSupports {
err = waitConfirmations(s) err = waitConfirmations(s)
@ -350,7 +354,7 @@ func (s *Sync) processTransfers() (e error) {
return err return err
} }
isTip := true isTip := true
summary, err := s.daemon.SupportCreate(s.lbryChannelID, fmt.Sprintf("%.6f", supportAmount), &isTip, nil, nil) summary, err := s.daemon.SupportCreate(s.lbryChannelID, fmt.Sprintf("%.6f", supportAmount), &isTip, nil, []string{defaultAccount})
if err != nil { if err != nil {
return errors.Err(err) return errors.Err(err)
} }

View file

@ -82,13 +82,14 @@ func (a *APIConfig) FetchChannels(status string, cp *SyncProperties) ([]YoutubeC
if err != nil { if err != nil {
return nil, errors.Err(err) return nil, errors.Err(err)
} }
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
if res.StatusCode != http.StatusOK { if res.StatusCode != http.StatusOK {
util.SendErrorToSlack("Error %d while trying to call %s. Waiting to retry", res.StatusCode, endpoint) util.SendErrorToSlack("Error %d while trying to call %s. Waiting to retry", res.StatusCode, endpoint)
log.Debugln(body)
time.Sleep(30 * time.Second) time.Sleep(30 * time.Second)
return a.FetchChannels(status, cp) return a.FetchChannels(status, cp)
} }
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
var response apiJobsResponse var response apiJobsResponse
err = json.Unmarshal(body, &response) err = json.Unmarshal(body, &response)
if err != nil { if err != nil {
@ -139,14 +140,14 @@ func (a *APIConfig) SetChannelCert(certHex string, channelID string) error {
if err != nil { if err != nil {
return errors.Err(err) return errors.Err(err)
} }
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
if res.StatusCode != http.StatusOK { if res.StatusCode != http.StatusOK {
util.SendErrorToSlack("Error %d while trying to call %s. Waiting to retry", res.StatusCode, endpoint) util.SendErrorToSlack("Error %d while trying to call %s. Waiting to retry", res.StatusCode, endpoint)
log.Debugln(body)
time.Sleep(30 * time.Second) time.Sleep(30 * time.Second)
return a.SetChannelCert(certHex, channelID) return a.SetChannelCert(certHex, channelID)
} }
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
var response apiSetChannelCertResponse var response apiSetChannelCertResponse
err = json.Unmarshal(body, &response) err = json.Unmarshal(body, &response)
if err != nil { if err != nil {
@ -182,13 +183,14 @@ func (a *APIConfig) SetChannelStatus(channelID string, status string, failureRea
if err != nil { if err != nil {
return nil, nil, errors.Err(err) return nil, nil, errors.Err(err)
} }
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
if res.StatusCode != http.StatusOK { if res.StatusCode != http.StatusOK {
util.SendErrorToSlack("Error %d while trying to call %s. Waiting to retry", res.StatusCode, endpoint) util.SendErrorToSlack("Error %d while trying to call %s. Waiting to retry", res.StatusCode, endpoint)
log.Debugln(body)
time.Sleep(30 * time.Second) time.Sleep(30 * time.Second)
return a.SetChannelStatus(channelID, status, failureReason, transferState) return a.SetChannelStatus(channelID, status, failureReason, transferState)
} }
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
var response apiChannelStatusResponse var response apiChannelStatusResponse
err = json.Unmarshal(body, &response) err = json.Unmarshal(body, &response)
if err != nil { if err != nil {
@ -226,13 +228,14 @@ func (a *APIConfig) SetChannelClaimID(channelID string, channelClaimID string) e
if err != nil { if err != nil {
return errors.Err(err) return errors.Err(err)
} }
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
if res.StatusCode != http.StatusOK { if res.StatusCode != http.StatusOK {
util.SendErrorToSlack("Error %d while trying to call %s. Waiting to retry", res.StatusCode, endpoint) util.SendErrorToSlack("Error %d while trying to call %s. Waiting to retry", res.StatusCode, endpoint)
log.Debugln(body)
time.Sleep(30 * time.Second) time.Sleep(30 * time.Second)
return a.SetChannelClaimID(channelID, channelClaimID) return a.SetChannelClaimID(channelID, channelClaimID)
} }
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
var response apiChannelStatusResponse var response apiChannelStatusResponse
err = json.Unmarshal(body, &response) err = json.Unmarshal(body, &response)
if err != nil { if err != nil {
@ -264,13 +267,14 @@ func (a *APIConfig) DeleteVideos(videos []string) error {
if err != nil { if err != nil {
return errors.Err(err) return errors.Err(err)
} }
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
if res.StatusCode != http.StatusOK { if res.StatusCode != http.StatusOK {
util.SendErrorToSlack("Error %d while trying to call %s. Waiting to retry", res.StatusCode, endpoint) util.SendErrorToSlack("Error %d while trying to call %s. Waiting to retry", res.StatusCode, endpoint)
log.Debugln(body)
time.Sleep(30 * time.Second) time.Sleep(30 * time.Second)
return a.DeleteVideos(videos) return a.DeleteVideos(videos)
} }
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
var response struct { var response struct {
Success bool `json:"success"` Success bool `json:"success"`
Error null.String `json:"error"` Error null.String `json:"error"`
@ -336,13 +340,14 @@ func (a *APIConfig) MarkVideoStatus(status VideoStatus) error {
if err != nil { if err != nil {
return errors.Err(err) return errors.Err(err)
} }
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
if res.StatusCode != http.StatusOK { if res.StatusCode != http.StatusOK {
util.SendErrorToSlack("Error %d while trying to call %s. Waiting to retry", res.StatusCode, endpoint) util.SendErrorToSlack("Error %d while trying to call %s. Waiting to retry", res.StatusCode, endpoint)
log.Debugln(body)
time.Sleep(30 * time.Second) time.Sleep(30 * time.Second)
return a.MarkVideoStatus(status) return a.MarkVideoStatus(status)
} }
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
var response struct { var response struct {
Success bool `json:"success"` Success bool `json:"success"`
Error null.String `json:"error"` Error null.String `json:"error"`