Skip to content

Commit

Permalink
Refactor transaction sending #463
Browse files Browse the repository at this point in the history
  • Loading branch information
afalaleev committed Jan 22, 2022
1 parent 2f7fc37 commit 82aba19
Show file tree
Hide file tree
Showing 12 changed files with 874 additions and 706 deletions.
5 changes: 1 addition & 4 deletions proxy/common_neon/costs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import base58
import psycopg2

from ..environment import EVM_LOADER_ID, WRITE_TRANSACTION_COST_IN_DB
from ..environment import EVM_LOADER_ID
from ..indexer.sql_dict import POSTGRES_USER, POSTGRES_HOST, POSTGRES_DB, POSTGRES_PASSWORD

class SQLCost():
Expand Down Expand Up @@ -48,9 +48,6 @@ def insert(self, hash, cost, used_gas, sender, to_address, sig, status, reason):


def update_transaction_cost(receipt, eth_trx, extra_sol_trx=False, reason=None):
if not WRITE_TRANSACTION_COST_IN_DB:
return

cost = receipt['meta']['preBalances'][0] - receipt['meta']['postBalances'][0]
if eth_trx:
hash = eth_trx.hash_signed().hex()
Expand Down
17 changes: 13 additions & 4 deletions proxy/common_neon/eth_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(self, *args, **kwargs):
@classmethod
def fromString(cls, s):
return rlp.decode(s, Trx)

def chainId(self):
# chainid*2 + 35 xxxxx0 + 100011 xxxx0 + 100010 +1
# chainid*2 + 36 xxxxx0 + 100100 xxxx0 + 100011 +1
Expand All @@ -56,16 +56,25 @@ def unsigned_msg(self):
def signature(self):
return keys.Signature(vrs=[1 if self.v % 2 == 0 else 0, self.r, self.s]).to_bytes()

def sender(self):
def _sender(self):
hash = keccak_256(self.unsigned_msg()).digest()
sig = keys.Signature(vrs=[1 if self.v%2==0 else 0, self.r, self.s])
sig = keys.Signature(vrs=[1 if self.v % 2 == 0 else 0, self.r, self.s])
pub = sig.recover_public_key_from_msg_hash(hash)
return pub.to_canonical_address().hex()
return pub.to_canonical_address()

def sender(self):
return self._sender().hex()

def hash_signed(self):
return keccak_256(rlp.encode((self.nonce, self.gasPrice, self.gasLimit, self.toAddress, self.value, self.callData,
self.v, self.r, self.s))).digest()

def contract(self):
if self.toAddress:
return None
contract_addr = rlp.encode((self._sender(), self.nonce))
return keccak_256(contract_addr).digest()[-20:].hex()

#class JsonEncoder(json.JSONEncoder):
# def default(self, obj):
# if isinstance(obj, bytes):
Expand Down
18 changes: 10 additions & 8 deletions proxy/common_neon/neon_instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def create_collateral_pool_address(collateral_pool_index):

def create_account_with_seed_trx(self, account, seed, lamports, space):
seed_str = str(seed, 'utf8')
logger.debug("createAccountWithSeedTrx base(%s) account(%s) seed(%s)", type(self.operator_account),account, seed_str)
logger.debug(f"createAccountWithSeedTrx {self.operator_account} account({account} seed({seed_str})")
return TransactionInstruction(
keys=[
AccountMeta(pubkey=self.operator_account, is_signer=True, is_writable=True),
Expand All @@ -134,6 +134,8 @@ def create_account_with_seed_trx(self, account, seed, lamports, space):


def make_create_eth_account_trx(self, eth_address: EthereumAddress, code_acc=None) -> Tuple[Transaction, PublicKey]:
if isinstance(eth_address, str):
eth_address = EthereumAddress(eth_address)
pda_account, nonce = ether2program(eth_address)
neon_token_account = getTokenAddr(PublicKey(pda_account))
logger.debug(f'Create eth account: {eth_address}, sol account: {pda_account}, neon_token_account: {neon_token_account}, nonce: {nonce}')
Expand Down Expand Up @@ -220,15 +222,15 @@ def make_trx_with_create_and_airdrop(self, eth_account, code_acc=None) -> Transa
return trx


def make_resize_instruction(self, acc_desc, code_account_new, seed) -> TransactionInstruction:
def make_resize_instruction(self, account, code_account_old, code_account_new, seed) -> TransactionInstruction:
return TransactionInstruction(
program_id = EVM_LOADER_ID,
data = bytearray.fromhex("11") + bytes(seed), # 17- ResizeStorageAccount
keys = [
AccountMeta(pubkey=PublicKey(acc_desc["account"]), is_signer=False, is_writable=True),
AccountMeta(pubkey=PublicKey(account), is_signer=False, is_writable=True),
(
AccountMeta(pubkey=acc_desc["contract"], is_signer=False, is_writable=True)
if acc_desc["contract"] else
AccountMeta(pubkey=code_account_old, is_signer=False, is_writable=True)
if code_account_old else
AccountMeta(pubkey=PublicKey("11111111111111111111111111111111"), is_signer=False, is_writable=False)
),
AccountMeta(pubkey=code_account_new, is_signer=False, is_writable=True),
Expand Down Expand Up @@ -300,7 +302,7 @@ def make_cancel_transaction(self) -> Transaction:
))


def make_partial_call_or_continue_instruction(self, steps: int = 0) -> TransactionInstruction:
def make_partial_call_or_continue_instruction(self, steps=0) -> TransactionInstruction:
data = bytearray.fromhex("0D") + self.collateral_pool_index_buf + steps.to_bytes(8, byteorder="little") + self.msg
return TransactionInstruction(
program_id = EVM_LOADER_ID,
Expand All @@ -322,14 +324,14 @@ def make_partial_call_or_continue_instruction(self, steps: int = 0) -> Transacti
)


def make_partial_call_or_continue_transaction(self, steps: int = 0, length_before: int = 0) -> Transaction:
def make_partial_call_or_continue_transaction(self, steps=0, length_before=0) -> Transaction:
trx = Transaction()
trx.add(self.make_keccak_instruction(length_before + 1, len(self.eth_trx.unsigned_msg()), 13))
trx.add(self.make_partial_call_or_continue_instruction(steps))
return trx


def make_partial_call_or_continue_from_account_data(self, steps, index=None) -> Transaction:
def make_partial_call_or_continue_from_account_data(self, steps, index=0) -> Transaction:
data = bytearray.fromhex("0E") + self.collateral_pool_index_buf + steps.to_bytes(8, byteorder='little')
if index:
data = data + index.to_bytes(8, byteorder="little")
Expand Down
Loading

0 comments on commit 82aba19

Please sign in to comment.