Skip to content

Commit

Permalink
Merge pull request #1713 from skalenetwork/bug/1702-adopt-precompiled…
Browse files Browse the repository at this point in the history
…-oracle

Bug/1702 adopt precompiled oracle
  • Loading branch information
olehnikolaiev authored Nov 23, 2023
2 parents c33639d + d301345 commit 1de9f45
Show file tree
Hide file tree
Showing 14 changed files with 669 additions and 69 deletions.
1 change: 1 addition & 0 deletions libethcore/ChainOperationParams.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ struct SChain {
time_t verifyDaSigsPatchTimestamp = 0;
time_t storageDestructionPatchTimestamp = 0;
time_t powCheckPatchTimestamp = 0;
time_t precompiledConfigPatchTimestamp = 0;
time_t pushZeroPatchTimestamp = 0;
time_t skipInvalidTransactionsPatchTimestamp = 0;

Expand Down
14 changes: 11 additions & 3 deletions libethereum/ChainParams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ ChainParams ChainParams::loadConfig(
sChainObj.at( "powCheckPatchTimestamp" ).get_int64() :
0;

s.precompiledConfigPatchTimestamp =
sChainObj.count( "precompiledConfigPatchTimestamp" ) ?
sChainObj.at( "precompiledConfigPatchTimestamp" ).get_int64() :
0;

s.pushZeroPatchTimestamp = sChainObj.count( "pushZeroPatchTimestamp" ) ?
sChainObj.at( "pushZeroPatchTimestamp" ).get_int64() :
0;
Expand All @@ -288,9 +293,12 @@ ChainParams ChainParams::loadConfig(
auto groupNodesObj = nodeGroupObj["nodes"].get_obj();
for ( const auto& groupNodeConf : groupNodesObj ) {
auto groupNodeConfObj = groupNodeConf.second.get_array();
u256 sChainIndex = groupNodeConfObj[0].get_uint64();
u256 id = groupNodeConfObj[1].get_uint64();
std::string publicKey = groupNodeConfObj[2].get_str();
u256 sChainIndex = groupNodeConfObj.at( 0 ).get_uint64();
u256 id = groupNodeConfObj.at( 1 ).get_uint64();
std::string publicKey = groupNodeConfObj.at( 2 ).get_str();
if ( publicKey.empty() ) {
BOOST_THROW_EXCEPTION( std::runtime_error( "Empty public key in config" ) );
}
groupNodes.push_back( { id, sChainIndex, publicKey } );
}
std::sort( groupNodes.begin(), groupNodes.end(),
Expand Down
36 changes: 25 additions & 11 deletions libethereum/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include <libskale/ContractStorageLimitPatch.h>
#include <libskale/ContractStorageZeroValuePatch.h>
#include <libskale/POWCheckPatch.h>
#include <libskale/PrecompiledConfigPatch.h>
#include <libskale/PushZeroPatch.h>
#include <libskale/RevertableFSPatch.h>
#include <libskale/SkipInvalidTransactionsPatch.h>
Expand Down Expand Up @@ -167,6 +168,7 @@ Client::Client( ChainParams const& _params, int _networkID,
PushZeroPatch::setTimestamp( chainParams().sChain.pushZeroPatchTimestamp );
SkipInvalidTransactionsPatch::setTimestamp(
this->chainParams().sChain.skipInvalidTransactionsPatchTimestamp );
PrecompiledConfigPatch::setTimestamp( chainParams().sChain.precompiledConfigPatchTimestamp );
}


Expand Down Expand Up @@ -313,8 +315,12 @@ void Client::init( WithExisting _forceAction, u256 _networkId ) {
if ( m_dbPath.size() )
Defaults::setDBPath( m_dbPath );

if ( ChainParams().sChain.nodeGroups.size() > 0 )
initIMABLSPublicKey();
if ( chainParams().sChain.nodeGroups.size() > 0 ) {
initHistoricGroupIndex();
} else {
LOG( m_logger ) << "Empty node groups in config. "
"This is OK in tests but not OK in production";
}

// init snapshots for not newly created chains
if ( number() ) {
Expand Down Expand Up @@ -621,7 +627,7 @@ size_t Client::importTransactionsAsBlock(
}

if ( chainParams().sChain.nodeGroups.size() > 0 )
updateIMABLSPublicKey();
updateHistoricGroupIndex();

m_snapshotAgent->doSnapshotIfNeeded( number(), _timestamp );

Expand Down Expand Up @@ -661,6 +667,7 @@ size_t Client::syncTransactions(
POWCheckPatch::lastBlockTimestamp = blockChain().info().timestamp();
PushZeroPatch::lastBlockTimestamp = blockChain().info().timestamp();
SkipInvalidTransactionsPatch::lastBlockTimestamp = blockChain().info().timestamp();
PrecompiledConfigPatch::lastBlockTimestamp = blockChain().info().timestamp();

DEV_WRITE_GUARDED( x_working ) {
assert( !m_working.isSealed() );
Expand Down Expand Up @@ -1288,9 +1295,9 @@ ExecutionResult Client::call( Address const& _from, u256 _value, Address _dest,
return ret;
}

void Client::initIMABLSPublicKey() {
void Client::initHistoricGroupIndex() {
if ( number() == 0 ) {
imaBLSPublicKeyGroupIndex = 0;
historicGroupIndex = 0;
return;
}

Expand All @@ -1302,7 +1309,11 @@ void Client::initIMABLSPublicKey() {
chainParams().sChain.nodeGroups.end(),
[&currentBlockTimestamp](
const dev::eth::NodeGroup& ng ) { return currentBlockTimestamp <= ng.finishTs; } );
assert( it != chainParams().sChain.nodeGroups.end() );

if ( it == chainParams().sChain.nodeGroups.end() ) {
BOOST_THROW_EXCEPTION(
std::runtime_error( "Assertion failed: it == chainParams().sChain.nodeGroups.end()" ) );
}

if ( it != chainParams().sChain.nodeGroups.begin() ) {
auto prevIt = std::prev( it );
Expand All @@ -1311,15 +1322,18 @@ void Client::initIMABLSPublicKey() {
it = prevIt;
}

imaBLSPublicKeyGroupIndex = std::distance( chainParams().sChain.nodeGroups.begin(), it );
historicGroupIndex = std::distance( chainParams().sChain.nodeGroups.begin(), it );
}

void Client::updateIMABLSPublicKey() {
void Client::updateHistoricGroupIndex() {
uint64_t blockTimestamp = blockInfo( hashFromNumber( number() ) ).timestamp();
uint64_t currentFinishTs = chainParams().sChain.nodeGroups[imaBLSPublicKeyGroupIndex].finishTs;
uint64_t currentFinishTs = chainParams().sChain.nodeGroups.at( historicGroupIndex ).finishTs;
if ( blockTimestamp >= currentFinishTs )
++imaBLSPublicKeyGroupIndex;
assert( imaBLSPublicKeyGroupIndex < chainParams().sChain.nodeGroups.size() );
++historicGroupIndex;
if ( historicGroupIndex >= chainParams().sChain.nodeGroups.size() ) {
BOOST_THROW_EXCEPTION( std::runtime_error(
"Assertion failed: historicGroupIndex >= chainParams().sChain.nodeGroups.size())" ) );
}
}

// new block watch
Expand Down
27 changes: 23 additions & 4 deletions libethereum/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,25 @@ class Client : public ClientBase, protected Worker {
}

std::array< std::string, 4 > getIMABLSPublicKey() const {
return chainParams().sChain.nodeGroups[imaBLSPublicKeyGroupIndex].blsPublicKey;
return chainParams().sChain.nodeGroups.at( historicGroupIndex ).blsPublicKey;
}

// get node id for historic node in chain
std::string getHistoricNodeId( unsigned _id ) const {
return chainParams().sChain.nodeGroups.at( historicGroupIndex ).nodes.at( _id ).id.str();
}

// get schain index for historic node in chain
std::string getHistoricNodeIndex( unsigned _idx ) const {
return chainParams()
.sChain.nodeGroups.at( historicGroupIndex )
.nodes.at( _idx )
.schainIndex.str();
}

// get node owner for historic node in chain
std::string getHistoricNodePublicKey( unsigned _idx ) const {
return chainParams().sChain.nodeGroups.at( historicGroupIndex ).nodes.at( _idx ).publicKey;
}

void doStateDbCompaction() const { m_state.getOriginalDb()->doCompaction(); }
Expand Down Expand Up @@ -532,10 +550,11 @@ class Client : public ClientBase, protected Worker {
fs::path m_dbPath;

private:
void initIMABLSPublicKey();
void updateIMABLSPublicKey();
void initHistoricGroupIndex();
void updateHistoricGroupIndex();

unsigned imaBLSPublicKeyGroupIndex = 0;
// which group corresponds to the current block timestamp on this node
unsigned historicGroupIndex = 0;

public:
FILE* performance_fd;
Expand Down
Loading

0 comments on commit 1de9f45

Please sign in to comment.