Skip to content

Commit

Permalink
remove pool.transform
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyan-dfinity committed Sep 5, 2024
1 parent 360dd3d commit 10e4c74
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 51 deletions.
21 changes: 8 additions & 13 deletions dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,27 @@
]
},
"backend": {
"type": "custom",
"dependencies": ["wasm-utils"],
"specified_id": "mwrha-maaaa-aaaab-qabqq-cai",
"main": "service/pool/Main.mo",
"type": "motoko",
"build": ["mops-cli build service/pool/Main.mo"],
"candid": "target/pool/pool.did",
"wasm": "target/pool/pool.wasm",
"optimize": "cycles"
},
"saved": {
"type": "custom",
"specified_id": "vhtho-raaaa-aaaab-qadoq-cai",
"main": "service/saved/Saved.mo",
"type": "motoko",
"build": ["mops-cli build service/saved/Saved.mo"],
"candid": "target/Saved/Saved.did",
"wasm": "target/Saved/Saved.wasm",
"optimize": "cycles"
},
"react_app": {
"dependencies": ["backend", "saved"],
"specified_id": "m7sm4-2iaaa-aaaab-qabra-cai",
"frontend": {
"entrypoint": "public/index.html"
},
"source": ["build"],
"type": "assets"
}
},
"defaults": {
"build": {
"output": "build",
"packtool": "mops sources"
}
}
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
"clean": "dfx stop && dfx start --clean --background",
"prestart": "dfx start --background; [ -f .dfx/local/canister_ids.json ] || dfx deploy",
"predeploy": "npm run prestart",
"prepare": "husky install",
"postinstall": "mops install"
"prepare": "husky install"
},
"eslintConfig": {
"extends": [
Expand Down
34 changes: 12 additions & 22 deletions service/pool/Main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -629,23 +629,16 @@ shared (creator) actor class Self(opt_params : ?Types.InitParams) = this {
{ request with transform = null };
};
case (?transform) {
if (Option.isSome(pool.getTransform(caller))) {
throw Error.reject "No concurrent http_request allowed";
};
pool.rememberTransform(caller, transform);
let payload = { caller; transform };
let fake_actor: actor { __transform: ICType.transform_function } = actor(Principal.toText(Principal.fromActor this));
let new_transform = ?{ function = fake_actor.__transform; context = Principal.toBlob caller };
let new_transform = ?{ function = fake_actor.__transform; context = to_candid(payload) };
{ request with transform = new_transform };
};
};
try {
let res = await IC.http_request(new_request);
let refunded = -Cycles.refunded();
assert(pool.spendCycles(caller, refunded) == true);
res;
} finally {
pool.removeTransform(caller);
};
let res = await IC.http_request(new_request);
let refunded = -Cycles.refunded();
assert(pool.spendCycles(caller, refunded) == true);
res;
} else {
throw Error.reject "http_request exceeds cycle spend limit";
};
Expand All @@ -655,16 +648,13 @@ shared (creator) actor class Self(opt_params : ?Types.InitParams) = this {
if (caller != Principal.fromText("aaaaa-aa") and caller != Principal.fromText("2vxsx-fae")) {
throw Error.reject "Only the management canister can call __transform";
};
let id = Principal.fromBlob context;
// Query state may be behind the latest commit point, so there is a small chance that id is not in the pool yet.
switch (pool.getTransform(id)) {
case null {
throw Error.reject "No transform found";
};
case (?transform) {
await transform.function({ context = transform.context; response });
};
let ?raw : ?{ caller: Principal; transform: {context: Blob; function: ICType.transform_function} } = from_candid context else {
throw Error.reject "__transform: Invalid context";
};
if (not pool.findId(raw.caller)) {
throw Error.reject "__transform: Only a canister managed by the Motoko Playground can call __transform";
};
await raw.transform.function({ context = raw.transform.context; response });
};

system func inspect({
Expand Down
11 changes: 0 additions & 11 deletions service/pool/Types.mo
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ module {
var snapshots = TrieMap.TrieMap<Principal, Blob>(Principal.equal, Principal.hash);
// Cycles spent by each canister, not persisted for upgrades
let cycles = TrieMap.TrieMap<Principal, Int>(Principal.equal, Principal.hash);
type TransformType = { context: Blob; function: ICType.transform_function };
let transforms = TrieMap.TrieMap<Principal, TransformType>(Principal.equal, Principal.hash);

public type NewId = { #newId; #reuse:CanisterInfo; #outOfCapacity:Nat };

Expand Down Expand Up @@ -204,15 +202,6 @@ module {
cycles.put(cid, new);
true;
};
public func rememberTransform(cid: Principal, transform: TransformType) {
transforms.put(cid, transform);
};
public func getTransform(cid: Principal) : ?TransformType {
transforms.get cid
};
public func removeTransform(cid: Principal) {
transforms.delete cid;
};

private func notExpired(info: CanisterInfo, now: Int) : Bool = (info.timestamp > now - ttl);

Expand Down
4 changes: 2 additions & 2 deletions service/wasm-utils/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/wasm-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ic-cdk = "0.16"
serde = "1.0"
serde_bytes = "0.11"
candid = "0.10"
ic-wasm = { version = "0.8.3", default-features = false }
ic-wasm = { version = "0.8.5", default-features = false }
sha2 = "0.10.6"

[profile.release]
Expand Down

0 comments on commit 10e4c74

Please sign in to comment.