Skip to content

Commit

Permalink
Add fixture for xrp and add test for transaction build
Browse files Browse the repository at this point in the history
  • Loading branch information
KhalilBellakrid committed Feb 13, 2019
1 parent fe71360 commit ed2bd8c
Show file tree
Hide file tree
Showing 14 changed files with 384 additions and 22 deletions.
10 changes: 10 additions & 0 deletions core/src/config/Networks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ namespace ledger {
);
}

RippleLikeNetworkParameters Networks::ripple() {
return RippleLikeNetworkParameters(
"xrp",
"XRP signed message:\n",
{0x04, 0x88, 0xB2, 0x1E},
{},
0
);
}

}
}
}
1 change: 1 addition & 0 deletions core/src/config/Networks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include <api/BitcoinLikeNetworkParameters.hpp>
#include <api/EthereumLikeNetworkParameters.hpp>
#include <api/RippleLikeNetworkParameters.hpp>
#include <api/Networks.hpp>


Expand Down
46 changes: 28 additions & 18 deletions core/src/database/migrations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,29 @@ namespace ledger {

}

template <> void rollback<5>(soci::session& sql) {
// ERC20 tokens
sql << "DROP TABLE erc20_tokens";

// ERC20 operations
sql << "DROP TABLE erc20_operations";

// ERC20 accounts
sql << "DROP TABLE erc20_accounts";

// ETH operations
sql << "DROP TABLE ethereum_operations";

// ETH transactions
sql << "DROP TABLE ethereum_transactions";

// ETH accounts
sql << "DROP TABLE ethereum_accounts";

// ETH currencies
sql << "DROP TABLE ethereum_currencies";
}

