Skip to content

Commit

Permalink
New user account creation in pocketnet_social_tests and code formating
Browse files Browse the repository at this point in the history
  • Loading branch information
tawmaz committed Mar 21, 2022
1 parent 0221ddc commit 0b3a87c
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/Makefile.bench.include
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ if ENABLE_WALLET
bench_bench_pocketcoin_SOURCES += bench/wallet_balance.cpp
endif

bench_bench_pocketcoin_LDADD += $(BOOST_LIBS) $(BDB_LIBS) $(MINIUPNPC_LIBS) $(EVENT_PTHREADS_LIBS) $(EVENT_LIBS) $(ZMQ_LIBS) $(CRYPTO_LIBS) $(SSL_LIBS)
bench_bench_pocketcoin_LDADD += $(BOOST_LIBS) $(BDB_LIBS) $(EVENT_PTHREADS_LIBS) $(EVENT_LIBS) $(MINIUPNPC_LIBS) $(SQLITE_LIBS)
bench_bench_pocketcoin_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS) $(PTHREAD_FLAGS)

CLEAN_POCKETCOIN_BENCH = bench/*.gcda bench/*.gcno $(GENERATED_BENCH_FILES)
Expand Down
2 changes: 1 addition & 1 deletion src/bench/bench.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ class BenchRunner
#define BENCHMARK(n) \
benchmark::BenchRunner BOOST_PP_CAT(bench_, BOOST_PP_CAT(__LINE__, n))(BOOST_PP_STRINGIZE(n), n);

#endif // BITCOIN_BENCH_BENCH_H
#endif // POCKETCOIN_BENCH_BENCH_H
14 changes: 12 additions & 2 deletions src/bench/bench_pocketcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@
#include <crypto/sha256.h>
#include <util/strencodings.h>
#include <util/system.h>
#include <pocketdb/SQLiteDatabase.h>
#include <chainparams.h>


#include <chrono>
#include <cstdint>
#include <iostream>
#include <sstream>
#include <vector>

#include <script/sigcache.h>
#include "pocketdb/SQLiteDatabase.h"
#include "pocketdb/pocketnet.h"


static const char* DEFAULT_BENCH_FILTER = ".*";
static constexpr int64_t DEFAULT_MIN_TIME_MS{10};
Expand Down
31 changes: 4 additions & 27 deletions src/bench/block_assemble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@

#include <bench/bench.h>
#include <consensus/validation.h>
#include <consensus/merkle.h>
#include <crypto/sha256.h>
#include <test/util/mining.h>
#include <test/util/setup_common.h>
#include <test/util/wallet.h>
#include <txmempool.h>
#include <validation.h>
#include <miner.h>
#include <pow.h>
#include <consensus/merkle.h>


#include <vector>
#include "pocketdb/services/Serializer.h"


static std::shared_ptr<CBlock> StakeBlock(CTxMemPool &mempool, const CScript& coinbase_scriptPubKey)
static std::shared_ptr<CBlock> StakeBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
{
auto block = std::make_shared<CBlock>(
BlockAssembler(mempool, Params())
BlockAssembler{*Assert(node.mempool), Params()}
.CreateNewBlock(coinbase_scriptPubKey, /*fProofOfStake */ true)
->block);

Expand All @@ -33,33 +32,11 @@ static std::shared_ptr<CBlock> StakeBlock(CTxMemPool &mempool, const CScript& co
}


static CTxIn MineBlock(NodeContext &node, const CScript& coinbase_scriptPubKey)
{
auto block = PrepareBlock(node, coinbase_scriptPubKey);

while (!CheckProofOfWork(block->GetHash(), block->nBits, Params().GetConsensus(), 0)) {
++block->nNonce;
assert(block->nNonce);
}

BlockValidationState state;
UniValue pocketData;
bool ignored;
auto[deserializeOk, pocketBlock] = PocketServices::Serializer::DeserializeBlock(*block);
auto pocketBlockRef = std::make_shared<PocketBlock>(pocketBlock);
bool processed = node.chainman->ProcessNewBlock(state, Params(), block, pocketBlockRef, true, &ignored);
assert(processed);

return CTxIn{block->vtx[0]->GetHash(), 0};
}


