Skip to content

Commit

Permalink
tx-exec: refactor predefined cu limits
Browse files Browse the repository at this point in the history
  • Loading branch information
afalaleev committed Nov 28, 2024
1 parent 9e8a4d6 commit c632c7e
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 37 deletions.
7 changes: 7 additions & 0 deletions common/neon/neon_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ class NeonProg:
TreasuryGas: ClassVar[int] = 0
BaseGas: ClassVar[int] = SignatureGas + 0

# Holder IX CUs limit
CuLimitHolderWrite: Final[int] = 25_000
CuLimitHolderCreate: Final[int] = 7_500
CuLimitHolderDestroy: Final[int] = 7_500
# Operator balance CUs limit
CuLimitOpCreateBalance: Final[int] = 150_000

# The notation is as follows:
# The first, without +, goes required accounts for the Neon instruction.
# Then, with + prefix, follows accounts that aren't listed in the Neon instruction, but are a part of Solana
Expand Down
6 changes: 6 additions & 0 deletions common/solana/alt_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ class SolAltProg:
MaxTxAccountCnt: Final[int] = 27
MaxAltAccountCnt: Final[int] = _alt.LOOKUP_TABLE_MAX_ADDRESSES

# CU limits for instructions
CuLimitCreate: Final[int] = 3_400
CuLimitExtend: Final[int] = 2_200
CuLimitDeactivate: Final[int] = 3_000
CuLimitClose: Final[int] = 3_000

def __init__(self, payer: SolPubKey) -> None:
self._payer = payer

Expand Down
4 changes: 4 additions & 0 deletions common/solana/cb_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ class SolCuIxCode(IntEnum):

class SolCbProg:
ID: Final[SolPubKey] = SolPubKey.from_raw(_cb.ID)
# CUs limit
MaxCuLimit: Final[int] = SOLANA_MAX_CU_LIMIT
DefCuLimit: Final[int] = SOLANA_DEFAULT_CU_LIMIT
# HEAP size
MaxHeapSize: Final[int] = SOLANA_MAX_HEAP_SIZE
# CU prices less than 10_000 doesn't work
BaseCuPrice: Final[int] = 10_500
# Base unit
MicroLamport: Final[int] = (10 ** 6)

@classmethod
Expand Down
26 changes: 13 additions & 13 deletions common/solana_rpc/alt_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ def clear(self) -> None:
self._extend_alt_tx_list.clear()

@property
def tx_list_list(self) -> list[list[SolTx]]:
def tx_list_list(self) -> Sequence[Sequence[SolTx]]:
return self._built_tx_list_list()

@reset_cached_method
def _built_tx_list_list(self) -> list[list[SolTx]]:
def _built_tx_list_list(self) -> Sequence[Sequence[SolTx]]:
tx_list_list: list[list[SolTx]] = list()

if self._create_alt_tx_list:
Expand Down Expand Up @@ -109,13 +109,13 @@ def build_alt_tx_set(self, alt: SolAltInfo) -> SolAltTxSet:
# Tx to create an Address Lookup Table
create_alt_tx_list: list[SolLegacyTx] = list()
if not is_alt_exist:
ix_list = list()
if self._cu_price:
ix_list.append(self._cb_prog.make_cu_price_ix(self._cu_price))
ix_list.append(self._cb_prog.make_cu_limit_ix(3_400))
ix_list.append(self._alt_prog.make_create_alt_ix(alt.ident))
ix_list = tuple([
self._cb_prog.make_cu_price_ix(self._cu_price),
self._cb_prog.make_cu_limit_ix(self._alt_prog.CuLimitCreate),
self._alt_prog.make_create_alt_ix(alt.ident),

create_alt_tx = SolLegacyTx(name=self._create_name, ix_list=tuple(ix_list))
])
create_alt_tx = SolLegacyTx(name=self._create_name, ix_list=ix_list)
create_alt_tx_list.append(create_alt_tx)

# List of accounts to write to the Address Lookup Table
Expand All @@ -126,11 +126,11 @@ def build_alt_tx_set(self, alt: SolAltInfo) -> SolAltTxSet:
max_tx_acct_cnt = SolAltProg.MaxTxAccountCnt
while acct_list:
acct_list_part, acct_list = acct_list[:max_tx_acct_cnt], acct_list[max_tx_acct_cnt:]
ix_list = list()
if self._cu_price:
ix_list.append(self._cb_prog.make_cu_price_ix(self._cu_price))
ix_list.append(self._cb_prog.make_cu_limit_ix(2_200))
ix_list.append(self._alt_prog.make_extend_alt_ix(alt.ident, acct_list_part))
ix_list = tuple([
self._cb_prog.make_cu_price_ix(self._cu_price),
self._cb_prog.make_cu_limit_ix(self._alt_prog.CuLimitExtend),
self._alt_prog.make_extend_alt_ix(alt.ident, acct_list_part),
])
tx = SolLegacyTx(name=self._extend_name, ix_list=ix_list)
extend_alt_tx_list.append(tx)

