Skip to content

Commit 1e4954b

Browse files
#355 Resize storage without existing code account (#263)
1 parent 130dc84 commit 1e4954b

File tree

2 files changed

+104
-2
lines changed

2 files changed

+104
-2
lines changed

proxy/plugin/solana_rest_api_tools.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ def create_account_list_by_emulate(signer, client, ethTrx):
850850
if acc_desc["new"] == False:
851851

852852
address = bytes.fromhex(acc_desc["address"][2:])
853-
if acc_desc["code_size_current"] and acc_desc["code_size"]:
853+
if acc_desc["code_size_current"] is not None and acc_desc["code_size"] is not None:
854854
if acc_desc["code_size"] > acc_desc["code_size_current"]:
855855
code_size = acc_desc["code_size"] + 2048
856856
seed = b58encode(ACCOUNT_SEED_VERSION + os.urandom(20))
@@ -863,7 +863,11 @@ def create_account_list_by_emulate(signer, client, ethTrx):
863863
resize_instr.append(TransactionInstruction(
864864
keys=[
865865
AccountMeta(pubkey=PublicKey(acc_desc["account"]), is_signer=False, is_writable=True),
866-
AccountMeta(pubkey=acc_desc["contract"], is_signer=False, is_writable=True),
866+
(
867+
AccountMeta(pubkey=acc_desc["contract"], is_signer=False, is_writable=True)
868+
if acc_desc["contract"] else
869+
AccountMeta(pubkey=PublicKey("11111111111111111111111111111111"), is_signer=False, is_writable=False)
870+
),
867871
AccountMeta(pubkey=code_account_new, is_signer=False, is_writable=True),
868872
AccountMeta(pubkey=signer.public_key(), is_signer=True, is_writable=False)
869873
],

proxy/test_create_account_block.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import unittest
2+
import os
3+
import rlp
4+
from web3 import Web3
5+
from solcx import install_solc
6+
7+
# install_solc(version='latest')
8+
install_solc(version='0.7.0')
9+
from solcx import compile_source
10+
11+
proxy_url = os.environ.get('PROXY_URL', 'http://127.0.0.1:9090/solana')
12+
proxy = Web3(Web3.HTTPProvider(proxy_url))
13+
eth_account = proxy.eth.account.create()
14+
proxy.eth.default_account = eth_account.address
15+
16+
17+
STORAGE_SOLIDITY_SOURCE = '''
18+
pragma solidity >=0.7.0 <0.9.0;
19+
20+
contract Storage {
21+
uint256 number;
22+
/**
23+
* @dev Store value in variable
24+
* @param num value to store
25+
*/
26+
function store(uint256 num) public {
27+
number = num;
28+
}
29+
/**
30+
* @dev Return value
31+
* @return value of 'number'
32+
*/
33+
function retrieve() public view returns (uint256){
34+
return number;
35+
}
36+
}
37+
'''
38+
39+
40+
41+
class Test_createAccountBlock(unittest.TestCase):
42+
@classmethod
43+
def setUpClass(cls):
44+
print("\n\nhttps://github.com/neonlabsorg/proxy-model.py/issues/147")
45+
print('eth_account.address:', eth_account.address)
46+
print('eth_account.key:', eth_account.key.hex())
47+
print('balance:', proxy.eth.get_balance(eth_account.address))
48+
49+
# Create caller account in NeonEVM
50+
cls.deploy_contract(cls)
51+
52+
def deploy_contract(self):
53+
compiled_sol = compile_source(STORAGE_SOLIDITY_SOURCE)
54+
contract_id, contract_interface = compiled_sol.popitem()
55+
storage = proxy.eth.contract(abi=contract_interface['abi'], bytecode=contract_interface['bin'])
56+
trx_deploy = proxy.eth.account.sign_transaction(dict(
57+
nonce=proxy.eth.get_transaction_count(eth_account.address),
58+
chainId=proxy.eth.chain_id,
59+
gas=987654321,
60+
gasPrice=0,
61+
to='',
62+
value=0,
63+
data=storage.bytecode),
64+
eth_account.key
65+
)
66+
67+
trx_deploy_hash = proxy.eth.send_raw_transaction(trx_deploy.rawTransaction)
68+
return proxy.eth.wait_for_transaction_receipt(trx_deploy_hash)
69+
70+
def transfer(self, target_account, value):
71+
trx_transfer = proxy.eth.account.sign_transaction(dict(
72+
nonce=proxy.eth.get_transaction_count(eth_account.address),
73+
chainId=proxy.eth.chain_id,
74+
gas=987654321,
75+
gasPrice=0,
76+
to=bytes(target_account),
77+
value=value),
78+
eth_account.key
79+
)
80+
81+
transfer_hash = proxy.eth.send_raw_transaction(trx_transfer.rawTransaction)
82+
return proxy.eth.wait_for_transaction_receipt(transfer_hash)
83+
84+
def test_blockAccount(self):
85+
nonce = proxy.eth.get_transaction_count(eth_account.address)
86+
expected_contract_address = proxy.keccak(rlp.encode((bytes.fromhex(eth_account.address[2:]), nonce + 1)))[-20:]
87+
88+
# Create expected contract account
89+
transfer_receipt = self.transfer(expected_contract_address, 1_000_000_000)
90+
self.assertEqual(transfer_receipt["status"], 1)
91+
92+
# Try to deploy to expected contract account
93+
deploy_receipt = self.deploy_contract()
94+
self.assertEqual(deploy_receipt["status"], 1)
95+
96+
97+
if __name__ == '__main__':
98+
unittest.main()

0 commit comments

Comments
 (0)