Refactor in preparation for socket.io getDetailedTransaction method

indexv1
Martin Boehm 2018-02-19 16:32:16 +01:00
parent af0e85ce43
commit aac52a3fcb
1 changed files with 43 additions and 33 deletions

View File

@ -142,6 +142,8 @@ var onMessageHandlers = map[string]func(*SocketIoServer, json.RawMessage) (inter
"\"getInfo\"": func(s *SocketIoServer, params json.RawMessage) (rv interface{}, err error) { "\"getInfo\"": func(s *SocketIoServer, params json.RawMessage) (rv interface{}, err error) {
return s.getInfo() return s.getInfo()
}, },
// getDetailedTransaction
// sendTransaction
} }
func (s *SocketIoServer) onMessage(c *gosocketio.Channel, req map[string]json.RawMessage) interface{} { func (s *SocketIoServer) onMessage(c *gosocketio.Channel, req map[string]json.RawMessage) interface{} {
@ -223,7 +225,7 @@ type addressHistoryIndexes struct {
OutputIndexes []int `json:"outputIndexes"` OutputIndexes []int `json:"outputIndexes"`
} }
type addressHistoryInputs struct { type txInputs struct {
PrevTxID string `json:"prevTxId"` PrevTxID string `json:"prevTxId"`
OutputIndex int `json:"outputIndex"` OutputIndex int `json:"outputIndex"`
Script string `json:"script"` Script string `json:"script"`
@ -233,7 +235,7 @@ type addressHistoryInputs struct {
Satoshis int64 `json:"satoshis"` Satoshis int64 `json:"satoshis"`
} }
type addressHistoryOutputs struct { type txOutputs struct {
Satoshis int64 `json:"satoshis"` Satoshis int64 `json:"satoshis"`
Script string `json:"script"` Script string `json:"script"`
ScriptAsm string `json:"scriptAsm"` ScriptAsm string `json:"scriptAsm"`
@ -243,11 +245,7 @@ type addressHistoryOutputs struct {
Address string `json:"address"` Address string `json:"address"`
} }
type addressHistoryItem struct { type resTx struct {
Addresses map[string]addressHistoryIndexes `json:"addresses"`
Satoshis int `json:"satoshis"`
Confirmations int `json:"confirmations"`
Tx struct {
Hex string `json:"hex"` Hex string `json:"hex"`
BlockHash string `json:"blockHash"` BlockHash string `json:"blockHash"`
Height int `json:"height"` Height int `json:"height"`
@ -256,12 +254,18 @@ type addressHistoryItem struct {
Hash string `json:"hash"` Hash string `json:"hash"`
Locktime int `json:"locktime"` Locktime int `json:"locktime"`
Size int `json:"size"` Size int `json:"size"`
Inputs []addressHistoryInputs `json:"inputs"` Inputs []txInputs `json:"inputs"`
InputSatoshis int64 `json:"inputSatoshis"` InputSatoshis int64 `json:"inputSatoshis"`
Outputs []addressHistoryOutputs `json:"outputs"` Outputs []txOutputs `json:"outputs"`
OutputSatoshis int64 `json:"outputSatoshis"` OutputSatoshis int64 `json:"outputSatoshis"`
FeeSatoshis int64 `json:"feeSatoshis"` FeeSatoshis int64 `json:"feeSatoshis"`
} `json:"tx"` }
type addressHistoryItem struct {
Addresses map[string]addressHistoryIndexes `json:"addresses"`
Satoshis int `json:"satoshis"`
Confirmations int `json:"confirmations"`
Tx resTx `json:"tx"`
} }
type resultGetAddressHistory struct { type resultGetAddressHistory struct {
@ -293,6 +297,23 @@ func stringInSlice(a string, list []string) bool {
return false return false
} }
func txToResTx(tx *bchain.Tx, height int, hi []txInputs, ho []txOutputs) resTx {
return resTx{
BlockHash: tx.BlockHash,
BlockTimestamp: tx.Blocktime,
// FeeSatoshis,
Hash: tx.Txid,
Height: height,
Hex: tx.Hex,
Inputs: hi,
// InputSatoshis,
Locktime: int(tx.LockTime),
Outputs: ho,
// OutputSatoshis,
Version: int(tx.Version),
}
}
func (s *SocketIoServer) getAddressHistory(addr []string, rr *reqRange) (res resultGetAddressHistory, err error) { func (s *SocketIoServer) getAddressHistory(addr []string, rr *reqRange) (res resultGetAddressHistory, err error) {
txids, err := s.getAddressTxids(addr, rr) txids, err := s.getAddressTxids(addr, rr)
if err != nil { if err != nil {
@ -313,10 +334,10 @@ func (s *SocketIoServer) getAddressHistory(addr []string, rr *reqRange) (res res
return res, err return res, err
} }
ads := make(map[string]addressHistoryIndexes) ads := make(map[string]addressHistoryIndexes)
hi := make([]addressHistoryInputs, 0) hi := make([]txInputs, 0)
ho := make([]addressHistoryOutputs, 0) ho := make([]txOutputs, 0)
for _, vin := range tx.Vin { for _, vin := range tx.Vin {
ai := addressHistoryInputs{ ai := txInputs{
Script: vin.ScriptSig.Hex, Script: vin.ScriptSig.Hex,
ScriptAsm: vin.ScriptSig.Asm, ScriptAsm: vin.ScriptSig.Asm,
Sequence: int64(vin.Sequence), Sequence: int64(vin.Sequence),
@ -353,7 +374,7 @@ func (s *SocketIoServer) getAddressHistory(addr []string, rr *reqRange) (res res
hi = append(hi, ai) hi = append(hi, ai)
} }
for _, vout := range tx.Vout { for _, vout := range tx.Vout {
ao := addressHistoryOutputs{ ao := txOutputs{
Satoshis: int64(vout.Value * 10E8), Satoshis: int64(vout.Value * 10E8),
Script: vout.ScriptPubKey.Hex, Script: vout.ScriptPubKey.Hex,
ScriptAsm: vout.ScriptPubKey.Asm, ScriptAsm: vout.ScriptPubKey.Asm,
@ -379,18 +400,7 @@ func (s *SocketIoServer) getAddressHistory(addr []string, rr *reqRange) (res res
ahi := addressHistoryItem{} ahi := addressHistoryItem{}
ahi.Addresses = ads ahi.Addresses = ads
ahi.Confirmations = int(tx.Confirmations) ahi.Confirmations = int(tx.Confirmations)
ahi.Tx.BlockHash = tx.BlockHash ahi.Tx = txToResTx(tx, int(bestheight)-int(tx.Confirmations), hi, ho)
ahi.Tx.BlockTimestamp = tx.Blocktime
// ahi.Tx.FeeSatoshis
ahi.Tx.Hash = tx.Txid
ahi.Tx.Height = int(bestheight) - ahi.Confirmations
ahi.Tx.Hex = tx.Hex
ahi.Tx.Inputs = hi
// ahi.Tx.InputSatoshis
ahi.Tx.Locktime = int(tx.LockTime)
ahi.Tx.Outputs = ho
// ahi.Tx.OutputSatoshis
ahi.Tx.Version = int(tx.Version)
res.Result.Items = append(res.Result.Items, ahi) res.Result.Items = append(res.Result.Items, ahi)
} }
} }