Skip to content

Commit

Permalink
fix: Add missing initialization for transaction version (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
ciband authored and faustbrian committed Aug 7, 2019
1 parent 03288a3 commit 2c0fe64
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/include/cpp-crypto/transactions/builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ class Builder {
private:
Builder();

static Transaction sign(
Transaction transaction,
static void sign(
Transaction& transaction,
std::string passphrase,
std::string secondPassphrase = "",
const Configuration& configuration = {});
Expand Down
4 changes: 4 additions & 0 deletions src/include/cpp-crypto/transactions/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ struct TransactionAsset {
class Transaction {
public:
Transaction() = default;
Transaction(const Transaction&) = default;
Transaction& operator=(const Transaction&) = default;
Transaction(Transaction&&) = default;
Transaction& operator=(Transaction&&) = default;

std::string getId() const;

Expand Down
35 changes: 26 additions & 9 deletions src/transactions/builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@ Transaction Builder::buildTransfer(
Transaction transaction;
if (amount < 1ULL) { return transaction; }

transaction.version = 0x01;
transaction.type = defaults::TransactionTypes::Transfer;
transaction.fee = configuration.getFee(defaults::TransactionTypes::Transfer);
transaction.recipient = std::move(recipient);
transaction.amount = amount;
transaction.vendorField = std::move(vendorField);

return sign(
sign(
transaction,
std::move(passphrase),
std::move(secondPassphrase));

return transaction;
}

/**/
Expand All @@ -45,6 +48,8 @@ Transaction Builder::buildSecondSignatureRegistration(
std::string secondPassphrase,
const Configuration& configuration) {
Transaction transaction;

transaction.version = 0x01;
transaction.type = defaults::TransactionTypes::SecondSignatureRegistration;
transaction.fee = configuration.getFee(
defaults::TransactionTypes::SecondSignatureRegistration);
Expand All @@ -53,10 +58,12 @@ Transaction Builder::buildSecondSignatureRegistration(
secondPassphrase.c_str());
transaction.asset.signature.publicKey = publicKey.toString();

return sign(
sign(
transaction,
std::move(passphrase),
std::move(secondPassphrase));

return transaction;
}

/**/
Expand All @@ -67,15 +74,19 @@ Transaction Builder::buildDelegateRegistration(
std::string secondPassphrase,
const Configuration& configuration) {
Transaction transaction;

transaction.version = 0x01;
transaction.type = defaults::TransactionTypes::DelegateRegistration;
transaction.fee = configuration.getFee(
defaults::TransactionTypes::DelegateRegistration);
transaction.asset.delegate.username = std::move(username);

return sign(
sign(
transaction,
std::move(passphrase),
std::move(secondPassphrase));

return transaction;
}

/**/
Expand All @@ -86,6 +97,8 @@ Transaction Builder::buildVote(
std::string secondPassphrase,
const Configuration& configuration) {
Transaction transaction;

transaction.version = 0x01;
transaction.type = defaults::TransactionTypes::Vote;
transaction.fee = configuration.getFee(defaults::TransactionTypes::Vote);
transaction.asset.votes = std::move(votes);
Expand All @@ -95,9 +108,11 @@ Transaction Builder::buildVote(
configuration.getNetwork().version());
transaction.recipient = recipient.toString();

return sign(transaction,
sign(transaction,
std::move(passphrase),
std::move(secondPassphrase));

return transaction;
}

/**/
Expand All @@ -110,6 +125,8 @@ Transaction Builder::buildMultiSignatureRegistration(
std::string secondPassphrase,
const Configuration& configuration) {
Transaction transaction;

transaction.version = 0x01;
transaction.type = defaults::TransactionTypes::MultiSignatureRegistration;
transaction.fee = (
keysgroup.size() + 1)
Expand All @@ -123,16 +140,18 @@ Transaction Builder::buildMultiSignatureRegistration(
configuration.getNetwork().version());
transaction.recipient = recipient.toString();

return sign(
sign(
transaction,
std::move(passphrase),
std::move(secondPassphrase));

return transaction;
}

/**/

Transaction Builder::sign(
Transaction transaction,
void Builder::sign(
Transaction& transaction,
std::string passphrase,
std::string secondPassphrase,
const Configuration& configuration) {
Expand All @@ -145,8 +164,6 @@ Transaction Builder::sign(
};

transaction.id = transaction.getId();

return transaction;
}

} // namespace Transactions
Expand Down
8 changes: 5 additions & 3 deletions src/transactions/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ std::map<std::string, std::string> Ark::Crypto::Transactions::Transaction::toArr
snprintf(type, sizeof(type), "%u", this->type);

// Version
snprintf(version, sizeof(version), "%d", this->version);
snprintf(version, sizeof(version), "%u", this->version);

return {
{ "amount", amount },
Expand All @@ -319,7 +319,9 @@ std::map<std::string, std::string> Ark::Crypto::Transactions::Transaction::toArr
std::string Ark::Crypto::Transactions::Transaction::toJson() {
std::map<std::string, std::string> txArray = this->toArray();

const size_t docCapacity = 900;
// Update this value if the size of the JSON document changes
static const size_t docCapacity = 913;

DynamicJsonDocument doc(docCapacity);

// Amount
Expand Down Expand Up @@ -423,7 +425,7 @@ std::string Ark::Crypto::Transactions::Transaction::toJson() {
if (txArray["version"] != "0") {
doc["version"] = atoi(txArray["version"].c_str());
};

char jsonChar[docCapacity];
serializeJson(doc, jsonChar, docCapacity);

Expand Down
20 changes: 20 additions & 0 deletions test/transactions/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ TEST(transactions, transaction_to_array) { // NOLINT

// Type
ASSERT_STREQ("0", tArray["type"].c_str());

// Version
ASSERT_STREQ("1", tArray["version"].c_str());

// Type 1 //
auto secondSignatureRegistration = Builder::buildSecondSignatureRegistration(
Expand Down Expand Up @@ -89,6 +92,9 @@ TEST(transactions, transaction_to_array) { // NOLINT

// Type
ASSERT_STREQ("1", ssArray["type"].c_str());

// Version
ASSERT_STREQ("1", ssArray["version"].c_str());

// Type 2 //
auto delegateRegistration = Builder::buildDelegateRegistration(
Expand Down Expand Up @@ -127,6 +133,9 @@ TEST(transactions, transaction_to_array) { // NOLINT

// Type
ASSERT_STREQ("2", dArray["type"].c_str());

// Version
ASSERT_STREQ("1", dArray["version"].c_str());

// Type 3 //
std::vector<std::string> votes = {
Expand Down Expand Up @@ -171,6 +180,9 @@ TEST(transactions, transaction_to_array) { // NOLINT

// Type
ASSERT_STREQ("3", vArray["type"].c_str());

// Version
ASSERT_STREQ("1", vArray["version"].c_str());
}

/**/
Expand Down Expand Up @@ -209,6 +221,8 @@ TEST(transactions, transaction_to_json) { // NOLINT
ASSERT_LT(tDoc["timestamp"].as<unsigned long>(), 1000000000UL);

ASSERT_EQ(tDoc["type"].as<int>(), 0);

ASSERT_EQ(1, tDoc["version"].as<int>());

// Type 1 //
auto secondSignatureRegistration = Builder::buildSecondSignatureRegistration(
Expand Down Expand Up @@ -245,6 +259,8 @@ TEST(transactions, transaction_to_json) { // NOLINT
ASSERT_LT(ssDoc["timestamp"].as<unsigned long>(), 1000000000UL);

ASSERT_EQ(ssDoc["type"].as<int>(), 1);

ASSERT_EQ(1, ssDoc["version"].as<int>());

// Type 2 //
auto delegateRegistration = Builder::buildDelegateRegistration(
Expand Down Expand Up @@ -279,6 +295,8 @@ TEST(transactions, transaction_to_json) { // NOLINT
ASSERT_LT(dDoc["timestamp"].as<unsigned long>(), 1000000000UL);

ASSERT_EQ(dDoc["type"].as<int>(), 2);

ASSERT_EQ(1, dDoc["version"].as<int>());

// Type 3 //
std::vector<std::string> votes = {
Expand Down Expand Up @@ -323,6 +341,8 @@ TEST(transactions, transaction_to_json) { // NOLINT
ASSERT_LT(vDoc["timestamp"].as<unsigned long>(), 1000000000UL);

ASSERT_EQ(vDoc["type"].as<int>(), 3);

ASSERT_EQ(1, vDoc["version"].as<int>());
};

/**/
Expand Down

0 comments on commit 2c0fe64

Please sign in to comment.