Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getVersionOfPreviousWrite to TypeScript TypedKvMap #5451

Merged
merged 8 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
[4.0.5]: https://github.com/microsoft/CCF/releases/tag/ccf-4.0.5

- Debug logging is now available in non-SGX builds by default, and controlled by a run-time CLI argument (`--enclave-log-level`). On SGX this remains a build-time decision (#5375).
- Added `getVersionOfPreviousWrite` to TypeScript `TypedKvMap` interface (#5451).

## [4.0.4]

Expand Down
4 changes: 4 additions & 0 deletions js/ccf-app/src/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export class TypedKvMap<K, V> {
return v === undefined ? undefined : this.vt.decode(v);
}

getVersionOfPreviousWrite(key: K): number | undefined {
return this.kv.getVersionOfPreviousWrite(this.kt.encode(key));
}

set(key: K, value: V): TypedKvMap<K, V> {
this.kv.set(this.kt.encode(key), this.vt.encode(value));
return this;
Expand Down
27 changes: 27 additions & 0 deletions tests/js-modules/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,19 +781,41 @@ def test_npm_app(network, args):
r = c.get("/app/log?id=42")
assert r.status_code == http.HTTPStatus.NOT_FOUND, r.status_code

r = c.get("/app/log/version?id=42")
assert r.status_code == http.HTTPStatus.NOT_FOUND, r.status_code

r = c.post("/app/log?id=42", {"msg": "Hello!"})
assert r.status_code == http.HTTPStatus.OK, r.status_code

r = c.get("/app/log?id=42")
assert r.status_code == http.HTTPStatus.OK, r.status_code
body = r.body.json()
assert body["id"] == 42, r.body
assert body["msg"] == "Hello!", r.body

r = c.get("/app/log/version?id=42")
assert r.status_code == http.HTTPStatus.OK, r.status_code
v0 = r.body.json()["version"]

r = c.post("/app/log?id=42", {"msg": "Saluton!"})
assert r.status_code == http.HTTPStatus.OK, r.status_code

r = c.get("/app/log/version?id=42")
assert r.status_code == http.HTTPStatus.OK, r.status_code
v1 = r.body.json()["version"]
assert v1 > v0

r = c.get("/app/log/version?id=43")
assert r.status_code == http.HTTPStatus.NOT_FOUND, r.status_code

r = c.post("/app/log?id=43", {"msg": "Bonjour!"})
assert r.status_code == http.HTTPStatus.OK, r.status_code

r = c.get("/app/log/version?id=43")
assert r.status_code == http.HTTPStatus.OK, r.status_code
v2 = r.body.json()["version"]
assert v2 > v1

r = c.get("/app/log/all")
assert r.status_code == http.HTTPStatus.OK, r.status_code
body = r.body.json()
Expand All @@ -802,6 +824,11 @@ def test_npm_app(network, args):
assert {"id": 42, "msg": "Saluton!"} in body, body
assert {"id": 43, "msg": "Bonjour!"} in body, body

r = c.get("/app/log/version?id=42")
assert r.status_code == http.HTTPStatus.OK, r.status_code
v3 = r.body.json()["version"]
assert v3 == v1

test_apply_writes(c)

r = c.get("/app/jwt")
Expand Down
31 changes: 31 additions & 0 deletions tests/npm-app/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,37 @@
}
}
},
"/log/version": {
"get": {
"js_module": "endpoints/log.js",
"js_function": "getLogItemVersion",
"forwarding_required": "sometimes",
"authn_policies": ["user_cert"],
"mode": "readonly",
"openapi": {
"responses": {
"200": {
"description": "Ok",
"content": {
"application/json": {
"schema": {
"properties": {
"id": {
"type": "number"
},
"version": {
"type": "number"
}
},
"type": "object"
}
}
}
}
}
}
}
},
"/rpc/apply_writes": {
"post": {
"js_module": "endpoints/rpc.js",
Expand Down
32 changes: 26 additions & 6 deletions tests/npm-app/src/endpoints/log.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
import * as ccfapp from "@microsoft/ccf-app";

type LogContent = string;

interface LogItem {
msg: string;
msg: LogContent;
}

interface LogEntry extends LogItem {
id: number;
}

const logMap = ccfapp.typedKv("log", ccfapp.uint32, ccfapp.json<LogItem>());
interface LogVersion {
id: number;
version: number;
}

const logMap = ccfapp.typedKv("log", ccfapp.uint32, ccfapp.json<LogContent>());

export function getLogItem(request: ccfapp.Request): ccfapp.Response<LogItem> {
export function getLogItem(request: ccfapp.Request): ccfapp.Response<LogEntry> {
const id = parseInt(request.query.split("=")[1]);
if (!logMap.has(id)) {
return {
statusCode: 404,
};
}
return {
body: { id: id, msg: logMap.get(id) },
};
}
export function getLogItemVersion(
request: ccfapp.Request,
): ccfapp.Response<LogVersion> {
const id = parseInt(request.query.split("=")[1]);
if (!logMap.has(id)) {
return {
statusCode: 404,
};
}
return {
body: logMap.get(id),
body: { id: id, version: logMap.getVersionOfPreviousWrite(id) },
};
}

export function setLogItem(request: ccfapp.Request<LogItem>): ccfapp.Response {
const id = parseInt(request.query.split("=")[1]);
logMap.set(id, request.body.json());
logMap.set(id, request.body.json().msg);
return {};
}

Expand All @@ -33,7 +53,7 @@ export function getAllLogItems(
): ccfapp.Response<Array<LogEntry>> {
let items: Array<LogEntry> = [];
logMap.forEach(function (item, id) {
items.push({ id: id, msg: item.msg });
items.push({ id: id, msg: item });
});
return {
body: items,
Expand Down