Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
kladkogex committed Sep 20, 2023
1 parent 23702e5 commit f4ff5ab
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 66 deletions.
3 changes: 2 additions & 1 deletion libskale/ContractStorageZeroValuePatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ time_t ContractStorageZeroValuePatch::contractStorageZeroValuePatchTimestamp = 0
time_t ContractStorageZeroValuePatch::lastBlockTimestamp = 0;

bool ContractStorageZeroValuePatch::isEnabled() {
return true;
if ( contractStorageZeroValuePatchTimestamp == 0 ) {
return false;
return true;
}
return contractStorageZeroValuePatchTimestamp <= lastBlockTimestamp;
}
6 changes: 6 additions & 0 deletions libskale/OverlayDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,21 @@ void OverlayDB::commitStorageValues() {
for ( auto const& addressStoragePair : m_storageCache ) {
h160 const& address = addressStoragePair.first;
unordered_map< h256, h256 > const& storage = addressStoragePair.second;

std::cerr << "COMMITTING ADDRESS" << address << std::endl;

for ( auto const& stateAddressValuePair : storage ) {
h256 const& storageAddress = stateAddressValuePair.first;
h256 const& value = stateAddressValuePair.second;

std::cerr << "COMMITTING" << value << std::endl;

static const h256 ZERO_VALUE( 0 );

if ( ContractStorageZeroValuePatch::isEnabled() && value == ZERO_VALUE ) {
// if the value is zero, the pair will be deleted in LevelDB
// if it exists
std::cerr << "KILLING IN DATABASE" << std::endl;
m_db_face->kill(
skale::slicing::toSlice( getStorageKey( address, storageAddress ) ) );
} else {
Expand Down
13 changes: 12 additions & 1 deletion libskale/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@ void State::setStorage( Address const& _contract, u256 const& _key, u256 const&
void State::clearStorageValue(
Address const& _contract, u256 const& _key, u256 const& _currentValue ) {
m_changeLog.emplace_back( _contract, _key, _currentValue );

m_cache[_contract].setStorage( _key, 0 );

int count;
Expand Down Expand Up @@ -786,7 +787,8 @@ u256 State::originalStorageValue( Address const& _contract, u256 const& _key ) c
void State::clearStorage( Address const& _contract ) {
// only clear storage if the storage used is not 0

cdebug << "Self-destructing" << _contract;
cerr << "Clearing storage for " << _contract << endl;


Account* acc = account( _contract );
dev::s256 accStorageUsed = acc->storageUsed();
Expand All @@ -795,16 +797,25 @@ void State::clearStorage( Address const& _contract ) {
return;
}


cdebug << "Self-destructing" << _contract;

// clearStorage is called from functions that already hold a read
// or write lock over the state Therefore, we can use
// storage_WITHOUT_LOCK() here
for ( auto const& hashPairPair : storage_WITHOUT_LOCK( _contract ) ) {
auto const& key = hashPairPair.second.first;
auto const& value = hashPairPair.second.first;
clearStorageValue( _contract, key, value );
cerr << "CLEARED STORAGE VALUE" << endl;
acc->setStorageCache( key, 0 );
h256 ZERO(0);
// clear in database
//m_db_ptr->insert(_contract, key, ZERO);
}

m_db_ptr->commitStorageValues();

totalStorageUsed_ -= ( accStorageUsed + storageUsage[_contract] );
acc->updateStorageUsage( -accStorageUsed );
}
Expand Down
2 changes: 1 addition & 1 deletion libskale/StorageDestructionPatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ time_t StorageDestructionPatch::lastBlockTimestamp;

bool StorageDestructionPatch::isEnabled() {
if ( storageDestructionPatchTimestamp == 0 ) {
return false;
return true;
}
return storageDestructionPatchTimestamp <= lastBlockTimestamp;
}
8 changes: 1 addition & 7 deletions test/historicstate/hardhat/contracts/Lock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,14 @@ contract Lock {
}





function initialize() public {
require(!initialized, "Contract instance has already been initialized");
initialized = true;
constructor() public {

name = "Lock";
symbol = "LOCK";
decimals = 18;
owner = msg.sender;
counter = 1;


mint(10000000000000000000000000000000000000000);


Expand Down
57 changes: 1 addition & 56 deletions test/historicstate/hardhat/scripts/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ async function deployContractsProxy() {

console.log(`Contract deploy`);


const Lock = await hre.ethers.getContractFactory("Lock");
const lock = await Lock.deploy();

Expand All @@ -134,23 +133,11 @@ async function deployContractsProxy() {
console.log(`Contract deployed to ${lockContract.address} at block ${deployBn}`);


// b = await lockContract.balanceOf(OWNER_ADDRESS, {blockTag : previousBlock});
// owner = await lockContract.owner({blockTag : previousBlock});
// console.log(`Contract owner is ${owner}`);

// CHECK(b == INITIAL_MINT)


console.log(`Now writing 10,000 values into the state`);

transferReceipt = await lockContract.writeValues();
await transferReceipt.wait();

previousBlock = await waitUntilNextBlock();

console.log(`Now testing self-destruct`);

transferReceipt2 = await lockContract.die("0x690b9a9e9aa1c9db991c7721a92d351db4fac990");
transferReceipt2 = await lockContract.die("0x690b9a9e9aa1c9db991c7721a92d351db4fac991");
await transferReceipt2.wait();

console.log(`Successfully self destructed`);
Expand All @@ -160,13 +147,6 @@ async function deployContractsProxy() {

console.log(`PASSED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!`);


const MultiSend = await hre.ethers.getContractFactory("MultiSend");
const multiSend = await MultiSend.deploy({value: ethers.utils.parseEther("100000")});
multiSendContract = await multiSend.deployed();
console.log(`Multisend deployed to ${multiSend.address}`);


}


Expand All @@ -188,41 +168,6 @@ async function main() {
await deployContractsProxy();


await multisend();


currentTime = Date.now();
for (let i = 0; i < 10000000; i++) {
console.log("Sending batch of transactions ...");

promises = [];

for (let k = 0; k < WALLETS_COUNT; k++) {
promises.push(lockContract.connect(wallets[k]).store());
}

console.log("Sent");


for (let k = 0; k < WALLETS_COUNT; k++) {
await promises[k];
}

NUMBER_OF_READS = 100;

console.log(`Calling block number ${NUMBER_OF_READS} times ...`);
for (let j = 0; j < 1; j++) {
//await hre.ethers.provider.getBlockNumber();
balance = await wallets[0].getBalance();
//await lockContract.blockNumber();
}


console.log("Execution time:" + (Date.now() - currentTime))
currentTime = Date.now();
}


}


Expand Down

0 comments on commit f4ff5ab

Please sign in to comment.