-
Notifications
You must be signed in to change notification settings - Fork 12
/
liveRNodeTest.js
151 lines (128 loc) · 4.19 KB
/
liveRNodeTest.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
const { RNode, RHOCore } = require('../index');
/**
* log with JSON replacer: stringify Buffer data as hex
*/
module.exports.logged = logged;
function logged(obj /*: mixed */, label /*: ?string */) {
console.log(label, JSON.stringify(obj, bufAsHex, 2));
return obj;
}
function bufAsHex(prop, val) {
if (prop === 'data' && 'type' in this && this.type === 'Buffer') {
return Buffer.from(val).toString('hex');
}
return val;
}
/**
* Integration test for major features. Requires a running node.
*/
/*
const defaultPayment = {
from: '0x1',
nonce: 0,
phloPrice: 1,
phloLimit: 100000,
};
async function integrationTest({ node, clock }) {
const user = h2b('464f6780d71b724525be14348b59c53dc8795346dfd7576c9f01c397ee7523e6');
const timestamp = clock().valueOf();
// Test deploys and listens
const term = `
new x, y, z, out(\`rho:io:stdout\`) in {
out!(["names:", *x, *y, *z, *out]) |
x!("x") | y!("y") | z!("z")
}
`;
const deployData = { term, user, timestamp, ...defaultPayment };
const preview = await node.previewPrivateIds(deployData, 3);
console.log(preview.map(b2h));
const deployMessage = await node.doDeploy(deployData);
console.log('doDeploy result:', deployMessage);
console.log('create: ', await node.createBlock());
const idToPar = id => ({ ids: [{ id }] });
const blockResults = await node.listenForDataAtName(idToPar(preview[0]));
blockResults.forEach((b) => {
b.postBlockData.forEach((d) => {
logged(RHOCore.toJSData(d), 'Data at x');
});
});
}
*/
//////////////////////////////////////////////////////
/**
* Integration test for major features. Requires a running node.
*/
async function integrationTest({ grpc, endpoint, clock }) {
// Now make an RNode instance
console.log({ endpoint });
const rchain = RNode(grpc, endpoint);
// Test deploys and listens
const term = `
new private, print(\`rho:io:stdout\`) in {
print!(*private) |
private!("Get this text into javascript") |
@"public"!(*private) |
for(@{Int} <- private){Nil} |
for(_ <- @"chan1"; _ <- @"chan2"){Nil}
}
`;
const deployData = {
term,
timestamp: clock().valueOf(),
from: '0x1',
nonce: 0,
phloPrice: 1,
phloLimit: 100000,
};
try {
// Deploy term
const deployMessage = await rchain.doDeploy(deployData, true);
console.log('doDeploy result:', deployMessage);
// Listen for data at public name
let blockResults = await rchain.listenForDataAtPublicName('public');
const lastBlock = blockResults.slice(-1).pop();
const privateNameId = await lastBlock.postBlockData.slice(-1).pop();
// Listen for data at private name
blockResults = await rchain.listenForDataAtName(privateNameId);
blockResults.forEach((b) => {
b.postBlockData.forEach((d) => {
logged(RHOCore.toRholang(d), 'Data Received from unforgeable name');
});
});
// Listen for continuation joined public names
blockResults = await rchain.listenForContinuationAtPublicName(['chan1', 'chan2']);
if (blockResults.length > 0) {
console.log('Got continuation at joined public names');
} else {
console.log('Failed to get continuation at joined public names');
}
// Listen for continuation at single private name
// NOTE: This test only passes reliably on a fresh node
// becuase of unintuitive behavior of listen...AtName
// as documented in https://gist.github.com/JoshOrndorff/b0fe7aed93d16beabc2885484c6e8c54
blockResults = await rchain.listenForContinuationAtName([privateNameId], 1);
if (blockResults.length > 0) {
console.log('Got continuation at single private name');
} else {
console.log('Failed to get continuation at single private name');
}
} catch (oops) {
console.log(oops);
}
}
///////////////////////////////////////////////////////////////////
if (require.main === module) {
// Access ambient stuff only when invoked as main module.
/* eslint-disable global-require */
const endpoint = {
host: process.env.npm_config_host || 'localhost',
port: parseInt(process.env.npm_config_port || '40401', 10),
};
integrationTest(
{
endpoint,
grpc: require('grpc'),
clock: () => new Date(),
},
);
}