Expand Down
18 changes: 11 additions & 7 deletions proxy/base/rpc_gas_limit_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,22 @@ def _neon_prog(self) -> NeonProg:

def _sol_tx_from_eth_tx(self, eth_tx: EthTx, resp: EmulNeonCallResp) -> SolLegacyTx:
cb_prog = self._cb_prog
ix_list = [
cb_prog.make_cu_price_ix(cb_prog.BaseCuPrice),
cb_prog.make_heap_size_ix(cb_prog.MaxHeapSize),
cb_prog.make_cu_limit_ix(cb_prog.MaxCuLimit),
]

neon_prog = self._neon_prog
neon_prog.init_neon_tx(EthTxHash.from_raw(eth_tx.neon_tx_hash), eth_tx.to_bytes())
neon_prog.init_account_meta_list(resp.sol_account_meta_list)
ix_list.append(neon_prog.make_tx_step_from_data_ix(NeonIxMode.Default, self._cfg.max_emulate_evm_step_cnt, 101))

sol_tx = SolLegacyTx(name="Estimate", ix_list=tuple(ix_list))
neon_ix = neon_prog.make_tx_step_from_data_ix(NeonIxMode.Default, self._cfg.max_emulate_evm_step_cnt, 101)

ix_list = tuple([
cb_prog.make_cu_price_ix(cb_prog.BaseCuPrice),
cb_prog.make_heap_size_ix(cb_prog.MaxHeapSize),
cb_prog.make_cu_limit_ix(cb_prog.MaxCuLimit),
neon_ix,
])


sol_tx = SolLegacyTx(name="Estimate", ix_list=ix_list)
sol_tx.recent_block_hash = SolBlockHash.fake()
return sol_tx

