optimize block parser

pull/1/head
Jan Pochyla 2017-09-06 10:59:58 +02:00
parent d2ebc62d12
commit 1e83636b57
1 changed files with 13 additions and 8 deletions

View File

@ -22,13 +22,16 @@ type BitcoinBlockParser struct {
Params *chaincfg.Params Params *chaincfg.Params
} }
func (p *BitcoinBlockParser) parseOutputScript(b []byte) (addrs []string, err error) { func (p *BitcoinBlockParser) parseOutputScript(b []byte) ([]string, error) {
_, addresses, _, err := txscript.ExtractPkScriptAddrs(b, p.Params) _, addresses, _, err := txscript.ExtractPkScriptAddrs(b, p.Params)
for _, a := range addresses { if err != nil {
addr := a.EncodeAddress() return nil, err
addrs = append(addrs, addr)
} }
return addrs, err addrs := make([]string, len(addresses))
for i, a := range addresses {
addrs[i] = a.EncodeAddress()
}
return addrs, nil
} }
func (p *BitcoinBlockParser) ParseBlock(b []byte) (*Block, error) { func (p *BitcoinBlockParser) ParseBlock(b []byte) (*Block, error) {
@ -38,8 +41,10 @@ func (p *BitcoinBlockParser) ParseBlock(b []byte) (*Block, error) {
return nil, err return nil, err
} }
block := &Block{} block := &Block{
for _, t := range w.Transactions { Txs: make([]*Tx, len(w.Transactions)),
}
for ti, t := range w.Transactions {
tx := &Tx{ tx := &Tx{
Txid: t.TxHash().String(), Txid: t.TxHash().String(),
Version: t.Version, Version: t.Version,
@ -81,7 +86,7 @@ func (p *BitcoinBlockParser) ParseBlock(b []byte) (*Block, error) {
ScriptPubKey: s, ScriptPubKey: s,
} }
} }
block.Txs = append(block.Txs, tx) block.Txs[ti] = tx
} }
return block, nil return block, nil