Skip to content

Commit

Permalink
resolve some issue
Browse files Browse the repository at this point in the history
  • Loading branch information
winnie-chaintope authored and Naviabheeman committed Jun 5, 2020
1 parent babecf2 commit 5769437
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 62 deletions.
27 changes: 26 additions & 1 deletion src/federationparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ CPubKey CFederationParams::ReadAggregatePubkey(const std::vector<unsigned char>&
aggPubkeyAndHeight p;
p.aggpubkey = CPubKey(pubkey.begin(), pubkey.end());
p.height = height;
aggregatePubkeyHeight.push_back(p);
if(!p.aggpubkey.IsFullyValid()) {
throw std::runtime_error(strprintf("Aggregate Public Key for Signed Block is invalid: %s", HexStr(pubkey)));
}

if (p.aggpubkey.size() != CPubKey::COMPRESSED_PUBLIC_KEY_SIZE) {
throw std::runtime_error(strprintf("Aggregate Public Key for Signed Block is invalid: %s", HexStr(pubkey)));
}
aggregatePubkeyHeight.push_back(p);
return p.aggpubkey;

} else if(pubkey[0] == 0x04 || pubkey[0] == 0x06 || pubkey[0] == 0x07) {
Expand Down Expand Up @@ -204,3 +204,28 @@ bool CFederationParams::ReadGenesisBlock(std::string genesisHex)

return true;
}

uint& CFederationParams::GetHeightFromAggregatePubkey(std::vector<unsigned char> aggpubkey)
{
for (auto& c : aggregatePubkeyHeight)
if (c.aggpubkey == CPubKey(aggpubkey.begin(), aggpubkey.end())) {
return c.height;
break;
} else {
continue;
}
}

CPubKey& CFederationParams::GetAggPubkeyFromHeight(uint height)
{
if(height < 1 || aggregatePubkeyHeight.size() == 1)
return aggregatePubkeyHeight.at(0).aggpubkey;

if(height > aggregatePubkeyHeight.back().height)
return aggregatePubkeyHeight.back().aggpubkey;

for(unsigned int i = 0; i < aggregatePubkeyHeight.size(); i++) {
if(height == aggregatePubkeyHeight.at(i).height || (aggregatePubkeyHeight.at(i).height < height && height < aggregatePubkeyHeight.at(i+1).height))
return aggregatePubkeyHeight.at(i).aggpubkey;
}
}
35 changes: 5 additions & 30 deletions src/federationparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,34 +48,10 @@ class CFederationParams
const std::vector<SeedSpec6>& FixedSeeds() const { return vFixedSeeds; }
/** Return the list of hostnames to look up for DNS seeds */
const std::vector<std::string>& DNSSeeds() const { return vSeeds; }
const uint& GetHeightFromAggregatePubkey(std::vector<unsigned char> aggpubkey) const {
for (auto& c : aggregatePubkeyHeight)
if (c.aggpubkey == CPubKey(aggpubkey.begin(), aggpubkey.end())) {
return c.height;
break;
} else {
continue;
}
};
const CPubKey& GetAggPubkeyFromHeight(uint height) const {
if((height < 1) || (aggregatePubkeyHeight.size() == 1)) {
return aggregatePubkeyHeight.at(0).aggpubkey;
} else {
for(decltype(aggregatePubkeyHeight.size()) i=0; i<aggregatePubkeyHeight.size(); i++) {
if((aggregatePubkeyHeight.at(i).height < height) && (height <= aggregatePubkeyHeight.at(i+1).height)) {
if (height == aggregatePubkeyHeight.at(i+1).height)
{
return aggregatePubkeyHeight.at(i+1).aggpubkey;
} else {
return aggregatePubkeyHeight.at(i).aggpubkey;
}
break;
} else {
continue;
}
}
}
};
uint& GetHeightFromAggregatePubkey(std::vector<unsigned char> aggpubkey);
CPubKey& GetAggPubkeyFromHeight(uint height);
CPubKey RemoveAggregatePubKey(const std::vector<unsigned char>& pubkey);


CFederationParams();
CFederationParams(const int networkId, const std::string dataDirName, const std::string genesisHex);
Expand All @@ -85,7 +61,7 @@ class CFederationParams
CMessageHeader::MessageStartChars pchMessageStart;
std::string strNetworkID;
std::string dataDir;
std::vector<aggPubkeyAndHeight> aggregatePubkeyHeight;
mutable std::vector<aggPubkeyAndHeight> aggregatePubkeyHeight;
CBlock genesis;
std::vector<std::string> vSeeds;
std::vector<SeedSpec6> vFixedSeeds;
Expand Down Expand Up @@ -122,5 +98,4 @@ std::string ReadGenesisBlock(fs::path genesisPath = GetDataDir(false));
*/
CBlock createGenesisBlock(const CPubKey& aggregatePubkey, const CKey& privateKey, const time_t blockTime=time(0), const std::string paytoAddress="");


#endif // BITCOIN_FEDERATIONPARAMS_H
17 changes: 5 additions & 12 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <consensus/tx_verify.h>
#include <consensus/validation.h>
#include <cuckoocache.h>
#include <federationparams.h>
#include <hash.h>
#include <index/txindex.h>
#include <policy/fees.h>
Expand Down Expand Up @@ -2881,9 +2882,9 @@ static bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state,
if(chainActive.Tip())
height = chainActive.Tip()->nHeight;

