Skip to content

Commit 02af41d

Browse files
Merge pull request #121 from thibault-martinez/replay-bundle
replayBundle & replayBundleResponse
2 parents 897d4f1 + c67f363 commit 02af41d

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

include/API/Extended.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <API/Responses/getBundleResponse.hpp>
3535
#include <API/Responses/getNewAddressesResponse.hpp>
3636
#include <API/Responses/getTransfersResponse.hpp>
37+
#include <API/Responses/replayBundleResponse.hpp>
3738
#include <API/Responses/sendTransferResponse.hpp>
3839
#include <API/Responses/storeTransactionsResponse.hpp>
3940
#include <Crypto/SpongeFactory.hpp>
@@ -320,6 +321,17 @@ class Extended : public Core {
320321
const Type::Trytes& remainderAddress,
321322
const std::vector<std::string>& signatureFragments) const;
322323

324+
/**
325+
* Replays a transfer by doing Proof of Work again.
326+
*
327+
* @param transaction The transaction.
328+
* @param depth The depth.
329+
* @param minWeightMagnitude The minimum weight magnitude.
330+
* @return Analyzed Transaction objects.
331+
*/
332+
replayBundleResponse replayBundle(const Type::Trytes& transaction, int depth,
333+
int minWeightMagnitude);
334+
323335
private:
324336
/**
325337
* Generates a new address.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// MIT License
3+
//
4+
// Copyright (c) 2017 Thibault Martinez
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in all
14+
// copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
//
24+
//
25+
26+
#pragma once
27+
28+
#include <API/Responses/genericResponse.hpp>
29+
30+
#include <vector>
31+
32+
class replayBundleResponse : public genericResponse {
33+
public:
34+
replayBundleResponse(const std::vector<bool>& successful, long duration);
35+
virtual ~replayBundleResponse() = default;
36+
37+
public:
38+
/**
39+
* @return successful operations.
40+
*/
41+
const std::vector<bool>& getSuccessfully() const;
42+
43+
private:
44+
std::vector<bool> successfully_;
45+
};

source/API/Extended.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,28 @@ Extended::addRemainder(const Type::Trytes& seed, const unsigned int& security,
617617
throw Errors::IllegalState("Not enough balance");
618618
}
619619

620+
replayBundleResponse
621+
Extended::replayBundle(const Type::Trytes& transaction, int depth, int minWeightMagnitude) {
622+
Utils::StopWatch stopWatch;
623+
624+
auto bundleResponse = getBundle(transaction);
625+
626+
std::vector<Type::Trytes> bundleTrytes;
627+
for (const auto& trx : bundleResponse.getTransactions()) {
628+
bundleTrytes.push_back(trx.toTrytes());
629+
}
630+
631+
auto trxs = sendTrytes(bundleTrytes, depth, minWeightMagnitude);
632+
633+
std::vector<bool> successful;
634+
for (const auto& trx : trxs) {
635+
auto response = findTransactionsByBundles({ trx.getBundle() });
636+
successful.push_back(!response.getHashes().empty());
637+
}
638+
639+
return { successful, stopWatch.getElapsedTimeMiliSeconds().count() };
640+
}
641+
620642
/*
621643
* Private methods.
622644
*/
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// MIT License
3+
//
4+
// Copyright (c) 2017 Thibault Martinez
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in all
14+
// copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
//
24+
//
25+
26+
#include <API/Responses/replayBundleResponse.hpp>
27+
28+
replayBundleResponse::replayBundleResponse(const std::vector<bool>& successful, long duration)
29+
: genericResponse(duration), successfully_(successful) {
30+
}
31+
32+
const std::vector<bool>&
33+
replayBundleResponse::getSuccessfully() const {
34+
return successfully_;
35+
}

0 commit comments

Comments
 (0)