From 1887ab3b5756f8d11926c0c4212b95699d006c3d Mon Sep 17 00:00:00 2001 From: sleepdefic1t Date: Mon, 24 Feb 2020 09:43:31 -0800 Subject: [PATCH 1/3] chore(builder): move config to initial constructor - move network configuration to the initial builder constructor. - e.g. `builder::Transfer().build(config);` -> `builder::Transfer(config).build();` - update examples. --- examples/ESP32/ESP32.ino | 4 +-- examples/ESP8266/ESP8266.ino | 4 +-- src/transactions/builders/common.hpp | 27 +++++++------------ .../builders/delegate_registration.hpp | 3 ++- .../builders/delegate_resignation.hpp | 3 ++- src/transactions/builders/htlc_claim.hpp | 3 ++- src/transactions/builders/htlc_lock.hpp | 3 ++- src/transactions/builders/htlc_refund.hpp | 3 ++- src/transactions/builders/ipfs.hpp | 3 ++- src/transactions/builders/multi_payment.hpp | 3 ++- .../builders/second_signature.hpp | 3 ++- src/transactions/builders/transfer.hpp | 3 ++- src/transactions/builders/vote.hpp | 3 ++- 13 files changed, 33 insertions(+), 32 deletions(-) diff --git a/examples/ESP32/ESP32.ino b/examples/ESP32/ESP32.ino index d7eff74f..942929c7 100644 --- a/examples/ESP32/ESP32.ino +++ b/examples/ESP32/ESP32.ino @@ -181,12 +181,12 @@ void createBridgechainTransaction() { const Configuration cfg(BridgechainNetwork); // Use the Transaction Builder to make a transaction. - const auto bridgechainTransaction = builder::Transfer() + const auto bridgechainTransaction = builder::Transfer(cfg) .recipientId("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib") .vendorField("this is a custom bridgechain transaction") .sign(Passphrase) .secondSign(SecondPassphrase) - .build(cfg); + .build(); // Create and Print the Json representation of the Transaction. const auto transactionJson = bridgechainTransaction.toJson(); diff --git a/examples/ESP8266/ESP8266.ino b/examples/ESP8266/ESP8266.ino index 6a7288b1..fcee5463 100644 --- a/examples/ESP8266/ESP8266.ino +++ b/examples/ESP8266/ESP8266.ino @@ -160,11 +160,11 @@ void createBridgechainTransaction() { const Configuration cfg(BridgechainNetwork); // Use the Transaction Builder to make a transaction. - const auto bridgechainTransaction = builder::Transfer() + const auto bridgechainTransaction = builder::Transfer(cfg) .recipientId("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib") .vendorField("this is a custom bridgechain transaction") .sign(Passphrase) - .build(cfg); + .build(); // Create and Print the Json representation of the Transaction. const auto transactionJson = bridgechainTransaction.toJson(); diff --git a/src/transactions/builders/common.hpp b/src/transactions/builders/common.hpp index a2a09206..c526aea0 100644 --- a/src/transactions/builders/common.hpp +++ b/src/transactions/builders/common.hpp @@ -164,28 +164,19 @@ template class Common { } //////////////////////////////////////////////////////////////////////////// - // Build - // - // If calling 'builder::sign()' before calling 'build()', - // ensure the Fees and Network are properly configured first. - // - // --- - Transaction &build(const Configuration &config = {}) { - // if the transaction fee is the default of '0', use the config fees. - if (this->transaction.data.fee == 0ULL) { - this->transaction.data.fee = config.getFee(this->transaction.data.type); - } - - // Use the configuration network version if it's different. - if (this->transaction.data.network == Devnet.version && - config.getNetwork().version != Devnet.version) { - this->transaction.data.network = config.getNetwork().version; - } - + // Finish the build pattern + // !! should always be the last call !! + Transaction &build() { return this->transaction; } protected: + //////////////////////////////////////////////////////////////////////////// + void configure(const Configuration &config) { + this->transaction.data.fee = config.getFee(this->transaction.data.type); + this->transaction.data.network = config.getNetwork().version; + } + //////////////////////////////////////////////////////////////////////////// Transaction transaction; }; diff --git a/src/transactions/builders/delegate_registration.hpp b/src/transactions/builders/delegate_registration.hpp index 5a22903e..1aaff8e1 100644 --- a/src/transactions/builders/delegate_registration.hpp +++ b/src/transactions/builders/delegate_registration.hpp @@ -63,8 +63,9 @@ class DelegateRegistration : public Common { } //////////////////////////////////////////////////////////////////////////// - DelegateRegistration() { + DelegateRegistration(const Configuration &config = {}) { this->transaction.data.type = DELEGATE_REGISTRATION_TYPE; + this->configure(config); } }; diff --git a/src/transactions/builders/delegate_resignation.hpp b/src/transactions/builders/delegate_resignation.hpp index ad74249c..e641bddb 100644 --- a/src/transactions/builders/delegate_resignation.hpp +++ b/src/transactions/builders/delegate_resignation.hpp @@ -24,8 +24,9 @@ class DelegateResignation; //////////////////////////////////////////////////////////////////////////////// class DelegateResignation : public Common { public: - DelegateResignation() { + DelegateResignation(const Configuration &config = {}) { this->transaction.data.type = DELEGATE_RESIGNATION_TYPE; + this->configure(config); } }; diff --git a/src/transactions/builders/htlc_claim.hpp b/src/transactions/builders/htlc_claim.hpp index c29e0ba6..f2c61546 100644 --- a/src/transactions/builders/htlc_claim.hpp +++ b/src/transactions/builders/htlc_claim.hpp @@ -50,8 +50,9 @@ class HtlcClaim : public Common { } //////////////////////////////////////////////////////////////////////////// - HtlcClaim() { + HtlcClaim(const Configuration &config = {}) { this->transaction.data.type = HTLC_CLAIM_TYPE; + this->configure(config); } }; diff --git a/src/transactions/builders/htlc_lock.hpp b/src/transactions/builders/htlc_lock.hpp index 0ed9ce70..d56760dd 100644 --- a/src/transactions/builders/htlc_lock.hpp +++ b/src/transactions/builders/htlc_lock.hpp @@ -88,8 +88,9 @@ class HtlcLock : public Common { } //////////////////////////////////////////////////////////////////////////// - HtlcLock() { + HtlcLock(const Configuration &config = {}) { this->transaction.data.type = HTLC_LOCK_TYPE; + this->configure(config); } }; diff --git a/src/transactions/builders/htlc_refund.hpp b/src/transactions/builders/htlc_refund.hpp index 1b6ee16d..9628f6bd 100644 --- a/src/transactions/builders/htlc_refund.hpp +++ b/src/transactions/builders/htlc_refund.hpp @@ -40,8 +40,9 @@ class HtlcRefund : public Common { } //////////////////////////////////////////////////////////////////////////// - HtlcRefund() { + HtlcRefund(const Configuration &config = {}) { this->transaction.data.type = HTLC_REFUND_TYPE; + this->configure(config); } }; diff --git a/src/transactions/builders/ipfs.hpp b/src/transactions/builders/ipfs.hpp index cd1863c0..c3395ec3 100644 --- a/src/transactions/builders/ipfs.hpp +++ b/src/transactions/builders/ipfs.hpp @@ -40,8 +40,9 @@ class Ipfs : public Common { } //////////////////////////////////////////////////////////////////////////// - Ipfs() { + Ipfs(const Configuration &config = {}) { this->transaction.data.type = IPFS_TYPE; + this->configure(config); } }; diff --git a/src/transactions/builders/multi_payment.hpp b/src/transactions/builders/multi_payment.hpp index 1374d082..9062aba1 100644 --- a/src/transactions/builders/multi_payment.hpp +++ b/src/transactions/builders/multi_payment.hpp @@ -67,8 +67,9 @@ class MultiPayment : public Common { } //////////////////////////////////////////////////////////////////////////// - MultiPayment() { + MultiPayment(const Configuration &config = {}) { this->transaction.data.type = MULTI_PAYMENT_TYPE; + this->configure(config); } }; diff --git a/src/transactions/builders/second_signature.hpp b/src/transactions/builders/second_signature.hpp index d31f432b..2b87c672 100644 --- a/src/transactions/builders/second_signature.hpp +++ b/src/transactions/builders/second_signature.hpp @@ -42,8 +42,9 @@ class SecondSignature : public Common { } //////////////////////////////////////////////////////////////////////////// - SecondSignature() { + SecondSignature(const Configuration &config = {}) { this->transaction.data.type = SECOND_SIGNATURE_TYPE; + this->configure(config); } }; diff --git a/src/transactions/builders/transfer.hpp b/src/transactions/builders/transfer.hpp index b3b4b052..faef7bf2 100644 --- a/src/transactions/builders/transfer.hpp +++ b/src/transactions/builders/transfer.hpp @@ -72,8 +72,9 @@ class Transfer : public Common { } //////////////////////////////////////////////////////////////////////////// - Transfer() { + Transfer(const Configuration &config = {}) { this->transaction.data.type = TRANSFER_TYPE; + this->configure(config); } }; diff --git a/src/transactions/builders/vote.hpp b/src/transactions/builders/vote.hpp index 7a2a975c..216a3a35 100644 --- a/src/transactions/builders/vote.hpp +++ b/src/transactions/builders/vote.hpp @@ -42,8 +42,9 @@ class Vote : public Common { } //////////////////////////////////////////////////////////////////////////// - Vote() { + Vote(const Configuration &config = {}) { this->transaction.data.type = VOTE_TYPE; + this->configure(config); } }; From 983f595a602305cf4ad9f4b57bc907c5e13cfd01 Mon Sep 17 00:00:00 2001 From: sleepdefic1t Date: Mon, 24 Feb 2020 09:47:37 -0800 Subject: [PATCH 2/3] chore(builder): enable passing config arguments to builder Enable passing Configuration constructor arguments to the builder constructor. - rm `explicit` in `Configuration` constructors. --- src/common/configuration.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common/configuration.hpp b/src/common/configuration.hpp index b35284f4..e9818519 100644 --- a/src/common/configuration.hpp +++ b/src/common/configuration.hpp @@ -24,8 +24,8 @@ class Configuration : public managers::NetworkManager, //////////////////////////////////////////////////////////////////////////// // Default initialization: ARK Devnet w/StaticFees Configuration() = default; - explicit Configuration(const Network &network); - explicit Configuration(const FeePolicy &policy); + Configuration(const Network &network); + Configuration(const FeePolicy &policy); Configuration(const Network &network, const FeePolicy &policy); //////////////////////////////////////////////////////////////////////////// From 3bd8336d145634bf2c8542a1684423b9cf8204d2 Mon Sep 17 00:00:00 2001 From: sleepdefic1t Date: Mon, 24 Feb 2020 09:48:43 -0800 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7b1fad8..9aa3abf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,15 +10,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Added - added Delegate Resignation Builder ([#208]) +### Changed +- improved transaction builder ([#217]) + ## [1.0.0-arduino] - 2020-02-13 +### Added +- added AIP-11 support for Core v.2.6 Transactions ([#198]) + ### Changed - removed use of monolithic `arkCrypto.h` header ([#190]) - break up unit tests to support platforms with limited RAM ([#172]) -### Added -- added AIP-11 support for Core v.2.6 Transactions ([#198]) - ### Fixed - fixed `transaction::to_json` tests on ESP8266 ([#180]) - fixed `transaction::to_array` tests on ESP8266 ([#178]) @@ -112,4 +115,5 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. [1.0.0-arduino]: https://github.com/ArkEcosystem/cpp-crypto/compare/0.7.0-arduino...1.0.0-arduino [1.0.0]: https://github.com/ArkEcosystem/cpp-crypto/compare/0.7.0-arduino...1.0.0 [#208]: https://github.com/ArkEcosystem/cpp-crypto/pull/208 +[#217]: https://github.com/ArkEcosystem/cpp-crypto/pull/217 [unreleased]: https://github.com/ArkEcosystem/cpp-crypto/compare/1.0.0-arduino...develop