Skip to content

Commit

Permalink
IWallet implementation improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Juarez committed Aug 5, 2015
1 parent 50cdbfa commit 49572fc
Show file tree
Hide file tree
Showing 50 changed files with 699 additions and 263 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
/build
/tags
.idea
.ycm_extra_conf.py
.ycm_extra_conf.pyc
Release
Debug
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ else()
set(RELEASE_FLAGS "-Ofast -DNDEBUG -Wno-unused-variable")
if(NOT APPLE)
# There is a clang bug that does not allow to compile code that uses AES-NI intrinsics if -flto is enabled
if (CMAKE_C_COMPILER_ID STREQUAL "GNU" AND ${CMAKE_SYSTEM_NAME} STREQUAL "Linux"
AND ${CMAKE_BUILD_TYPE} STREQUAL "Release" AND ((CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.9) OR (CMAKE_C_COMPILER_VERSION VERSION_EQUAL 4.9)))
if (CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_SYSTEM_NAME STREQUAL "Linux"
AND CMAKE_BUILD_TYPE STREQUAL "Release" AND ((CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.9) OR (CMAKE_C_COMPILER_VERSION VERSION_EQUAL 4.9)))
# On linux, to build in lto mode, check that ld.gold linker is used: 'update-alternatives --install /usr/bin/ld ld /usr/bin/ld.gold HIGHEST_PRIORITY'
set(CMAKE_AR gcc-ar)
set(CMAKE_RANLIB gcc-ranlib)
Expand Down
3 changes: 2 additions & 1 deletion src/CryptoNoteConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ const CheckpointData CHECKPOINTS[] = {
{780000, "8dd55a9bae429e3685b90317281e633917023d3512eb7f37372209d1a5fc1070"},
{785500, "de1a487d70964d25ed6f7de196866f357a293e867ee81313e7fd0352d0126bdd"},
{789000, "acef490bbccce3b7b7ae8554a414f55413fbf4ca1472c6359b126a4439bd9f01"},
{796000, "04e387a00d35db21d4d93d04040b31f22573972a7e61d72cc07d0ab69bcb9c44"}
{796000, "04e387a00d35db21d4d93d04040b31f22573972a7e61d72cc07d0ab69bcb9c44"},
{800000, "d7fa4eea02e5ce60b949136569c0ea7ac71ea46e0065311054072ac415560b86"}
};
} // CryptoNote

