Handle nil amounts in socket.io interface

This commit is contained in:
Martin Boehm 2019-01-17 19:58:59 +01:00
parent 522e6528d3
commit d1f30e27cf
3 changed files with 25 additions and 9 deletions

View File

@ -76,6 +76,16 @@ func (a *Amount) AsBigInt() big.Int {
return big.Int(*a) return big.Int(*a)
} }
// AsInt64 returns Amount as int64 (0 if Amount is nil).
// It is used only for legacy interfaces (socket.io)
// and generally not recommended to use for possible loss of precision.
func (a *Amount) AsInt64() int64 {
if a == nil {
return 0
}
return (*big.Int)(a).Int64()
}
// Vin contains information about single transaction input // Vin contains information about single transaction input
type Vin struct { type Vin struct {
Txid string `json:"txid,omitempty"` Txid string `json:"txid,omitempty"`

View File

@ -191,7 +191,9 @@ func (w *Worker) GetTransactionFromBchainTx(bchainTx *bchain.Tx, height uint32,
} }
} }
} }
valInSat.Add(&valInSat, (*big.Int)(vin.ValueSat)) if vin.ValueSat != nil {
valInSat.Add(&valInSat, (*big.Int)(vin.ValueSat))
}
} }
} else if w.chainType == bchain.ChainEthereumType { } else if w.chainType == bchain.ChainEthereumType {
if len(bchainVin.Addresses) > 0 { if len(bchainVin.Addresses) > 0 {

View File

@ -15,7 +15,7 @@ import (
"github.com/golang/glog" "github.com/golang/glog"
"github.com/juju/errors" "github.com/juju/errors"
"github.com/martinboehm/golang-socketio" gosocketio "github.com/martinboehm/golang-socketio"
"github.com/martinboehm/golang-socketio/transport" "github.com/martinboehm/golang-socketio/transport"
) )
@ -312,7 +312,7 @@ func txToResTx(tx *api.Tx) resTx {
Script: &script, Script: &script,
Sequence: int64(vin.Sequence), Sequence: int64(vin.Sequence),
OutputIndex: int(vin.Vout), OutputIndex: int(vin.Vout),
Satoshis: (*big.Int)(vin.ValueSat).Int64(), Satoshis: vin.ValueSat.AsInt64(),
} }
if len(vin.Addresses) > 0 { if len(vin.Addresses) > 0 {
a := vin.Addresses[0] a := vin.Addresses[0]
@ -325,7 +325,7 @@ func txToResTx(tx *api.Tx) resTx {
vout := &tx.Vout[i] vout := &tx.Vout[i]
script := vout.Hex script := vout.Hex
output := txOutputs{ output := txOutputs{
Satoshis: (*big.Int)(vout.ValueSat).Int64(), Satoshis: vout.ValueSat.AsInt64(),
Script: &script, Script: &script,
} }
if len(vout.Addresses) > 0 { if len(vout.Addresses) > 0 {
@ -342,15 +342,15 @@ func txToResTx(tx *api.Tx) resTx {
} }
return resTx{ return resTx{
BlockTimestamp: tx.Blocktime, BlockTimestamp: tx.Blocktime,
FeeSatoshis: (*big.Int)(tx.FeesSat).Int64(), FeeSatoshis: tx.FeesSat.AsInt64(),
Hash: tx.Txid, Hash: tx.Txid,
Height: h, Height: h,
Hex: tx.Hex, Hex: tx.Hex,
Inputs: inputs, Inputs: inputs,
InputSatoshis: (*big.Int)(tx.ValueInSat).Int64(), InputSatoshis: tx.ValueInSat.AsInt64(),
Locktime: int(tx.Locktime), Locktime: int(tx.Locktime),
Outputs: outputs, Outputs: outputs,
OutputSatoshis: (*big.Int)(tx.ValueOutSat).Int64(), OutputSatoshis: tx.ValueOutSat.AsInt64(),
Version: int(tx.Version), Version: int(tx.Version),
} }
} }
@ -407,7 +407,9 @@ func (s *SocketIoServer) getAddressHistory(addr []string, opts *addrOpts) (res r
ads[a] = hi ads[a] = hi
} }
hi.InputIndexes = append(hi.InputIndexes, int(vin.N)) hi.InputIndexes = append(hi.InputIndexes, int(vin.N))
totalSat.Sub(&totalSat, (*big.Int)(vin.ValueSat)) if vin.ValueSat != nil {
totalSat.Sub(&totalSat, (*big.Int)(vin.ValueSat))
}
} }
} }
for i := range tx.Vout { for i := range tx.Vout {
@ -420,7 +422,9 @@ func (s *SocketIoServer) getAddressHistory(addr []string, opts *addrOpts) (res r
ads[a] = hi ads[a] = hi
} }
hi.OutputIndexes = append(hi.OutputIndexes, int(vout.N)) hi.OutputIndexes = append(hi.OutputIndexes, int(vout.N))
totalSat.Add(&totalSat, (*big.Int)(vout.ValueSat)) if vout.ValueSat != nil {
totalSat.Add(&totalSat, (*big.Int)(vout.ValueSat))
}
} }
} }
ahi := addressHistoryItem{} ahi := addressHistoryItem{}