Skip to content

Commit

Permalink
Revert "Milestone #5"
Browse files Browse the repository at this point in the history
  • Loading branch information
monstrobishi authored Jul 16, 2020
1 parent dd8542b commit f8fac16
Show file tree
Hide file tree
Showing 117 changed files with 2,108 additions and 5,366 deletions.
24 changes: 5 additions & 19 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ DEFI_CORE_H = \
core_memusage.h \
cuckoocache.h \
flatfile.h \
flushablestorage.h \
fs.h \
httprpc.h \
httpserver.h \
Expand All @@ -152,18 +151,10 @@ DEFI_CORE_H = \
dbwrapper.h \
limitedmap.h \
logging.h \
masternodes/accounts.h \
masternodes/anchors.h \
masternodes/balances.h \
masternodes/criminals.h \
masternodes/masternodes.h \
masternodes/mn_checks.h \
masternodes/order.h \
masternodes/orders.h \
masternodes/res.h \
masternodes/tokens.h \
masternodes/undo.h \
masternodes/undos.h \
masternodes/mn_txdb.h \
memusage.h \
merkleblock.h \
miner.h \
Expand Down Expand Up @@ -272,7 +263,7 @@ obj/build.h: FORCE
"$(abs_top_srcdir)"
libdefi_util_a-clientversion.$(OBJEXT): obj/build.h

libdefi_spv_a_CPPFLAGS = $(AM_CPPFLAGS) -Wpointer-arith -fpermissive -I./spv -I./spv/support -I./secp256k1 -I./secp256k1/src -Wno-format-extra-args -lm -lbsd
libdefi_spv_a_CPPFLAGS = $(AM_CPPFLAGS) -Wpointer-arith -fpermissive -I./spv -I./spv/support -I./secp256k1 -I./secp256k1/src -Wno-format-extra-args -lm -lbsd
libdefi_spv_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
libdefi_spv_a_SOURCES = \
spv/bcash/BRBCashAddr.cpp \
Expand Down Expand Up @@ -322,9 +313,9 @@ libdefi_spv_a_SOURCES = \
# spv/support/BRAssert.cpp
# spv/support/BRAssert.h
# spv/support/BRFileService.cpp
# spv/support/BRFileService.h
# spv/support/BRFileService.h
# spv/bitcoin/BRWalletManager.cpp
# spv/bitcoin/BRWalletManager.h
# spv/bitcoin/BRWalletManager.h

# for gen /secp256k1/src/ecmult_static_context.h
LIBSECP256K1_GEN = $(srcdir)/secp256k1/src/ecmult_static_context.h
Expand Down Expand Up @@ -356,15 +347,11 @@ libdefi_server_a_SOURCES = \
interfaces/node.cpp \
init.cpp \
dbwrapper.cpp \
masternodes/accounts.cpp \
masternodes/anchors.cpp \
masternodes/criminals.cpp \
masternodes/masternodes.cpp \
masternodes/mn_checks.cpp \
masternodes/mn_txdb.cpp \
masternodes/mn_rpc.cpp \
masternodes/orders.cpp \
masternodes/tokens.cpp \
masternodes/undos.cpp \
miner.cpp \
net.cpp \
net_processing.cpp \
Expand Down Expand Up @@ -527,7 +514,6 @@ libdefi_consensus_a_SOURCES = \
script/script_error.cpp \
script/script_error.h \
serialize.h \
serialize_optional.h \
span.h \
tinyformat.h \
uint256.cpp \
Expand Down
1 change: 0 additions & 1 deletion src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ DEFI_TESTS =\
test/sighash_tests.cpp \
test/sigopcount_tests.cpp \
test/skiplist_tests.cpp \
test/storage_tests.cpp \
test/streams_tests.cpp \
test/sync_tests.cpp \
test/util_threadnames_tests.cpp \
Expand Down
126 changes: 0 additions & 126 deletions src/amount.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,138 +7,12 @@
#define DEFI_AMOUNT_H