Expand Down
2 changes: 1 addition & 1 deletion src/CryptoNoteCore/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ bool core::check_tx_semantic(const Transaction& tx, bool keeped_by_block) {
get_inputs_money_amount(tx, amount_in);
uint64_t amount_out = get_outs_money_amount(tx);

if (amount_in <= amount_out) {
if (amount_in < amount_out) {
logger(ERROR) << "tx with wrong amounts: ins " << amount_in << ", outs " << amount_out << ", rejected for tx id= " << getObjectHash(tx);
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/CryptoNoteCore/TransactionPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ namespace CryptoNote {

uint64_t outputs_amount = get_outs_money_amount(tx);

if (outputs_amount >= inputs_amount) {
if (outputs_amount > inputs_amount) {
logger(INFO) << "transaction use more money then it has: use " << m_currency.formatAmount(outputs_amount) <<
", have " << m_currency.formatAmount(inputs_amount);
tvc.m_verifivation_failed = true;
Expand Down
17 changes: 3 additions & 14 deletions src/PaymentGate/WalletService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,7 @@
#include <sstream>
#include <unordered_set>

#ifdef WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#else
#include <unistd.h>
#include <stdio.h>
#endif
#include <boost/filesystem/operations.hpp>

#include <System/Timer.h>
#include <System/InterruptedException.h>
Expand Down Expand Up @@ -114,11 +106,8 @@ std::string createTemporaryFile(const std::string& path, std::fstream& tempFile)

//returns true on success
bool deleteFile(const std::string& filename) {
#ifdef WIN32
return DeleteFile(filename.c_str()) != 0;
#else
return unlink(filename.c_str()) == 0;
#endif
boost::system::error_code err;
return boost::filesystem::remove(filename, err) && !err;
}

void replaceWalletFiles(const std::string &path, const std::string &tempFilePath) {
Expand Down
33 changes: 17 additions & 16 deletions src/Platform/Linux/System/Dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <string.h>
#include <ucontext.h>
#include <unistd.h>
#include "ErrorMessage.h"

namespace System {

Expand All @@ -40,7 +41,7 @@ class MutextGuard {
MutextGuard(pthread_mutex_t& _mutex) : mutex(_mutex) {
auto ret = pthread_mutex_lock(&mutex);
if (ret != 0) {
throw std::runtime_error("failed to acquire mutex, errno=" + std::to_string(ret) + ": " + strerror(ret));
throw std::runtime_error("pthread_mutex_lock failed, " + errorMessage(ret));
}
}

Expand All @@ -62,15 +63,15 @@ Dispatcher::Dispatcher() {
std::string message;
epoll = ::epoll_create1(0);
if (epoll == -1) {
message = "epoll_create1() fail errno=" + std::to_string(errno);
message = "epoll_create1 failed, " + lastErrorMessage();
} else {
mainContext.ucontext = new ucontext_t;
if (getcontext(reinterpret_cast<ucontext_t*>(mainContext.ucontext)) == -1) {
message = "getcontext() fail errno=" + std::to_string(errno);
message = "getcontext failed, " + lastErrorMessage();
} else {
remoteSpawnEvent = eventfd(0, O_NONBLOCK);
if(remoteSpawnEvent == -1) {
message = "eventfd() fail errno=" + std::to_string(errno);
message = "eventfd failed, " + lastErrorMessage();
} else {
remoteSpawnEventContext.writeContext = nullptr;
remoteSpawnEventContext.readContext = nullptr;
Expand All @@ -80,7 +81,7 @@ Dispatcher::Dispatcher() {
remoteSpawnEventEpollEvent.data.ptr = &remoteSpawnEventContext;

if (epoll_ctl(epoll, EPOLL_CTL_ADD, remoteSpawnEvent, &remoteSpawnEventEpollEvent) == -1) {
message = "epoll_ctl() failed, errno=" + std::to_string(errno);
message = "epoll_ctl failed, " + lastErrorMessage();
} else {
*reinterpret_cast<pthread_mutex_t*>(this->mutex) = pthread_mutex_t(PTHREAD_MUTEX_INITIALIZER);

Expand Down Expand Up @@ -155,7 +156,7 @@ void Dispatcher::clear() {
while (!timers.empty()) {
int result = ::close(timers.top());
if (result == -1) {
throw std::runtime_error("Dispatcher::clear, close failed, errno=" + std::to_string(errno));
throw std::runtime_error("Dispatcher::clear, close failed, " + lastErrorMessage());
}

timers.pop();
Expand All @@ -179,7 +180,7 @@ void Dispatcher::dispatch() {
uint64_t buf;
auto transferred = read(remoteSpawnEvent, &buf, sizeof buf);
if(transferred == -1) {
throw std::runtime_error("Dispatcher::dispatch() read(remoteSpawnEvent) fail errno=" + std::to_string(errno));
throw std::runtime_error("Dispatcher::dispatch, read(remoteSpawnEvent) failed, " + lastErrorMessage());
}

MutextGuard guard(*reinterpret_cast<pthread_mutex_t*>(this->mutex));
Expand All @@ -206,15 +207,15 @@ void Dispatcher::dispatch() {
}

if (errno != EINTR) {
throw std::runtime_error("Dispatcher::dispatch(), epoll_wait() failed, errno=" + std::to_string(errno));
throw std::runtime_error("Dispatcher::dispatch, epoll_wait failed, " + lastErrorMessage());
}
}

if (context != currentContext) {
ucontext_t* oldContext = static_cast<ucontext_t*>(currentContext->ucontext);
currentContext = context;
if (swapcontext(oldContext, static_cast<ucontext_t *>(context->ucontext)) == -1) {
throw std::runtime_error("Dispatcher::dispatch() swapcontext() failed, errno=" + std::to_string(errno));
throw std::runtime_error("Dispatcher::dispatch, swapcontext failed, " + lastErrorMessage());
}
}
}
Expand Down Expand Up @@ -269,7 +270,7 @@ void Dispatcher::remoteSpawn(std::function<void()>&& procedure) {
uint64_t one = 1;
auto transferred = write(remoteSpawnEvent, &one, sizeof one);
if(transferred == - 1) {
throw std::runtime_error("Dispatcher::remoteSpawn, write() failed errno = " + std::to_string(errno));
throw std::runtime_error("Dispatcher::remoteSpawn, write failed, " + lastErrorMessage());
}
}

Expand Down Expand Up @@ -308,7 +309,7 @@ void Dispatcher::yield() {
uint64_t buf;
auto transferred = read(remoteSpawnEvent, &buf, sizeof buf);
if(transferred == -1) {
throw std::runtime_error("Dispatcher::dispatch() read(remoteSpawnEvent) fail errno=" + std::to_string(errno));
throw std::runtime_error("Dispatcher::dispatch, read(remoteSpawnEvent) failed, " + lastErrorMessage());
}

MutextGuard guard(*reinterpret_cast<pthread_mutex_t*>(this->mutex));
Expand All @@ -334,7 +335,7 @@ void Dispatcher::yield() {
}
} else {
if (errno != EINTR) {
throw std::runtime_error("Dispatcher::dispatch(), epoll_wait() failed, errno=" + std::to_string(errno));
throw std::runtime_error("Dispatcher::dispatch, epoll_wait failed, " + lastErrorMessage());
}
}
}
Expand All @@ -353,7 +354,7 @@ NativeContext& Dispatcher::getReusableContext() {
if(firstReusableContext == nullptr) {
ucontext_t* newlyCreatedContext = new ucontext_t;
if (getcontext(newlyCreatedContext) == -1) { //makecontext precondition
throw std::runtime_error("Dispatcher::getReusableContext(), getcontext() fail errno=" + std::to_string(errno));
throw std::runtime_error("Dispatcher::getReusableContext, getcontext failed, " + lastErrorMessage());
}

auto stackPointer = new uint8_t[STACK_SIZE];
Expand All @@ -365,7 +366,7 @@ NativeContext& Dispatcher::getReusableContext() {

ucontext_t* oldContext = static_cast<ucontext_t*>(currentContext->ucontext);
if (swapcontext(oldContext, newlyCreatedContext) == -1) {
throw std::runtime_error("Dispatcher::getReusableContext() swapcontext() failed, errno=" + std::to_string(errno));
throw std::runtime_error("Dispatcher::getReusableContext, swapcontext failed, " + lastErrorMessage());
}

assert(firstReusableContext != nullptr);
Expand Down Expand Up @@ -393,7 +394,7 @@ int Dispatcher::getTimer() {
timerEvent.data.ptr = nullptr;

if (epoll_ctl(getEpoll(), EPOLL_CTL_ADD, timer, &timerEvent) == -1) {
throw std::runtime_error("Dispatcher::getTimer(), epoll_ctl() failed, errno=" + std::to_string(errno));
throw std::runtime_error("Dispatcher::getTimer, epoll_ctl failed, " + lastErrorMessage());
}
} else {
timer = timers.top();
Expand All @@ -416,7 +417,7 @@ void Dispatcher::contextProcedure(void* ucontext) {
firstReusableContext = &context;
ucontext_t* oldContext = static_cast<ucontext_t*>(context.ucontext);
if (swapcontext(oldContext, static_cast<ucontext_t*>(currentContext->ucontext)) == -1) {
throw std::runtime_error("Dispatcher::contextProcedure() swapcontext() failed, errno=" + std::to_string(errno));
throw std::runtime_error("Dispatcher::contextProcedure, swapcontext failed, " + lastErrorMessage());
}

for (;;) {
Expand Down
32 changes: 32 additions & 0 deletions src/Platform/Linux/System/ErrorMessage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
//
// This file is part of Bytecoin.
//
// Bytecoin is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Bytecoin is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.

#include "ErrorMessage.h"
#include <cerrno>
#include <cstring>

namespace System {

std::string lastErrorMessage() {
return errorMessage(errno);
}

std::string errorMessage(int err) {
return "result=" + std::to_string(err) + ", " + std::strerror(err);
}

}
25 changes: 25 additions & 0 deletions src/Platform/Linux/System/ErrorMessage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
//
// This file is part of Bytecoin.
//
// Bytecoin is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Bytecoin is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.

#pragma once

#include <string>

namespace System {
std::string lastErrorMessage();
std::string errorMessage(int);
}
3 changes: 2 additions & 1 deletion src/Platform/Linux/System/Ipv4Resolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <netdb.h>

#include <System/Dispatcher.h>
#include <System/ErrorMessage.h>
#include <System/InterruptedException.h>
#include <System/Ipv4Address.h>

Expand Down Expand Up @@ -62,7 +63,7 @@ Ipv4Address Ipv4Resolver::resolve(const std::string& host) {
addrinfo* addressInfos;
int result = getaddrinfo(host.c_str(), NULL, &hints, &addressInfos);
if (result != 0) {
throw std::runtime_error("Ipv4Resolver::resolve, getaddrinfo failed, result=" + std::to_string(result));
throw std::runtime_error("Ipv4Resolver::resolve, getaddrinfo failed, " + errorMessage(result));
}

std::size_t count = 0;
Expand Down
Loading

0 comments on commit 49572fc

Please sign in to comment.