Skip to content

Commit

Permalink
JSON-RPC output fixes
Browse files Browse the repository at this point in the history
* Fixed the VMState output from JSON-RPC so it properly reflects the same string output as the C# implementation.

* Fixed integer value outputs so that they are represented as a String, which is consistent with the C# implementation.

* Fixed `ContractParameterType` outputs so that they use the String representation instead of a hex representation for consistency with the C# implementation.

CityOfZion#233
  • Loading branch information
Brian Lenz committed Feb 28, 2018
1 parent ab863e5 commit 0e86850
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion neo/SmartContract/ContractParameter.py
Expand Up @@ -47,7 +47,7 @@ def ToParameter(item: StackItem):
return ContractParameter(type=ContractParameterType.ByteArray, value=item.GetByteArray())

elif isinstance(item, Integer):
return ContractParameter(type=ContractParameterType.Integer, value=item.GetBigInteger())
return ContractParameter(type=ContractParameterType.Integer, value=str(item.GetBigInteger()))

elif isinstance(item, InteropInterface):
return ContractParameter(type=ContractParameterType.InteropInterface, value=item.GetInterface())
Expand Down
2 changes: 1 addition & 1 deletion neo/SmartContract/ContractParameterType.py
Expand Up @@ -26,7 +26,7 @@ class ContractParameterType(Enum):
Void = 0xff

def __str__(self):
return str(self.value.to_bytes(1, 'little').hex())
return self.name

@staticmethod
def FromString(val):
Expand Down
15 changes: 15 additions & 0 deletions neo/VM/VMState.py
Expand Up @@ -3,3 +3,18 @@
HALT = 1 << 0
FAULT = 1 << 1
BREAK = 1 << 2


def VMStateStr(_VMState):
if _VMState == NONE:
return "NONE"

state = []
if _VMState & HALT:
state.append("HALT")
if _VMState & FAULT:
state.append("FAULT")
if _VMState & BREAK:
state.append("BREAK")

return ", ".join(state)
3 changes: 2 additions & 1 deletion neo/api/JSONRPC/JsonRpcApi.py
Expand Up @@ -29,6 +29,7 @@
from neo.SmartContract.ApplicationEngine import ApplicationEngine
from neo.SmartContract.ContractParameter import ContractParameter
from neo.VM.ScriptBuilder import ScriptBuilder
from neo.VM.VMState import VMStateStr


class JsonRpcError(Exception):
Expand Down Expand Up @@ -288,7 +289,7 @@ def get_invoke_result(self, script):
appengine = ApplicationEngine.Run(script=script)
return {
"script": script.decode('utf-8'),
"state": appengine.State,
"state": VMStateStr(appengine.State),
"gas_consumed": appengine.GasConsumed().ToString(),
"stack": [ContractParameter.ToParameter(item).ToJson() for item in appengine.EvaluationStack.Items]
}
Expand Down
11 changes: 6 additions & 5 deletions neo/api/JSONRPC/test_json_invoke_rpc_api.py
Expand Up @@ -16,6 +16,7 @@
from neocore.UInt160 import UInt160
import binascii
from neo.VM import VMState
from neo.VM.VMState import VMStateStr


def mock_request(body):
Expand Down Expand Up @@ -66,7 +67,7 @@ def test_invoke_1(self):
req = self._gen_rpc_req("invoke", params=[contract_hash, jsn])
mock_req = mock_request(json.dumps(req).encode("utf-8"))
res = json.loads(self.app.home(mock_req))
self.assertEqual(res['result']['state'], VMState.HALT + VMState.BREAK)
self.assertEqual(res['result']['state'], VMStateStr(VMState.HALT + VMState.BREAK))
self.assertEqual(res['result']['gas_consumed'], '0.205')
results = []
for p in res['result']['stack']:
Expand Down Expand Up @@ -95,7 +96,7 @@ def test_invoke_2(self):
req = self._gen_rpc_req("invoke", params=[contract_hash, jsn])
mock_req = mock_request(json.dumps(req).encode("utf-8"))
res = json.loads(self.app.home(mock_req))
self.assertEqual(res['result']['state'], VMState.HALT + VMState.BREAK)
self.assertEqual(res['result']['state'], VMStateStr(VMState.HALT + VMState.BREAK))
results = []
for p in res['result']['stack']:
results.append(ContractParameter.FromJson(p))
Expand All @@ -108,7 +109,7 @@ def test_invoke_3(self):
req = self._gen_rpc_req("invokefunction", params=[contract_hash, 'symbol'])
mock_req = mock_request(json.dumps(req).encode("utf-8"))
res = json.loads(self.app.home(mock_req))
self.assertEqual(res['result']['state'], VMState.HALT + VMState.BREAK)
self.assertEqual(res['result']['state'], VMStateStr(VMState.HALT + VMState.BREAK))
results = []
for p in res['result']['stack']:
results.append(ContractParameter.FromJson(p))
Expand All @@ -124,7 +125,7 @@ def test_invoke_4(self):
req = self._gen_rpc_req("invokefunction", params=[contract_hash, 'balanceOf', params])
mock_req = mock_request(json.dumps(req).encode("utf-8"))
res = json.loads(self.app.home(mock_req))
self.assertEqual(res['result']['state'], VMState.HALT + VMState.BREAK)
self.assertEqual(res['result']['state'], VMStateStr(VMState.HALT + VMState.BREAK))
results = []
for p in res['result']['stack']:
results.append(ContractParameter.FromJson(p))
Expand All @@ -137,7 +138,7 @@ def test_invoke_5(self):
req = self._gen_rpc_req("invokescript", params=[test_script])
mock_req = mock_request(json.dumps(req).encode("utf-8"))
res = json.loads(self.app.home(mock_req))
self.assertEqual(res['result']['state'], VMState.HALT + VMState.BREAK)
self.assertEqual(res['result']['state'], VMStateStr(VMState.HALT + VMState.BREAK))

results = []
for p in res['result']['stack']:
Expand Down

0 comments on commit 0e86850

Please sign in to comment.