From 58bc38a55e2636fb6ab55893cfed5d2ffe502ad4 Mon Sep 17 00:00:00 2001 From: Harry Kalodner Date: Thu, 3 May 2018 14:40:42 -0400 Subject: [PATCH] BlockSci interface cleanups --- include/blocksci/chain/algorithms.hpp | 13 +------ include/blocksci/chain/transaction.hpp | 15 ++++++++ include/blocksci/index/address_index.hpp | 2 +- include/blocksci/scripts/multisig_script.hpp | 2 ++ .../blocksci/scripts/pubkey_base_script.hpp | 2 +- src/index/address_index.cpp | 36 +++++++++---------- src/scripts/pubkey_base_script.cpp | 2 +- 7 files changed, 39 insertions(+), 33 deletions(-) diff --git a/include/blocksci/chain/algorithms.hpp b/include/blocksci/chain/algorithms.hpp index 47ebdb7f..cc037972 100644 --- a/include/blocksci/chain/algorithms.hpp +++ b/include/blocksci/chain/algorithms.hpp @@ -210,18 +210,7 @@ namespace blocksci { } inline int64_t BLOCKSCI_EXPORT fee(const Transaction &tx) { - if (tx.isCoinbase()) { - return 0; - } else { - int64_t total = 0; - for (auto &input : tx.rawInputs()) { - total += input.getValue(); - } - for (auto &output : tx.rawOutputs()) { - total -= output.getValue(); - } - return total; - } + return tx.fee(); } template diff --git a/include/blocksci/chain/transaction.hpp b/include/blocksci/chain/transaction.hpp index 61b88c54..125f926d 100644 --- a/include/blocksci/chain/transaction.hpp +++ b/include/blocksci/chain/transaction.hpp @@ -94,6 +94,21 @@ namespace blocksci { return virtualSize(); } + int64_t fee() const { + if (isCoinbase()) { + return 0; + } else { + int64_t total = 0; + for (auto &input : rawInputs()) { + total += input.getValue(); + } + for (auto &output : rawOutputs()) { + total -= output.getValue(); + } + return total; + } + } + uint32_t locktime() const { return data->locktime; } diff --git a/include/blocksci/index/address_index.hpp b/include/blocksci/index/address_index.hpp index 742982a9..51323135 100644 --- a/include/blocksci/index/address_index.hpp +++ b/include/blocksci/index/address_index.hpp @@ -46,7 +46,7 @@ namespace blocksci { ranges::any_view getOutputPointers(const Address &address) const; std::vector
getPossibleNestedEquivalent(const Address &address) const; - std::vector
getIncludingMultisigs(const Address &searchAddress) const; + ranges::any_view
getIncludingMultisigs(const Address &searchAddress) const; void addNestedAddresses(std::vector> nestedCache); void addOutputAddresses(std::vector> outputCache); diff --git a/include/blocksci/scripts/multisig_script.hpp b/include/blocksci/scripts/multisig_script.hpp index d9186e8e..e4e475f4 100644 --- a/include/blocksci/scripts/multisig_script.hpp +++ b/include/blocksci/scripts/multisig_script.hpp @@ -11,12 +11,14 @@ #include "script.hpp" #include "script_access.hpp" +#include "multisig_pubkey_script.hpp" #include #include #include #include +#include namespace blocksci { template <> diff --git a/include/blocksci/scripts/pubkey_base_script.hpp b/include/blocksci/scripts/pubkey_base_script.hpp index 09400793..d739c0c5 100644 --- a/include/blocksci/scripts/pubkey_base_script.hpp +++ b/include/blocksci/scripts/pubkey_base_script.hpp @@ -35,7 +35,7 @@ namespace blocksci { } } - std::vector
getIncludingMultisigs() const; + ranges::any_view
getIncludingMultisigs() const; }; } diff --git a/src/index/address_index.cpp b/src/index/address_index.cpp index 56d2a799..8074fdb3 100644 --- a/src/index/address_index.cpp +++ b/src/index/address_index.cpp @@ -42,6 +42,24 @@ namespace blocksci { }); } + ranges::any_view
AddressIndex::getIncludingMultisigs(const Address &searchAddress) const { + if (dedupType(searchAddress.type) != DedupAddressType::PUBKEY) { + return {}; + } + + auto prefixData = reinterpret_cast(&searchAddress.scriptNum); + std::vector prefix(prefixData, prefixData + sizeof(searchAddress.scriptNum)); + auto rawDedupAddressRange = ColumnIterator(impl->db.get(), impl->getNestedColumn(AddressType::MULTISIG_PUBKEY).get(), prefix); + auto access = &searchAddress.getAccess(); + return rawDedupAddressRange | ranges::view::transform([access](std::pair pair) -> Address { + auto &key = pair.first; + key.data += sizeof(uint32_t); + DedupAddress rawParent; + memcpy(&rawParent, key.data, sizeof(rawParent)); + return Address{rawParent.scriptNum, AddressType::MULTISIG, *access}; + }); + } + void AddressIndex::compactDB() { impl->compactDB(); } @@ -127,24 +145,6 @@ namespace blocksci { return false; } - std::vector
AddressIndex::getIncludingMultisigs(const Address &searchAddress) const { - if (dedupType(searchAddress.type) != DedupAddressType::PUBKEY) { - return {}; - } - - rocksdb::Slice key{reinterpret_cast(&searchAddress.scriptNum), sizeof(searchAddress.scriptNum)}; - std::vector
addresses; - auto it = impl->getNestedIterator(AddressType::MULTISIG_PUBKEY); - for (it->Seek(key); it->Valid() && it->key().starts_with(key); it->Next()) { - auto foundKey = it->key(); - foundKey.remove_prefix(sizeof(uint32_t)); - DedupAddress rawParent; - memcpy(&rawParent, foundKey.data(), sizeof(rawParent)); - addresses.emplace_back(rawParent.scriptNum, AddressType::MULTISIG, searchAddress.getAccess()); - } - return addresses; - } - std::unordered_set
AddressIndex::getPossibleNestedEquivalentDown(const Address &searchAddress) const { std::unordered_set
addressesToSearch{searchAddress}; std::unordered_set
searchedAddresses; diff --git a/src/scripts/pubkey_base_script.cpp b/src/scripts/pubkey_base_script.cpp index fc6c6dab..50285c3b 100644 --- a/src/scripts/pubkey_base_script.cpp +++ b/src/scripts/pubkey_base_script.cpp @@ -10,7 +10,7 @@ #include namespace blocksci { - std::vector
PubkeyAddressBase::getIncludingMultisigs() const { + ranges::any_view
PubkeyAddressBase::getIncludingMultisigs() const { return getAccess().getAddressIndex().getIncludingMultisigs(*this); } } // namespace blocksci