template <> void migrate<6>(soci::session& sql) {

sql << "CREATE TABLE ripple_currencies("
Expand Down Expand Up @@ -395,27 +418,14 @@ namespace ledger {
")";
}

template <> void rollback<5>(soci::session& sql) {
// ERC20 tokens
sql << "DROP TABLE erc20_tokens";

// ERC20 operations
sql << "DROP TABLE erc20_operations";

// ERC20 accounts
sql << "DROP TABLE erc20_accounts";
template <> void rollback<6>(soci::session& sql) {
sql << "DROP TABLE ripple_currencies";

// ETH operations
sql << "DROP TABLE ethereum_operations";

// ETH transactions
sql << "DROP TABLE ethereum_transactions";
sql << "DROP TABLE ripple_accounts";

// ETH accounts
sql << "DROP TABLE ethereum_accounts";
sql << "DROP TABLE ripple_transactions";

// ETH currencies
sql << "DROP TABLE ethereum_currencies";
sql << "DROP TABLE ripple_operations";
}
}
}
3 changes: 3 additions & 0 deletions core/src/database/migrations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ namespace ledger {

template <> void migrate<5>(soci::session& sql);
template <> void rollback<5>(soci::session& sql);

template <> void migrate<6>(soci::session& sql);
template <> void rollback<6>(soci::session& sql);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ namespace ledger {

RippleLikeTransactionApi & RippleLikeTransactionApi::setValue(const std::shared_ptr<BigInt>& value) {
if (!value) {
throw make_exception(api::ErrorCode::INVALID_ARGUMENT, "RippleLikeTransactionApi::setValuet: Invalid Value");
throw make_exception(api::ErrorCode::INVALID_ARGUMENT, "RippleLikeTransactionApi::setValue: Invalid Value");
}

_value = std::make_shared<Amount>(_currency, 0, *value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ namespace ledger {
NodeRippleLikeBodyRequest bodyRequest;
bodyRequest.setMethod("account_tx");
bodyRequest.pushParameter("account", addresses[0]);
bodyRequest.pushParameter("ledger_index", std::string("validated"));
auto requestBody = bodyRequest.getString();
return _http->POST("", std::vector<uint8_t>(requestBody.begin(), requestBody.end()))
.template json<TransactionsBulk, Exception>(
Expand Down
1 change: 0 additions & 1 deletion core/src/wallet/ripple/rippleNetworks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ namespace ledger {
static const api::RippleLikeNetworkParameters RIPPLE(
"xrp",
"XRP signed message:\n",
//{0x00},
{0x04, 0x88, 0xB2, 0x1E},
{},
0
Expand Down
23 changes: 23 additions & 0 deletions core/test/fixtures/xrp_fixtures.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// This file was GENERATED by command:
// generate_fixtures.py
// DO NOT EDIT BY HAND!!!
#include "xrp_fixtures.h"

namespace ledger {
namespace testing {
namespace xrp {
core::api::AccountCreationInfo XPUB_INFO(
0, {"xrp"}, {"44'/144'/0'"},
{ledger::core::hex::toByteArray("024819f9d4bd29318226e3c807cdd2da84161abaf5619c5d2bbfe5be63c74cc9ed")} , {ledger::core::hex::toByteArray("b4f8427e7e19f284dfe7b99f107c55d00b3eae56df9569f0c4d56722742a5d71")}
);
std::shared_ptr<core::RippleLikeAccount> inflate(const std::shared_ptr<core::WalletPool>& pool, const std::shared_ptr<core::AbstractWallet>& wallet) {
auto account = std::dynamic_pointer_cast<core::RippleLikeAccount>(wait(wallet->newAccountWithInfo(XPUB_INFO)));
soci::session sql(pool->getDatabaseSessionPool()->getPool());
sql.begin(); account->putTransaction(sql, *core::JSONUtils::parse<core::RippleLikeTransactionParser>(TX_1));
sql.commit();
return account;
}
const std::string TX_1 = "{\"status\": \"success\", \"Account\": \"rsvAf4P8Tx6tBUdWPNesMngXDmbZ2LMVF8\", \"Fee\": \"10\", \"hash\": \"AF4BB95DE86A640B90B2AF3C696EF26EFE7DD71864CC959D8030B448DD48E756\", \"ledger_index\": 44196232, \"LastLedgerSequence\": 44196239, \"Sequence\": 1, \"SigningPubKey\": \"0215A9EE08A4B4747E27F348365F93BEB5897FA7E8776BEDAE2CB56917DCDBBF2F\", \"Destination\": \"rMspb4Kxa3EwdF4uN5TMqhHfsAkBit6w7k\", \"date\": 600017851, \"Amount\": \"22000000\", \"meta\": {\"TransactionResult\": \"tesSUCCESS\", \"delivered_amount\": \"22000000\", \"TransactionIndex\": 1, \"AffectedNodes\": [{\"CreatedNode\": {\"NewFields\": {\"Account\": \"rMspb4Kxa3EwdF4uN5TMqhHfsAkBit6w7k\", \"Balance\": \"22000000\", \"Sequence\": 1}, \"LedgerEntryType\": \"AccountRoot\", \"LedgerIndex\": \"5AF22704F03359EC7B35095324F071B04A361CAB5BE44D03122E33143FE9343E\"}}, {\"ModifiedNode\": {\"LedgerEntryType\": \"AccountRoot\", \"PreviousTxnID\": \"B02920CA663943E272886C9216CB779028C82492EE9C4DD73BAA12C3190E9F10\", \"FinalFields\": {\"OwnerCount\": 0, \"Account\": \"rsvAf4P8Tx6tBUdWPNesMngXDmbZ2LMVF8\", \"Balance\": \"35169770\", \"Flags\": 0, \"Sequence\": 2}, \"LedgerIndex\": \"699C329231D8720F61CEC57756413A9967C822CE073FA3C8BEC305FAF1AB363E\", \"PreviousTxnLgrSeq\": 43216867, \"PreviousFields\": {\"Balance\": \"57169780\", \"Sequence\": 1}}}]}, \"Flags\": 2147483648, \"TxnSignature\": \"3045022100F2AB61EC941462D692514BFDDB00BC0D31BA7DA66981193E67A04E90578C18B1022064A2375ECB5A68C22EE3038B783BE6A9E1F2C882A8E8BBEE43C4CFA93B536926\", \"inLedger\": 44196232, \"validated\": true, \"TransactionType\": \"Payment\"}";
}
}
}
44 changes: 44 additions & 0 deletions core/test/fixtures/xrp_fixtures.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// This file was GENERATED by command:
// generate_fixtures.py
// DO NOT EDIT BY HAND!!!
#ifndef LEDGER_FIXTURES_XRP
#define LEDGER_FIXTURES_XRP
#include <gtest/gtest.h>
#include <async/QtThreadDispatcher.hpp>
#include <src/database/DatabaseSessionPool.hpp>
#include <NativePathResolver.hpp>
#include <unordered_set>
#include <src/wallet/pool/WalletPool.hpp>
#include <CoutLogPrinter.hpp>
#include <src/api/DynamicObject.hpp>
#include <wallet/common/CurrencyBuilder.hpp>
#include <wallet/ripple/explorers/api/RippleLikeTransactionParser.h>
#include <wallet/ripple/RippleLikeWallet.h>
#include <wallet/ripple/database/RippleLikeTransactionDatabaseHelper.h>
#include <wallet/common/database/AccountDatabaseHelper.h>
#include <wallet/pool/database/PoolDatabaseHelper.hpp>
#include <utils/JSONUtils.h>
#include <async/async_wait.h>
#include <wallet/ripple/RippleLikeAccount.h>
#include <api/RippleLikeOperation.hpp>
#include <api/RippleLikeTransaction.hpp>
#include <api/BigInt.hpp>
#include <net/QtHttpClient.hpp>
#include <events/LambdaEventReceiver.hpp>
#include <soci.h>
#include <utils/hex.h>
#include <api/Account.hpp>
#include <api/RippleLikeAccount.hpp>

namespace ledger {
namespace testing {
namespace xrp {
extern core::api::AccountCreationInfo XPUB_INFO;
extern const std::string TX_1;

std::shared_ptr<core::RippleLikeAccount> inflate(const std::shared_ptr<core::WalletPool>& pool, const std::shared_ptr<core::AbstractWallet>& wallet);
}
}
}

#endif // LEDGER_FIXTURES_XRP
5 changes: 4 additions & 1 deletion core/test/integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ add_test (NAME ledger-core-integration-WalletTests.CreateAccountBug COMMAND ledg
add_test (NAME ledger-core-integration-EthereumLikeWalletSynchronization.MediumXpubSynchronization COMMAND ledger-core-integration-tests --gtest_filter="EthereumLikeWalletSynchronization.MediumXpubSynchronization")
add_test (NAME ledger-core-integration-EthereumLikeWalletSynchronization.XpubSynchronization COMMAND ledger-core-integration-tests --gtest_filter="EthereumLikeWalletSynchronization.XpubSynchronization")

add_test (NAME ledger-core-integration-EthereumMakeTransaction.CreateStandardWithOneOutput COMMAND ledger-core-integration-tests --gtest_filter="EthereumMakeTransaction.CreateStandardWithOneOutput")
add_test (NAME ledger-core-integration-EthereumMakeTransaction.CreateTx COMMAND ledger-core-integration-tests --gtest_filter="EthereumMakeTransaction.CreateTx")
add_test (NAME ledger-core-integration-EthereumMakeTransaction.ParseUnsignedRawTransaction COMMAND ledger-core-integration-tests --gtest_filter="EthereumMakeTransaction.ParseUnsignedRawTransaction")

add_test (NAME ledger-core-integration-RippleLikeWalletSynchronization.MediumXpubSynchronization COMMAND ledger-core-integration-tests --gtest_filter="RippleLikeWalletSynchronization.MediumXpubSynchronization")

add_test (NAME ledger-core-integration-RippleMakeTransaction.CreateTx COMMAND ledger-core-integration-tests --gtest_filter="RippleMakeTransaction.CreateTx")
add_test (NAME ledger-core-integration-RippleMakeTransaction.ParseUnsignedRawTransaction COMMAND ledger-core-integration-tests --gtest_filter="RippleMakeTransaction.ParseUnsignedRawTransaction")
103 changes: 103 additions & 0 deletions core/test/integration/transactions/ripple_transaction_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
*
* ripple_transaction_tests
*
* Created by El Khalil Bellakrid on 14/07/2018.
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Ledger
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

#include "../BaseFixture.h"
#include "../../fixtures/xrp_fixtures.h"
#include <api/KeychainEngines.hpp>
#include "transaction_test_helper.h"
#include <utils/hex.h>
#include <utils/DateUtils.hpp>
#include <wallet/ripple/database/RippleLikeAccountDatabaseHelper.h>
#include <wallet/ripple/api_impl/RippleLikeTransactionApi.h>
#include <wallet/currencies.hpp>
#include <iostream>
using namespace std;

struct RippleMakeTransaction : public RippleMakeBaseTransaction {
void SetUpConfig() override {
auto configuration = DynamicObject::newInstance();
testData.configuration = configuration;
testData.walletName = "my_wallet";
testData.currencyName = "ripple";
testData.inflate_xrp = ledger::testing::xrp::inflate;
}
};

TEST_F(RippleMakeTransaction, CreateTx) {
auto builder = tx_builder();
auto balance = wait(account->getBalance());
auto fromDate = "2018-01-01T13:38:23Z";
auto toDate = DateUtils::toJSON(DateUtils::now());
auto balanceHistory = wait(account->getBalanceHistory(fromDate, toDate, api::TimePeriod::MONTH));

EXPECT_EQ(balanceHistory[balanceHistory.size() - 1]->toLong(), balance->toLong());

builder->setFees(api::Amount::fromLong(currency, 200000));
builder->sendToAddress(api::Amount::fromLong(currency, 200000), "rMspb4Kxa3EwdF4uN5TMqhHfsAkBit6w7k");
auto f = builder->build();
auto tx = ::wait(f);
auto serializedTx = tx->serialize();
auto parsedTx = RippleLikeTransactionBuilder::parseRawUnsignedTransaction(wallet->getCurrency(), serializedTx);
auto serializedParsedTx = parsedTx->serialize();
EXPECT_EQ(serializedTx, serializedParsedTx);

auto date = "2000-03-27T09:10:22Z";
auto formatedDate = DateUtils::fromJSON(date);

//Delete account
auto code = wait(wallet->eraseDataSince(formatedDate));
EXPECT_EQ(code, api::ErrorCode::FUTURE_WAS_SUCCESSFULL);

//Check if account was successfully deleted
auto newAccountCount = wait(wallet->getAccountCount());
EXPECT_EQ(newAccountCount, 0);
{
soci::session sql(pool->getDatabaseSessionPool()->getPool());
RippleLikeAccountDatabaseEntry entry;
auto result = RippleLikeAccountDatabaseHelper::queryAccount(sql, account->getAccountUid(), entry);
EXPECT_EQ(result, false);
}

//Delete wallet
auto walletCode = wait(pool->eraseDataSince(formatedDate));
EXPECT_EQ(walletCode, api::ErrorCode::FUTURE_WAS_SUCCESSFULL);

//Check if wallet was successfully deleted
auto walletCount = wait(pool->getWalletCount());
EXPECT_EQ(walletCount, 0);

}

TEST_F(RippleMakeTransaction, ParseUnsignedRawTransaction) {
//Tx hash 4858a0a3d5f1de0c0f5729f25c3501bda946093aed07f842e53a90ac65d66f70
auto strTx = "E800850165A0BC0083030D4094A49386FFF4E0DD767B145E75D92F7FBA8854553E8301E0F380018080";
auto tx = api::RippleLikeTransactionBuilder::parseRawUnsignedTransaction(ledger::core::currencies::ETHEREUM, hex::toByteArray(strTx));
EXPECT_EQ(hex::toString(tx->serialize()), strTx);
}
41 changes: 41 additions & 0 deletions core/test/integration/transactions/transaction_test_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
#include <wallet/ethereum/EthereumLikeWallet.h>
#include <wallet/ethereum/EthereumLikeAccount.h>
#include <wallet/ethereum/transaction_builders/EthereumLikeTransactionBuilder.h>
#include <wallet/ripple/RippleLikeWallet.h>
#include <wallet/ripple/RippleLikeAccount.h>
#include <wallet/ripple/transaction_builders/RippleLikeTransactionBuilder.h>
#include "../BaseFixture.h"


Expand All @@ -53,6 +56,9 @@ struct TransactionTestData {
const std::shared_ptr<AbstractWallet>& )> inflate_btc;
std::function<std::shared_ptr<EthereumLikeAccount> (const std::shared_ptr<WalletPool>&,
const std::shared_ptr<AbstractWallet>& )> inflate_eth;

std::function<std::shared_ptr<RippleLikeAccount> (const std::shared_ptr<WalletPool>&,
const std::shared_ptr<AbstractWallet>& )> inflate_xrp;
};

struct BitcoinMakeBaseTransaction : public BaseFixture {
Expand Down Expand Up @@ -125,6 +131,41 @@ struct EthereumMakeBaseTransaction : public BaseFixture {
virtual void SetUpConfig() = 0;
};

struct RippleMakeBaseTransaction : public BaseFixture {

void SetUp() override {
BaseFixture::SetUp();
SetUpConfig();
recreate();
}

void recreate() {
pool = newDefaultPool();
wallet = wait(pool->createWallet(testData.walletName, testData.currencyName, testData.configuration));
account = testData.inflate_xrp(pool, wallet);
currency = wallet->getCurrency();
}

void TearDown() override {
BaseFixture::TearDown();
pool = nullptr;
wallet = nullptr;
account = nullptr;
}

std::shared_ptr<RippleLikeTransactionBuilder> tx_builder() {
return std::dynamic_pointer_cast<RippleLikeTransactionBuilder>(account->buildTransaction());
}
std::shared_ptr<WalletPool> pool;
std::shared_ptr<AbstractWallet> wallet;
std::shared_ptr<RippleLikeAccount> account;
api::Currency currency;
TransactionTestData testData;

protected:
virtual void SetUpConfig() = 0;
};



#endif //LEDGER_CORE_TRANSACTION_TEST_HELPER_H
2 changes: 2 additions & 0 deletions tools/fixture_generator/generate_xrp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
python generate_xrp_fixtures.py "xrp" "rMspb4Kxa3EwdF4uN5TMqhHfsAkBit6w7k" "024819f9d4bd29318226e3c807cdd2da84161abaf5619c5d2bbfe5be63c74cc9ed" "b4f8427e7e19f284dfe7b99f107c55d00b3eae56df9569f0c4d56722742a5d71"
Loading

0 comments on commit ed2bd8c

Please sign in to comment.