#include <stdint.h>
#include <util/strencodings.h>
#include <serialize.h>
#include <masternodes/res.h>

#include <map>

/** Amount in satoshis (Can be negative) */
typedef int64_t CAmount;

// Defi Custom Token ID
struct DCT_ID {
uint32_t v;

std::string ToString() const {
//uint32_t v_be = htobe32(v);
//return HexStr(&v_be, &v_be + sizeof(v_be));
return std::to_string(v);
}

static ResVal<DCT_ID> FromString(std::string const & str) noexcept {
try {
return {DCT_ID{(uint32_t) std::stoul(str)}, Res::Ok()};
} catch (std::exception& e) {
return Res::Err("failed to deserialize DCT_ID: %s", e.what());
} catch (...) {
return Res::Err("failed to deserialize DCT_ID");
}
}

friend bool operator<(const DCT_ID& a, const DCT_ID& b)
{
return a.v < b.v;
}

friend bool operator>(const DCT_ID& a, const DCT_ID& b)
{
return a.v > b.v;
}

friend bool operator<=(const DCT_ID& a, const DCT_ID& b)
{
return a.v <= b.v;
}

friend bool operator>=(const DCT_ID& a, const DCT_ID& b)
{
return a.v >= b.v;
}

friend bool operator==(const DCT_ID& a, const DCT_ID& b)
{
return a.v == b.v;
}

friend bool operator!=(const DCT_ID& a, const DCT_ID& b)
{
return !(a == b);
}

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(v);
}
};

static const CAmount COIN = 100000000;

typedef std::map<DCT_ID, CAmount> TAmounts;

struct CTokenAmount { // simple std::pair is less informative
DCT_ID nTokenId;
CAmount nValue;

std::string ToString() const {
return std::to_string(nValue / COIN) + "." + std::to_string(nValue % COIN) + "@" + nTokenId.ToString();
}

Res Add(CAmount amount) {
// safety checks
if (amount < 0) {
return Sub(-amount);
}
// add
auto sumRes = SafeAdd(this->nValue, amount);
if (!sumRes.ok) {
return sumRes.res();
}
this->nValue = *sumRes.val;
return Res::Ok();
}
Res Sub(CAmount amount) {
// safety checks
if (amount < 0) {
return Add(-amount);
}
if (this->nValue < amount) {
return Res::Err("Amount %s is less than %s", this->nValue, CTokenAmount{nTokenId, amount}.ToString());
}
// sub
this->nValue -= amount;
return Res::Ok();
}

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(VARINT(nTokenId.v));
READWRITE(nValue);
}

private:
static ResVal<CAmount> SafeAdd(CAmount _a, CAmount _b) {
// check limits
if (_a < 0 || _b < 0) {
return Res::Err("negative amount");
}
// convert to unsigned, because signed overflow is UB
const uint64_t a = (uint64_t) _a;
const uint64_t b = (uint64_t) _b;

const uint64_t sum = a + b;
// check overflow
if ((sum - a) != b || ((uint64_t)std::numeric_limits<CAmount>::max()) < sum) {
return Res::Err("overflow");
}
return {(CAmount) sum, Res::Ok()};
}
};

/** No amount larger than this (in satoshi) is valid.
*
* Note that this constant is *not* the total money supply, which in Defi
Expand Down
4 changes: 2 additions & 2 deletions src/bench/wallet_balance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ static void WalletBalance(benchmark::State& state, const bool set_dirty, const b
while (state.KeepRunning()) {
if (set_dirty) wallet.MarkDirty();
bal = wallet.GetBalance();
if (add_mine) assert(bal.m_mine_trusted[DCT_ID{0}] > 0); // tokens `0`
if (add_watchonly) assert(bal.m_watchonly_trusted[DCT_ID{0}] > 0); // tokens `0`
if (add_mine) assert(bal.m_mine_trusted > 0);
if (add_watchonly) assert(bal.m_watchonly_trusted > 0);
}
}

Expand Down
Loading

0 comments on commit f8fac16

Please sign in to comment.