Skip to content

Commit

Permalink
BlockSci interface cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
hkalodner committed May 3, 2018
1 parent 1b97e97 commit 58bc38a
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 33 deletions.
13 changes: 1 addition & 12 deletions include/blocksci/chain/algorithms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T>
Expand Down
15 changes: 15 additions & 0 deletions include/blocksci/chain/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion include/blocksci/index/address_index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace blocksci {
ranges::any_view<OutputPointer> getOutputPointers(const Address &address) const;

std::vector<Address> getPossibleNestedEquivalent(const Address &address) const;
std::vector<Address> getIncludingMultisigs(const Address &searchAddress) const;
ranges::any_view<Address> getIncludingMultisigs(const Address &searchAddress) const;

void addNestedAddresses(std::vector<std::pair<blocksci::RawAddress, blocksci::DedupAddress>> nestedCache);
void addOutputAddresses(std::vector<std::pair<blocksci::RawAddress, blocksci::OutputPointer>> outputCache);
Expand Down
2 changes: 2 additions & 0 deletions include/blocksci/scripts/multisig_script.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

#include "script.hpp"
#include "script_access.hpp"
#include "multisig_pubkey_script.hpp"

#include <blocksci/blocksci_export.h>
#include <blocksci/core/address_info.hpp>
#include <blocksci/util/data_access.hpp>

#include <sstream>
#include <vector>

namespace blocksci {
template <>
Expand Down
2 changes: 1 addition & 1 deletion include/blocksci/scripts/pubkey_base_script.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace blocksci {
}
}

std::vector<Address> getIncludingMultisigs() const;
ranges::any_view<Address> getIncludingMultisigs() const;
};
}

Expand Down
36 changes: 18 additions & 18 deletions src/index/address_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ namespace blocksci {
});
}

ranges::any_view<Address> AddressIndex::getIncludingMultisigs(const Address &searchAddress) const {
if (dedupType(searchAddress.type) != DedupAddressType::PUBKEY) {
return {};
}

auto prefixData = reinterpret_cast<const char *>(&searchAddress.scriptNum);
std::vector<char> 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<MemoryView, MemoryView> 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();
}
Expand Down Expand Up @@ -127,24 +145,6 @@ namespace blocksci {
return false;
}

std::vector<Address> AddressIndex::getIncludingMultisigs(const Address &searchAddress) const {
if (dedupType(searchAddress.type) != DedupAddressType::PUBKEY) {
return {};
}

rocksdb::Slice key{reinterpret_cast<const char *>(&searchAddress.scriptNum), sizeof(searchAddress.scriptNum)};
std::vector<Address> 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<Address> AddressIndex::getPossibleNestedEquivalentDown(const Address &searchAddress) const {
std::unordered_set<Address> addressesToSearch{searchAddress};
std::unordered_set<Address> searchedAddresses;
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/pubkey_base_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <blocksci/util/data_access.hpp>

namespace blocksci {
std::vector<Address> PubkeyAddressBase::getIncludingMultisigs() const {
ranges::any_view<Address> PubkeyAddressBase::getIncludingMultisigs() const {
return getAccess().getAddressIndex().getIncludingMultisigs(*this);
}
} // namespace blocksci

0 comments on commit 58bc38a

Please sign in to comment.