Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pop fork resolution #41

Merged
merged 1 commit into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions depends/packages/altintegration.mk
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package=altintegration
$(package)_version=7362db2fb529157d39d4273f45b36e0fa0f1f7f7
$(package)_version=c32afd4ed526b14631a4764ae5897e2e461cb4fe
$(package)_download_path=https://github.com/VeriBlock/alt-integration-cpp/archive/
$(package)_file_name=$($(package)_version).tar.gz
$(package)_sha256_hash=567d323d32fb88037469f404502fbe525d1ba6cced4ab391d61ac4baa358178a
$(package)_sha256_hash=543128f8483f8a6a4bb78f203e37989151463318d8de81cad3fb08ce1cb45d03

define $(package)_config_cmds
cmake -DCMAKE_INSTALL_PREFIX=$($(package)_staging_dir)$(host_prefix) -DCMAKE_BUILD_TYPE=Release -DTESTING=OFF -DWITH_ROCKSDB=OFF -DSHARED=OFF -B .
Expand Down
4 changes: 2 additions & 2 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ BITCOIN_TESTS =\
test/uint256_tests.cpp \
test/util_tests.cpp \
test/validation_block_tests.cpp \
test/versionbits_tests.cpp
# test/txvalidationcache_tests.cpp
test/versionbits_tests.cpp \
test/txvalidationcache_tests.cpp
# test/txindex_tests.cpp
# test/blockfilter_index_tests.cpp

Expand Down
19 changes: 10 additions & 9 deletions src/bootstraps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ void printConfig(const altintegration::Config& config)
config.alt->getIdentifier());
}

