Skip to content

Commit

Permalink
[PSL-407] Sapling transactions are treated as invalid, causing peer b…
Browse files Browse the repository at this point in the history
…anning and stalled sync (#158)

* [PSL-407] Sapling transactions are treated as invalid, causing peer banning and stalled sync

- fixed ban policy in ContextualCheckTransaction
- corrected gtests

* - reorganized overwiner/sapling tx checks in ContextualCheckTransaction

- fixed gtests for transaction checking

* - skip "tx" messages in IBD mode

* - cleanup blocks in-flight from stalled peer, remove these blocks from the global in-flight block map

- log in-flight blocks from disconnected peer in "net" log category
- fixed an issue with wallet_changeindicator.py - generate one block to reset IBD mode

* python test fix

* - moved network type from chainparams to consensus params to be used in IsInitialDownload function

- disabled last blocktime check in IsInitialDownload function for REGTEST network
- added log when "tx" message is skipped
  • Loading branch information
akobrin1 authored Sep 21, 2022
1 parent afac6a6 commit b7c3bb4
Show file tree
Hide file tree
Showing 49 changed files with 875 additions and 709 deletions.
8 changes: 6 additions & 2 deletions qa/rpc-tests/test_framework/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,18 @@ def start_node(i, dirname, extra_args=None, rpchost=None, timewait=None, binary=
proxy.url = url # store URL on proxy for info
return proxy


def start_nodes(num_nodes, dirname, extra_args=None, rpchost=None, binary=None):
"""
Start multiple pastelds, return RPC connections to them
"""
if extra_args is None: extra_args = [ None for i in range(num_nodes) ]
if binary is None: binary = [ None for i in range(num_nodes) ]
if extra_args is None:
extra_args = [ None for i in range(num_nodes) ]
if binary is None:
binary = [ None for i in range(num_nodes) ]
return [ start_node(i, dirname, extra_args[i], rpchost, binary=binary[i]) for i in range(num_nodes) ]


def log_filename(dirname, n_node, logname):
return os.path.join(dirname, "node"+str(n_node), "regtest", logname)

Expand Down
16 changes: 15 additions & 1 deletion qa/rpc-tests/wallet_changeindicator.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
#!/usr/bin/env python3
# Copyright (c) 2018 The Zcash developers
# Copyright (c) 2018-2022 The Pastel Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or https://www.opensource.org/licenses/mit-license.php .

from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, assert_true, assert_false, wait_and_assert_operationid_status
from test_framework.util import (
assert_equal,
assert_true,
assert_false,
wait_and_assert_operationid_status,
start_nodes
)

from decimal import Decimal

class WalletChangeIndicatorTest (BitcoinTestFramework):
def setup_nodes(self):
return start_nodes(self.num_nodes, self.options.tmpdir,
extra_args=[['-debug=net']] * self.num_nodes)

# Tests
def run_test(self):
taddr = self.nodes[1].getnewaddress()
zaddr1 = self.nodes[1].z_getnewaddress()
zaddr2 = self.nodes[1].z_getnewaddress()

# generate one block to make sure initial block download (IBD) mode is reset
self.generate_and_sync_inc(1)

self.nodes[0].sendtoaddress(taddr, Decimal('1.0'))
self.generate_and_sync_inc(1)

Expand Down
37 changes: 21 additions & 16 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,13 @@ static CBlock CreateRegtestGenesisBlock()

const arith_uint256 maxUint = UintToArith256(uint256S("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));

class CMainParams : public CChainParams {
class CMainParams : public CChainParams
{
public:
CMainParams() {
CMainParams() :
CChainParams(ChainNetwork::MAIN)
{
strNetworkID = "main";
network = CBaseChainParams::Network::MAIN;
strCurrencyUnits = "PSL";
bip44CoinType = 133; // As registered in https://github.com/patoshilabs/slips/blob/master/slip-0044.md
consensus.nSubsidyHalvingInterval = 840'000;
Expand Down Expand Up @@ -418,11 +420,13 @@ class CMainParams : public CChainParams {
/**
* Testnet (v3)
*/
class CTestNetParams : public CChainParams {
class CTestNetParams : public CChainParams
{
public:
CTestNetParams() {
CTestNetParams() :
CChainParams(ChainNetwork::TESTNET)
{
strNetworkID = "test";
network = CBaseChainParams::Network::TESTNET;
strCurrencyUnits = "LSP";
bip44CoinType = 1;
consensus.nSubsidyHalvingInterval = 840'000;
Expand Down Expand Up @@ -519,12 +523,13 @@ class CTestNetParams : public CChainParams {
/**
* Regression test
*/
class CRegTestParams : public CChainParams {
class CRegTestParams : public CChainParams
{
public:
CRegTestParams()
CRegTestParams() :
CChainParams(ChainNetwork::REGTEST)
{
strNetworkID = "regtest";
network = CBaseChainParams::Network::REGTEST;
strCurrencyUnits = "REG";
bip44CoinType = 1;
consensus.nSubsidyHalvingInterval = 150;
Expand Down Expand Up @@ -626,20 +631,20 @@ const CChainParams &Params()
* \param network - MAIN, TESTNET or REGTEST
* \return unique_ptr with chain parameters
*/
std::unique_ptr<const CChainParams> CreateChainParams(const CBaseChainParams::Network network)
std::unique_ptr<const CChainParams> CreateChainParams(const ChainNetwork network)
{
std::unique_ptr<CChainParams> ChainParams;
switch (network)
{
case CBaseChainParams::Network::MAIN:
case ChainNetwork::MAIN:
ChainParams = std::make_unique<CMainParams>();
break;

case CBaseChainParams::Network::TESTNET:
case ChainNetwork::TESTNET:
ChainParams = std::make_unique<CTestNetParams>();
break;

case CBaseChainParams::Network::REGTEST:
case ChainNetwork::REGTEST:
ChainParams = std::make_unique<CRegTestParams>();
break;

Expand All @@ -656,16 +661,16 @@ std::unique_ptr<const CChainParams> CreateChainParams(const CBaseChainParams::Ne
*
* \param network - blockchain network type (MAIN, TESTNET or REGTEST)
*/
void SelectParams(const CBaseChainParams::Network network)
void SelectParams(const ChainNetwork network)
{
SelectBaseParams(network);
globalChainParams = CreateChainParams(network);
}

bool SelectParamsFromCommandLine()
{
CBaseChainParams::Network network = NetworkIdFromCommandLine();
if (network == CBaseChainParams::Network::MAX_NETWORK_TYPES)
const ChainNetwork network = NetworkIdFromCommandLine();
if (network == ChainNetwork::MAX_NETWORK_TYPES)
return false;

SelectParams(network);
Expand Down
22 changes: 11 additions & 11 deletions src/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,15 @@ class CChainParams : public CBaseKeyConstants
const std::vector<SeedSpec6>& FixedSeeds() const noexcept { return vFixedSeeds; }
const CCheckpointData& Checkpoints() const noexcept { return checkpointData; }

bool IsMainNet() const noexcept { return network == CBaseChainParams::Network::MAIN; }
bool IsTestNet() const noexcept { return network == CBaseChainParams::Network::TESTNET; }
bool IsRegTest() const noexcept { return network == CBaseChainParams::Network::REGTEST; }
CChainParams(ChainNetwork network) :
consensus(network)
{
memset(&pchMessageStart, 0, sizeof(pchMessageStart));
}

bool IsMainNet() const noexcept { return consensus.network == ChainNetwork::MAIN; }
bool IsTestNet() const noexcept { return consensus.network == ChainNetwork::TESTNET; }
bool IsRegTest() const noexcept { return consensus.network == ChainNetwork::REGTEST; }

void UpdateNetworkUpgradeParameters(Consensus::UpgradeIndex idx, const uint32_t nActivationHeight)
{
Expand All @@ -102,11 +108,6 @@ class CChainParams : public CBaseKeyConstants
}

protected:
CChainParams()
{
memset(&pchMessageStart, 0, sizeof(pchMessageStart));
}

Consensus::Params consensus;
CMessageHeader::MessageStartChars pchMessageStart;
//! Raw pub key bytes for the broadcast alert signing key.
Expand All @@ -115,7 +116,6 @@ class CChainParams : public CBaseKeyConstants
uint64_t nPruneAfterHeight = 0;
std::vector<CDNSSeedData> vSeeds;
std::string strNetworkID;
CBaseChainParams::Network network = CBaseChainParams::Network::MAIN;
std::string strCurrencyUnits;
uint32_t bip44CoinType = 0;
CBlock genesis;
Expand All @@ -135,10 +135,10 @@ class CChainParams : public CBaseKeyConstants
const CChainParams &Params();

// Create blockchain parameters based on network type.
std::unique_ptr<const CChainParams> CreateChainParams(const CBaseChainParams::Network network);
std::unique_ptr<const CChainParams> CreateChainParams(const ChainNetwork network);

/** Sets the params returned by Params() to those for the given network. */
void SelectParams(const CBaseChainParams::Network network);
void SelectParams(const ChainNetwork network);

/**
* Looks for -regtest or -testnet and then calls SelectParams as appropriate.
Expand Down
39 changes: 20 additions & 19 deletions src/chainparamsbase.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Copyright (c) 2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin Core developers
// Copyright (c) 2018-2022 The Pastel Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "chainparamsbase.h"
#include "port_config.h"
#include "util.h"
// file COPYING or https://www.opensource.org/licenses/mit-license.php.

#include <assert.h>

#include <chainparamsbase.h>
#include <port_config.h>
#include <util.h>

/**
* Main network
*/
Expand Down Expand Up @@ -73,20 +74,20 @@ const CBaseChainParams& BaseParams()
* \param network - blockchain type (MAIN, TESTNET or REGTEST)
* \return std::unique_ptr<CBaseChainParams>
*/
std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const CBaseChainParams::Network network)
std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const ChainNetwork network)
{
std::unique_ptr<CBaseChainParams> BaseChainParams;
switch (network)
{
case CBaseChainParams::Network::MAIN:
case ChainNetwork::MAIN:
BaseChainParams = std::make_unique<CBaseMainParams>();
break;

case CBaseChainParams::Network::TESTNET:
case ChainNetwork::TESTNET:
BaseChainParams = std::make_unique<CBaseTestNetParams>();
break;

case CBaseChainParams::Network::REGTEST:
case ChainNetwork::REGTEST:
BaseChainParams = std::make_unique<CBaseRegTestParams>();
break;

Expand All @@ -99,29 +100,29 @@ std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const CBaseChainParams::
}

/** Sets the params returned by Params() to those for the given network. */
void SelectBaseParams(const CBaseChainParams::Network network)
void SelectBaseParams(const ChainNetwork network)
{
globalChainBaseParams = CreateBaseChainParams(network);
}

CBaseChainParams::Network NetworkIdFromCommandLine()
ChainNetwork NetworkIdFromCommandLine()
{
bool fRegTest = GetBoolArg("-regtest", false);
bool fTestNet = GetBoolArg("-testnet", false);
const bool fRegTest = GetBoolArg("-regtest", false);
const bool fTestNet = GetBoolArg("-testnet", false);

if (fTestNet && fRegTest)
return CBaseChainParams::Network::MAX_NETWORK_TYPES;
return ChainNetwork::MAX_NETWORK_TYPES;
if (fRegTest)
return CBaseChainParams::Network::REGTEST;
return ChainNetwork::REGTEST;
if (fTestNet)
return CBaseChainParams::Network::TESTNET;
return CBaseChainParams::Network::MAIN;
return ChainNetwork::TESTNET;
return ChainNetwork::MAIN;
}

bool SelectBaseParamsFromCommandLine()
{
CBaseChainParams::Network network = NetworkIdFromCommandLine();
if (network == CBaseChainParams::Network::MAX_NETWORK_TYPES)
const ChainNetwork network = NetworkIdFromCommandLine();
if (network == ChainNetwork::MAX_NETWORK_TYPES)
return false;

SelectBaseParams(network);
Expand Down
17 changes: 5 additions & 12 deletions src/chainparamsbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,15 @@
#include <string>
#include <memory>

#include <consensus/consensus.h>

/**
* CBaseChainParams defines the base parameters (shared between pastel-cli and pasteld)
* of a given instance of the Bitcoin system.
*/
class CBaseChainParams
{
public:
enum class Network
{
MAIN,
TESTNET,
REGTEST,

MAX_NETWORK_TYPES
};

const std::string& DataDir() const noexcept { return strDataDir; }
int RPCPort() const noexcept { return nRPCPort; }

Expand All @@ -36,10 +29,10 @@ class CBaseChainParams
* Creates and returns a std::unique_ptr<CBaseChainParams>. This won't change after app
* startup, except for unit tests.
*/
std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const CBaseChainParams::Network network);
std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const ChainNetwork network);

/** Sets the params returned by Params() to those for the given network. */
void SelectBaseParams(const CBaseChainParams::Network network);
void SelectBaseParams(const ChainNetwork network);

/**
* Return the currently selected parameters. This won't change after app
Expand All @@ -51,7 +44,7 @@ const CBaseChainParams& BaseParams();
* Looks for -regtest or -testnet and returns the appropriate Network ID.
* Returns MAX_NETWORK_TYPES if an invalid combination is given.
*/
CBaseChainParams::Network NetworkIdFromCommandLine();
ChainNetwork NetworkIdFromCommandLine();

/**
* Calls NetworkIdFromCommandLine() and then calls SelectParams as appropriate.
Expand Down
9 changes: 9 additions & 0 deletions src/consensus/consensus.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://www.opensource.org/licenses/mit-license.php.

enum class ChainNetwork
{
MAIN = 0,
TESTNET,
REGTEST,

MAX_NETWORK_TYPES
};

/** The minimum allowed block version (network rule) */
constexpr int32_t MIN_BLOCK_VERSION = 4;
/** The minimum allowed transaction version (network rule) */
Expand Down
6 changes: 6 additions & 0 deletions src/consensus/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <uint256.h>
#include <key_constants.h>
#include <enum_util.h>
#include <consensus/consensus.h>

namespace Consensus {

Expand Down Expand Up @@ -94,7 +95,11 @@ struct Params
int64_t nMaxGovernanceAmount;
// The period before a network upgrade activates, where connections to upgrading peers are preferred (in blocks)
uint32_t nNetworkUpgradePeerPreferenceBlockPeriod = 0;
ChainNetwork network;

Params(const ChainNetwork aNetwork) :
network(aNetwork)
{}

/**
* Add network upgrade.
Expand All @@ -115,5 +120,6 @@ struct Params
const auto nUpgradeIndex = to_integral_type(idx);
vUpgrades[nUpgradeIndex].nActivationHeight = nActivationHeight;
}

};
} // namespace Consensus
Loading

0 comments on commit b7c3bb4

Please sign in to comment.