-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 :) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: BTW, this was one of my favorite pages when I was first learning go: |
||
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) | ||
|
@@ -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() | ||
|
@@ -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 != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
txNew.IsCoinBase = true | ||
txNew.ValueIn = 0 | ||
txNew.Fees = 0 | ||
|
@@ -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) | ||
|
||
|
@@ -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 { | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ( There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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 | ||
} |
There was a problem hiding this comment.
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