void SelectPopConfig(
std::string btcnet,
std::string vbknet,
void selectPopConfig(
const std::string& btcnet,
const std::string& vbknet,
bool popautoconfig,
int btcstart,
std::string btcblocks,
const std::string& btcblocks,
int vbkstart,
std::string vbkblocks)
const std::string& vbkblocks)
{
altintegration::Config popconfig;

Expand All @@ -82,7 +82,7 @@ void SelectPopConfig(
if (vbknet == "test") {
auto param = std::make_shared<altintegration::VbkChainParamsTest>();
if (popautoconfig) {
popconfig.setVBK(testnetBTCstartHeight, testnetBTCblocks, param);
popconfig.setVBK(testnetVBKstartHeight, testnetVBKblocks, param);
} else {
popconfig.setVBK(vbkstart, parseBlocks(vbkblocks), param);
}
Expand All @@ -101,19 +101,20 @@ void SelectPopConfig(

auto& config = VeriBlock::getService<VeriBlock::Config>();
config.popconfig = std::move(popconfig);
printConfig(config.popconfig);
}

void selectPopConfig(const ArgsManager& args)
{
std::string btcnet = args.GetArg("-popbtcnetwork", "test");
std::string vbknet = args.GetArg("-popvbknetwork", "test");
std::string btcnet = args.GetArg("-popbtcnetwork", "regtest");
std::string vbknet = args.GetArg("-popvbknetwork", "regtest");
bool popautoconfig = args.GetBoolArg("-popautoconfig", true);
int btcstart = args.GetArg("-popbtcstartheight", 0);
std::string btcblocks = args.GetArg("-popbtcblocks", "");
int vbkstart = args.GetArg("-popvbkstartheight", 0);
std::string vbkblocks = args.GetArg("-popvbkblocks", "");

SelectPopConfig(btcnet, vbknet, popautoconfig, btcstart, btcblocks, vbkstart, vbkblocks);
selectPopConfig(btcnet, vbknet, popautoconfig, btcstart, btcblocks, vbkstart, vbkblocks);
}

int testnetVBKstartHeight = 412589;
Expand Down
10 changes: 5 additions & 5 deletions src/bootstraps.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ struct AltChainParamsVBTC : public altintegration::AltChainParams {

void printConfig(const altintegration::Config& config);
void selectPopConfig(const ArgsManager& mgr);
void SelectPopConfig(
std::string btcnet,
std::string vbknet,
void selectPopConfig(
const std::string& btcnet,
const std::string& vbknet,
bool popautoconfig = true,
int btcstart = 0,
std::string btcblocks = {},
const std::string& btcblocks = {},
int vbkstart = 0,
std::string vbkblocks = {});
const std::string& vbkblocks = {});


#endif
5 changes: 4 additions & 1 deletion src/test/util/setup_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
ClearDatadirCache();
SelectParams(chainName);
VeriBlock::InitConfig();
SelectPopConfig("regtest", "regtest", true);
selectPopConfig("regtest", "regtest", true);
VeriBlock::InitPopService();
SeedInsecureRand();
gArgs.ForceSetArg("-printtoconsole", "0");
Expand Down Expand Up @@ -175,6 +175,9 @@ TestChain100Setup::TestChain100Setup(): RegTestingSetup()
CBlock b = CreateAndProcessBlock(noTxns, scriptPubKey);
m_coinbase_txns.push_back(b.vtx[0]);
}

assert(ChainActive().Tip() != nullptr);
assert(ChainActive().Tip()->nHeight == 100);
}

// Create a new block with just given transactions, coinbase paying to
Expand Down
2 changes: 1 addition & 1 deletion src/uint256.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class base_blob
s.read((char*)data, sizeof(data));
}

std::vector<uint8_t> asVector() {
std::vector<uint8_t> asVector() const {
return std::vector<uint8_t>{begin(), end()};
}
};
Expand Down
23 changes: 15 additions & 8 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1905,7 +1905,9 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,

//VeriBlock : added ContextualCheckBlock() here becuse merkleRoot calculation moved from the CheckBlock() to the ContextualCheckBlock()

if (!CheckBlock(block, state, chainparams.GetConsensus(), !fJustCheck) && !ContextualCheckBlock(block, state, chainparams.GetConsensus(), pindex->pprev, true)) {
if (!CheckBlock(block, state, chainparams.GetConsensus(), !fJustCheck)
/*&& !ContextualCheckBlock(block, state, chainparams.GetConsensus(), pindex->pprev, true)
*/) {
if (state.GetResult() == BlockValidationResult::BLOCK_MUTATED) {
// We don't write down blocks to disk if they may have been
// corrupted, so this should be impossible unless we're having hardware
Expand Down Expand Up @@ -2463,7 +2465,8 @@ bool CChainState::DisconnectTip(BlockValidationState& state, const CChainParams&
}
}

VeriBlock::getService<VeriBlock::PopService>().removeAllBlockPayloads(*pindexDelete);
auto& pop = VeriBlock::getService<VeriBlock::PopService>();
pop.disconnectBlock(pindexDelete->GetBlockHash());

m_chain.SetTip(pindexDelete->pprev);

Expand Down Expand Up @@ -2602,9 +2605,7 @@ bool CChainState::ConnectTip(BlockValidationState& state, const CChainParams& ch
disconnectpool.removeForBlock(blockConnecting.vtx);

if (!VeriBlock::getService<VeriBlock::PopService>().addAllBlockPayloads(*pindexNew, blockConnecting, state)) {
if (state.IsInvalid()) {
InvalidBlockFound(pindexNew, state);
}
InvalidBlockFound(pindexNew, state);
return error("%s : AddAllBlockPayloads %s failed, %s", __func__, pindexNew->GetBlockHash().ToString(), FormatStateMessage(state));
}

Expand Down Expand Up @@ -3666,7 +3667,8 @@ bool BlockManager::AcceptBlockHeader(const CBlockHeader& block, BlockValidationS
if (ppindex)
*ppindex = pindex;

return true;
auto& pop = VeriBlock::getService<VeriBlock::PopService>();
return pop.acceptBlock(*pindex, state);
}

// Exposed wrapper for AcceptBlockHeader
Expand Down Expand Up @@ -3713,8 +3715,6 @@ static FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, const CChai
}
}

// TODO save payloads on disk if it necessary
//VeriBlock::getService<VeriBlock::PopService>().savePopTxToDatabase(block, nHeight);
return blockPos;
}

Expand Down Expand Up @@ -3852,6 +3852,8 @@ bool TestBlockValidity(BlockValidationState& state, const CChainParams& chainpar
return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__, FormatStateMessage(state));
if (!CheckBlock(block, state, chainparams.GetConsensus(), fCheckPOW))
return error("%s: Consensus::CheckBlock: %s", __func__, FormatStateMessage(state));
if (!ContextualCheckBlock(block, state, chainparams.GetConsensus(), pindexPrev, fCheckMerkleRoot))
return error("%s: Consensus::ContextualCheckBlock: %s", __func__, state.GetRejectReason());
if (!::ChainstateActive().ConnectBlock(block, state, &indexDummy, viewNew, chainparams, true))
return false;
assert(state.IsValid());
Expand Down Expand Up @@ -4632,6 +4634,11 @@ bool CChainState::LoadGenesisBlock(const CChainParams& chainparams)
if (blockPos.IsNull())
return error("%s: writing genesis block to disk failed", __func__);
CBlockIndex* pindex = m_blockman.AddToBlockIndex(block);
auto& pop = VeriBlock::getService<VeriBlock::PopService>();
BlockValidationState state;
if(!pop.acceptBlock(*pindex, state)){
return false;
}
ReceivedBlockTransactions(block, pindex, blockPos, chainparams.GetConsensus());
} catch (const std::runtime_error& e) {
return error("%s: failed to write genesis block: %s", __func__, e.what());
Expand Down
1 change: 0 additions & 1 deletion src/vbk/merkle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ inline int GetPopMerkleRootCommitmentIndex(const CBlock& block)
auto& s = block.vtx[0]->vout[o].scriptPubKey;
if (s.size() >= 37 && s[0] == OP_RETURN && s[1] == 0x23 && s[2] == 0x3a && s[3] == 0xe6 && s[4] == 0xca) {
commitpos = o;
break;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/vbk/pop_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ struct PopService {
virtual bool parseBlockPopPayloads(const CBlock& block, const CBlockIndex& pindexPrev, const Consensus::Params& params, BlockValidationState& state, std::vector<altintegration::AltPayloads>* payloads) = 0;
virtual bool parseTxPopPayloads(const CBlock& block, const CTransaction& tx, const CBlockIndex& pindexPrev, const Consensus::Params& params, TxValidationState& state, altintegration::AltPayloads& payloads) = 0;

virtual bool acceptBlock(const CBlockIndex& indexNew, BlockValidationState& state) = 0;
virtual bool addAllBlockPayloads(const CBlockIndex& indexNew, const CBlock& fullBlock, BlockValidationState& state) = 0;
virtual void removeAllBlockPayloads(const CBlockIndex& block) = 0;
virtual void disconnectBlock(const uint256& block) = 0;

virtual bool evalScript(const CScript& script, std::vector<std::vector<unsigned char>>& stack, ScriptError* serror, altintegration::AltPayloads* pub, altintegration::ValidationState& state, bool with_checks) = 0;
virtual int compareForks(const CBlockIndex& left, const CBlockIndex& right) = 0;
Expand Down
52 changes: 30 additions & 22 deletions src/vbk/pop_service_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ altintegration::AltBlock cast(int nHeight, const CBlockHeader& block)
altintegration::AltBlock alt;
alt.height = nHeight;
alt.timestamp = block.nTime;
alt.previousBlock = std::vector<uint8_t>(block.hashPrevBlock.begin(), block.hashPrevBlock.end());
auto hash = block.GetHash();
alt.hash = std::vector<uint8_t>{hash.begin(), hash.end()};
alt.hash = std::vector<uint8_t>(hash.begin(), hash.end());
return alt;
}

Expand Down Expand Up @@ -229,23 +230,32 @@ bool PopServiceImpl::checkPopInputs(const CTransaction& tx, TxValidationState& s
return true;
}

bool PopServiceImpl::acceptBlock(const CBlockIndex& indexNew, BlockValidationState& state)
{
std::lock_guard<std::mutex> lock(mutex);
auto containing = cast(indexNew.nHeight, indexNew.GetBlockHeader());
altintegration::ValidationState instate;
if (!altTree->acceptBlock(containing, instate)) {
return state.Error(instate.GetDebugMessage());
}

return true;
}

bool PopServiceImpl::addAllBlockPayloads(const CBlockIndex& indexNew, const CBlock& connecting, BlockValidationState& state)
{
std::lock_guard<std::mutex> lock(mutex);
auto containing = cast(indexNew.nHeight, connecting.GetBlockHeader());
auto containing = cast(indexNew.nHeight, indexNew.GetBlockHeader());

altintegration::ValidationState instate;
std::vector<altintegration::AltPayloads> payloads;
if (!parseBlockPopPayloads(connecting, indexNew, Params().GetConsensus(), state, &payloads)) {
return false;
}

altintegration::ValidationState instate;
if (!altTree->acceptBlock(containing, instate)) {
return error("[%s] block %s has invalid block: %s, %s", __func__, connecting.GetHash().ToString(), instate.GetPath(), instate.GetDebugMessage());
return error("[%s] block %s failed stateless validation: %s, %s", __func__, connecting.GetHash().ToString(), instate.GetPath(), instate.GetDebugMessage());
;
}

if (!payloads.empty() && !altTree->addPayloads(containing, payloads, instate)) {
return error("[%s] block %s has invalid payloads: %s, %s", __func__, connecting.GetHash().ToString(), instate.GetPath(), instate.GetDebugMessage());
return error("[%s] block %s failed stateful pop validation: %s, %s", __func__, connecting.GetHash().ToString(), instate.GetPath(), instate.GetDebugMessage());
}

return true;
Expand Down Expand Up @@ -357,19 +367,6 @@ bool PopServiceImpl::evalScript(const CScript& script, std::vector<std::vector<u
return true;
}


void PopServiceImpl::removeAllBlockPayloads(const CBlockIndex& connecting)
{
std::lock_guard<std::mutex> lock(mutex);
altintegration::ValidationState instate;
auto block = cast(connecting.nHeight, connecting.GetBlockHeader());
bool ret = altTree->setState(block.previousBlock, instate);
assert(ret);
assert(instate.IsValid());
(void)ret;
}


std::vector<BlockBytes> PopServiceImpl::getLastKnownVBKBlocks(size_t blocks)
{
std::lock_guard<std::mutex> lock(mutex);
Expand All @@ -385,6 +382,10 @@ std::vector<BlockBytes> PopServiceImpl::getLastKnownBTCBlocks(size_t blocks)
// Forkresolution
int PopServiceImpl::compareForks(const CBlockIndex& leftForkTip, const CBlockIndex& rightForkTip)
{
if (&leftForkTip == &rightForkTip) {
return 0;
}

std::lock_guard<std::mutex> lock(mutex);
auto left = leftForkTip.GetBlockHash().asVector();
auto right = rightForkTip.GetBlockHash().asVector();
Expand Down Expand Up @@ -478,4 +479,11 @@ VeriBlock::PopServiceImpl::PopServiceImpl(const altintegration::Config& config)
auto tree = altintegration::Altintegration::create(config);
altTree = std::make_shared<altintegration::AltTree>(std::move(tree));
}

void PopServiceImpl::disconnectBlock(const uint256& block)
{
std::lock_guard<std::mutex> lock(mutex);
auto v = block.asVector();
altTree->invalidateBlockByHash(v);
}
} // namespace VeriBlock
4 changes: 3 additions & 1 deletion src/vbk/pop_service_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ class PopServiceImpl : public PopService
bool parseBlockPopPayloads(const CBlock& block, const CBlockIndex& pindexPrev, const Consensus::Params& params, BlockValidationState& state, std::vector<altintegration::AltPayloads>* payloads) override;
bool parseTxPopPayloads(const CBlock& block, const CTransaction& tx, const CBlockIndex& pindexPrev, const Consensus::Params& params, TxValidationState& state, altintegration::AltPayloads& payloads) override;

bool acceptBlock(const CBlockIndex& indexNew, BlockValidationState& state) override;
bool addAllBlockPayloads(const CBlockIndex& indexNew, const CBlock& fullBlock, BlockValidationState& state) override;
void removeAllBlockPayloads(const CBlockIndex& block) override;
void disconnectBlock(const uint256& block) override;


bool evalScript(const CScript& script, std::vector<std::vector<unsigned char>>& stack, ScriptError* serror, altintegration::AltPayloads* pub, altintegration::ValidationState& state, bool with_checks) override;
int compareForks(const CBlockIndex& left, const CBlockIndex& right) override;
Expand Down