peerplays_migrated/libraries/plugins/peerplays_sidechain/bitcoin/bitcoin_transaction.cpp
Nathan Hourt 4d836dacb9 Ref !3/#376: Graphene Updates
This adds the most important updates to Graphene from BitShares. Most notably,
https://github.com/bitshares/bitshares-core/issues/1506

Second most notably, it updates Peerplays' FC to be in sync with BitShares FC.

This is a squash commit of several subcommits. The subcommit messages are
reproduced below:

Replace fc::uint128 with boost::multiprecision::uint128_t

replace smart_ref with shared_ptr

Fixes/Remove Unused

Remove NTP time

Remove old macro

This macro is now in FC, so no need to define it here anymore

Replaced fc::array with std::array

Separate exception declaration and implementation

Adapted to fc promise changes

Fixes

Add back in some of Peter's fixes that got lost in the cherry pick

_hash endianness fixes

Remove all uses of fc/smart_ref

It's gone, can't use it anymore

Replace improper static_variant operator overloads with comparators

Fixes

Remove boost::signals from build system; it's header-only so it's not
listed in cmake anymore.

Also remove some unused hashing code

Impl. pack/unpack functions for extension class

Ref #1506: Isolate chain/protocol to its own library

Ref #1506: Add object_downcast_t

Allows the more concise expression `object_downcast_t<xyz>` instead of
the old `typename object_downcast<xyz>::type`

Ref #1506: Move ID types from db to protocol

The ID types, object_id and object_id_type, were defined in the db
library, and the protocol library depends on db to get these types.
Technically, the ID types are defined by the protocol and used by the
database, and not vice versa. Therefore these types should be in the
protocol library, and db should depend on protocol to get them.

This commit makes it so.

Ref #1506: Isolate chain/protocol to its own library

Remove commented-out index code

Wrap overlength line

Remove unused key types

Probably fix Docker build

Fix build after rebase

Ref #1506/#1737: Some requested changes

Ref #1506/#1737: Macro-fy ID type definitions

Define macros to fully de-boilerplate ID type definitions.

Externalities:
 - Rename transaction_object -> transaction_history_object
 - Rename impl_asset_dynamic_data_type ->
impl_asset_dynamic_data_object_type
 - Rename impl_asset_bitasset_data_type ->
impl_asset_bitasset_data_object_type

The first is to avoid a naming collision on transaction_id_type, and the
other two are to maintain consistency with the naming of the other
types.

Ref #1506/#1737: Fix clean_name()

Ref #1506/#1737: Oops

Fix .gitignore

Externalized serialization in protocol library

Fix compile sets

