Skip to content

Commit

Permalink
Added reset <arg> to wallet & RPC.
Browse files Browse the repository at this point in the history
Supply a blockchain height to save time resync wallet from the start.
Also fixed bug where reset would not pick up old transactions.
  • Loading branch information
Adrian Herridge committed Jan 22, 2018
1 parent 112e161 commit 87ed25a
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 24 deletions.
3 changes: 2 additions & 1 deletion include/IWalletLegacy.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class IWalletLegacy {
virtual void initWithKeys(const AccountKeys& accountKeys, const std::string& password) = 0;
virtual void shutdown() = 0;
virtual void reset() = 0;
virtual void reset(uint64_t height) = 0;

virtual void save(std::ostream& destination, bool saveDetailed = true, bool saveCache = true) = 0;

Expand All @@ -111,7 +112,7 @@ class IWalletLegacy {
virtual std::error_code cancelTransaction(size_t transferId) = 0;

virtual void getAccountKeys(AccountKeys& keys) = 0;
virtual void syncAll(bool syncWalletFromZero = 0) = 0;
virtual void syncAll(bool syncWalletFromZero = 0, uint64_t height = 0) = 0;
};

}
26 changes: 23 additions & 3 deletions src/SimpleWallet/SimpleWallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,9 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) {

std::string walletFileName;
sync_from_zero = command_line::get_arg(vm, arg_SYNC_FROM_ZERO);
if (sync_from_zero) {
sync_from_height = 0;
}
if (!m_generate_new.empty() || !m_import_new.empty()) {
std::string ignoredString;
if (!m_generate_new.empty()) {
Expand Down Expand Up @@ -611,6 +614,9 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) {
}

sync_from_zero = command_line::get_arg(vm, arg_SYNC_FROM_ZERO);
if (sync_from_zero) {
sync_from_height = 0;
}
if (!m_generate_new.empty()) {
std::string walletAddressFile = prepareWalletAddressFilename(m_generate_new);
boost::system::error_code ignore;
Expand Down Expand Up @@ -672,7 +678,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) {
m_wallet.reset(new WalletLegacy(m_currency, *m_node));


m_wallet->syncAll(sync_from_zero);
m_wallet->syncAll(sync_from_zero, 0);

try {
m_wallet_file = tryToOpenWalletOrLoadKeysOrThrow(logger, m_wallet, m_wallet_file_arg, pwd_container.password());
Expand Down Expand Up @@ -723,7 +729,7 @@ bool simple_wallet::new_wallet(const std::string &wallet_file, const std::string
try {
m_initResultPromise.reset(new std::promise<std::error_code>());
std::future<std::error_code> f_initError = m_initResultPromise->get_future();
m_wallet->syncAll(sync_from_zero);
m_wallet->syncAll(sync_from_zero, 0);
m_wallet->initAndGenerate(password);
auto initError = f_initError.get();
m_initResultPromise.reset(nullptr);
Expand Down Expand Up @@ -849,7 +855,21 @@ bool simple_wallet::reset(const std::vector<std::string> &args) {
m_walletSynchronized = false;
}

m_wallet->reset();
if(0 == args.size()) {
success_msg_writer(true) << "Resetting wallet from block height 0";
m_wallet->syncAll(true, 0);
m_wallet->reset();
} else {
uint64_t height = 0;
bool ok = Common::fromString(args[0], height);
if (ok) {
success_msg_writer(true) << "Resetting wallet from block height " << height;
m_wallet->syncAll(true, height);
m_wallet->reset(height);
}
}


success_msg_writer(true) << "Reset completed successfully.";

std::unique_lock<std::mutex> lock(m_walletSynchronizedMutex);
Expand Down
1 change: 1 addition & 0 deletions src/SimpleWallet/SimpleWallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ std::string m_import_new;

std::string m_wallet_file;
bool sync_from_zero;
uint64_t sync_from_height;

std::unique_ptr<std::promise<std::error_code>> m_initResultPromise;

Expand Down
33 changes: 21 additions & 12 deletions src/Wallet/WalletRpcServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ void wallet_rpc_server::init_options(boost::program_options::options_description
}
//------------------------------------------------------------------------------------------------------------------------------
wallet_rpc_server::wallet_rpc_server(
System::Dispatcher& dispatcher,
Logging::ILogger& log,
System::Dispatcher& dispatcher,
Logging::ILogger& log,
CryptoNote::IWalletLegacy&w,
CryptoNote::INode& n,
CryptoNote::Currency& currency,
CryptoNote::INode& n,
CryptoNote::Currency& currency,
const std::string& walletFile)
:
HttpServer(dispatcher, log),
logger(log, "WalletRpc"),
m_dispatcher(dispatcher),
m_stopComplete(dispatcher),
:
HttpServer(dispatcher, log),
logger(log, "WalletRpc"),
m_dispatcher(dispatcher),
m_stopComplete(dispatcher),
m_wallet(w),
m_node(n),
m_node(n),
m_currency(currency),
m_walletFilename(walletFile) {
}
Expand Down Expand Up @@ -108,7 +108,11 @@ void wallet_rpc_server::processRequest(const CryptoNote::HttpRequest& request, C
{ "get_payments", makeMemberMethod(&wallet_rpc_server::on_get_payments) },
{ "get_transfers", makeMemberMethod(&wallet_rpc_server::on_get_transfers) },
{ "get_height", makeMemberMethod(&wallet_rpc_server::on_get_height) },
{ "reset", makeMemberMethod(&wallet_rpc_server::on_reset) }
{ "reset", makeMemberMethod(&wallet_rpc_server::on_reset) },
{ "reset_from", makeMemberMethod(&wallet_rpc_server::on_reset_from) }
//{ "stop_wallet", makeMemberMethod(&wallet_rpc_server::on_stop_wallet) },
//{ "get_address", makeMemberMethod(&wallet_rpc_server::on_get_address) },
//{ "view_keys", makeMemberMethod(&wallet_rpc_server::on_view_keys) }
};

auto it = s_methods.find(jsonRequest.getMethod());
Expand Down Expand Up @@ -149,7 +153,7 @@ bool wallet_rpc_server::on_transfer(const wallet_rpc::COMMAND_RPC_TRANSFER::requ

Crypto::Hash payment_id;
if (!CryptoNote::parsePaymentId(payment_id_str, payment_id)) {
throw JsonRpc::JsonRpcError(WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID,
throw JsonRpc::JsonRpcError(WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID,
"Payment id has invalid format: \"" + payment_id_str + "\", expected 64-character string");
}

Expand Down Expand Up @@ -292,4 +296,9 @@ bool wallet_rpc_server::on_reset(const wallet_rpc::COMMAND_RPC_RESET::request& r
return true;
}

bool wallet_rpc_server::on_reset_from(const wallet_rpc::COMMAND_RPC_RESET_FROM::request& req, wallet_rpc::COMMAND_RPC_RESET_FROM::response& res) {
m_wallet.reset();
return true;
}

}
9 changes: 5 additions & 4 deletions src/Wallet/WalletRpcServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ namespace Tools
public:

wallet_rpc_server(
System::Dispatcher& dispatcher,
System::Dispatcher& dispatcher,
Logging::ILogger& log,
CryptoNote::IWalletLegacy &w,
CryptoNote::INode &n,
CryptoNote::IWalletLegacy &w,
CryptoNote::INode &n,
CryptoNote::Currency& currency,
const std::string& walletFilename);


static void init_options(boost::program_options::options_description& desc);
bool init(const boost::program_options::variables_map& vm);

bool run();
void send_stop_signal();

Expand All @@ -66,6 +66,7 @@ namespace Tools
bool on_get_transfers(const wallet_rpc::COMMAND_RPC_GET_TRANSFERS::request& req, wallet_rpc::COMMAND_RPC_GET_TRANSFERS::response& res);
bool on_get_height(const wallet_rpc::COMMAND_RPC_GET_HEIGHT::request& req, wallet_rpc::COMMAND_RPC_GET_HEIGHT::response& res);
bool on_reset(const wallet_rpc::COMMAND_RPC_RESET::request& req, wallet_rpc::COMMAND_RPC_RESET::response& res);
bool on_reset_from(const wallet_rpc::COMMAND_RPC_RESET_FROM::request& req, wallet_rpc::COMMAND_RPC_RESET_FROM::response& res);

bool handle_command_line(const boost::program_options::variables_map& vm);

Expand Down
16 changes: 16 additions & 0 deletions src/Wallet/WalletRpcServerCommandsDefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,21 @@ using CryptoNote::ISerializer;
typedef CryptoNote::EMPTY_STRUCT request;
typedef CryptoNote::EMPTY_STRUCT response;
};

struct COMMAND_RPC_RESET_FROM {

struct request
{
uint64_t height;

void serialize(ISerializer& s) {
KV_MEMBER(height)
}
};

typedef CryptoNote::EMPTY_STRUCT response;

};

}
}
19 changes: 17 additions & 2 deletions src/WalletLegacy/WalletLegacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,12 @@ void WalletLegacy::initSync() {
sub.transactionSpendableAge = 1;
sub.syncStart.height = 0;
sub.syncStart.timestamp = m_account.get_createtime() - ACCOUN_CREATE_TIME_ACCURACY;
if (m_syncAll == 1)
if (m_syncAll == 1) {
sub.syncStart.timestamp = 0;
if (m_syncStartHeight) {
sub.syncStart.height = m_syncStartHeight;
}
}
std::cout << "Sync from timestamp: " << sub.syncStart.timestamp << std::endl;

auto& subObject = m_transfersSync.addSubscription(sub);
Expand Down Expand Up @@ -311,6 +315,12 @@ void WalletLegacy::reset() {
}
}

void WalletLegacy::reset(uint64_t height) {
m_syncAll = 1;
m_syncStartHeight = height;
reset();
}

void WalletLegacy::save(std::ostream& destination, bool saveDetailed, bool saveCache) {
if(m_isStopping) {
m_observerManager.notify(&IWalletLegacyObserver::saveCompleted, make_error_code(CryptoNote::error::OPERATION_CANCELLED));
Expand Down Expand Up @@ -598,8 +608,13 @@ void WalletLegacy::notifyIfBalanceChanged() {

}

void WalletLegacy::syncAll(bool syncWalletFromZero) {
void WalletLegacy::syncAll(bool syncWalletFromZero, uint64_t height) {
m_syncAll = syncWalletFromZero;
if (height) {
m_syncStartHeight = height;
} else {
m_syncStartHeight = 0;
}
}
void WalletLegacy::getAccountKeys(AccountKeys& keys) {
if (m_state == NOT_INITIALIZED) {
Expand Down
6 changes: 4 additions & 2 deletions src/WalletLegacy/WalletLegacy.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class WalletLegacy :
virtual void initWithKeys(const AccountKeys& accountKeys, const std::string& password) override;
virtual void shutdown() override;
virtual void reset() override;
virtual void reset(uint64_t height) override;

virtual void save(std::ostream& destination, bool saveDetailed = true, bool saveCache = true) override;

Expand All @@ -84,7 +85,7 @@ class WalletLegacy :
virtual TransactionId sendTransaction(const std::vector<WalletLegacyTransfer>& transfers, uint64_t fee, const std::string& extra = "", uint64_t mixIn = 0, uint64_t unlockTimestamp = 0) override;
virtual std::error_code cancelTransaction(size_t transactionId) override;

void syncAll(bool syncWalletFromZero) override;
void syncAll(bool syncWalletFromZero, uint64_t height) override;
virtual void getAccountKeys(AccountKeys& keys) override;

private:
Expand Down Expand Up @@ -141,7 +142,8 @@ class WalletLegacy :
Tools::ObserverManager<CryptoNote::IWalletLegacyObserver> m_observerManager;

std::unique_ptr<SyncStarter> m_onInitSyncStarter;
bool m_syncAll = 0;
bool m_syncAll = 0;
uint64_t m_syncStartHeight = 0;
};

} //namespace CryptoNote

0 comments on commit 87ed25a

Please sign in to comment.