Skip to content

Commit

Permalink
removeExtrinsic implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
gregzaitsev committed Jul 30, 2019
1 parent 4842931 commit 715c6cb
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 60 deletions.
59 changes: 20 additions & 39 deletions src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -624,11 +624,7 @@ string CPolkaApi::getStorage(const string &jsonPrm, const string &module, const
Json query = Json::object{{"method", "state_getStorage"}, {"params", Json::array{key, headHash->hash}}};
Json response = _jsonRpc->request(query);

// Strip quotes
string retval = response.dump();
if (retval[0] == '\"')
retval = retval.substr(1, retval.length() - 2);
return retval;
return response.string_value();
}

string CPolkaApi::getStorageHash(const string &jsonPrm, const string &module, const string &variable) {
Expand All @@ -639,11 +635,7 @@ string CPolkaApi::getStorageHash(const string &jsonPrm, const string &module, co
Json query = Json::object{{"method", "state_getStorageHash"}, {"params", Json::array{key, headHash->hash}}};
Json response = _jsonRpc->request(query);

// Strip quotes
string retval = response.dump();
if (retval[0] == '\"')
retval = retval.substr(1, retval.length() - 2);
return retval;
return response.string_value();
}

int CPolkaApi::getStorageSize(const string &jsonPrm, const string &module, const string &variable) {
Expand All @@ -654,11 +646,7 @@ int CPolkaApi::getStorageSize(const string &jsonPrm, const string &module, const
Json query = Json::object{{"method", "state_getStorageSize"}, {"params", Json::array{key, headHash->hash}}};
Json response = _jsonRpc->request(query);

// Strip quotes
string retval = response.dump();
if (retval[0] == '\"')
retval = retval.substr(1, retval.length() - 2);
return atoi(retval.c_str());
return response.int_value();
}

int CPolkaApi::pendingExtrinsics(GenericExtrinsic *buf, int bufferSize) {
Expand Down Expand Up @@ -727,11 +715,7 @@ string CPolkaApi::getChildKeys(const string &childStorageKey, const string &stor
{"params", Json::array{childStorageKey, storageKey, headHash->hash}}};
Json response = _jsonRpc->request(query);

// Strip quotes
string retval = response.dump();
if (retval[0] == '\"')
retval = retval.substr(1, retval.length() - 2);
return retval;
return response.string_value();
}

string CPolkaApi::getChildStorage(const string &childStorageKey, const string &storageKey) {
Expand All @@ -742,11 +726,7 @@ string CPolkaApi::getChildStorage(const string &childStorageKey, const string &s
{"params", Json::array{childStorageKey, storageKey, headHash->hash}}};
Json response = _jsonRpc->request(query);

// Strip quotes
string retval = response.dump();
if (retval[0] == '\"')
retval = retval.substr(1, retval.length() - 2);
return retval;
return response.string_value();
}

string CPolkaApi::getChildStorageHash(const string &childStorageKey, const string &storageKey) {
Expand All @@ -757,11 +737,7 @@ string CPolkaApi::getChildStorageHash(const string &childStorageKey, const strin
{"params", Json::array{childStorageKey, storageKey, headHash->hash}}};
Json response = _jsonRpc->request(query);

// Strip quotes
string retval = response.dump();
if (retval[0] == '\"')
retval = retval.substr(1, retval.length() - 2);
return retval;
return response.string_value();
}

int CPolkaApi::getChildStorageSize(const string &childStorageKey, const string &storageKey) {
Expand All @@ -772,11 +748,7 @@ int CPolkaApi::getChildStorageSize(const string &childStorageKey, const string &
{"params", Json::array{childStorageKey, storageKey, headHash->hash}}};
Json response = _jsonRpc->request(query);

// Strip quotes
string retval = response.dump();
if (retval[0] == '\"')
retval = retval.substr(1, retval.length() - 2);
return atoi(retval.c_str());
return response.int_value();
}

string CPolkaApi::stateCall(const string &name, const string &data, const string &hash) {
Expand Down Expand Up @@ -1076,7 +1048,7 @@ void CPolkaApi::submitAndSubcribeExtrinsic(uint8_t *encodedMethodBytes, unsigned
string module, string method, string sender, string privateKey,
std::function<void(string)> callback) {

_logger->info("=== Starting a Invoke Extrinsic ===");
_logger->info("=== Started Invoking Extrinsic ===");

// Get account Nonce
unsigned long nonce = getAccountNonce(sender);
Expand Down Expand Up @@ -1181,7 +1153,7 @@ void CPolkaApi::submitAndSubcribeExtrinsic(uint8_t *encodedMethodBytes, unsigned
string CPolkaApi::submitExtrinsic(uint8_t *encodedMethodBytes, unsigned int encodedMethodBytesSize, string module,
string method, string sender, string privateKey) {

_logger->info("=== Starting a Invoke Extrinsic ===");
_logger->info("=== Started Invoking Extrinsic ===");

// Get account Nonce
unsigned long nonce = getAccountNonce(sender);
Expand Down Expand Up @@ -1281,7 +1253,17 @@ string CPolkaApi::submitExtrinsic(uint8_t *encodedMethodBytes, unsigned int enco
// Send == Subscribe callback to completion
Json response = _jsonRpc->request(query);

return response.dump();
return response.string_value();
}

bool CPolkaApi::removeExtrinsic(string extrinsicHash) {
Json query = Json::object{{"method", "author_removeExtrinsic"}, {"params", Json::array{extrinsicHash}}};
Json response = _jsonRpc->request(query);

if (response.is_null())
throw ApplicationException("Not supported");

return false;
}

void CPolkaApi::signAndSendTransfer(string sender, string privateKey, string recipient, uint128 amount,
Expand Down Expand Up @@ -1338,7 +1320,6 @@ void CPolkaApi::signAndSendTransfer(string sender, string privateKey, string rec
}

Json query = Json::object{{"method", "author_submitAndWatchExtrinsic"}, {"params", Json::array{teStr}}};
cout << query.dump();

// Send == Subscribe callback to completion
_transactionCompletionSubscriber = callback;
Expand Down
1 change: 1 addition & 0 deletions src/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class CPolkaApi : public IApplication, IWebSocketMessageObserver {

virtual string submitExtrinsic(uint8_t *encodedMethodBytes, unsigned int encodedMethodBytesSize, string module,
string method, string sender, string privateKey);
virtual bool removeExtrinsic(string extrinsicHash);

virtual int subscribeBlockNumber(std::function<void(long long)> callback);
virtual int unsubscribeBlockNumber();
Expand Down
12 changes: 9 additions & 3 deletions src/interfaces/iapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,18 +312,24 @@ class IApplication {
/**
* Submit a fully formatted extrinsic for block inclusion
*
*
* @param encodedMethodBytesSize - parametrs size in bytes
* @param module - invokable module name
* @param method - invokable module name
* @param sender - sender address
* @param privateKey - sender private key
*
* @return Node responce
* @return Extrinsic hash
*/
virtual string submitExtrinsic(uint8_t *encodedMethodBytes, unsigned int encodedMethodBytesSize, string module,
string method, string sender, string privateKey) = 0;

/**
* Remove given extrinsic from the pool and temporarily ban it to prevent reimporting
*
* @param extrinsicHash - hash of extrinsic as returned by submitExtrisic
* @return Operation result
*/
virtual bool removeExtrinsic(string extrinsicHash) = 0;

/**
* Subscribe to most recent block number. Only one subscription at a time is allowed. If a subscription already
* exists, old subscription will be discarded and replaced with the new one. Until unsubscribeBlockNumber method is
Expand Down
65 changes: 65 additions & 0 deletions test/remove_extrinsic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "../src/polkadot.h"

class MetadataUtils {
public:
static unsigned int GetModuleIndex(string moduleName, Metadata metadata) { return 0; };
};

int main(int argc, char *argv[]) {

// This is a manual test due to lack of test DOTs
if (argc < 4) {
cout << "This is intended to be a manual test" << endl;
cout << "Usage: ";
cout << argv[0] << " <sender address> <recipient address> <amount in fDOTs> <sender private key (hex)>" << endl;
cout << "success" << endl;
return 0;
}

// Extract cli parameters
string senderAddr(argv[1]);
string recipientAddr(argv[2]);
string amountStr(argv[3]);
string senderPrivateKeyStr(argv[4]);

// Connect
auto app = polkadot::api::getInstance()->app();
app->connect();

// Send extrinsic without subscribing to updates
auto receiverPublicKey = AddressUtils::getPublicKeyFromAddr(recipientAddr);
uint8_t receiverBytes[SR25519_PUBLIC_SIZE];
memcpy(receiverBytes, receiverPublicKey.bytes, SR25519_PUBLIC_SIZE);

int mmWrittenLength = 0;
u_int8_t buf2[2048];

// Receiving address public key
memcpy(buf2 + mmWrittenLength, receiverBytes, SR25519_PUBLIC_SIZE);
mmWrittenLength += SR25519_PUBLIC_SIZE;

// Compact-encode amount
auto compactAmount = scale::encodeCompactInteger(stoll(amountStr));

// Amount
mmWrittenLength += scale::writeCompactToBuf(compactAmount, buf2 + mmWrittenLength);

string exHash =
app->submitExtrinsic(buf2, mmWrittenLength, "balances", "transfer", senderAddr, senderPrivateKeyStr);

cout << endl << "Sent extrinsic with hash: " << exHash << endl;
cout << "Now let's try to cancel it... " << endl << endl;

try {
app->removeExtrinsic(exHash);
} catch (ApplicationException) {
cout << endl << "Yeah, looks like canceling is not yet supported" << endl << endl;
}

// Unsubscribe and close connection
app->disconnect();

cout << "success" << endl;

return 0;
}
22 changes: 4 additions & 18 deletions test/submit_extrinsicNWatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,11 @@ int main(int argc, char *argv[]) {
string amountStr(argv[3]);
string senderPrivateKeyStr(argv[4]);

JsonRpcParams params;
params.jsonrpcVersion = "2.0";
// Connect
auto app = polkadot::api::getInstance()->app();
app->connect();

EasyLogger logger;
CJsonRpc jsonRpc(CWebSocketClient::getInstance(&logger), &logger, params);

CPolkaApi app(&logger, &jsonRpc);
app.connect();

// Subscribe to account nonce updates
// Submit extrinsic and watch
bool done = false;

auto receiverPublicKey = AddressUtils::getPublicKeyFromAddr(recipientAddr);
Expand Down Expand Up @@ -60,15 +55,6 @@ int main(int argc, char *argv[]) {
done = true;
});

// example
// app.submitAndSubcribeExtrinsic(buf2, mmWrittenLength, "balances", "transfer",
// "5GuuxfuxbvaiwteUrV9U7Mj2Fz7TWK84WhLaZdMMJRvSuzr4",
// "0xa81056d713af1ff17b599e60d287952e89301b5208324a0529b62dc7369c745defc9c8dd67b7c59b201bc164163a8978d40010c22743db142a47f2e064480d4b",
// [&](Json response){
// cout << endl << endl << "Response json: " << response.dump() << endl;
// done = true;
// });

// Wait until transaction is mined
while (!done)
usleep(10000);
Expand Down

0 comments on commit 715c6cb

Please sign in to comment.