Skip to content

Commit 4f39ffe

Browse files
committed
Refactor arweave module
1 parent a808250 commit 4f39ffe

File tree

4 files changed

+65
-83
lines changed

4 files changed

+65
-83
lines changed

bin/helper.js

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import GitHelper from "./lib/git.js";
77
import LineHelper from "./lib/line.js";
88
import Arweave from "arweave";
99
import {
10+
arweave,
1011
makeDataItem,
1112
makeUpdateRefTx,
1213
parseArgitRemoteURI,
1314
makeBundledDataTx,
1415
postTransaction,
15-
waitTxPropogation,
1616
sendPSTFee,
1717
} from "./lib/arweave.js";
1818
import { getAllRefs } from "./lib/graphql.js";
@@ -23,7 +23,7 @@ import ArweaveBundles from "arweave-bundles";
2323

2424
export const VERSION = "0.1.8";
2525

26-
const CHUNK_SIZE = 128 * 1024 * 1024; // 128Mb
26+
const CHUNK_SIZE = 100 * 1024; // 128Mb
2727

2828
const _timeout = async (duration) => {
2929
return new Promise((resolve, reject) => {
@@ -75,11 +75,7 @@ export default class Helper {
7575
// create dirs
7676
// fs.ensureDirSync(path.join(this.path, "refs", "remotes", this.name));
7777
// fs.ensureDirSync(path.join(this.path, "dgit", "refs"));
78-
this._arweave = Arweave.init({
79-
host: "arweave.net",
80-
port: 443,
81-
protocol: "https",
82-
});
78+
8379
const deps = {
8480
utils: Arweave.utils,
8581
crypto: Arweave.crypto,
@@ -207,7 +203,7 @@ export default class Helper {
207203
const spinner = ora("Fetching remote refs from Gitopia").start();
208204

209205
try {
210-
const refs = await getAllRefs(this._arweave, this.url);
206+
const refs = await getAllRefs(this.url);
211207

212208
spinner.succeed("Remote refs fetched from Gitopia");
213209
return refs;
@@ -288,7 +284,7 @@ export default class Helper {
288284
try {
289285
spinner = ora(`Checking permissions over ${this.address}`).start();
290286
// check push permission for repo
291-
const address = await this._arweave.wallets.jwkToAddress(this.wallet);
287+
const address = await arweave.wallets.jwkToAddress(this.wallet);
292288
const { repoOwnerAddress } = parseArgitRemoteURI(this.url);
293289

294290
if (address === repoOwnerAddress) {
@@ -365,46 +361,31 @@ export default class Helper {
365361

366362
// Git object bundles
367363
for (let i = 0; i < bundledDatas.length; i++) {
368-
let bundledDataTx = null;
369-
370-
do {
371-
bundledDataTx = await makeBundledDataTx(
372-
this._arweave,
373-
this.wallet,
374-
this.url,
375-
bundledDatas[i]
376-
);
377-
bundledDataTxInfo.push({
378-
id: bundledDataTx.id,
379-
reward: bundledDataTx.reward,
380-
});
381-
382-
await postTransaction(this._arweave, bundledDataTx);
383-
} while (
384-
(await waitTxPropogation(this._arweave, bundledDataTx)) !== 202
385-
);
386-
}
387-
388-
// update ref
389-
let updateRefTx = null;
390-
do {
391-
updateRefTx = await makeUpdateRefTx(
392-
this._arweave,
364+
let bundledDataTx = await makeBundledDataTx(
393365
this.wallet,
394366
this.url,
395-
dst,
396-
srcOid,
397-
bundledDataTxInfo
367+
bundledDatas[i]
398368
);
369+
bundledDataTxInfo.push({
370+
id: bundledDataTx.id,
371+
reward: bundledDataTx.reward,
372+
});
399373

400-
await postTransaction(this._arweave, updateRefTx);
401-
} while (
402-
(await waitTxPropogation(this._arweave, updateRefTx)) !== 202
374+
await postTransaction(bundledDataTx);
375+
}
376+
377+
// update ref
378+
const updateRefTx = await makeUpdateRefTx(
379+
this.wallet,
380+
this.url,
381+
dst,
382+
srcOid,
383+
bundledDataTxInfo
403384
);
385+
await postTransaction(updateRefTx);
404386

405387
// PST Fee
406388
await sendPSTFee(
407-
this._arweave,
408389
this.wallet,
409390
this.url,
410391
bundledDataTxInfo,

bin/lib/arweave.js

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Arweave from "arweave";
12
import * as smartweave from "smartweave";
23
import shell from "shelljs";
34
import pkg from "bignumber.js";
@@ -6,13 +7,21 @@ const { BigNumber } = pkg;
67
import { VERSION } from "../helper.js";
78
import { newProgressBar } from "./util.js";
89

10+
export const arweave = Arweave.init({
11+
host: "arweave.net", // Arweave Gateway
12+
//host: 'arweave.dev', // Arweave Dev Gateway
13+
port: 443,
14+
protocol: "https",
15+
timeout: 600000,
16+
});
17+
918
// prettier-ignore
1019
const argitRemoteURIRegex = '^gitopia:\/\/([a-zA-Z0-9-_]{43})\/([A-Za-z0-9_.-]*)'
1120
const contractId = "1ljLAR55OhtenU0iDWkLGT6jF4ApxeQd5P0gXNyNJXg";
1221

1322
const sleep = async (ms) => new Promise((resolve) => setTimeout(resolve, ms));
1423

15-
const getStatus = async (arweave, txid) =>
24+
const getStatus = async (txid) =>
1625
(await arweave.transactions.getStatus(txid)).status;
1726

1827
export function parseArgitRemoteURI(remoteURI) {
@@ -24,7 +33,6 @@ export function parseArgitRemoteURI(remoteURI) {
2433
}
2534

2635
export async function makeUpdateRefTx(
27-
arweave,
2836
wallet,
2937
remoteURI,
3038
ref,
@@ -93,7 +101,7 @@ export const makeDataItem = async (
93101
return await arData.sign(item, wallet);
94102
};
95103

96-
export const makeBundledDataTx = async (arweave, wallet, remoteURI, bundle) => {
104+
export const makeBundledDataTx = async (wallet, remoteURI, bundle) => {
97105
const { repoName } = parseArgitRemoteURI(remoteURI);
98106
const data = JSON.stringify(bundle);
99107
const tx = await arweave.createTransaction({ data }, wallet);
@@ -110,7 +118,7 @@ export const makeBundledDataTx = async (arweave, wallet, remoteURI, bundle) => {
110118
return tx;
111119
};
112120

113-
export const postTransaction = async (arweave, tx) => {
121+
export const postTransaction = async (tx) => {
114122
const uploader = await arweave.transactions.getUploader(tx);
115123

116124
const bar = newProgressBar();
@@ -124,14 +132,14 @@ export const postTransaction = async (arweave, tx) => {
124132
bar.stop();
125133
};
126134

127-
export const waitTxPropogation = async (arweave, tx) => {
128-
let status = await getStatus(arweave, tx.id);
135+
export const waitTxPropogation = async (tx) => {
136+
let status = await getStatus(tx.id);
129137

130138
let wait = 6;
131139
while (status === 404 && wait--) {
132140
await sleep(5000);
133141
try {
134-
status = await getStatus(arweave, tx.id);
142+
status = await getStatus(tx.id);
135143
} catch (err) {
136144
wait++;
137145
status = 404;
@@ -152,7 +160,7 @@ export const waitTxPropogation = async (arweave, tx) => {
152160
do {
153161
await sleep(40000); //40 secs
154162
try {
155-
status = await getStatus(arweave, tx.id);
163+
status = await getStatus(tx.id);
156164
} catch (err) {
157165
tries++;
158166
status = 404;
@@ -167,7 +175,6 @@ export const waitTxPropogation = async (arweave, tx) => {
167175
};
168176

169177
export const sendPSTFee = async (
170-
arweave,
171178
wallet,
172179
remoteURI,
173180
transactionsInfo,
@@ -197,22 +204,16 @@ export const sendPSTFee = async (
197204
? pstFee.toFixed(0)
198205
: arweave.ar.arToWinston("0.01");
199206

200-
let pstTx = null;
201-
do {
202-
pstTx = await arweave.createTransaction(
203-
{ target: holder, quantity },
204-
wallet
205-
);
206-
pstTx.addTag("Reference-Id", referenceId);
207-
pstTx.addTag("Repo", repoName);
208-
pstTx.addTag("Version", "0.0.2");
209-
pstTx.addTag("App-Name", "Gitopia");
210-
pstTx.addTag(
211-
"Unix-Time",
212-
Math.round(new Date().getTime() / 1000).toString()
213-
);
214-
215-
await arweave.transactions.sign(pstTx, wallet);
216-
await arweave.transactions.post(pstTx);
217-
} while ((await waitTxPropogation(arweave, pstTx)) !== 202);
207+
const pstTx = await arweave.createTransaction(
208+
{ target: holder, quantity },
209+
wallet
210+
);
211+
pstTx.addTag("Reference-Id", referenceId);
212+
pstTx.addTag("Repo", repoName);
213+
pstTx.addTag("Version", "0.0.2");
214+
pstTx.addTag("App-Name", "Gitopia");
215+
pstTx.addTag("Unix-Time", Math.round(new Date().getTime() / 1000).toString());
216+
217+
await arweave.transactions.sign(pstTx, wallet);
218+
await arweave.transactions.post(pstTx);
218219
};

bin/lib/git.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ export default class GitHelper {
3030

3131
if (await this.exists(oid)) return;
3232

33-
const objects = await fetchGitObjects(
34-
this.helper._arweave,
35-
this.helper.ArData,
36-
this.helper.url
37-
);
33+
const objects = await fetchGitObjects(this.helper.ArData, this.helper.url);
3834

3935
for (const object of objects) {
4036
dumps.push(this.dump(object.oid, object.data));

bin/lib/graphql.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axios from "axios";
2-
import { parseArgitRemoteURI } from "./arweave.js";
2+
import { arweave, parseArgitRemoteURI } from "./arweave.js";
33
import { newProgressBar } from "./util.js";
44

55
const graphQlEndpoint = "https://arweave.net/graphql";
@@ -12,7 +12,7 @@ const getTagValue = (tagName, tags) => {
1212
}
1313
};
1414

15-
export const getOidByRef = async (arweave, remoteURI, ref) => {
15+
export const getOidByRef = async (remoteURI, ref) => {
1616
const { repoOwnerAddress, repoName } = parseArgitRemoteURI(remoteURI);
1717
const { data } = await axios({
1818
url: graphQlEndpoint,
@@ -58,7 +58,7 @@ export const getOidByRef = async (arweave, remoteURI, ref) => {
5858
return JSON.parse(response);
5959
};
6060

61-
export const getAllRefs = async (arweave, remoteURI) => {
61+
export const getAllRefs = async (remoteURI) => {
6262
let refs = new Set();
6363
let refOidObj = {};
6464
const { repoOwnerAddress, repoName } = parseArgitRemoteURI(remoteURI);
@@ -103,7 +103,7 @@ export const getAllRefs = async (arweave, remoteURI) => {
103103
}
104104

105105
for (const ref of refs) {
106-
const { oid } = await getOidByRef(arweave, remoteURI, ref);
106+
const { oid } = await getOidByRef(remoteURI, ref);
107107
refOidObj[ref] = oid;
108108
}
109109

@@ -143,7 +143,7 @@ export const getTransactionIdByObjectId = async (remoteURI, oid) => {
143143
return edges[0].node.id;
144144
};
145145

146-
export const fetchGitObjects = async (arweave, arData, remoteURI) => {
146+
export const fetchGitObjects = async (arData, remoteURI) => {
147147
const objects = [];
148148
const { repoOwnerAddress, repoName } = parseArgitRemoteURI(remoteURI);
149149
const { data } = await axios({
@@ -188,9 +188,10 @@ export const fetchGitObjects = async (arweave, arData, remoteURI) => {
188188
decode: true,
189189
string: true,
190190
});
191-
const items = await arData.unbundleData(txData);
192-
await Promise.all(
193-
items.map(async (item) => {
191+
192+
try {
193+
const items = await arData.unbundleData(txData);
194+
for (const item of items) {
194195
const data = await arData.decodeData(item, { string: false });
195196
for (let i = 0; i < item.tags.length; i++) {
196197
const tag = await arData.decodeTag(item.tags[i]);
@@ -200,8 +201,11 @@ export const fetchGitObjects = async (arweave, arData, remoteURI) => {
200201
break;
201202
}
202203
}
203-
})
204-
);
204+
}
205+
} catch (err) {
206+
// corrupt bundled data
207+
}
208+
205209
bar1.increment();
206210
})
207211
);

0 commit comments

Comments
 (0)