Skip to content

Commit

Permalink
Change donation
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Juarez committed Oct 1, 2015
1 parent 6c35e24 commit 00915e6
Show file tree
Hide file tree
Showing 52 changed files with 932 additions and 389 deletions.
38 changes: 33 additions & 5 deletions include/IWallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const uint32_t WALLET_UNCONFIRMED_TRANSACTION_HEIGHT = std::numeric_limits<uint3
enum class WalletTransactionState : uint8_t {
SUCCEEDED = 0,
FAILED,
CANCELLED
CANCELLED,
CREATED
};

enum WalletEventType {
Expand Down Expand Up @@ -77,11 +78,37 @@ struct WalletTransaction {
bool isBase;
};

enum class WalletTransferType : uint8_t {
USUAL = 0,
DONATION
};

struct WalletOrder {
std::string address;
uint64_t amount;
};

struct WalletTransfer {
WalletTransferType type;
std::string address;
int64_t amount;
};

struct DonationSettings {
std::string address;
uint64_t threshold = 0;
};

struct TransactionParameters {
std::string sourceAddress;
std::vector<WalletOrder> destinations;
uint64_t fee = 0;
uint64_t mixIn = 0;
std::string extra;
uint64_t unlockTimestamp = 0;
DonationSettings donation;
};

class IWallet {
public:
virtual ~IWallet() {}
Expand Down Expand Up @@ -113,10 +140,11 @@ class IWallet {
virtual size_t getTransactionTransferCount(size_t transactionIndex) const = 0;
virtual WalletTransfer getTransactionTransfer(size_t transactionIndex, size_t transferIndex) const = 0;

virtual size_t transfer(const WalletTransfer& destination, uint64_t fee, uint64_t mixIn = 0, const std::string& extra = "", uint64_t unlockTimestamp = 0) = 0;
virtual size_t transfer(const std::vector<WalletTransfer>& destinations, uint64_t fee, uint64_t mixIn = 0, const std::string& extra = "", uint64_t unlockTimestamp = 0) = 0;
virtual size_t transfer(const std::string& sourceAddress, const WalletTransfer& destination, uint64_t fee, uint64_t mixIn = 0, const std::string& extra = "", uint64_t unlockTimestamp = 0) = 0;
virtual size_t transfer(const std::string& sourceAddress, const std::vector<WalletTransfer>& destinations, uint64_t fee, uint64_t mixIn = 0, const std::string& extra = "", uint64_t unlockTimestamp = 0) = 0;
virtual size_t transfer(const WalletOrder& destination, uint64_t fee, uint64_t mixIn = 0, const std::string& extra = "", uint64_t unlockTimestamp = 0) = 0;
virtual size_t transfer(const std::vector<WalletOrder>& destinations, uint64_t fee, uint64_t mixIn = 0, const std::string& extra = "", uint64_t unlockTimestamp = 0) = 0;
virtual size_t transfer(const std::string& sourceAddress, const WalletOrder& destination, uint64_t fee, uint64_t mixIn = 0, const std::string& extra = "", uint64_t unlockTimestamp = 0) = 0;
virtual size_t transfer(const std::string& sourceAddress, const std::vector<WalletOrder>& destinations, uint64_t fee, uint64_t mixIn = 0, const std::string& extra = "", uint64_t unlockTimestamp = 0) = 0;
virtual size_t transfer(const TransactionParameters& sendingTransaction) = 0;

virtual void start() = 0;
virtual void stop() = 0;
Expand Down
86 changes: 84 additions & 2 deletions src/BlockchainExplorer/BlockchainExplorer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,72 @@ class NodeRequest {
const std::function<void(const INode::Callback&)> requestFunc;
};

BlockchainExplorer::PoolUpdateGuard::PoolUpdateGuard() :
m_state(State::NONE) {
}

bool BlockchainExplorer::PoolUpdateGuard::beginUpdate() {
auto state = m_state.load();
for (;;) {
switch (state) {
case State::NONE:
return true;

case State::UPDATING:
if (m_state.compare_exchange_weak(state, State::UPDATE_REQUIRED)) {
return false;
}

case State::UPDATE_REQUIRED:
return false;

default:
assert(false);
return false;
}
}
}

bool BlockchainExplorer::PoolUpdateGuard::endUpdate() {
auto state = m_state.load();
for (;;) {
assert(state != State::NONE);

if (m_state.compare_exchange_weak(state, State::NONE)) {
return state == State::UPDATE_REQUIRED;
}
}
}

class ScopeExitHandler {
public:
ScopeExitHandler(std::function<void()>&& handler) :
m_handler(std::move(handler)),
m_cancelled(false) {
}

~ScopeExitHandler() {
if (!m_cancelled) {
m_handler();
}
}

void reset() {
m_cancelled = true;
}

private:
std::function<void()> m_handler;
bool m_cancelled;
};

BlockchainExplorer::BlockchainExplorer(INode& node, Logging::ILogger& logger) :
node(node),
logger(logger, "BlockchainExplorer"),
state(NOT_INITIALIZED),
synchronized(false),
observersCounter(0) {}
observersCounter(0) {
}

BlockchainExplorer::~BlockchainExplorer() {}

Expand Down Expand Up @@ -432,6 +492,12 @@ void BlockchainExplorer::poolChanged() {
return;
}

if (!poolUpdateGuard.beginUpdate()) {
return;
}

ScopeExitHandler poolUpdateEndGuard(std::bind(&BlockchainExplorer::poolUpdateEndHandler, this));

std::unique_lock<std::mutex> lock(mutex);

std::shared_ptr<std::vector<std::unique_ptr<ITransactionReader>>> rawNewTransactionsPtr = std::make_shared<std::vector<std::unique_ptr<ITransactionReader>>>();
Expand All @@ -454,8 +520,11 @@ void BlockchainExplorer::poolChanged() {
);
}
);

request.performAsync(asyncContextCounter,
[this, rawNewTransactionsPtr, removedTransactionsPtr, isBlockchainActualPtr](std::error_code ec) {
ScopeExitHandler poolUpdateEndGuard(std::bind(&BlockchainExplorer::poolUpdateEndHandler, this));

if (ec) {
logger(ERROR) << "Can't send poolChanged notification because can't get pool symmetric difference: " << ec.message();
return;
Expand Down Expand Up @@ -503,21 +572,34 @@ void BlockchainExplorer::poolChanged() {
std::placeholders::_1
)
);

request.performAsync(asyncContextCounter,
[this, newTransactionsHashesPtr, newTransactionsPtr, removedTransactionsHashesPtr](std::error_code ec) {
ScopeExitHandler poolUpdateEndGuard(std::bind(&BlockchainExplorer::poolUpdateEndHandler, this));

if (ec) {
logger(ERROR) << "Can't send poolChanged notification because can't get transactions: " << ec.message();
return;
}

if (!newTransactionsPtr->empty() || !removedTransactionsHashesPtr->empty()) {
observerManager.notify(&IBlockchainObserver::poolUpdated, *newTransactionsPtr, *removedTransactionsHashesPtr);
logger(DEBUGGING) << "poolUpdated notification was successfully sent.";
}
}
);

poolUpdateEndGuard.reset();
}
);


poolUpdateEndGuard.reset();
}

