Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api/insight: use actual net params #1

Merged
merged 1 commit into from
Jun 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/insight/apimiddleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ func (c *insightApiContext) PostAddrsUtxoCtx(next http.Handler) http.Handler {
})
}

// GetAddressCommandCtx returns a http.HandlerFunc that embeds the value at the
// url part {command} into the request context.
// AddressCommandCtx returns a http.HandlerFunc that embeds the value at the url
// part {command} into the request context.
func (c *insightApiContext) AddressCommandCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
command := chi.URLParam(r, "command")
Expand Down
10 changes: 5 additions & 5 deletions api/insight/apiroutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ func (c *insightApiContext) getTransactionHex(w http.ResponseWriter, r *http.Req
}

txHex := c.BlockData.GetTransactionHex(txid)

if txHex == "" {
writeInsightNotFound(w, fmt.Sprintf("Unable to get transaction (%s)", txHex))
return
}

hexOutput := new(apitypes.InsightRawTx)
hexOutput.Rawtx = txHex
hexOutput := &apitypes.InsightRawTx{
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct literal init is preferred, at least for consistency

Rawtx: txHex,
}

writeJSON(w, hexOutput, c.getIndentQuery(r))
}
Expand Down Expand Up @@ -183,7 +183,7 @@ func (c *insightApiContext) getBlockSummary(w http.ResponseWriter, r *http.Reque
}

