From f0be987b2a3d5ef9b6072a6217e28ba7c36d0cac Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Mon, 15 Nov 2021 13:06:36 -0600 Subject: [PATCH] indexers: Convert to use stdscript. This converts the blockchain/indexers package to use the stdscript package instead of txscript. This is part of a series of commits to convert all packages in the repository to make use of the new stdscript package. --- blockchain/indexers/addrindex.go | 21 ++--- blockchain/indexers/addrindex_test.go | 9 +- blockchain/indexers/existsaddrindex.go | 96 +++++++++------------ blockchain/indexers/existsaddrindex_test.go | 14 +-- 4 files changed, 56 insertions(+), 84 deletions(-) diff --git a/blockchain/indexers/addrindex.go b/blockchain/indexers/addrindex.go index 92d11aae..3faea602 100644 --- a/blockchain/indexers/addrindex.go +++ b/blockchain/indexers/addrindex.go @@ -16,8 +16,8 @@ import ( "github.com/decred/dcrd/chaincfg/v3" "github.com/decred/dcrd/database/v3" "github.com/decred/dcrd/dcrutil/v4" - "github.com/decred/dcrd/txscript/v4" "github.com/decred/dcrd/txscript/v4/stdaddr" + "github.com/decred/dcrd/txscript/v4/stdscript" "github.com/decred/dcrd/wire" ) @@ -748,14 +748,14 @@ type writeIndexData map[[addrKeySize]byte][]int func (idx *AddrIndex) indexPkScript(data writeIndexData, scriptVersion uint16, pkScript []byte, txIdx int, isSStx bool, isTreasuryEnabled bool) { // Nothing to index if the script is non-standard or otherwise doesn't // contain any addresses. - class, addrs, _, err := txscript.ExtractPkScriptAddrs(scriptVersion, - pkScript, idx.chainParams, isTreasuryEnabled) - if err != nil { + params := idx.chainParams + scriptType, addrs := stdscript.ExtractAddrs(scriptVersion, pkScript, params) + if scriptType == stdscript.STNonStandard { return } - if isSStx && class == txscript.NullDataTy { - addr, err := stake.AddrFromSStxPkScrCommitment(pkScript, idx.chainParams) + if isSStx && scriptType == stdscript.STNullData { + addr, err := stake.AddrFromSStxPkScrCommitment(pkScript, params) if err != nil { return } @@ -993,13 +993,10 @@ func (idx *AddrIndex) EntriesForAddress(dbTx database.Tx, addr stdaddr.Address, // // This function is safe for concurrent access. func (idx *AddrIndex) indexUnconfirmedAddresses(scriptVersion uint16, pkScript []byte, tx *dcrutil.Tx, isSStx bool, isTreasuryEnabled bool) { - // The error is ignored here since the only reason it can fail is if the - // script fails to parse and it was already validated before being - // admitted to the mempool. - class, addrs, _, _ := txscript.ExtractPkScriptAddrs(scriptVersion, pkScript, - idx.chainParams, isTreasuryEnabled) + params := idx.chainParams + scriptType, addrs := stdscript.ExtractAddrs(scriptVersion, pkScript, params) - if isSStx && class == txscript.NullDataTy { + if isSStx && scriptType == stdscript.STNullData { addr, err := stake.AddrFromSStxPkScrCommitment(pkScript, idx.chainParams) if err != nil { // Fail if this fails to decode. It should. diff --git a/blockchain/indexers/addrindex_test.go b/blockchain/indexers/addrindex_test.go index 33886b5c..56ce6eec 100644 --- a/blockchain/indexers/addrindex_test.go +++ b/blockchain/indexers/addrindex_test.go @@ -15,7 +15,7 @@ import ( "github.com/decred/dcrd/blockchain/v4/chaingen" "github.com/decred/dcrd/chaincfg/v3" "github.com/decred/dcrd/database/v3" - "github.com/decred/dcrd/txscript/v4" + "github.com/decred/dcrd/txscript/v4/stdscript" "github.com/decred/dcrd/wire" ) @@ -425,11 +425,8 @@ func TestAddrIndexAsync(t *testing.T) { // Fetch the first address paid to by bk5's coinbase. out := bk5.MsgBlock().Transactions[0].TxOut[0] - _, addrs, _, err := txscript.ExtractPkScriptAddrs(out.Version, - out.PkScript, addrIdx.chainParams, true) - if err != nil { - t.Fatal(err) - } + _, addrs := stdscript.ExtractAddrs(out.Version, out.PkScript, + addrIdx.chainParams) // Ensure the address index has the first address paid to by bk5's // coinbase indexed. diff --git a/blockchain/indexers/existsaddrindex.go b/blockchain/indexers/existsaddrindex.go index 7a0edeab..0590a719 100644 --- a/blockchain/indexers/existsaddrindex.go +++ b/blockchain/indexers/existsaddrindex.go @@ -14,8 +14,8 @@ import ( "github.com/decred/dcrd/chaincfg/v3" "github.com/decred/dcrd/database/v3" "github.com/decred/dcrd/dcrutil/v4" - "github.com/decred/dcrd/txscript/v4" "github.com/decred/dcrd/txscript/v4/stdaddr" + "github.com/decred/dcrd/txscript/v4/stdscript" "github.com/decred/dcrd/wire" ) @@ -330,45 +330,37 @@ func (idx *ExistsAddrIndex) connectBlock(dbTx database.Tx, block, parent *dcruti isSStx := stake.IsSStx(msgTx) for _, txIn := range msgTx.TxIn { // Note that the functions used here require v0 scripts. Hence it - // is used for the script version. This will ultimately need to + // is used for the script version. This will ultimately need to be // updated to support new script versions. - const scriptVersion = 0 - if txscript.IsMultisigSigScript(txIn.SignatureScript) { - rs := txscript.MultisigRedeemScriptFromScriptSig( - txIn.SignatureScript) - class, addrs, _, err := txscript.ExtractPkScriptAddrs( - scriptVersion, rs, idx.chain.ChainParams(), - isTreasuryEnabled) + if !stdscript.IsMultiSigSigScriptV0(txIn.SignatureScript) { + continue + } + rs := stdscript.MultiSigRedeemScriptFromScriptSigV0(txIn.SignatureScript) + typ, addrs := stdscript.ExtractAddrsV0(rs, idx.chain.ChainParams()) + if typ != stdscript.STMultiSig { + // This should never happen, but be paranoid. + continue + } + + for _, addr := range addrs { + k, err := addrToKey(addr) if err != nil { - // Non-standard outputs are skipped. - continue - } - if class != txscript.MultiSigTy { - // This should never happen, but be paranoid. continue } - for _, addr := range addrs { - k, err := addrToKey(addr) - if err != nil { - continue - } - - usedAddrs[k] = struct{}{} - } + usedAddrs[k] = struct{}{} } } for _, txOut := range tx.MsgTx().TxOut { - class, addrs, _, err := txscript.ExtractPkScriptAddrs( - txOut.Version, txOut.PkScript, idx.chain.ChainParams(), - isTreasuryEnabled) - if err != nil { + scriptType, addrs := stdscript.ExtractAddrs(txOut.Version, + txOut.PkScript, idx.chain.ChainParams()) + if scriptType == stdscript.STNonStandard { // Non-standard outputs are skipped. continue } - if isSStx && class == txscript.NullDataTy { + if isSStx && scriptType == stdscript.STNullData { addr, err := stake.AddrFromSStxPkScrCommitment(txOut.PkScript, idx.chain.ChainParams()) if err != nil { @@ -453,47 +445,41 @@ func (idx *ExistsAddrIndex) disconnectBlock(dbTx database.Tx, block, parent *dcr func (idx *ExistsAddrIndex) addUnconfirmedTx(tx *wire.MsgTx, isTreasuryEnabled bool) { isSStx := stake.IsSStx(tx) for _, txIn := range tx.TxIn { - if txscript.IsMultisigSigScript(txIn.SignatureScript) { - // Note that the functions used here require v0 scripts. Hence it - // is used for the script version. This will ultimately need to - // updated to support new script versions. - const scriptVersion = 0 - rs := txscript.MultisigRedeemScriptFromScriptSig( - txIn.SignatureScript) - class, addrs, _, err := txscript.ExtractPkScriptAddrs( - scriptVersion, rs, idx.chain.ChainParams(), - isTreasuryEnabled) + // Note that the functions used here require v0 scripts. Hence it is + // used for the script version. This will ultimately need to be updated + // to support new script versions. + if !stdscript.IsMultiSigSigScriptV0(txIn.SignatureScript) { + continue + } + + rs := stdscript.MultiSigRedeemScriptFromScriptSigV0(txIn.SignatureScript) + scriptType, addrs := stdscript.ExtractAddrsV0(rs, idx.chain.ChainParams()) + if scriptType != stdscript.STMultiSig { + // This should never happen, but be paranoid. + continue + } + + for _, addr := range addrs { + k, err := addrToKey(addr) if err != nil { - // Non-standard outputs are skipped. - continue - } - if class != txscript.MultiSigTy { - // This should never happen, but be paranoid. continue } - for _, addr := range addrs { - k, err := addrToKey(addr) - if err != nil { - continue - } - - if _, exists := idx.mpExistsAddr[k]; !exists { - idx.mpExistsAddr[k] = struct{}{} - } + if _, exists := idx.mpExistsAddr[k]; !exists { + idx.mpExistsAddr[k] = struct{}{} } } } for _, txOut := range tx.TxOut { - class, addrs, _, err := txscript.ExtractPkScriptAddrs(txOut.Version, - txOut.PkScript, idx.chain.ChainParams(), isTreasuryEnabled) - if err != nil { + scriptType, addrs := stdscript.ExtractAddrs(txOut.Version, + txOut.PkScript, idx.chain.ChainParams()) + if scriptType == stdscript.STNonStandard { // Non-standard outputs are skipped. continue } - if isSStx && class == txscript.NullDataTy { + if isSStx && scriptType == stdscript.STNullData { addr, err := stake.AddrFromSStxPkScrCommitment(txOut.PkScript, idx.chain.ChainParams()) if err != nil { diff --git a/blockchain/indexers/existsaddrindex_test.go b/blockchain/indexers/existsaddrindex_test.go index f811637c..50a57b60 100644 --- a/blockchain/indexers/existsaddrindex_test.go +++ b/blockchain/indexers/existsaddrindex_test.go @@ -7,7 +7,7 @@ import ( "github.com/decred/dcrd/blockchain/v4/chaingen" "github.com/decred/dcrd/chaincfg/v3" - "github.com/decred/dcrd/txscript/v4" + "github.com/decred/dcrd/txscript/v4/stdscript" ) // TestExistsAddrIndexAsync ensures the exist address index @@ -100,18 +100,10 @@ func TestExistsAddrIndexAsync(t *testing.T) { bk5.Hash().String(), tipHash.String()) } - isTreasuryEnabled, err := idx.chain.IsTreasuryAgendaActive(bk5.Hash()) - if err != nil { - t.Fatal(err) - } - // Fetch the first address paid to by bk5's coinbase. out := bk5.MsgBlock().Transactions[0].TxOut[0] - _, addrs, _, err := txscript.ExtractPkScriptAddrs(out.Version, out.PkScript, - idx.chain.ChainParams(), isTreasuryEnabled) - if err != nil { - t.Fatal(err) - } + _, addrs := stdscript.ExtractAddrs(out.Version, out.PkScript, + idx.chain.ChainParams()) // Ensure index has the first address paid to by bk5's coinbase indexed. indexed, err := idx.ExistsAddress(addrs[0])