Skip to content

Commit

Permalink
https://github.com/neo-project/neo/pull/2237
Browse files Browse the repository at this point in the history
  • Loading branch information
ixje committed Feb 15, 2021
1 parent 5ab2c68 commit 1e20ee0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 23 deletions.
23 changes: 17 additions & 6 deletions neo3/contracts/abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,18 +239,29 @@ def __eq__(self, other):
return (self.methods == other.methods
and self.events == other.events)

def get_method(self, name) -> Optional[contracts.ContractMethodDescriptor]:
def get_method(self, name, parameter_count: int) -> Optional[contracts.ContractMethodDescriptor]:
"""
Return the ContractMethodDescriptor matching the name or None otherwise.
Return the ContractMethodDescriptor matching the name (and optional parameter count) or None otherwise.
Args:
name: the name of the method to return.
parameter_count: the expected number of parameters teh method has.
"""
for m in self.methods:
if m.name == name:
return m
if parameter_count < -1 or parameter_count > 0xFFFF:
raise ValueError("Parameter count is out of range")

if parameter_count >= 0:
for m in self.methods:
if m.name == name and len(m.parameters) == parameter_count:
return m
else:
return None
else:
return None
for m in self.methods:
if m.name == name:
return m
else:
return None

def to_json(self) -> dict:
"""
Expand Down
20 changes: 8 additions & 12 deletions neo3/contracts/applicationengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,23 +338,20 @@ def step_out(self) -> None:

def load_contract(self,
contract: storage.ContractState,
method: str,
flags: contracts.CallFlags,
has_return_value: bool = False) -> Optional[vm.ExecutionContext]:
method_descriptor = contract.manifest.abi.get_method(method)
if method_descriptor is None:
return None
method_descriptor: contracts.ContractMethodDescriptor,
flags: contracts.CallFlags) -> Optional[vm.ExecutionContext]:

rvcount = 0 if method_descriptor.return_type == contracts.ContractParameterType.VOID else 1
context = self.load_script_with_callflags(vm.Script(contract.script),
flags,
method_descriptor.offset,
int(has_return_value),
rvcount,
contract)
# configure state
context.call_flags = int(flags)
context.scripthash_bytes = contract.hash.to_array()

init = contract.manifest.abi.get_method("_initialize")
init = contract.manifest.abi.get_method("_initialize", 0)
if init is not None:
self.load_context(context.clone(init.offset))
return context
Expand Down Expand Up @@ -393,7 +390,7 @@ def _contract_call_internal(self,
if target_contract is None:
raise ValueError("[System.Contract.Call] Can't find target contract")

method_descriptor = target_contract.manifest.abi.get_method(method)
method_descriptor = target_contract.manifest.abi.get_method(method, len(args))
if method_descriptor is None:
raise ValueError(f"[System.Contract.Call] Method '{method}' does not exist on target contract")
return self._contract_call_internal2(target_contract, method_descriptor, flags, has_return_value, args)
Expand Down Expand Up @@ -430,9 +427,8 @@ def _contract_call_internal2(self,
raise ValueError("Return value type does not match")

context_new = self.load_contract(target_contract,
method_descriptor.name,
flags & calling_flags,
has_return_value)
method_descriptor,
flags & calling_flags)
if context_new is None:
raise ValueError
context_new.calling_scripthash_bytes = calling_script_hash_bytes
Expand Down
4 changes: 2 additions & 2 deletions neo3/contracts/interop/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def contract_call(engine: contracts.ApplicationEngine,
if target_contract is None:
raise ValueError("[System.Contract.Call] Can't find target contract")

method_descriptor = target_contract.manifest.abi.get_method(method)
method_descriptor = target_contract.manifest.abi.get_method(method, len(args))
if method_descriptor is None:
raise ValueError(f"[System.Contract.Call] Method '{method}' does not exist on target contract")

Expand Down Expand Up @@ -66,7 +66,7 @@ def native_on_persist(engine: contracts.ApplicationEngine) -> None:
# because ManagementContract.on_persist will call _initialize() on all other native contracts
# which is needed for the other contracts to work properly when their on_persist() is called
management = contracts.ManagementContract()
others: List[Any] = list(contracts.NativeContract._contracts.values())
others: List[Any] = list(contracts.NativeContract()._contracts.values())
others.remove(management)
ordered_contracts = [management] + others
for contract in ordered_contracts:
Expand Down
4 changes: 2 additions & 2 deletions neo3/contracts/native/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def contract_create(self,
engine.snapshot.storages.put(key, storage.StorageItem(contract.to_array()))

engine.push(engine._native_to_stackitem(contract, storage.ContractState))
method_descriptor = contract.manifest.abi.get_method("_deploy")
method_descriptor = contract.manifest.abi.get_method("_deploy", 2)
if method_descriptor is not None:
engine.call_from_native(hash_, hash_, method_descriptor.name, [data, vm.BooleanStackItem(False)])

Expand Down Expand Up @@ -224,7 +224,7 @@ def contract_update(self,
contract.update_counter += 1

if len(nef_file) != 0:
method_descriptor = contract.manifest.abi.get_method("_deploy")
method_descriptor = contract.manifest.abi.get_method("_deploy", 2)
if method_descriptor is not None:
engine.call_from_native(self.hash,
contract.hash,
Expand Down
2 changes: 1 addition & 1 deletion neo3/contracts/native/nativecontract.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def init(self):
sb.emit_push(self.id)
sb.emit_syscall(1736177434) # "System.Contract.CallNative"
self._script: bytes = sb.to_array()
self.nef = contracts.NEF("ScriptBuilder", "3.0", self._script)
self.nef = contracts.NEF("neo-core-v3.0", self._script)
sender = types.UInt160.zero() # OpCode.PUSH1
sb = vm.ScriptBuilder()
sb.emit(vm.OpCode.ABORT)
Expand Down

0 comments on commit 1e20ee0

Please sign in to comment.