-
Notifications
You must be signed in to change notification settings - Fork 12
/
proxy.js
211 lines (195 loc) · 5.89 KB
/
proxy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* eslint-disable no-await-in-loop */
/* eslint-disable no-use-before-define */
// @ts-check
import { sign as signDeploy } from './deploySig';
import { RhoExpr } from './rho-expr';
const { freeze } = Object;
/**
* Find deploy, fetch its block, and extract relevant DeployInfo
*
* @param {Observer} observer
* @param {string} deployId
* @returns { Promise<?DeployInfo> } null if not (yet) available
* @throws { Error } in case of deploy execution error
*
* @typedef { import("./rnode-openapi-schema").DeployInfo } DeployInfo
*/
export async function checkForDeployInfo(observer, deployId) {
// Request a block with the deploy
const block = await observer.findDeploy(deployId).catch((ex) => {
// Handle response code 400 / deploy not found
if (ex.status !== 400) throw ex;
});
if (!block) return null;
const { deploys } = await observer.getBlock(block.blockHash);
const deploy = deploys.find(({ sig }) => sig === deployId);
if (!deploy) {
// This should not be possible if block is returned
throw Error(`Deploy is not found in the block (${block.blockHash}).`);
}
return deploy;
}
/**
* Get result of deploy
* @param {Observer} observer
* @param {DeployInfo} deploy
* @returns { Promise<RhoExprWithBlock> }
* @throws { Error } in case of execution error or missing data
*
* @typedef { import("./rnode-openapi-schema").RhoExprWithBlock } RhoExprWithBlock
*/
export async function listenAtDeployId(observer, deploy) {
// Check deploy errors
const { errored, systemDeployError } = deploy;
if (errored) {
throw Error(`Deploy error when executing Rholang code.`);
} else if (systemDeployError) {
throw Error(`${systemDeployError} (system error).`);
}
const target = { depth: 1, name: { UnforgDeploy: { data: deploy.sig } } };
// Request for data at deploy signature (deployId)
const { exprs } = await observer.listenForDataAtName(target);
// Return data with cost (assumes data in one block)
if (!exprs.length) throw new Error('no data at deployId');
// TODO: return all exprs; let caller pick 1st
return exprs[0];
}
/**
*
* @param {string} pkHex
* @param { Observer } observer
* @param {{
* setTimeout: typeof setTimeout,
* clock: () => Promise<number>,
* period?: number
* }} sched
* @returns { Account }
*
* @typedef {{
* sign: (term: string) => Promise<DeployRequest>,
* polling: () => Promise<void>, // throws to abort
* }} Account
*/
export function makeAccount(
pkHex,
observer,
{ setTimeout, clock, period = 7500 },
{ phloPrice = 1, phloLimit = 250000 },
) {
const polling = () =>
new Promise((resolve) => {
setTimeout(resolve, period);
});
return freeze({
polling,
/**
* @param {string} term
* @returns { Promise<DeployRequest> }
*/
async sign(term) {
const [timestamp, [recent]] = await Promise.all([
clock(),
observer.getBlocks(1),
]);
return signDeploy(pkHex, {
term,
phloPrice,
phloLimit,
timestamp,
validAfterBlockNumber: recent.blockNumber,
});
},
});
}
/**
* Sign, deploy, get block with deploy
* @param {string} term
* @param {Validator} validator
* @param {Observer} observer
* @param {Account} account
* @returns {Promise<DeployInfo>}
*/
export async function startTerm(
/** @type {string} */ term,
validator,
observer,
account,
) {
const signed = await account.sign(term);
console.log('startTerm', { deployRequest: signed });
console.log(term);
const step1 = await validator.deploy(signed);
if (!step1.startsWith('Success')) throw new Error(step1);
for (;;) {
const deploy = await checkForDeployInfo(observer, signed.signature);
if (deploy) {
return deploy;
}
await account.polling();
}
}
/**
* @param {Validator} validator
* @param {Observer} observer
* @param {{
* sign: (term: string) => Promise<DeployRequest>,
* polling: () => Promise<void>, // throws to abort
* }} account
*
* TODO: marshalling / unmarshalling of object references
*
* @typedef { null | boolean | number | string } Scalar
* @typedef {Scalar[] | {[k: string]: Scalar}} Complex
*
* @typedef {import('./rnode').Validator} Validator
* @typedef {import('./rnode').Observer} Observer
* @typedef {import('./rnode').DeployRequest} DeployRequest
*/
export function makeConnection(validator, observer, account) {
const { stringify: lit } = JSON;
const spread = (items) => lit(items).slice(1, -1); // strip []
const start = (/** @type string */ term) =>
startTerm(term, validator, observer, account);
/**
* @param {Scalar | Complex} tag
* @param {string | Symbol | number} method
* @param {(Scalar | Complex)[]} args
*/
async function sendMessage(tag, method, args) {
const term = `new return(\`rho:rchain:deployId\`), deployerId(\`rho:rchain:deployerId\`) in {
match {[*deployerId, ${lit(tag)}]} {
{*target} => target!(${spread([method, ...args])}, *return)
}
}`;
const deploy = await start(term);
const { expr } = await listenAtDeployId(observer, deploy);
return RhoExpr.parse(expr);
}
function proxy(/** @type {Scalar | Complex} */ tag) {
return new Proxy(freeze({}), {
get: (obj, method) => {
console.log('proxy get', { obj, tag, method });
if (method === 'then') return null; // I'm not a Thenable.
return (...args) => {
console.log('proxy sendMessage', { tag, method, args });
return sendMessage(tag, method, args);
};
},
});
}
return freeze({
/**
* @param {Scalar | Complex} tag
* @param {string} targetProc proc with defining use of the name `target`
*/
async spawn(tag, targetProc) {
const term = `new deployerId(\`rho:rchain:deployerId\`) in {
match {[*deployerId, ${lit(tag)}]} {
{*target} => { ${targetProc} }
}
}`;
await start(term);
return proxy(tag);
},
});
}