# Initialize the state database and precompiled contract
# 初始化状态数据库和预编译合约
state_db=StateDB()precompile=PrecompiledContract()# Test 1: Valid activation and deactivation
# 测试 1:有效的激活和停用
caller="0x0123"delegated_addr=bytes.fromhex("1122334455667788990011223344556677889900")active_code=PrecompiledContract.DELEGATED_CODE_PREFIX+delegated_addr# Active state # 激活状态
state_db.set_code(caller,active_code)error,gas_left=precompile.execute(caller,state_db,gas=15000)asserterror==b""assertstate_db.get_code(caller)==active_code+b"\x00"# Deactivated state # 停用状态
assertgas_left==15000-PrecompiledContract.PRECOMPILE_GAS_COSTerror,gas_left=precompile.execute(caller,state_db,gas=15000)asserterror==b""assertstate_db.get_code(caller)==active_code# Turns to the active state again # 再次变为激活状态
assertgas_left==15000-PrecompiledContract.PRECOMPILE_GAS_COST# Test 2: Error cases
# 测试 2:错误情况
error,gas_left=precompile.execute(caller,state_db,gas=15000,read_only=True)asserterror==b"STATICCALL disallows state modification"assertgas_left==0error,gas_left=precompile.execute(caller,state_db,gas=PrecompiledContract.PRECOMPILE_GAS_COST-1)asserterror==b"insufficient gas"assertgas_left==0caller="0x4567"# EOA that is not delegated to code # 未委托给代码的 EOA
error,gas_left=precompile.execute(caller,state_db,gas=15000)asserterror==b"the address is not an EOA delegated to code"assertgas_left==0caller="0x89ab"# This is a contract address. # 这是一个合约地址。
state_db.set_code(caller,bytes.fromhex("60006000f3"))error,gas_left=precompile.execute(caller,state_db,gas=15000)asserterror==b"the address is not an EOA delegated to code"assertgas_left==0
参考实现
classPrecompiledContract:DELEGATED_CODE_PREFIX=bytes.fromhex("ef0100")PRECOMPILE_GAS_COST=13000defexecute(self,caller,state_db,gas,read_only=False):"""
Switch the private key state of delegated EOAs between active and deactivated.
在委托 EOA 的活动和停用状态之间切换私钥状态。
Parameters:
- caller: The address calling the contract
- caller: 调用合约的地址
- state_db: The state database
- state_db: 状态数据库
- gas: Gas provided for execution
- gas: 为执行提供的 Gas
- read_only: Whether called in a read-only context
- read_only: 是否在只读上下文中调用
Returns:
- Tuple of (result, gas_left)
- (结果,剩余 gas) 的元组
result: error bytes on failure, empty bytes on success
result: 失败时的错误字节,成功时的空字节
gas_left: remaining gas, 0 on error
gas_left: 剩余 gas,错误时为 0
"""# Check gas
# 检查 gas
ifgas<self.PRECOMPILE_GAS_COST:returnb"insufficient gas",0# Check STATICCALL
# 检查 STATICCALL
ifread_only:returnb"STATICCALL disallows state modification",0# Get and validate caller's code
# 获取并验证调用者的代码
code=state_db.get_code(caller)ifnotcode.startswith(self.DELEGATED_CODE_PREFIX):returnb"the address is not an EOA delegated to code",0# Update delegated code based on length
# 根据长度更新委托代码
iflen(code)==23:# Active state # 激活状态
state_db.set_code(caller,code+b"\x00")# Deactivate # 停用
eliflen(code)==24:# Deactivated state # 停用状态
state_db.set_code(caller,code[:-1])# Activate # 激活
else:# Although this is not possible, it's added for completeness # 虽然这不可能,但为了完整性而添加
returnb"invalid delegated code length",0returnb"",gas-self.PRECOMPILE_GAS_COSTclassStateDB:"""Simplified state database, omitting other account fields""""""简化的状态数据库,省略了其他账户字段"""def__init__(self):self.accounts={}defget_code(self,addr):returnself.accounts.get(addr,{}).get("code",b"")defset_code(self,addr,value):ifaddrnotinself.accounts:self.accounts[addr]={}self.accounts[addr]["code"]=value