Skip to content

Commit

Permalink
Decode msgpack and json consistently (#566)
Browse files Browse the repository at this point in the history
* Use msgpack's decoder

* raw quote is a byte-array, not string

* Schema changed

* Remove manual re-encode
  • Loading branch information
eddyashton authored Nov 18, 2019
1 parent e66f5ce commit 91c7295
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 38 deletions.
34 changes: 17 additions & 17 deletions samples/apps/txregulator/tests/txregulatorclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def run(args):
)
check(
c.rpc("REG_get", {"id": regulator[0]}),
result=[regulator[1].encode(), regulator[2].encode()],
result=[regulator[1], regulator[2]],
)

check(
Expand All @@ -77,7 +77,7 @@ def run(args):
check = infra.checker.Checker()

check(c.rpc("BK_register", {"country": bank[1]}), result=bank[0])
check(c.rpc("BK_get", {"id": bank[0]}), result=bank[1].encode())
check(c.rpc("BK_get", {"id": bank[0]}), result=bank[1])

check(
c.rpc("REG_register", {"country": bank[1]}),
Expand Down Expand Up @@ -109,31 +109,31 @@ def run(args):
check(
c.rpc("TX_get", {"tx_id": tx_id}),
result={
"amt": amount.encode(),
"amt": amount,
"bank_id": bank[0],
"dst": transaction["dst"].encode(),
"dst_country": transaction["dst_country"].encode(),
"src": transaction["src"].encode(),
"src_country": transaction["src_country"].encode(),
"timestamp": transaction["timestamp"].encode(),
"type": transaction["type"].encode(),
"dst": transaction["dst"],
"dst_country": transaction["dst_country"],
"src": transaction["src"],
"src_country": transaction["src_country"],
"timestamp": transaction["timestamp"],
"type": transaction["type"],
},
)
if float(amount) > flagged_amt:
check(
c.rpc("FLAGGED_TX_get", {"tx_id": tx_id}),
result=[reg_id, False, transaction["timestamp"].encode()],
result=[reg_id, False, transaction["timestamp"]],
)
flagged_tx = {
"amt": amount.encode(),
"amt": amount,
"bank_id": bank[0],
"dst": transaction["dst"].encode(),
"dst_country": transaction["dst_country"].encode(),
"src": transaction["src"].encode(),
"src_country": transaction["src_country"].encode(),
"timestamp": transaction["timestamp"].encode(),
"dst": transaction["dst"],
"dst_country": transaction["dst_country"],
"src": transaction["src"],
"src_country": transaction["src_country"],
"timestamp": transaction["timestamp"],
"tx_id": tx_id,
"type": transaction["type"].encode(),
"type": transaction["type"],
}
flagged_ids.append(tx_id)
flagged_txs[tx_id] = flagged_tx
Expand Down
7 changes: 6 additions & 1 deletion sphinx/source/schemas/getQuotes_result.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
"type": "number"
},
"raw": {
"type": "string"
"items": {
"maximum": 255,
"minimum": 0,
"type": "number"
},
"type": "array"
}
},
"required": [
Expand Down
2 changes: 1 addition & 1 deletion src/node/nodestate.h
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ namespace ccf
{
GetQuotes::Quote quote;
quote.node_id = nid;
quote.raw = std::string(ni.quote.begin(), ni.quote.end());
quote.raw = ni.quote;

#ifdef GET_QUOTE
oe_report_t parsed_quote = {0};
Expand Down
4 changes: 2 additions & 2 deletions src/node/rpc/nodecalltypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ namespace ccf
struct Quote
{
NodeId node_id = {};
std::string raw = {};
std::vector<uint8_t> raw = {};

std::string error = {};
std::string mrenclave = {};
std::string mrenclave = {}; // < Hex-encoded
};

struct Out
Expand Down
2 changes: 1 addition & 1 deletion tests/governance.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def run(args):
assert len(quotes) == len(hosts)
primary_quote = quotes[0]
assert primary_quote["node_id"] == 0
mrenclave = primary_quote["mrenclave"].decode()
mrenclave = primary_quote["mrenclave"]

oed = subprocess.run(
[args.oesign, "dump", "-e", f"{args.package}.so.signed"],
Expand Down
19 changes: 3 additions & 16 deletions tests/infra/jsonrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,27 +108,14 @@ def to_dict(self):
return d

def _from_parsed(self, parsed):
def decode(sl, is_key=False):
if is_key and hasattr(sl, "decode"):
return sl.decode()
if hasattr(sl, "items"):
return {decode(k, is_key=True): decode(v) for k, v in sl.items()}
elif isinstance(sl, list):
return [decode(e) for e in sl]
else:
return sl

parsed_s = {
decode(attr, is_key=True): decode(value) for attr, value in parsed.items()
}
unexpected = parsed_s.keys() - self._attrs
unexpected = parsed.keys() - self._attrs
if unexpected:
raise ValueError("Unexpected keys in response: {}".format(unexpected))
for attr, value in parsed_s.items():
for attr, value in parsed.items():
setattr(self, attr, value)

def from_msgpack(self, data):
parsed = msgpack.unpackb(data)
parsed = msgpack.unpackb(data, raw=False)
self._from_parsed(parsed)

def from_json(self, data):
Expand Down

0 comments on commit 91c7295

Please sign in to comment.