void BlockchainExplorer::poolUpdateEndHandler() {
if (poolUpdateGuard.endUpdate()) {
poolChanged();
}
}

void BlockchainExplorer::blockchainSynchronized(uint32_t topHeight) {
Expand Down
21 changes: 20 additions & 1 deletion src/BlockchainExplorer/BlockchainExplorer.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,25 @@ class BlockchainExplorer : public IBlockchainExplorer, public INodeObserver {
typedef WalletAsyncContextCounter AsyncContextCounter;

private:
void poolUpdateEndHandler();

class PoolUpdateGuard {
public:
PoolUpdateGuard();

bool beginUpdate();
bool endUpdate();

private:
enum class State {
NONE,
UPDATING,
UPDATE_REQUIRED
};

std::atomic<State> m_state;
};

enum State {
NOT_INITIALIZED,
INITIALIZED
Expand All @@ -94,6 +113,6 @@ class BlockchainExplorer : public IBlockchainExplorer, public INodeObserver {
Logging::LoggerRef logger;

AsyncContextCounter asyncContextCounter;

PoolUpdateGuard poolUpdateGuard;
};
}
2 changes: 1 addition & 1 deletion src/Common/StreamTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ void readVarint(IInputStream& in, uint64_t& value) {
throw std::runtime_error("readVarint, value overflow");
}

temp |= static_cast<size_t>(piece & 0x7f) << shift;
temp |= static_cast<uint64_t>(piece & 0x7f) << shift;
if ((piece & 0x80) == 0) {
if (piece == 0 && shift != 0) {
throw std::runtime_error("readVarint, invalid value representation");
Expand Down
3 changes: 2 additions & 1 deletion src/CryptoNoteConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ const CheckpointData CHECKPOINTS[] = {
{804000, "bcc8b3782499aae508c40d5587d1cc5d68281435ea9bfc6804a262047f7b934d"},
{810500, "302b2349f221232820adc3dadafd8a61b035491e33af669c78a687949eb0a381"},
{816000, "32b7fdd4e4d715db81f8f09f4ba5e5c78e8113f2804d61a57378baee479ce745"},
{822000, "a3c9603c6813a0dc0efc40db288c356d1a7f02d1d2e47bee04346e73715f8984"}
{822000, "a3c9603c6813a0dc0efc40db288c356d1a7f02d1d2e47bee04346e73715f8984"},
{841000, "2cffb6504ee38f708a6256a63585f9382b3b426e64b4504236c70678bd160dce"}
};
} // CryptoNote

Expand Down
32 changes: 16 additions & 16 deletions src/CryptoNoteCore/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,18 @@ namespace CryptoNote {
core(const Currency& currency, i_cryptonote_protocol* pprotocol, Logging::ILogger& logger);
~core();

bool on_idle();
virtual bool handle_incoming_tx(const BinaryArray& tx_blob, tx_verification_context& tvc, bool keeped_by_block); //Deprecated. Should be removed with CryptoNoteProtocolHandler.
bool handle_incoming_block_blob(const BinaryArray& block_blob, block_verification_context& bvc, bool control_miner, bool relay_block);
virtual i_cryptonote_protocol* get_protocol(){return m_pprotocol;}
bool on_idle() override;
virtual bool handle_incoming_tx(const BinaryArray& tx_blob, tx_verification_context& tvc, bool keeped_by_block) override; //Deprecated. Should be removed with CryptoNoteProtocolHandler.
bool handle_incoming_block_blob(const BinaryArray& block_blob, block_verification_context& bvc, bool control_miner, bool relay_block) override;
virtual i_cryptonote_protocol* get_protocol() override {return m_pprotocol;}
const Currency& currency() const { return m_currency; }

//-------------------- IMinerHandler -----------------------
virtual bool handle_block_found(Block& b);
virtual bool get_block_template(Block& b, const AccountPublicAddress& adr, difficulty_type& diffic, uint32_t& height, const BinaryArray& ex_nonce);
virtual bool handle_block_found(Block& b) override;
virtual bool get_block_template(Block& b, const AccountPublicAddress& adr, difficulty_type& diffic, uint32_t& height, const BinaryArray& ex_nonce) override;

bool addObserver(ICoreObserver* observer);
bool removeObserver(ICoreObserver* observer);
bool addObserver(ICoreObserver* observer) override;
bool removeObserver(ICoreObserver* observer) override;

miner& get_miner() { return *m_miner; }
static void init_options(boost::program_options::options_description& desc);
Expand Down Expand Up @@ -93,13 +93,13 @@ namespace CryptoNote {
virtual bool removeMessageQueue(MessageQueue<BlockchainMessage>& messageQueue) override;

uint32_t get_current_blockchain_height();
bool have_block(const Crypto::Hash& id);
bool have_block(const Crypto::Hash& id) override;
std::vector<Crypto::Hash> buildSparseChain() override;
std::vector<Crypto::Hash> buildSparseChain(const Crypto::Hash& startBlockId) override;
void on_synchronized();
void on_synchronized() override;
bool is_ready() override;

virtual void get_blockchain_top(uint32_t& height, Crypto::Hash& top_id);
virtual void get_blockchain_top(uint32_t& height, Crypto::Hash& top_id) override;
bool get_blocks(uint32_t start_offset, uint32_t count, std::list<Block>& blocks, std::list<Transaction>& txs);
bool get_blocks(uint32_t start_offset, uint32_t count, std::list<Block>& blocks);
template<class t_ids_container, class t_blocks_container, class t_missed_container>
Expand Down Expand Up @@ -129,13 +129,13 @@ namespace CryptoNote {
//bool get_outs(uint64_t amount, std::list<Crypto::PublicKey>& pkeys);
virtual std::vector<Crypto::Hash> findBlockchainSupplement(const std::vector<Crypto::Hash>& remoteBlockIds, size_t maxCount,
uint32_t& totalBlockCount, uint32_t& startBlockIndex) override;
bool get_stat_info(core_stat_info& st_inf);
bool get_stat_info(core_stat_info& st_inf) override;

virtual bool get_tx_outputs_gindexs(const Crypto::Hash& tx_id, std::vector<uint32_t>& indexs);
virtual bool get_tx_outputs_gindexs(const Crypto::Hash& tx_id, std::vector<uint32_t>& indexs) override;
Crypto::Hash get_tail_id();
virtual bool get_random_outs_for_amounts(const COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS_request& req, COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS_response& res);
void pause_mining();
void update_block_template_and_resume_mining();
virtual bool get_random_outs_for_amounts(const COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS_request& req, COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS_response& res) override;
void pause_mining() override;
void update_block_template_and_resume_mining() override;
//Blockchain& get_blockchain_storage(){return m_blockchain;}
//debug functions
void print_blockchain(uint32_t start_index, uint32_t end_index);
Expand Down
8 changes: 4 additions & 4 deletions src/CryptoNoteProtocol/CryptoNoteProtocolHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ namespace CryptoNote

CryptoNoteProtocolHandler(const Currency& currency, System::Dispatcher& dispatcher, ICore& rcore, IP2pEndpoint* p_net_layout, Logging::ILogger& log);

virtual bool addObserver(ICryptoNoteProtocolObserver* observer);
virtual bool removeObserver(ICryptoNoteProtocolObserver* observer);
virtual bool addObserver(ICryptoNoteProtocolObserver* observer) override;
virtual bool removeObserver(ICryptoNoteProtocolObserver* observer) override;

void set_p2p_endpoint(IP2pEndpoint* p2p);
// ICore& get_core() { return m_core; }
Expand All @@ -68,8 +68,8 @@ namespace CryptoNote
bool get_payload_sync_data(CORE_SYNC_DATA& hshd);
bool process_payload_sync_data(const CORE_SYNC_DATA& hshd, CryptoNoteConnectionContext& context, bool is_inital);
int handleCommand(bool is_notify, int command, const BinaryArray& in_buff, BinaryArray& buff_out, CryptoNoteConnectionContext& context, bool& handled);
virtual size_t getPeerCount() const;
virtual uint32_t getObservedHeight() const;
virtual size_t getPeerCount() const override;
virtual uint32_t getObservedHeight() const override;
void requestMissingPoolTransactions(const CryptoNoteConnectionContext& context);

private:
Expand Down
2 changes: 1 addition & 1 deletion src/HTTP/HttpResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const char* getErrorBody(CryptoNote::HttpResponse::HTTP_STATUS status) {
case CryptoNote::HttpResponse::STATUS_404:
return "Requested url is not found\n";
case CryptoNote::HttpResponse::STATUS_500:
return "Internal server error is occured\n";
return "Internal server error is occurred\n";
default:
throw std::runtime_error("Error body for given status is not available");
}
Expand Down
6 changes: 3 additions & 3 deletions src/InProcessNode/InProcessNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ class InProcessNode : public INode, public CryptoNote::ICryptoNoteProtocolObserv
virtual bool addObserver(INodeObserver* observer) override;
virtual bool removeObserver(INodeObserver* observer) override;

virtual size_t getPeerCount() const;
virtual uint32_t getLastLocalBlockHeight() const;
virtual uint32_t getLastKnownBlockHeight() const;
virtual size_t getPeerCount() const override;
virtual uint32_t getLastLocalBlockHeight() const override;
virtual uint32_t getLastKnownBlockHeight() const override;
virtual uint32_t getLocalBlockCount() const override;
virtual uint32_t getKnownBlockCount() const override;
virtual uint64_t getLastLocalBlockTimestamp() const override;
Expand Down
20 changes: 10 additions & 10 deletions src/NodeRpcProxy/NodeRpcProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,25 @@ class NodeRpcProxy : public CryptoNote::INode {
NodeRpcProxy(const std::string& nodeHost, unsigned short nodePort);
virtual ~NodeRpcProxy();

virtual bool addObserver(CryptoNote::INodeObserver* observer);
virtual bool removeObserver(CryptoNote::INodeObserver* observer);
virtual bool addObserver(CryptoNote::INodeObserver* observer) override;
virtual bool removeObserver(CryptoNote::INodeObserver* observer) override;

virtual bool addObserver(CryptoNote::INodeRpcProxyObserver* observer);
virtual bool removeObserver(CryptoNote::INodeRpcProxyObserver* observer);

virtual void init(const Callback& callback);
virtual bool shutdown();
virtual void init(const Callback& callback) override;
virtual bool shutdown() override;

virtual size_t getPeerCount() const;
virtual uint32_t getLastLocalBlockHeight() const;
virtual uint32_t getLastKnownBlockHeight() const;
virtual size_t getPeerCount() const override;
virtual uint32_t getLastLocalBlockHeight() const override;
virtual uint32_t getLastKnownBlockHeight() const override;
virtual uint32_t getLocalBlockCount() const override;
virtual uint32_t getKnownBlockCount() const override;
virtual uint64_t getLastLocalBlockTimestamp() const override;

virtual void relayTransaction(const CryptoNote::Transaction& transaction, const Callback& callback);
virtual void getRandomOutsByAmounts(std::vector<uint64_t>&& amounts, uint64_t outsCount, std::vector<COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount>& result, const Callback& callback);
virtual void getNewBlocks(std::vector<Crypto::Hash>&& knownBlockIds, std::vector<CryptoNote::block_complete_entry>& newBlocks, uint32_t& startHeight, const Callback& callback);
virtual void relayTransaction(const CryptoNote::Transaction& transaction, const Callback& callback) override;
virtual void getRandomOutsByAmounts(std::vector<uint64_t>&& amounts, uint64_t outsCount, std::vector<COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount>& result, const Callback& callback) override;
virtual void getNewBlocks(std::vector<Crypto::Hash>&& knownBlockIds, std::vector<CryptoNote::block_complete_entry>& newBlocks, uint32_t& startHeight, const Callback& callback) override;
virtual void getTransactionOutsGlobalIndices(const Crypto::Hash& transactionHash, std::vector<uint32_t>& outsGlobalIndices, const Callback& callback) override;
virtual void queryBlocks(std::vector<Crypto::Hash>&& knownBlockIds, uint64_t timestamp, std::vector<BlockShortEntry>& newBlocks, uint32_t& startHeight, const Callback& callback) override;
virtual void getPoolSymmetricDifference(std::vector<Crypto::Hash>&& knownPoolTxIds, Crypto::Hash knownBlockId, bool& isBcActual,
Expand Down
Loading

0 comments on commit 00915e6

Please sign in to comment.