-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
170 lines (152 loc) · 4.72 KB
/
test.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
const {EthStorage, FlatDirectory} = require("./dist/index.cjs.js");
const {NodeFile} = require("./dist/file.cjs.js");
const fs = require('fs');
const os = require('os');
const dotenv = require("dotenv")
dotenv.config()
const privateKey = process.env.pk;
const filePath = '/Users/lmp/Downloads/dist/img122.jpeg';
const filePath2 = '/Users/lmp/Downloads/dist/img123.jpeg';
const name = filePath.substring(filePath.lastIndexOf("/") + 1);
const saveFile = (data) => {
const exp = new Date();
const path = `${os.tmpdir()}/${exp.getTime()}`;
fs.writeFileSync(path, data);
return path;
}
async function EthStorageTest() {
const es = await EthStorage.create({
rpc: 'https://rpc.testnet.l2.quarkchain.io:8545',
ethStorageRpc: 'https://rpc.testnet.l2.ethstorage.io:9540',
privateKey
})
const content = fs.readFileSync(filePath);
// estimate cost
const cost = await es.estimateCost(name, content.length > 126976 ? content.subarray(0, 126976) : content);
console.log("cost:", cost)
// write
let status = await es.write(name, content.length > 126976 ? content.subarray(0, 126976) : content);
console.log("status:", status)
// read
let buff = await es.read(name);
const p = saveFile(buff);
console.log(p)
// put blobs
const keys = ["key1", "key2"];
const blobData = [Buffer.from("some data1"), Buffer.from("some data2")];
status = await es.writeBlobs(keys, blobData);
console.log("status:", status)
// read
buff = await es.read('key2');
console.log(Buffer.from(buff).toString());
}
// EthStorageTest();
async function FlatDirectoryTest() {
const fd = await FlatDirectory.create({
rpc: 'https://rpc.testnet.l2.quarkchain.io:8545',
ethStorageRpc: 'https://rpc.testnet.l2.ethstorage.io:9540',
privateKey,
// address: "0x91F57C2d88C55B7a2Dd6DC76ddae3891b8003CE8"
})
await fd.deploy();
const uploadCallback = {
onProgress: (progress, count, isChange) => {
console.log(`progress:${progress}, count:${count}, isChange:${isChange}`);
},
onFail: (err) => {
console.log(err);
},
onFinish: (totalUploadChunks, totalUploadSize, totalStorageCost) => {
console.log(`totalUploadChunks:${totalUploadChunks}, totalUploadSize:${totalUploadSize}, totalStorageCost:${totalStorageCost}`);
}
};
const hashes = await fd.fetchHashes(["file.jpg", "blobFile.jpg"]);
console.log(hashes);
// calldata
// data
let request = {
type: 1,
key: "data",
content: Buffer.from("12345678"),
gasIncPct: 10,
callback: uploadCallback
}
let cost = await fd.estimateCost(request);
console.log(cost);
await fd.upload(request);
cost = await fd.estimateCost(request);
console.log(cost);
console.log("");
// file
let file = new NodeFile(filePath);
request = {
type: 1,
key: "file.jpg",
content: file,
gasIncPct: 10,
callback: uploadCallback
}
cost = await fd.estimateCost(request);
console.log(cost);
await fd.upload(request);
cost = await fd.estimateCost(request);
console.log(cost);
console.log("");
// blob
// data
request = {
type: 2,
key: "blobData",
content: Buffer.from("12345678"),
gasIncPct: 5,
callback: uploadCallback
}
cost = await fd.estimateCost(request);
console.log(cost);
await fd.upload(request);
cost = await fd.estimateCost(request);
console.log(cost);
console.log("");
// file
file = new NodeFile(filePath2);
request = {
type: 2,
key: "blobFile.jpg",
content: file,
gasIncPct: 5,
chunkHashes: hashes[1],
callback: uploadCallback
}
cost = await fd.estimateCost(request);
console.log(cost);
await fd.upload(request);
cost = await fd.estimateCost(request);
console.log(cost);
console.log("");
// download
await fd.download("data", {
onProgress: (progress, count, data) => {
console.log(progress, count, Buffer.from(data).toString());
},
onFail: (err) => {
console.log(err)
},
onFinish: () => {
console.log('download finish');
}
});
await fd.download("blobFile.jpg", {
onProgress: (progress, count, data) => {
console.log(progress, count, data.length);
},
onFail: (err) => {
console.log(err)
},
onFinish: () => {
console.log('download finish');
}
});
const hashes2 = await fd.fetchHashes(["file.jpg", "blobFile.jpg"]);
console.log(hashes2);
}
FlatDirectoryTest();