blockSummary := []*dcrjson.GetBlockVerboseResult{blockDcrd}
blockInsight, err := c.BlockConverter(blockSummary)
blockInsight, err := c.DcrToInsightBlock(blockSummary)
if err != nil {
apiLog.Errorf("Unable to process block (%s)", hash)
writeInsightError(w, "Unable to Process Block")
Expand Down Expand Up @@ -537,7 +537,7 @@ func (c *insightApiContext) getAddressesTxn(w http.ResponseWriter, r *http.Reque
}

// Convert to Insight API struct
txsNew, err := c.TxConverterWithParams(txsOld, noAsm, noScriptSig, noSpent)
txsNew, err := c.DcrToInsightTxns(txsOld, noAsm, noScriptSig, noSpent)
if err != nil {
apiLog.Error("Unable to process transactions")
writeInsightError(w, fmt.Sprintf("Unable to convert transactions (%s)", err))
Expand Down
129 changes: 60 additions & 69 deletions api/insight/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,60 @@ package insight

import (
"github.com/decred/dcrd/blockchain"
"github.com/decred/dcrd/chaincfg"
"github.com/decred/dcrd/dcrjson"
"github.com/decred/dcrd/dcrutil"
apitypes "github.com/decred/dcrdata/api/types"
)

// TxConverter converts dcrd-tx to insight tx
func (c *insightApiContext) TxConverter(txs []*dcrjson.TxRawResult) ([]apitypes.InsightTx, error) {
return c.TxConverterWithParams(txs, false, false, false)
return c.DcrToInsightTxns(txs, false, false, false)
}

// TxConverterWithParams takes struct with filter params
func (c *insightApiContext) TxConverterWithParams(txs []*dcrjson.TxRawResult, noAsm bool, noScriptSig bool, noSpent bool) ([]apitypes.InsightTx, error) {
newTxs := []apitypes.InsightTx{}
// DcrToInsightTxns takes struct with filter params
func (c *insightApiContext) DcrToInsightTxns(txs []*dcrjson.TxRawResult,
noAsm, noScriptSig, noSpent bool) ([]apitypes.InsightTx, error) {
var newTxs []apitypes.InsightTx
for _, tx := range txs {

vInSum := float64(0)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var vInSum float64 inits the variable to the zero value for the data type

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, i'm still unclear when you want me to use := vs var inits. we are getting there :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well same here, we're not super strict, but we try to be internally consistent.

A couple resources for idiomatic go:
https://golang.org/doc/effective_go.html?
https://dmitri.shuralyov.com/idiomatic-go

BTW, this was one of my favorite pages when I was first learning go:
http://devs.cloudimmunity.com/gotchas-and-common-mistakes-in-go-golang/

vOutSum := float64(0)

// Build new model. Based on the old api responses of
txNew := apitypes.InsightTx{}
txNew.Txid = tx.Txid
txNew.Version = tx.Version
txNew.Locktime = tx.LockTime
// Build new InsightTx
txNew := apitypes.InsightTx{
Txid: tx.Txid,
Version: tx.Version,
Locktime: tx.LockTime,
Blockhash: tx.BlockHash,
Blockheight: tx.BlockHeight,
Confirmations: tx.Confirmations,
Time: tx.Time,
Blocktime: tx.Blocktime,
Size: uint32(len(tx.Hex) / 2),
}

// Vins fill
var vInSum, vOutSum float64

for vinID, vin := range tx.Vin {
vinEmpty := &apitypes.InsightVin{}
emptySS := &apitypes.InsightScriptSig{}
txNew.Vins = append(txNew.Vins, vinEmpty)
txNew.Vins[vinID].Txid = vin.Txid
txNew.Vins[vinID].Vout = vin.Vout
txNew.Vins[vinID].Sequence = vin.Sequence

txNew.Vins[vinID].CoinBase = vin.Coinbase
InsightVin := &apitypes.InsightVin{
Txid: vin.Txid,
Vout: vin.Vout,
Sequence: vin.Sequence,
N: vinID,
Value: vin.AmountIn,
CoinBase: vin.Coinbase,
}

// init ScriptPubKey
if !noScriptSig {
txNew.Vins[vinID].ScriptSig = emptySS
InsightVin.ScriptSig = new(apitypes.InsightScriptSig)
if vin.ScriptSig != nil {
if !noAsm {
txNew.Vins[vinID].ScriptSig.Asm = vin.ScriptSig.Asm
InsightVin.ScriptSig.Asm = vin.ScriptSig.Asm
}
txNew.Vins[vinID].ScriptSig.Hex = vin.ScriptSig.Hex
InsightVin.ScriptSig.Hex = vin.ScriptSig.Hex
}
}

txNew.Vins[vinID].N = vinID

txNew.Vins[vinID].Value = vin.AmountIn

// Lookup addresses OPTION 2
// Note, this only gathers information from the database which does not include mempool transactions
_, addresses, value, err := c.BlockData.ChainDB.RetrieveAddressIDsByOutpoint(vin.Txid, vin.Vout)
Expand All @@ -65,42 +68,37 @@ func (c *insightApiContext) TxConverterWithParams(txs []*dcrjson.TxRawResult, no
// Update Vin due to DCRD AMOUNTIN - START
// NOTE THIS IS ONLY USEFUL FOR INPUT AMOUNTS THAT ARE NOT ALSO FROM MEMPOOL
if tx.Confirmations == 0 {
txNew.Vins[vinID].Value = dcrutil.Amount(value).ToCoin()
InsightVin.Value = dcrutil.Amount(value).ToCoin()
}
// Update Vin due to DCRD AMOUNTIN - END
txNew.Vins[vinID].Addr = addresses[0]
InsightVin.Addr = addresses[0]
}
}
dcramt, _ := dcrutil.NewAmount(txNew.Vins[vinID].Value)
txNew.Vins[vinID].ValueSat = int64(dcramt)
vInSum += txNew.Vins[vinID].Value
dcramt, _ := dcrutil.NewAmount(InsightVin.Value)
InsightVin.ValueSat = int64(dcramt)

vInSum += InsightVin.Value
txNew.Vins = append(txNew.Vins, InsightVin)

}

// Vout fill
for _, v := range tx.Vout {
voutEmpty := &apitypes.InsightVout{}
emptyPubKey := apitypes.InsightScriptPubKey{}
txNew.Vouts = append(txNew.Vouts, voutEmpty)
txNew.Vouts[v.N].Value = v.Value
vOutSum += v.Value
txNew.Vouts[v.N].N = v.N
// pk block
txNew.Vouts[v.N].ScriptPubKey = emptyPubKey
InsightVout := &apitypes.InsightVout{
Value: v.Value,
N: v.N,
ScriptPubKey: apitypes.InsightScriptPubKey{
Addresses: v.ScriptPubKey.Addresses,
Type: v.ScriptPubKey.Type,
},
}
if !noAsm {
txNew.Vouts[v.N].ScriptPubKey.Asm = v.ScriptPubKey.Asm
InsightVout.ScriptPubKey.Asm = v.ScriptPubKey.Asm
}
txNew.Vouts[v.N].ScriptPubKey.Hex = v.ScriptPubKey.Hex
txNew.Vouts[v.N].ScriptPubKey.Type = v.ScriptPubKey.Type
txNew.Vouts[v.N].ScriptPubKey.Addresses = v.ScriptPubKey.Addresses
}

txNew.Blockhash = tx.BlockHash
txNew.Blockheight = tx.BlockHeight
txNew.Confirmations = tx.Confirmations
txNew.Time = tx.Time
txNew.Blocktime = tx.Blocktime
txNew.Size = uint32(len(tx.Hex) / 2)
txNew.Vouts = append(txNew.Vouts, InsightVout)
vOutSum += v.Value
}

dcramt, _ := dcrutil.NewAmount(vOutSum)
txNew.ValueOut = dcramt.ToCoin()
Expand All @@ -112,7 +110,7 @@ func (c *insightApiContext) TxConverterWithParams(txs []*dcrjson.TxRawResult, no
txNew.Fees = dcramt.ToCoin()

// Return true if coinbase value is not empty, return 0 at some fields
if txNew.Vins != nil && len(txNew.Vins[0].CoinBase) > 0 {
if txNew.Vins != nil && txNew.Vins[0].CoinBase != "" {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

either way is valid, but for strings, the semantics are clear from the syntax on the right without even knowing the type of CoinBase

txNew.IsCoinBase = true
txNew.ValueIn = 0
txNew.Fees = 0
Expand All @@ -123,7 +121,6 @@ func (c *insightApiContext) TxConverterWithParams(txs []*dcrjson.TxRawResult, no
}

if !noSpent {

// set of unique addresses for db query
uniqAddrs := make(map[string]string)

Expand All @@ -133,10 +130,11 @@ func (c *insightApiContext) TxConverterWithParams(txs []*dcrjson.TxRawResult, no
}
}

addresses := []string{}
var addresses []string
for addr := range uniqAddrs {
addresses = append(addresses, addr)
}

// Note, this only gathers information from the database which does not include mempool transactions
addrFull := c.BlockData.ChainDB.GetAddressSpendByFunHash(addresses, txNew.Txid)
for _, dbaddr := range addrFull {
Expand All @@ -150,19 +148,17 @@ func (c *insightApiContext) TxConverterWithParams(txs []*dcrjson.TxRawResult, no
return newTxs, nil
}

// BlockConverter converts dcrd-block to Insight block
func (c *insightApiContext) BlockConverter(inBlocks []*dcrjson.GetBlockVerboseResult) ([]*apitypes.InsightBlockResult, error) {
params := &chaincfg.MainNetParams
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure this was a bug. I was about to remove the receiver ((c *insightApiContext)) from the function signature since c wasn't used, but I was the actual chaincfg params were needed.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are right.


// DcrToInsightBlock converts a dcrjson.GetBlockVerboseResult to Insight block.
func (c *insightApiContext) DcrToInsightBlock(inBlocks []*dcrjson.GetBlockVerboseResult) ([]*apitypes.InsightBlockResult, error) {
RewardAtBlock := func(blocknum int64, voters uint16) float64 {
subsidyCache := blockchain.NewSubsidyCache(0, params)
work := blockchain.CalcBlockWorkSubsidy(subsidyCache, blocknum, voters, params)
stake := blockchain.CalcStakeVoteSubsidy(subsidyCache, blocknum, params) * int64(voters)
tax := blockchain.CalcBlockTaxSubsidy(subsidyCache, blocknum, voters, params)
subsidyCache := blockchain.NewSubsidyCache(0, c.params)
work := blockchain.CalcBlockWorkSubsidy(subsidyCache, blocknum, voters, c.params)
stake := blockchain.CalcStakeVoteSubsidy(subsidyCache, blocknum, c.params) * int64(voters)
tax := blockchain.CalcBlockTaxSubsidy(subsidyCache, blocknum, voters, c.params)
return dcrutil.Amount(work + stake + tax).ToCoin()
}

outBlocks := make([]*apitypes.InsightBlockResult, 0)
outBlocks := make([]*apitypes.InsightBlockResult, 0, len(inBlocks))
for _, inBlock := range inBlocks {
outBlock := apitypes.InsightBlockResult{
Hash: inBlock.Hash,
Expand All @@ -179,14 +175,9 @@ func (c *insightApiContext) BlockConverter(inBlocks []*dcrjson.GetBlockVerboseRe
PreviousHash: inBlock.PreviousHash,
NextHash: inBlock.NextHash,
Reward: RewardAtBlock(inBlock.Height, inBlock.Voters),
}
if inBlock.Height > 0 {
outBlock.IsMainChain = true
} else {
outBlock.IsMainChain = false
IsMainChain: inBlock.Height > 0,
}
outBlocks = append(outBlocks, &outBlock)

}
return outBlocks, nil
}