Expand Down
8 changes: 4 additions & 4 deletions proxy/executor/alt_destroyer.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,19 @@ async def _destroy_alt(self, signer_list: Sequence[SolPubKey], alt: _NeonAltInfo

async def _deactivate_alt(self, alt: SolAltID) -> bool:
ix = SolAltProg(alt.owner).make_deactivate_alt_ix(alt)
return await self._send_tx(alt, SolAltIxCode.Deactivate.name, ix)
return await self._send_tx(alt, SolAltIxCode.Deactivate.name, SolAltProg.CuLimitDeactivate, ix)

async def _close_alt(self, alt: SolAltID) -> bool:
ix = SolAltProg(alt.owner).make_close_alt_ix(alt)
return await self._send_tx(alt, SolAltIxCode.Close.name, ix)
return await self._send_tx(alt, SolAltIxCode.Close.name, SolAltProg.CuLimitClose, ix)

async def _send_tx(self, alt: SolAltID, name: str, ix: SolTxIx) -> bool:
async def _send_tx(self, alt: SolAltID, name: str, cu_limit: int, ix: SolTxIx) -> bool:
tx_list_signer = OpTxListSigner(dict(alt=alt.ctx_id), alt.owner, self._op_client)
watch_session = SolWatchTxSession(self._cfg, self._sol_client)

cb_prog = SolCbProg()
cu_price_ix = cb_prog.make_cu_price_ix(self._cfg.def_simple_cu_price)
cu_limit_ix = cb_prog.make_cu_limit_ix(3_000)
cu_limit_ix = cb_prog.make_cu_limit_ix(cu_limit)

tx = SolLegacyTx(name=name + "LookupTable", ix_list=[cu_price_ix, cu_limit_ix, ix])
return await SolTxListSender(self._cfg, self._stat_client, watch_session, tx_list_signer).send(tuple([tx]))
Expand Down
2 changes: 1 addition & 1 deletion proxy/executor/strategy_stage_write_holder.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def build_tx_list(self) -> list[list[SolTx]]:
ix_list = list()
if cu_price:
ix_list.append(SolCbProg.make_cu_price_ix(cu_price))
ix_list.append(SolCbProg.make_cu_limit_ix(7_500))
ix_list.append(SolCbProg.make_cu_limit_ix(neon_prog.CuLimitHolderWrite))
ix_list.append(neon_prog.make_write_ix(holder_msg_offset, holder_msg_part))

tx_list.append(SolLegacyTx(name=self.name, ix_list=ix_list))
Expand Down
15 changes: 9 additions & 6 deletions proxy/operator_resource/resource_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ async def _create_holder_acct(self, signer: SolSigner, op_holder: OpHolderInfo)
neon_prog = NeonProg(signer.pubkey).init_holder_address(op_holder.address)

cu_price_ix = cb_prog.make_cu_price_ix(self._cu_price)
cu_limit_ix = cb_prog.make_cu_limit_ix(7_500)
cu_limit_ix = cb_prog.make_cu_limit_ix(neon_prog.CuLimitHolderCreate)

create_acct_ix = sys_prog.make_create_account_with_seed_ix(
address=op_holder.address,
Expand Down Expand Up @@ -551,10 +551,12 @@ async def _delete_holder_acct(self, signer: SolSigner, op_holder: OpHolderInfo)

async def _delete_holder_by_address(self, signer: SolSigner, holder_address: SolPubKey) -> bool:
cb_prog = SolCbProg()
neon_prog = NeonProg(signer.pubkey).init_holder_address(holder_address)

cu_price_ix = cb_prog.make_cu_price_ix(self._cu_price)
cu_limit_ix = cb_prog.make_cu_limit_ix(7_500)
cu_limit_ix = cb_prog.make_cu_limit_ix(neon_prog.CuLimitHolderCreate)

delete_ix = NeonProg(signer.pubkey).init_holder_address(holder_address).make_delete_holder_ix()
delete_ix = neon_prog.make_delete_holder_ix()
tx = SolLegacyTx(name="deleteHolderAccount", ix_list=tuple([cu_price_ix, cu_limit_ix, delete_ix]))
if result := await self._send_tx(signer, tx):
self._deleted_holder_addr_set.add(holder_address)
Expand All @@ -563,11 +565,12 @@ async def _delete_holder_by_address(self, signer: SolSigner, holder_address: Sol
async def _validate_neon_acct_list(self, op_signer: OpSignerInfo, evm_cfg: EvmConfigModel) -> bool:
assert evm_cfg is not None

cb_prog = SolCbProg
cb_prog = SolCbProg()
neon_prog = NeonProg(op_signer.owner)

cu_price_ix = cb_prog.make_cu_price_ix(self._cu_price)
cu_limit_ix = cb_prog.make_cu_limit_ix(150_000)
cu_limit_ix = cb_prog.make_cu_limit_ix(neon_prog.CuLimitOpCreateBalance)

neon_prog = NeonProg(op_signer.owner)
token_sol_addr_dict: dict[int, SolPubKey] = dict()
ix_list: list[SolTxIx] = list()
for token in evm_cfg.token_dict.values():
Expand Down
16 changes: 10 additions & 6 deletions proxy_client/alt_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,23 @@ async def _destroy_alt(
cb_prog = SolCbProg()

cu_price_ix = cb_prog.make_cu_price_ix(self._cu_price)
cu_limit_ix = cb_prog.make_cu_limit_ix(10_000)
cu_limit_ix = cb_prog.make_cu_limit_ix(SolAltProg.CuLimitDeactivate)

ix_list: list[SolTxIx] = [cu_price_ix, cu_limit_ix]
alt_id = SolAltID(address=alt.address, owner=alt.owner, recent_slot=0, nonce=0)
if not alt.is_deactivated:
name = SolAltIxCode.Deactivate.name + "LookupTable"
ix_list.append(SolAltProg(alt.owner).make_deactivate_alt_ix(alt_id))
ix_code = SolAltIxCode.Deactivate
cu_limit = SolAltProg.CuLimitDeactivate
alt_ix = SolAltProg(alt.owner).make_deactivate_alt_ix(alt_id)
_LOG.debug("deactivate Address Lookup Table %s", address)
else:
name = SolAltIxCode.Close.name + "LookupTable"
ix_list.append(SolAltProg(alt.owner).make_close_alt_ix(alt_id))
ix_code = SolAltIxCode.Close
cu_limit = SolAltProg.CuLimitClose
alt_ix = SolAltProg(alt.owner).make_close_alt_ix(alt_id)
_LOG.debug("close Address Lookup Table %s", address)

name = ix_code.name + "LookupTable"

ix_list: Sequence[SolTxIx] = tuple([cu_price_ix, cu_limit_ix, alt_ix])
blockhash, _ = await sol_client.get_recent_blockhash(SolCommit.Finalized)
tx = SolLegacyTx(name=name, ix_list=ix_list, blockhash=blockhash)

Expand Down

0 comments on commit c632c7e

Please sign in to comment.