static void AssembleBlock(benchmark::Bench& bench)
{
TestingSetup test_setup{
CBaseChainParams::REGTEST,
/* extra_args */
{
/* extra_args */ {
"-nodebuglogfile",
"-nodebug",
},
Expand Down
17 changes: 4 additions & 13 deletions src/bench/checkblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,27 @@
#include <streams.h>
#include <validation.h>

namespace block_bench {
#include <bench/data/block1533073.h>
} // namespace block_bench

// These are the two major time-sinks which happen after we have fully received
// a block off the wire, but before we can relay the block on to peers using
// compact block relay.

static void DeserializeBlockTest(benchmark::Bench& bench)
{
CDataStream stream((const char*)block_bench::block1533073,
(const char*)&block_bench::block1533073[sizeof(block_bench::block1533073)],
SER_NETWORK, PROTOCOL_VERSION);
CDataStream stream(benchmark::data::block1533073, SER_NETWORK, PROTOCOL_VERSION);
char a = '\0';
stream.write(&a, 1); // Prevent compaction

bench.unit("block").run([&] {
CBlock block;
stream >> block;
bool rewound = stream.Rewind(sizeof(block_bench::block1533073));
bool rewound = stream.Rewind(benchmark::data::block1533073.size());
assert(rewound);
});
}

static void DeserializeAndCheckBlockTest(benchmark::Bench& bench)
{
CDataStream stream((const char*)block_bench::block1533073,
(const char*)&block_bench::block1533073[sizeof(block_bench::block1533073)],
SER_NETWORK, PROTOCOL_VERSION);
CDataStream stream(benchmark::data::block1533073, SER_NETWORK, PROTOCOL_VERSION);
char a = '\0';
stream.write(&a, 1); // Prevent compaction

Expand All @@ -48,7 +40,7 @@ static void DeserializeAndCheckBlockTest(benchmark::Bench& bench)
bench.unit("block").run([&] {
CBlock block; // Note that CBlock caches its checked state, so we need to recreate it here
stream >> block;
bool rewound = stream.Rewind(sizeof(block_bench::block1533073));
bool rewound = stream.Rewind(benchmark::data::block1533073.size());
assert(rewound);

BlockValidationState validationState;
Expand All @@ -57,6 +49,5 @@ static void DeserializeAndCheckBlockTest(benchmark::Bench& bench)
});
}


BENCHMARK(DeserializeBlockTest);
BENCHMARK(DeserializeAndCheckBlockTest);
5 changes: 4 additions & 1 deletion src/bench/data/block1533073.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
static unsigned const char block1533073[] = {
#include <cstdint>
#include <vector>

static std::vector<uint8_t> block1533073_raw = {
0x00,0x00,0x00,0x20,0x96,0xe2,0x3b,0xe3,
0xeb,0x68,0x5a,0xe9,0xff,0x13,0x87,0xa6,
0x98,0x0b,0xa8,0x61,0xaf,0x24,0xb3,0x46,
Expand Down
1 change: 0 additions & 1 deletion src/bench/gcs_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <bench/bench.h>
#include <blockfilter.h>
#include <validation.h>

static void ConstructGCSFilter(benchmark::Bench& bench)
{
Expand Down
2 changes: 1 addition & 1 deletion src/bench/nanobench.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019-2020 The Bitcoin Core developers
// Copyright (c) 2019-2020 The Pocketcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

Expand Down
3 changes: 0 additions & 3 deletions src/bench/prevector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <bench/bench.h>
#include <vector>
#include <prevector.h>
#include <reverse_iterator.h>
#include <serialize.h>
#include <streams.h>
#include <type_traits>

#include <bench/bench.h>
#include <validation.h>

// GCC 4.8 is missing some C++11 type_traits,
// https://www.gnu.org/software/gcc/gcc-5/changes.html
Expand Down
9 changes: 1 addition & 8 deletions src/bench/rpc_blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,9 @@

#include <univalue.h>

namespace block_bench {
#include <bench/data/block1533073.h>
} // namespace block_bench

static void BlockToJsonVerbose(benchmark::Bench& bench)
{
CDataStream stream((const char*)block_bench::block1533073,
(const char*)&block_bench::block1533073[sizeof(block_bench::block1533073)],
SER_NETWORK, PROTOCOL_VERSION);

CDataStream stream(benchmark::data::block1533073, SER_NETWORK, PROTOCOL_VERSION);
char a = '\0';
stream.write(&a, 1); // Prevent compaction

Expand Down
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1620,7 +1620,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
}, std::chrono::minutes{1});

// ********************************************************* Step 4b: Start PocketDB
uiInterface.InitMessage("Loading Pocket DB...");
uiInterface.InitMessage(_("Loading Pocket DB...").translated);

PocketDb::InitSQLite(GetDataDir() / "pocketdb");
PocketDb::InitSQLiteCheckpoints(GetDataDir() / "checkpoints");
Expand Down
3 changes: 2 additions & 1 deletion src/pocketdb/consensus/social/Post.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ namespace PocketConsensus
return {false, baseCheckCode};

// Check required fields
if (IsEmpty(ptx->GetAddress())) return {false, SocialConsensusResult_Failed};
if (IsEmpty(ptx->GetAddress()))
return {false, SocialConsensusResult_Failed};

return Success;
}
Expand Down
1 change: 0 additions & 1 deletion src/test/blockencodings_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,6 @@ BOOST_AUTO_TEST_CASE(EmptyBlockRoundTripTest)
}
}

/* Intermittent test failures with this test case, investigate */
BOOST_AUTO_TEST_CASE(TransactionsRequestSerializationTest) {
BlockTransactionsRequest req1;
req1.blockhash = InsecureRand256();
Expand Down
60 changes: 58 additions & 2 deletions src/test/pocketnet_social_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ BOOST_AUTO_TEST_SUITE(pocketnet_social_tests)

BOOST_FIXTURE_TEST_CASE(pocketnet_ratings, TestChain100Setup)
{
const auto ToMemPool = [this](const CMutableTransaction& tx, const PTransactionRef ptx) {
LOCK(cs_main);
LOCK(m_node.mempool->cs);

TxValidationState state;
return AcceptToMemoryPool(*m_node.mempool, state, MakeTransactionRef(tx), ptx,
nullptr /* plTxnReplaced */, true /* bypass_limits */);
};

CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
RPCHelpMan rpc = GenerateAddress();
UniValue keys;
keys.setArray();
Expand All @@ -43,11 +53,57 @@ BOOST_FIXTURE_TEST_CASE(pocketnet_ratings, TestChain100Setup)
keys.push_back(key);
util::Ref context;
JSONRPCRequest request(context);

request.params.setArray();
request.params.push_back(keys);

//rpc.HandleRequest(request);
//BOOST_CHECK(value != nullptr);

// Create a ACCOUNT_USER transaction
CMutableTransaction mTx;
mTx.nVersion = 1;
mTx.vin.resize(1);
// Create a transaction with input from earlier block
mTx.vin[0].prevout.hash = m_coinbase_txns[0]->GetHash();
mTx.vin[0].prevout.n = 0;
mTx.vout.resize(1);
mTx.vout[0].nValue = 11*CENT;
mTx.vout[0].scriptPubKey = scriptPubKey;

// Sign:
std::vector<unsigned char> vchSig;
uint256 hash = SignatureHash(scriptPubKey, mTx, 0, SIGHASH_ALL, 0, SigVersion::BASE);
BOOST_CHECK(coinbaseKey.Sign(hash, vchSig));
vchSig.push_back((unsigned char)SIGHASH_ALL);
mTx.vin[0].scriptSig << vchSig;

const auto tx = MakeTransactionRef(mTx);

auto txOutput = PocketDb::TransRepoInst.GetTxOutput(tx->vin[0].prevout.hash.GetHex(), tx->vin[0].prevout.n);
if (!txOutput) throw JSONRPCError(RPC_INVALID_PARAMS, "Invalid address");
string address = *txOutput->GetAddressHash();

UniValue pocketData;
pocketData.pushKV("r", "fakeaddress");
pocketData.pushKV("l", "en");
pocketData.pushKV("n", "bob");
pocketData.pushKV("a", "bob avatar");
pocketData.pushKV("s", "url");
pocketData.pushKV("k", "bob pubkey");
pocketData.pushKV("b", "bob donations");

// Deserialize incoming data
auto[deserializeOk, ptx] = PocketServices::Serializer::DeserializeTransactionRpc(tx, pocketData);
if (!deserializeOk)
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX deserialize failed");

// Set required fields
ptx->SetAddress(address);

// Insert into mempool
//return _accept_transaction(tx, ptx, *node.mempool, *node.connman); // TODO (losty-fur): possible null
BOOST_CHECK(ToMemPool(mTx, ptx));


}

BOOST_AUTO_TEST_SUITE_END()
1 change: 0 additions & 1 deletion src/test/txvalidationcache_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend, TestChain100Setup)
BOOST_CHECK_EQUAL(m_node.mempool->size(), 0U);
}


// Run CheckInputScripts (using CoinsTip()) on the given transaction, for all script
// flags. Test that CheckInputScripts passes for all flags that don't overlap with
// the failing_flags argument, but otherwise fails.
Expand Down

0 comments on commit 0b3a87c

Please sign in to comment.