From 48608729a5311fbbf68c36c9da3896eb3f57b0ee Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Fri, 6 Dec 2019 18:53:47 +0100 Subject: [PATCH] Fix number/string conversion problem --- chansummary.go | 37 ++++++++++++----------------- input.go | 60 +++++++++++++++++++++++++++++++----------------- sweeptimelock.go | 4 ++-- 3 files changed, 56 insertions(+), 45 deletions(-) diff --git a/chansummary.go b/chansummary.go index 985eb4b..19d0375 100644 --- a/chansummary.go +++ b/chansummary.go @@ -44,7 +44,7 @@ func collectChanSummary(cfg *config, channels []*SummaryEntry) error { } } else { summaryFile.OpenChannels++ - summaryFile.FundsOpenChannels += channel.LocalBalance + summaryFile.FundsOpenChannels += uint64(channel.LocalBalance) channel.ClosingTX = nil } @@ -92,11 +92,11 @@ func reportOutspend(api *chainApi, summaryFile *SummaryEntryFile, return err } - summaryFile.FundsClosedChannels += entry.LocalBalance + summaryFile.FundsClosedChannels += uint64(entry.LocalBalance) if isCoopClose(spendTx) { summaryFile.CoopClosedChannels++ - summaryFile.FundsCoopClose += entry.LocalBalance + summaryFile.FundsCoopClose += uint64(entry.LocalBalance) entry.ClosingTX.ForceClose = false return nil } @@ -104,36 +104,29 @@ func reportOutspend(api *chainApi, summaryFile *SummaryEntryFile, summaryFile.ForceClosedChannels++ entry.ClosingTX.ForceClose = true - numSpent := 0 + var utxo []*vout for _, vout := range spendTx.Vout { - if vout.outspend.Spent { - numSpent++ + if !vout.outspend.Spent { + utxo = append(utxo, vout) } } - if numSpent != len(spendTx.Vout) { + if len(utxo) > 0 { log.Debugf("Channel %s spent by %s:%d which has %d outputs of "+ - "which %d are spent.", entry.ChannelPoint, os.Txid, - os.Vin, len(spendTx.Vout), numSpent) - var utxo []*vout - for _, vout := range spendTx.Vout { - if !vout.outspend.Spent { - utxo = append(utxo, vout) - } - } + "which %d are unspent.", entry.ChannelPoint, os.Txid, + os.Vin, len(spendTx.Vout), len(utxo)) + entry.ClosingTX.AllOutsSpent = false summaryFile.ChannelsWithPotential++ if couldBeOurs(entry, utxo) { summaryFile.FundsForceClose += utxo[0].Value - outs := spendTx.Vout - switch { - case len(outs) == 1 && - outs[0].ScriptPubkeyType == "v0_p2wpkh" && - outs[0].outspend.Spent == false: + case len(utxo) == 1 && + utxo[0].ScriptPubkeyType == "v0_p2wpkh" && + utxo[0].outspend.Spent == false: - entry.ClosingTX.OurAddr = outs[0].ScriptPubkeyAddr + entry.ClosingTX.OurAddr = utxo[0].ScriptPubkeyAddr } } else { for idx, vout := range spendTx.Vout { @@ -150,7 +143,7 @@ func reportOutspend(api *chainApi, summaryFile *SummaryEntryFile, } } else { entry.ClosingTX.AllOutsSpent = true - summaryFile.FundsClosedSpent += entry.LocalBalance + summaryFile.FundsClosedSpent += uint64(entry.LocalBalance) summaryFile.FullySpentChannels++ } diff --git a/input.go b/input.go index ce9905d..3f15c9d 100644 --- a/input.go +++ b/input.go @@ -12,6 +12,24 @@ import ( "strings" ) +type NumberString uint64 + +func (n *NumberString) UnmarshalJSON(b []byte) error { + if b[0] != '"' { + return json.Unmarshal(b, (*uint64)(n)) + } + var s string + if err := json.Unmarshal(b, &s); err != nil { + return err + } + i, err := strconv.Atoi(s) + if err != nil { + return err + } + *n = NumberString(i) + return nil +} + type InputFile interface { AsSummaryEntries() ([]*SummaryEntry, error) } @@ -84,12 +102,12 @@ func (f *listChannelsFile) AsSummaryEntries() ([]*SummaryEntry, error) { } type listChannelsChannel struct { - RemotePubkey string `json:"remote_pubkey"` - ChannelPoint string `json:"channel_point"` - CapacityStr string `json:"capacity"` - Initiator bool `json:"initiator"` - LocalBalanceStr string `json:"local_balance"` - RemoteBalanceStr string `json:"remote_balance"` + RemotePubkey string `json:"remote_pubkey"` + ChannelPoint string `json:"channel_point"` + Capacity NumberString `json:"capacity"` + Initiator bool `json:"initiator"` + LocalBalance NumberString `json:"local_balance"` + RemoteBalance NumberString `json:"remote_balance"` } func (c *listChannelsChannel) AsSummaryEntry() *SummaryEntry { @@ -98,10 +116,10 @@ func (c *listChannelsChannel) AsSummaryEntry() *SummaryEntry { ChannelPoint: c.ChannelPoint, FundingTXID: fundingTXID(c.ChannelPoint), FundingTXIndex: fundingTXIndex(c.ChannelPoint), - Capacity: uint64(parseInt(c.CapacityStr)), + Capacity: uint64(c.Capacity), Initiator: c.Initiator, - LocalBalance: uint64(parseInt(c.LocalBalanceStr)), - RemoteBalance: uint64(parseInt(c.RemoteBalanceStr)), + LocalBalance: uint64(c.LocalBalance), + RemoteBalance: uint64(c.RemoteBalance), } } @@ -138,11 +156,11 @@ func (f *pendingChannelsFile) AsSummaryEntries() ([]*SummaryEntry, error) { type pendingChannelsChannel struct { Channel struct { - RemotePubkey string `json:"remote_node_pub"` - ChannelPoint string `json:"channel_point"` - CapacityStr string `json:"capacity"` - LocalBalanceStr string `json:"local_balance"` - RemoteBalanceStr string `json:"remote_balance"` + RemotePubkey string `json:"remote_node_pub"` + ChannelPoint string `json:"channel_point"` + Capacity NumberString `json:"capacity"` + LocalBalance NumberString `json:"local_balance"` + RemoteBalance NumberString `json:"remote_balance"` } `json:"channel"` } @@ -152,10 +170,10 @@ func (c *pendingChannelsChannel) AsSummaryEntry() *SummaryEntry { ChannelPoint: c.Channel.ChannelPoint, FundingTXID: fundingTXID(c.Channel.ChannelPoint), FundingTXIndex: fundingTXIndex(c.Channel.ChannelPoint), - Capacity: uint64(parseInt(c.Channel.CapacityStr)), + Capacity: uint64(c.Channel.Capacity), Initiator: false, - LocalBalance: uint64(parseInt(c.Channel.LocalBalanceStr)), - RemoteBalance: uint64(parseInt(c.Channel.RemoteBalanceStr)), + LocalBalance: uint64(c.Channel.LocalBalance), + RemoteBalance: uint64(c.Channel.RemoteBalance), } } @@ -179,10 +197,10 @@ func (c *channelDBFile) AsSummaryEntries() ([]*SummaryEntry, error) { FundingTXIndex: channel.FundingOutpoint.Index, Capacity: uint64(channel.Capacity), Initiator: channel.IsInitiator, - LocalBalance: uint64( + LocalBalance: uint64( channel.LocalCommitment.LocalBalance.ToSatoshis(), ), - RemoteBalance: uint64( + RemoteBalance: uint64( channel.LocalCommitment.RemoteBalance.ToSatoshis(), ), } @@ -212,10 +230,10 @@ func fundingTXIndex(chanPoint string) uint32 { return uint32(parseInt(parts[1])) } -func parseInt(str string) int { +func parseInt(str string) uint64 { index, err := strconv.Atoi(str) if err != nil { panic(fmt.Errorf("error parsing '%s' as int: %v", str, err)) } - return index + return uint64(index) } diff --git a/sweeptimelock.go b/sweeptimelock.go index 4c12bb0..f26a1c8 100644 --- a/sweeptimelock.go +++ b/sweeptimelock.go @@ -45,14 +45,14 @@ func sweepTimeLock(cfg *config, entries []*SummaryEntry, sweepAddr string, txindex := -1 if len(fc.Outs) == 1 { txindex = 0 - if fc.Outs[0].Value != entry.LocalBalance { + if fc.Outs[0].Value != uint64(entry.LocalBalance) { log.Errorf("Potential value mismatch! %d vs %d (%s)", fc.Outs[0].Value, entry.LocalBalance, entry.ChannelPoint) } } else { for idx, out := range fc.Outs { - if out.Value == entry.LocalBalance { + if out.Value == uint64(entry.LocalBalance) { txindex = idx } }