Skip to content

Commit 120559d

Browse files
committed
Wait for tx propogation
1 parent 87d9477 commit 120559d

File tree

3 files changed

+115
-70
lines changed

3 files changed

+115
-70
lines changed

bin/helper.js

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
parseArgitRemoteURI,
1313
makeBundledDataTx,
1414
postTransaction,
15+
waitTxPropogation,
1516
sendPSTFee,
1617
} from "./lib/arweave.js";
1718
import { getAllRefs } from "./lib/graphql.js";
@@ -253,7 +254,7 @@ export default class Helper {
253254

254255
return (async (resolve, reject) => {
255256
let spinner;
256-
let bundledDataTxs = [];
257+
let bundledDatas = [];
257258

258259
try {
259260
const refs = await this._fetchRefs();
@@ -334,19 +335,13 @@ export default class Helper {
334335
currentChunk.push(dataItem);
335336

336337
if (currentChunkSize >= CHUNK_SIZE || i === objects.length - 1) {
337-
const bundledDataTx = await makeBundledDataTx(
338-
this._arweave,
339-
this.ArData,
340-
this.wallet,
341-
this.url,
342-
currentChunk
343-
);
338+
const bundledData = await this.ArData.bundleData(currentChunk);
344339

345340
// Reset chunk
346341
currentChunkSize = 0;
347342
currentChunk = [];
348343

349-
bundledDataTxs.push(bundledDataTx);
344+
bundledDatas.push(bundledData);
350345
}
351346

352347
bar1.increment();
@@ -366,31 +361,53 @@ export default class Helper {
366361
"Uploading git objects to Gitopia [this may take a while]"
367362
);
368363

364+
const bundledDataTxInfo = [];
365+
369366
// Git object bundles
370-
await Promise.all(
371-
bundledDataTxs.map(
372-
async (bundleDataTx) =>
373-
await postTransaction(this._arweave, bundleDataTx)
374-
)
375-
);
367+
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+
}
376387

377388
// update ref
378-
const updateRefTx = await makeUpdateRefTx(
379-
this._arweave,
380-
this.wallet,
381-
this.url,
382-
dst,
383-
srcOid,
384-
bundledDataTxs
389+
let updateRefTx = null;
390+
do {
391+
updateRefTx = await makeUpdateRefTx(
392+
this._arweave,
393+
this.wallet,
394+
this.url,
395+
dst,
396+
srcOid,
397+
bundledDataTxInfo
398+
);
399+
400+
await postTransaction(this._arweave, updateRefTx);
401+
} while (
402+
(await waitTxPropogation(this._arweave, updateRefTx)) !== 202
385403
);
386-
await postTransaction(this._arweave, updateRefTx);
387404

388405
// PST Fee
389406
await sendPSTFee(
390407
this._arweave,
391408
this.wallet,
392409
this.url,
393-
bundledDataTxs,
410+
bundledDataTxInfo,
394411
updateRefTx.id
395412
);
396413

bin/lib/arweave.js

Lines changed: 73 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import { newProgressBar } from "./util.js";
1010
const argitRemoteURIRegex = '^gitopia:\/\/([a-zA-Z0-9-_]{43})\/([A-Za-z0-9_.-]*)'
1111
const contractId = "1ljLAR55OhtenU0iDWkLGT6jF4ApxeQd5P0gXNyNJXg";
1212

13+
const sleep = async (ms) => new Promise((resolve) => setTimeout(resolve, ms));
14+
15+
const getStatus = async (arweave, txid) =>
16+
(await arweave.transactions.getStatus(txid)).status;
17+
1318
export function parseArgitRemoteURI(remoteURI) {
1419
const matchGroups = remoteURI.match(argitRemoteURIRegex);
1520
const repoOwnerAddress = matchGroups[1];
@@ -24,7 +29,7 @@ export async function makeUpdateRefTx(
2429
remoteURI,
2530
ref,
2631
oid,
27-
bundledDataTxs
32+
bundledDataTxInfo
2833
) {
2934
const { repoName } = parseArgitRemoteURI(remoteURI);
3035
const numCommits = shell
@@ -54,11 +59,9 @@ export async function makeUpdateRefTx(
5459
tx.addTag("Origin", "git-remote-gitopia");
5560
}
5661

57-
const bundledDataTxIds = [];
58-
for (let i = 0; i < bundledDataTxs.length; i++) {
59-
bundledDataTxIds.push(bundledDataTxs[i].id);
60-
}
61-
62+
const bundledDataTxIds = bundledDataTxInfo.map(
63+
(bundledDataTx) => bundledDataTx.id
64+
);
6265
tx.addTag("Reference-Txs", JSON.stringify(bundledDataTxIds));
6366

6467
await arweave.transactions.sign(tx, wallet);
@@ -90,15 +93,8 @@ export const makeDataItem = async (
9093
return await arData.sign(item, wallet);
9194
};
9295

93-
export const makeBundledDataTx = async (
94-
arweave,
95-
arData,
96-
wallet,
97-
remoteURI,
98-
dataItems
99-
) => {
96+
export const makeBundledDataTx = async (arweave, wallet, remoteURI, bundle) => {
10097
const { repoName } = parseArgitRemoteURI(remoteURI);
101-
const bundle = await arData.bundleData(dataItems);
10298
const data = JSON.stringify(bundle);
10399
const tx = await arweave.createTransaction({ data }, wallet);
104100
tx.addTag("Repo", repoName);
@@ -128,11 +124,53 @@ export const postTransaction = async (arweave, tx) => {
128124
bar.stop();
129125
};
130126

127+
export const waitTxPropogation = async (arweave, tx) => {
128+
let status = await getStatus(arweave, tx.id);
129+
130+
let wait = 6;
131+
while (status === 404 && wait--) {
132+
await sleep(5000);
133+
try {
134+
status = await getStatus(arweave, tx.id);
135+
} catch (err) {
136+
wait++;
137+
status = 404;
138+
}
139+
}
140+
141+
if (status === 400 || status === 404 || status === 410) {
142+
return status;
143+
}
144+
145+
if (status === 202) {
146+
return 202;
147+
}
148+
149+
// we'll give it 2 minutes for propogation
150+
if (status === 404) {
151+
let tries = 3;
152+
do {
153+
await sleep(40000); //40 secs
154+
try {
155+
status = await getStatus(arweave, tx.id);
156+
} catch (err) {
157+
tries++;
158+
status = 404;
159+
}
160+
if (status === 200) {
161+
return 200;
162+
}
163+
} while (--tries);
164+
}
165+
166+
return 404;
167+
};
168+
131169
export const sendPSTFee = async (
132170
arweave,
133171
wallet,
134172
remoteURI,
135-
transactions,
173+
transactionsInfo,
136174
referenceId
137175
) => {
138176
const { repoName } = parseArgitRemoteURI(remoteURI);
@@ -146,8 +184,8 @@ export const sendPSTFee = async (
146184

147185
// PST Fee
148186
let totalTxFee = new BigNumber(0);
149-
for (let i = 0; i < transactions.length; i++) {
150-
const txFee = new BigNumber(transactions[i].reward);
187+
for (let i = 0; i < transactionsInfo.length; i++) {
188+
const txFee = new BigNumber(transactionsInfo[i].reward);
151189
totalTxFee = totalTxFee.plus(txFee);
152190
}
153191

@@ -159,16 +197,22 @@ export const sendPSTFee = async (
159197
? pstFee.toFixed(0)
160198
: arweave.ar.arToWinston("0.01");
161199

162-
const pstTx = await arweave.createTransaction(
163-
{ target: holder, quantity },
164-
wallet
165-
);
166-
pstTx.addTag("Reference-Id", referenceId);
167-
pstTx.addTag("Repo", repoName);
168-
pstTx.addTag("Version", "0.0.2");
169-
pstTx.addTag("App-Name", "Gitopia");
170-
pstTx.addTag("Unix-Time", Math.round(new Date().getTime() / 1000).toString());
171-
172-
await arweave.transactions.sign(pstTx, wallet);
173-
await arweave.transactions.post(pstTx);
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);
174218
};

bin/lib/graphql.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,11 @@ export const getOidByRef = async (arweave, remoteURI, ref) => {
2929
{ name: "Ref", values: ["${ref}"] }
3030
{ name: "App-Name", values: ["Gitopia"] }
3131
]
32-
first: 10
32+
first: 1
3333
) {
3434
edges {
3535
node {
3636
id
37-
tags {
38-
name
39-
value
40-
}
41-
block {
42-
height
43-
}
4437
}
4538
}
4639
}
@@ -56,15 +49,6 @@ export const getOidByRef = async (arweave, remoteURI, ref) => {
5649
};
5750
}
5851

59-
edges.sort((a, b) => {
60-
if (b.node.block.height - a.node.block.height < 50) {
61-
const bUnixTime = Number(getTagValue("Unix-Time", b.node.tags));
62-
const aUnixTime = Number(getTagValue("Unix-Time", a.node.tags));
63-
return bUnixTime - aUnixTime;
64-
}
65-
return 0;
66-
});
67-
6852
const id = edges[0].node.id;
6953
const response = await arweave.transactions.getData(id, {
7054
decode: true,

0 commit comments

Comments
 (0)