CPubKey aggregatePubkey;
CPubKey aggregatePubkey;
if(nHeight >= height)
aggregatePubkey = FederationParams().GetAggPubkeyFromHeight(nHeight);
aggregatePubkey = const_cast<CFederationParams&>(FederationParams()).GetAggPubkeyFromHeight(nHeight);
else
aggregatePubkey = FederationParams().GetLatestAggregatePubkey();

Expand Down Expand Up @@ -3267,11 +3268,7 @@ bool ProcessNewBlock(const std::shared_ptr<const CBlock> pblock, bool fForceProc
// Store to disk
ret = g_chainstate.AcceptBlock(pblock, state, &pindex, fForceProcessing, nullptr, fNewBlock);

const CPubKey&blockAggPubKey = CPubKey(pindex->aggPubkey.begin(), pindex->aggPubkey.end());

const CPubKey&federationAggPubKey = FederationParams().GetLatestAggregatePubkey();

if((pindex->aggPubkey.size() == 33) && (blockAggPubKey != federationAggPubKey))
if((pindex->aggPubkey.size() == 33) && (CPubKey(pindex->aggPubkey.begin(), pindex->aggPubkey.end()) != FederationParams().GetLatestAggregatePubkey()))
const_cast<CFederationParams&>(FederationParams()).ReadAggregatePubkey(pindex->aggPubkey, pindex->nHeight+1);
}
if (!ret) {
Expand Down Expand Up @@ -4135,11 +4132,7 @@ bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
std::multimap<uint256, CDiskBlockPos>::iterator it = range.first;
std::shared_ptr<CBlock> pblockrecursive = std::make_shared<CBlock>();

BlockMap::iterator miSelf = mapBlockIndex.find(hash);
CBlockIndex *pindex = nullptr;
pindex = miSelf->second;

if (ReadBlockFromDisk(*pblockrecursive, it->second, pindex->nHeight))
if (ReadBlockFromDisk(*pblockrecursive, it->second, pblockrecursive->vtx[0]->vin[0].prevout.n))
{
LogPrint(BCLog::REINDEX, "%s: Processing out of order child %s of %s\n", __func__, pblockrecursive->GetHash().ToString(),
head.ToString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,19 @@
# Copyright (c) 2019 Chaintope Inc.
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test node responses to invalid blocks.
"""Test node responses to federation blocks.
In this test we connect to one node over p2p, and test block requests:
1) Valid blocks should be requested and become chain tip.
2) Invalid block with duplicated transaction should be re-requested.
3) Invalid block with bad coinbase value should be rejected and not
re-requested.
1) Block create with new aggregate public key sign with old aggregate public key
2) The Next block will sign with the new aggregate public key
"""
import copy

from test_framework.blocktools import create_block, create_coinbase
from test_framework.messages import COIN
from test_framework.mininode import P2PDataStore
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal
from test_framework.util import hex_str_to_bytes

class InvalidBlockRequestTest(BitcoinTestFramework):
class FederationBlockTest(BitcoinTestFramework):
def set_test_params(self):
self.num_nodes = 1
self.setup_clean_chain = True
Expand All @@ -36,46 +31,40 @@ def run_test(self):
# tip = best_block.sha256
height = best_block["height"] + 1
block_time = best_block["time"] + 1
self.log.info(best_block)

self.log.info("Create 1st block without aggpubkey")
self.log.info("Create 1st block")
height = 1
block1 = create_block(tip, create_coinbase(height), block_time, self.signblockpubkey)
block1.solve(self.signblockprivkey)
node.p2p.send_blocks_and_test([block1], node, True)
self.log.info(block1)

best_block = node.getblock(node.getbestblockhash())

self.log.info(best_block)
tip = block1.sha256
# tip = int(node.getbestblockhash(), 16)
height = best_block["height"] + 1
block_time = best_block["time"] + 1

self.log.info("Create 2nd block with aggpubkey")
self.log.info("Create 2nd block with new aggpubkey")
self.signblockpubkey = "03842d51608d08bee79587fb3b54ea68f5279e13fac7d72515a7205e6672858ca2"
block2 = create_block(tip, create_coinbase(height), block_time, self.signblockpubkey)
block2.solve(self.signblockprivkey)
node.p2p.send_blocks_and_test([block2], node, True)
self.log.info(block2)

best_block = node.getblock(node.getbestblockhash())
self.log.info(best_block)
tip = block2.sha256
# tip = int(node.getbestblockhash(), 16)
height = best_block["height"] + 1
block_time = best_block["time"] + 1

self.signblockprivkey = "657440783dd10977c49f87c51dc68b63508e88c7ea9371dc19e6fcd0f5f8639e"
self.log.info("Create 3rd block without aggpubkey")
self.log.info("Create 3rd block")
block3 = create_block(tip, create_coinbase(height), block_time, self.signblockpubkey)
block3.solve(self.signblockprivkey)
node.p2p.send_blocks_and_test([block3], node, True)
self.log.info(block3)
best_block = node.getblock(node.getbestblockhash())
self.log.info(best_block)


if __name__ == '__main__':
InvalidBlockRequestTest().main()
FederationBlockTest().main()

0 comments on commit 5769437

Please sign in to comment.