Files
thehub/testing/api/TestLive.cpp
T

262 lines
10 KiB
C++
Raw Permalink Normal View History

2019-05-24 23:32:20 +02:00
/*
* This file is part of the Flowee project
* Copyright (C) 2019 Tom Zander <tomz@freedommail.ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TestLive.h"
#include <Message.h>
#include <streaming/MessageBuilder.h>
#include <streaming/MessageParser.h>
void TestApiLive::testBasic()
{
startHubs();
QCOMPARE((int) con.size(), 1);
2019-06-16 00:23:09 +02:00
Message m = waitForReply(0, Message(Api::APIService, Api::Meta::Version), Api::Meta::VersionReply);
2019-09-13 16:01:15 +02:00
QCOMPARE(m.serviceId(), (int) Api::APIService);
QCOMPARE(m.messageId(), (int) Api::Meta::VersionReply);
2019-05-24 23:32:20 +02:00
Streaming::MessageParser parser(m.body());
while (parser.next() == Streaming::FoundTag) {
if (parser.tag() == Api::GenericByteData) {
QVERIFY(parser.isString());
QVERIFY(QString::fromStdString(parser.stringData()).startsWith("Flowee:"));
return;
}
}
QVERIFY(false); // version not included in reply
}
2019-05-25 14:31:36 +02:00
void TestApiLive::testSendTx()
{
startHubs();
2019-06-10 16:44:17 +02:00
generate100();
2019-05-25 14:31:36 +02:00
Streaming::MessageBuilder builder(Streaming::NoHeader, 100000);
builder.add(Api::BlockChain::BlockHeight, 2);
2019-06-16 00:23:09 +02:00
Message m = waitForReply(0, builder.message(Api::BlockChainService, Api::BlockChain::GetBlock), Api::BlockChain::GetBlockReply);
2019-05-25 14:31:36 +02:00
QCOMPARE(m.serviceId(), (int) Api::BlockChainService);
QCOMPARE(m.messageId(), (int) Api::BlockChain::GetBlockReply);
Streaming::ConstBuffer coinbase;
2019-06-10 16:44:17 +02:00
Streaming::MessageParser parser(m.body());
2019-05-25 14:31:36 +02:00
while (parser.next() == Streaming::FoundTag) {
if (parser.tag() == Api::BlockChain::GenericByteData) {
coinbase = parser.bytesDataBuffer();
break;
}
}
QVERIFY(coinbase.size() > 0);
m_hubs[0].messages.clear();
for (int i = 0; i < 100; ++i) {
builder.add(Api::LiveTransactions::Transaction, coinbase);
con[0].send(builder.message(Api::LiveTransactionService, Api::LiveTransactions::SendTransaction));
}
2019-06-16 00:23:09 +02:00
waitForReply(0, Message(Api::APIService, Api::Meta::Version), Api::Meta::VersionReply);
2019-05-25 14:31:36 +02:00
auto messages = m_hubs[0].messages;
QCOMPARE((int) messages.size(), 101);
2019-09-13 16:01:15 +02:00
for (size_t i = 0; i < 100; ++i) {
2019-05-25 14:31:36 +02:00
// Streaming::MessageParser::debugMessage(messages[i]);
QCOMPARE(messages[i].messageId(), (int) Api::Meta::CommandFailed);
QCOMPARE(messages[i].serviceId(), (int) Api::APIService);
}
}
2019-06-10 16:44:17 +02:00
void TestApiLive::testUtxo()
{
startHubs();
generate100();
Streaming::MessageBuilder builder(Streaming::NoHeader);
builder.add(Api::BlockChain::BlockHeight, 2);
builder.add(Api::BlockChain::Include_TxId, true);
2019-06-16 00:23:09 +02:00
Message m = waitForReply(0, builder.message(Api::BlockChainService, Api::BlockChain::GetBlock), Api::BlockChain::GetBlockReply);
2019-06-10 16:44:17 +02:00
uint256 txid;
Streaming::MessageParser parser(m.body());
while (parser.next() == Streaming::FoundTag) {
if (parser.tag() == Api::BlockChain::TxId) {
QVERIFY(parser.isByteArray());
QCOMPARE(parser.dataLength(), 32);
txid = parser.uint256Data();
break;
}
}
QVERIFY(!txid.IsNull());
builder.add(Api::LiveTransactions::TxId, uint256S("0x1111111111111111111111111111111111111111111111111111111111111111"));
builder.add(Api::LiveTransactions::OutIndex, 1);
builder.add(Api::Separator, true);
builder.add(Api::LiveTransactions::TxId, txid);
builder.add(Api::LiveTransactions::OutIndex, 1);
builder.add(Api::Separator, false); // mix things up a little
builder.add(Api::LiveTransactions::TxId, txid);
builder.add(Api::LiveTransactions::OutIndex, 0);
Message request = builder.message(Api::LiveTransactionService, Api::LiveTransactions::IsUnspent);
2019-06-16 00:23:09 +02:00
m = waitForReply(0, request, Api::LiveTransactions::IsUnspentReply);
2019-06-10 16:44:17 +02:00
QCOMPARE(m.serviceId(), (int) Api::LiveTransactionService);
2019-09-13 16:01:15 +02:00
QCOMPARE(m.messageId(), (int) Api::LiveTransactions::IsUnspentReply);
2019-06-10 16:44:17 +02:00
parser = Streaming::MessageParser(m.body());
int index = 0;
int index2 = 0;
bool seenBlockHeight = false;
bool seenOffsetInBlock = false;
2019-11-30 18:56:25 +01:00
bool seenOutIndex = false;
2019-06-10 16:44:17 +02:00
while (parser.next() == Streaming::FoundTag) {
if (parser.tag() == Api::LiveTransactions::UnspentState) {
switch (index) {
case 0:
case 1:
QCOMPARE(parser.isBool(), true);
QCOMPARE(parser.boolData(), false);
break;
case 2:
QCOMPARE(parser.isBool(), true);
QCOMPARE(parser.boolData(), true);
break;
}
} else if (parser.tag() == Api::Separator ) {
QCOMPARE(seenBlockHeight, index == 2);
QCOMPARE(seenOffsetInBlock, index == 2);
QCOMPARE(index++, index2++);
} else if (parser.tag() == Api::LiveTransactions::BlockHeight) {
QCOMPARE(index, 2);
QVERIFY(parser.isInt());
QCOMPARE(parser.intData(), 2);
seenBlockHeight = true;
} else if (parser.tag() == Api::LiveTransactions::OffsetInBlock) {
QCOMPARE(index, 2);
QVERIFY(parser.isInt());
QCOMPARE(parser.intData(), 81);
seenOffsetInBlock = true;
2019-11-30 18:56:25 +01:00
} else if (parser.tag() == Api::LiveTransactions::OutIndex) {
QCOMPARE(index, 2);
QVERIFY(parser.isInt());
QCOMPARE(parser.intData(), 0);
seenOutIndex = true;
2019-06-10 16:44:17 +02:00
} else {
logFatal() << "tag that doesn't belong:" << parser.tag();
QVERIFY(false);
}
}
QCOMPARE(index, 2);
QCOMPARE(seenBlockHeight, true);
QCOMPARE(seenOffsetInBlock, true);
2019-11-30 18:56:25 +01:00
QCOMPARE(seenOutIndex, true);
2019-06-10 16:44:17 +02:00
request.setMessageId(Api::LiveTransactions::GetUnspentOutput);
con[0].send(request);
2019-06-16 00:23:09 +02:00
m = waitForReply(0, request, Api::LiveTransactions::GetUnspentOutputReply);
2019-09-13 16:01:15 +02:00
index = index2 = 0;
2019-06-10 16:44:17 +02:00
parser = Streaming::MessageParser(m.body());
seenBlockHeight = false;
seenOffsetInBlock = false;
2019-11-30 18:56:25 +01:00
seenOutIndex = false;
2019-06-10 16:44:17 +02:00
bool seenAmount = false;
bool seenOutputScript = false;
while (parser.next() == Streaming::FoundTag) {
if (parser.tag() == Api::LiveTransactions::UnspentState) {
switch (index) {
case 0:
case 1:
QCOMPARE(parser.isBool(), true);
QCOMPARE(parser.boolData(), false);
break;
case 2:
QCOMPARE(parser.isBool(), true);
QCOMPARE(parser.boolData(), true);
break;
}
} else if (parser.tag() == Api::LiveTransactions::BlockHeight) {
QCOMPARE(index, 2);
QVERIFY(parser.isInt());
QCOMPARE(parser.intData(), 2);
seenBlockHeight = true;
} else if (parser.tag() == Api::LiveTransactions::OffsetInBlock) {
QCOMPARE(index, 2);
QVERIFY(parser.isInt());
QCOMPARE(parser.intData(), 81);
seenOffsetInBlock = true;
2019-11-30 18:56:25 +01:00
} else if (parser.tag() == Api::LiveTransactions::OutIndex) {
QCOMPARE(index, 2);
QVERIFY(parser.isInt());
QCOMPARE(parser.intData(), 0);
seenOutIndex = true;
2019-06-10 16:44:17 +02:00
} else if (parser.tag() == Api::LiveTransactions::Amount) {
QCOMPARE(index, 2);
QVERIFY(parser.isLong());
QCOMPARE(parser.longData(), (uint64_t) 5000000000);
seenAmount = true;
} else if (parser.tag() == Api::LiveTransactions::OutputScript) {
QCOMPARE(index, 2);
QVERIFY(parser.isByteArray());
seenOutputScript = true;
} else if (parser.tag() == Api::Separator ) {
QCOMPARE(seenBlockHeight, index == 2);
QCOMPARE(seenOffsetInBlock, index == 2);
QCOMPARE(seenAmount, index == 2);
QCOMPARE(seenOutputScript, index == 2);
QCOMPARE(index++, index2++);
} else {
logFatal() << "tag that doesn't belong:" << parser.tag();
QVERIFY(false);
}
}
QCOMPARE(index, 2);
QCOMPARE(seenBlockHeight, true);
QCOMPARE(seenOffsetInBlock, true);
2019-11-30 18:56:25 +01:00
QCOMPARE(seenOutIndex, true);
2019-06-10 16:44:17 +02:00
2019-09-13 16:01:15 +02:00
// also check fetch using blockheight / offset instead of txid
builder.add(Api::LiveTransactions::BlockHeight, 2);
builder.add(Api::LiveTransactions::OffsetInBlock, 81);
builder.add(Api::LiveTransactions::OutIndex, 0);
request = builder.message(Api::LiveTransactionService, Api::LiveTransactions::IsUnspent);
m = waitForReply(0, request, Api::LiveTransactions::IsUnspentReply);
Streaming::MessageParser::debugMessage(m);
QCOMPARE(m.serviceId(), (int) Api::LiveTransactionService);
QCOMPARE(m.messageId(), (int) Api::LiveTransactions::IsUnspentReply);
parser = Streaming::MessageParser(m.body());
while (parser.next() == Streaming::FoundTag) {
if (parser.tag() == Api::LiveTransactions::UnspentState) {
QCOMPARE(parser.isBool(), true);
QCOMPARE(parser.boolData(), true);
}
}
2019-06-10 16:44:17 +02:00
}
Streaming::ConstBuffer TestApiLive::generate100(int nodeId)
{
2019-06-16 00:23:09 +02:00
Message m = waitForReply(nodeId, Message(Api::UtilService, Api::Util::CreateAddress), Api::Util::CreateAddressReply);
2019-06-10 16:44:17 +02:00
Streaming::MessageParser parser(m.body());
Streaming::ConstBuffer address;
while (parser.next() == Streaming::FoundTag) {
if (parser.tag() == Api::Util::BitcoinP2PKHAddress) {
2019-06-10 16:44:17 +02:00
address = parser.bytesDataBuffer();
break;
}
}
Streaming::MessageBuilder builder(Streaming::NoHeader);
builder.add(Api::RegTest::BitcoinP2PKHAddress, address);
2019-06-10 16:44:17 +02:00
builder.add(Api::RegTest::Amount, 101);
2019-06-16 00:23:09 +02:00
m = waitForReply(nodeId, builder.message(Api::RegTestService, Api::RegTest::GenerateBlock), Api::RegTest::GenerateBlockReply);
2019-06-10 16:44:17 +02:00
Q_ASSERT(m.serviceId() == Api::RegTestService);
return address;
}