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

migration tests #513

Merged
merged 2 commits into from
Feb 12, 2025
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
24 changes: 24 additions & 0 deletions contracts/common/Block.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

import "../libraries/CarefulMath.sol";


contract BlockTimestamp is CarefulMath {
event Result(uint256 block_timestamp);

uint256 public a;
uint public accrualBlockTimestamp;
uint public accrualBlockNumber;
Expand All @@ -13,7 +15,9 @@ contract BlockTimestamp is CarefulMath {
uint256 value1;
uint256 value2;
}

mapping(uint256 => Data) public dataByTimestamp;

event DataAdded(uint256 timestamp, uint256 value1, uint256 value2);

constructor() {
Expand Down Expand Up @@ -50,6 +54,7 @@ contract BlockTimestamp is CarefulMath {

accrualBlockNumber = currentBlockTimestamp;
}

function addDataToMapping(uint256 _value1, uint256 _value2) public {
uint256 currentTimestamp = block.timestamp % 1000000;
for (uint256 i = 0; i < 20; i++) {
Expand All @@ -72,6 +77,7 @@ contract BlockTimestamp is CarefulMath {

contract BlockTimestampDeployer {
BlockTimestamp public blockTimestamp;

event Log(address indexed addr);

constructor() {
Expand All @@ -84,6 +90,7 @@ contract BlockTimestampDeployer {
contract BlockNumber is CarefulMath {
event Log(address indexed sender, string message);
event Result(uint256 block_number);

bytes32[64] public b;


Expand All @@ -93,6 +100,7 @@ contract BlockNumber is CarefulMath {
uint256 value1;
uint256 value2;
}

mapping(uint256 => Data) public dataByNumber;
uint256 public a;

Expand Down Expand Up @@ -148,4 +156,20 @@ contract BlockNumber is CarefulMath {

accrualBlockNumber = currentBlockNumber;
}


function accrueInterestIterative() public {
uint currentBlockNumber = block.number;
uint accrualBlockNumberPrior = accrualBlockNumber;

bytes memory result = new bytes(1000);
for (uint256 i = 0; i < 1000; i++) {
result[i] = "a";
}

(MathError mathErr, uint blockDelta) = subUInt(currentBlockNumber, accrualBlockNumberPrior);
require(mathErr == MathError.NO_ERROR, "calc block delta error");

accrualBlockNumber = currentBlockNumber;
}
}
54 changes: 42 additions & 12 deletions integration/tests/migrations/test_account_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@

import os
import time
import typing as tp

import eth_abi
import pytest
from eth_utils import abi

from web3.contract import Contract
from web3.logs import DISCARD

from integration.tests.economy.steps import assert_profit
from eth_account.signers.local import LocalAccount
from utils.erc20wrapper import ERC20Wrapper
from utils.erc721ForMetaplex import ERC721ForMetaplex
from utils.helpers import gen_hash_of_block
from utils.solana_client import SolanaClient
from utils.web3client import NeonChainWeb3Client


@pytest.fixture(scope="class")
def accounts(web3_client):
def accounts(web3_client) -> tp.Generator[list[LocalAccount], None, None]:
print("VERSIONS", web3_client.get_neon_versions())

account_keys = os.environ.get("ACCOUNTS").split(",")
Expand Down Expand Up @@ -138,6 +141,22 @@ def erc721(web3_client, faucet, bob):
return erc721


@pytest.fixture(scope="class")
def block(web3_client, accounts) -> Contract:
contract_address = os.environ.get("BLOCK_ADDRESS")
if contract_address:
contract = web3_client.get_deployed_contract(
contract_address, contract_file="common/Block.sol", contract_name="BlockNumber"
)
print(f"Using BlockNumber deployed earlier at {contract_address}")
else:
contract, _ = web3_client.deploy_and_get_contract(
"common/Block.sol", "0.8.10", contract_name="BlockNumber", account=accounts[0]
)
print(f"BlockNumber deployed at address: {contract.address}")
return contract


def check_counter(sender, contract, web3_client, sol_client):
tx = web3_client.make_raw_tx(sender.address)

Expand Down Expand Up @@ -167,7 +186,7 @@ def get_solana_accounts_by_emulation(web3_client, sender, contract, function_sig
signed_tx = web3_client.eth.account.sign_transaction(tx, sender.key)
result = web3_client.get_neon_emulate(str(signed_tx.rawTransaction.hex())[2:])
print(result)
return [item["pubkey"] for item in result["result"]["solana_accounts"]]
return [item["pubkey"] for item in result["result"]["solanaAccounts"]]


def print_solana_accounts_info(sol_client, accounts, action):
Expand Down Expand Up @@ -197,7 +216,7 @@ def check_operator_balance(
token_diff = web3_client.to_main_currency(token_balance_after - token_balance_before)
assert_profit(sol_diff, sol_price, token_diff, neon_price, web3_client.native_token_name)

def test_transfers(self, alice, bob, accounts, web3_client, trx_list, check_operator_balance):
def test_transfers(self, alice, bob, accounts, web3_client, trx_list):
web3_client.send_neon(alice, bob, 5)
for i in range(5):
receipt = web3_client.send_neon(alice, accounts[i + 1], 5)
Expand All @@ -207,10 +226,10 @@ def test_transfers(self, alice, bob, accounts, web3_client, trx_list, check_oper
trx_list.append(receipt["transactionHash"])
assert receipt["status"] == 1

def test_contract_deploy_economics(self, alice, bob, web3_client, check_operator_balance):
def test_contract_deploy_economics(self, alice, bob, web3_client):
web3_client.deploy_and_get_contract("common/EventCaller", "0.8.12", bob)

def test_contract_deploy_and_interact(self, web3_client, accounts, trx_list, check_operator_balance):
def test_contract_deploy_and_interact(self, web3_client, accounts, trx_list):
acc1 = accounts[7]
acc2 = accounts[8]
contract_a, receipt = web3_client.deploy_and_get_contract(
Expand Down Expand Up @@ -246,13 +265,11 @@ def test_contract_deploy_and_interact(self, web3_client, accounts, trx_list, che
for log in (event_b2_logs, event_c1_logs, event_c2_logs):
assert log == (), f"Trx shouldn't contain logs for the events: eventB2, eventC1, eventC2_log0. Log: {log}"

def test_economics_for_erc721_mint(self, erc721, web3_client, check_operator_balance):
def test_economics_for_erc721_mint(self, erc721, web3_client):
seed = web3_client.text_to_bytes32(gen_hash_of_block(8))
erc721.mint(seed, erc721.account.address, "uri")

def test_erc721_interaction(
self, erc721, web3_client, sol_client, bob, alice, accounts, trx_list, check_operator_balance
):
def test_erc721_interaction(self, erc721, web3_client, sol_client, bob, alice, accounts, trx_list):
seed = web3_client.text_to_bytes32(gen_hash_of_block(8))

solana_accounts = get_solana_accounts_by_emulation(
Expand Down Expand Up @@ -314,7 +331,7 @@ def test_erc721_interaction(
assert balance_usr1_after - balance_usr1_before == -1
assert balance_usr2_after - balance_usr2_before == 1

def test_erc20_interaction(self, erc20, web3_client, bob, alice, accounts, trx_list, check_operator_balance):
def test_erc20_interaction(self, erc20, web3_client, bob, alice, accounts, trx_list):
balance_before = erc20.contract.functions.balanceOf(erc20.account.address).call()
amount = 500
resp = erc20.mint_tokens(erc20.account, erc20.account.address, amount)
Expand All @@ -333,7 +350,7 @@ def test_erc20_interaction(self, erc20, web3_client, bob, alice, accounts, trx_l

balance_before = erc20.contract.functions.balanceOf(tom.address).call()
total_before = erc20.contract.functions.totalSupply().call()
resp = erc20.burn(tom, tom.address, amount)
resp = erc20.burn(tom, amount)
trx_list.append(resp["transactionHash"])

balance_after = erc20.contract.functions.balanceOf(tom.address).call()
Expand Down Expand Up @@ -386,3 +403,16 @@ def test_simple_counter(self, web3_client, accounts, counter, sol_client):
def test_counter_with_map(self, web3_client, accounts, counter_with_map, sol_client):
sender = accounts[9]
check_counter(sender, counter_with_map, web3_client, sol_client)

def test_tx_with_timestamp(
self,
web3_client: NeonChainWeb3Client,
accounts: list[LocalAccount],
block: Contract,
sol_client: SolanaClient,
):
sender = accounts[9]
tx = web3_client.make_raw_tx(sender)
tx = block.functions.accrueInterestIterative().build_transaction(tx)
receipt = web3_client.send_transaction(sender, tx)
assert receipt.status == 1