Delete a couple of ghost files that were in the tree but not part
of the project (I accidentally added them to CMakeLists while
merging, but they're broken and not part of the Peerplays code), and
add several files that got dropped from the build during merge.

General fixes

Fix warnings, build issues, unused code, etc.

Fix #1772 by decprecating cli_wallet -H

More fixes

Fix errors and warnings and generally coax it to build

Fix test

I'm pretty sure this didn't break from what I did... But I can't build
the original code, so I can't tell. Anyways, this one now passes...
Others still fail...

Small fix

Fix crash in auth checks

Final fixes

Last round of fixes following the rebase to Beatrice

Rename project in CMakeLists.txt

The CMakeLists.txt declared this project as BitShares and not Peerplays,
which makes it confusing in IDEs. Rename it to be clear which project is
open.

Resolve #374

Replace all object refs in macros with IDs, and fix affected tests to look
up objects by ID rather than using invalidated refs.

A full audit of all tests should be performed to eliminate any further
usage of invalidated object references.

Resolve #373: Add object notifiers

Various fixes

Fixes to various issues, primarily reflections, that cropped up
during merge conflict resolution

Fix startup bug in Bookie plugin

Bookie plugin was preventing the node from starting up because it
registered its secondary indexes to create objects in its own primary
indexes to track objects being created in other primary indexes, and did
so during its `initialize()` step, which is to say, before the database
was loaded from disk at startup. This caused the secondary indexes to
create tracker objects when the observed indexes were loading objects
from disk. This then caused a failure when these tracker indexes were
later loaded from disk, and the first object IDs collided.

This is fixed by refraining from defining secondary indexes until the
`startup()` stage rather than the `initialize()` stage. Primary indexes
are registered in `initialize()`, secondary indexes are registered in
`startup()`.

This also involved adding a new method, "add_secondary_index()", to
`object_database`, as before there was no way to do this because you
couldn't get a non-const index from a non-const database.

I have no idea how this was working before I got here...

Fix egenesis install

Fixes after updates

Rebase on updated develop branch and fix conflicts
2021-11-11 11:25:47 -05:00

258 lines
8.2 KiB
C++

#include <fc/crypto/base58.hpp>
#include <fc/crypto/sha256.hpp>
#include <graphene/peerplays_sidechain/bitcoin/bitcoin_script.hpp>
#include <graphene/peerplays_sidechain/bitcoin/bitcoin_transaction.hpp>
#include <graphene/peerplays_sidechain/bitcoin/serialize.hpp>
namespace graphene { namespace peerplays_sidechain { namespace bitcoin {
bool out_point::operator==(const out_point &op) const {
if (this->hash == op.hash &&
this->n == op.n) {
return true;
}
return false;
}
bool tx_in::operator==(const tx_in &ti) const {
if (this->prevout == ti.prevout &&
this->scriptSig == ti.scriptSig &&
this->nSequence == ti.nSequence) {
return true;
}
return false;
}
bool tx_out::operator==(const tx_out &to) const {
if (this->value == to.value &&
this->scriptPubKey == to.scriptPubKey) {
return true;
}
return false;
}
bool tx_out::is_p2wsh() const {
if (scriptPubKey.size() == 34 && scriptPubKey[0] == static_cast<char>(0x00) && scriptPubKey[1] == static_cast<char>(0x20)) {
return true;
}
return false;
}
bool tx_out::is_p2wpkh() const {
if (scriptPubKey.size() == 22 && scriptPubKey[0] == static_cast<char>(0x00) && scriptPubKey[1] == static_cast<char>(0x14)) {
return true;
}
return false;
}
bool tx_out::is_p2pkh() const {
if (scriptPubKey.size() == 25 && scriptPubKey[0] == static_cast<char>(0x76) && scriptPubKey[1] == static_cast<char>(0xa9) &&
scriptPubKey[2] == static_cast<char>(0x14) && scriptPubKey[23] == static_cast<char>(0x88) && scriptPubKey[24] == static_cast<char>(0xac)) {
return true;
}
return false;
}
bool tx_out::is_p2sh() const {
if (scriptPubKey.size() == 23 && scriptPubKey[0] == static_cast<char>(0xa9) &&
scriptPubKey[1] == static_cast<char>(0x14) && scriptPubKey[22] == static_cast<char>(0x87)) {
return true;
}
return false;
}
bool tx_out::is_p2pk() const {
if (scriptPubKey.size() == 35 && scriptPubKey[0] == static_cast<char>(0x21) && scriptPubKey[34] == static_cast<char>(0xac)) {
return true;
}
return false;
}
bytes tx_out::get_data_or_script() const {
if (is_p2pkh()) {
return bytes(scriptPubKey.begin() + 3, scriptPubKey.begin() + 23);
} else if (is_p2sh() || is_p2wpkh()) {
return bytes(scriptPubKey.begin() + 2, scriptPubKey.begin() + 22);
} else if (is_p2wsh()) {
return bytes(scriptPubKey.begin() + 2, scriptPubKey.begin() + 34);
} else if (is_p2pk()) {
return bytes(scriptPubKey.begin() + 1, scriptPubKey.begin() + 34);
}
return scriptPubKey;
}
bool bitcoin_transaction::operator!=(const bitcoin_transaction &bt) const {
if (this->nVersion != bt.nVersion ||
this->vin != bt.vin ||
this->vout != bt.vout ||
this->nLockTime != bt.nLockTime) {
return true;
}
return false;
}
fc::sha256 bitcoin_transaction::get_hash() const {
const auto bytes = pack(*this, true);
const auto hash = fc::sha256::hash(fc::sha256::hash(bytes.data(), bytes.size()));
std::reverse(hash.data(), hash.data() + hash.data_size());
return hash;
}
fc::sha256 bitcoin_transaction::get_txid() const {
const auto bytes = pack(*this, false);
const auto hash = fc::sha256::hash(fc::sha256::hash(bytes.data(), bytes.size()));
std::reverse(hash.data(), hash.data() + hash.data_size());
return hash;
}
size_t bitcoin_transaction::get_vsize() const {
static const auto witness_scale_factor = 4;
fc::datastream<size_t> no_wit_ds;
pack(no_wit_ds, *this, false);
fc::datastream<size_t> wit_ds;
pack(wit_ds, *this, true);
const size_t weight = no_wit_ds.tellp() * (witness_scale_factor - 1) + wit_ds.tellp();
const size_t vsize = (weight + witness_scale_factor - 1) / witness_scale_factor;
return vsize;
}
void bitcoin_transaction_builder::set_version(int32_t version) {
tx.nVersion = version;
}
void bitcoin_transaction_builder::set_locktime(uint32_t lock_time) {
tx.nLockTime = lock_time;
}
void bitcoin_transaction_builder::add_in(const fc::sha256 &txid, uint32_t n_out, const bytes &script_code, bool front, uint32_t sequence) {
add_in(payment_type::P2WSH, txid, n_out, script_code, front, sequence);
}
void bitcoin_transaction_builder::add_in(payment_type type, const fc::sha256 &txid, uint32_t n_out, const bytes &script_code, bool front, uint32_t sequence) {
out_point prevout;
prevout.hash = txid;
prevout.n = n_out;
tx_in txin;
txin.prevout = prevout;
txin.nSequence = sequence;
add_in(type, txin, script_code, front);
}
void bitcoin_transaction_builder::add_in(payment_type type, tx_in txin, const bytes &script_code, bool front) {
switch (type) {
case payment_type::P2SH_WPKH:
case payment_type::P2SH_WSH:
FC_ASSERT(script_code != bytes());
txin.scriptSig = script_code;
break;
default: {
if (txin.prevout.hash == fc::sha256("0000000000000000000000000000000000000000000000000000000000000000")) { //coinbase
FC_ASSERT(script_code != bytes());
txin.scriptSig = script_code;
}
break;
}
}
if (front) {
tx.vin.insert(tx.vin.begin(), txin);
} else {
tx.vin.push_back(txin);
}
}
void bitcoin_transaction_builder::add_out(payment_type type, int64_t amount, const std::string &base58_address, bool front) {
// TODO: add checks
const auto address_bytes = fc::from_base58(base58_address);
add_out(type, amount, bytes(address_bytes.begin() + 1, address_bytes.begin() + 1 + 20), front);
}
void bitcoin_transaction_builder::add_out(payment_type type, int64_t amount, const fc::ecc::public_key_data &pubkey, bool front) {
FC_ASSERT(is_payment_to_pubkey(type));
if (type == payment_type::P2PK) {
const auto pubkey_bytes = bytes(pubkey.begin(), pubkey.begin() + pubkey.size());
add_out(type, amount, pubkey_bytes, front);
} else {
const auto hash256 = fc::sha256::hash((const char*)pubkey.begin(), pubkey.size());
const auto hash160 = fc::ripemd160::hash(hash256.data(), hash256.data_size());
add_out(type, amount, bytes(hash160.data(), hash160.data() + hash160.data_size()), front);
}
}
void bitcoin_transaction_builder::add_out(payment_type type, int64_t amount, const bytes &script_code, bool front) {
tx_out out;
out.value = amount;
out.scriptPubKey = get_script_pubkey(type, script_code);
if (front) {
tx.vout.insert(tx.vout.begin(), out);
} else {
tx.vout.push_back(out);
}
}
void bitcoin_transaction_builder::add_out_all_type(const uint64_t &amount, const bitcoin_address &address, bool front) {
switch (address.get_type()) {
case payment_type::P2PK: {
bytes raw_address(address.get_raw_address());
fc::ecc::public_key_data public_key;
std::copy(raw_address.begin(), raw_address.end(), public_key.begin());
add_out(address.get_type(), amount, public_key, front);
break;
}
case payment_type::P2PKH:
case payment_type::P2SH:
case payment_type::P2SH_WPKH:
case payment_type::P2SH_WSH: {
add_out(address.get_type(), amount, address.get_address(), front);
break;
}
case payment_type::P2WPKH:
case payment_type::P2WSH: {
add_out(address.get_type(), amount, address.get_raw_address(), front);
break;
}
default:
break;
}
}
inline bool bitcoin_transaction_builder::is_payment_to_pubkey(payment_type type) {
switch (type) {
case payment_type::P2PK:
case payment_type::P2PKH:
case payment_type::P2WPKH:
case payment_type::P2SH_WPKH:
return true;
default:
return false;
}
}
bytes bitcoin_transaction_builder::get_script_pubkey(payment_type type, const bytes &script_code) {
switch (type) {
case payment_type::NULLDATA:
return script_builder() << op::RETURN << script_code;
case payment_type::P2PK:
return script_builder() << script_code << op::CHECKSIG;
case payment_type::P2PKH:
return script_builder() << op::DUP << op::HASH160 << script_code << op::EQUALVERIFY << op::CHECKSIG;
case payment_type::P2SH:
case payment_type::P2SH_WPKH:
case payment_type::P2SH_WSH:
return script_builder() << op::HASH160 << script_code << op::EQUAL;
case payment_type::P2WPKH:
case payment_type::P2WSH:
return script_builder() << op::_0 << script_code;
default:
return script_code;
}
}
}}} // namespace graphene::peerplays_sidechain::bitcoin