Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Apr 2, 2024
1 parent a14c99f commit 67ff46d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 138 deletions.
30 changes: 15 additions & 15 deletions bots/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bots/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
},
"homepage": "https://github.com/initia-labs/batch-submitter#readme",
"devDependencies": {
"@initia/initia.js": "^0.1.36",
"@initia/initia.js": "^0.1.41",
"@koa/cors": "^4.0.0",
"@sentry/node": "^7.91.0",
"@testcontainers/postgresql": "^10.4.0",
Expand Down
130 changes: 10 additions & 120 deletions bots/src/celestia/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import Axios from 'axios';
import { sequenceLenBytes, shareInfoBytes, shareSize } from './builder';
import { createCommitment } from './commitment';
import { namespaceSize } from './namespace';
import { config } from 'config';
import { delay } from 'bluebird';
import {
Blob, Coin
} from '@initia/initia.js';
import { Blob } from '@initia/initia.js';

// constants
const befaultGasPerBlobByte = 8;
Expand All @@ -17,18 +13,17 @@ const firstSparseShareContentSize =
shareSize - namespaceSize - shareInfoBytes - sequenceLenBytes;
const continuationSparseShareContentSize =
shareSize - namespaceSize - shareInfoBytes;
const denom = "utia";
const namespaceIdLength = 28;

export function getCelestiaFeeGasLimit(length: number): number{
export function getCelestiaFeeGasLimit(length: number): number {
// calculate gas
return Math.floor(defaultEstimateGas([length]) * 1.2);
}

export function createBlob(data: Buffer): {
blob: Blob,
commitment: string,
namespace: string,
blob: Blob;
commitment: string;
namespace: string;
} {
const blob = {
namespaceId: new Uint8Array([
Expand All @@ -48,115 +43,19 @@ export function createBlob(data: Buffer): {
Buffer.from(config.CELESTIA_NAMESPACE_ID, 'hex').toString('base64'),
Buffer.from(blob.data).toString('base64'),
0,
0,
0
);
const namespace = Buffer.from([
blob.namespaceVersion,
...blob.namespaceId
]).toString('base64');
blob.namespaceVersion,
...blob.namespaceId
]).toString('base64');
return {
blob: res,
commitment,
namespace,
namespace
};
}

// async function submitPayForBlob(data: Buffer): Promise<string> {
// const lightNodeRpc = Axios.create({
// baseURL: config.CELESTIA_LIGHT_NODE_RPC_URI,
// headers: {
// Authorization: `Bearer ${config.CELESTIA_TOKEN_AUTH}`
// }
// });

// const blob = {
// namespaceId: new Uint8Array([
// ...Buffer.from(config.CELESTIA_NAMESPACE_ID, 'hex')
// ]),
// data: new Uint8Array([...data]),
// shareVersion: 0,
// namespaceVersion: 0,
// shareCommitment: new Uint8Array()
// };

// // generate commitment
// createCommitment(blob);

// // calculate gas
// const gaslimit = Math.floor(defaultEstimateGas([blob.data.length]) * 1.2);
// const fee = Math.floor(gaslimit * config.CELESTIA_GAS_PRICE).toString();

// const request = {
// id: 1,
// jsonrpc: '2.0',
// method: 'state.SubmitPayForBlob',
// params: [
// fee,
// gaslimit,
// [
// {
// namespace: Buffer.from([
// blob.namespaceVersion,
// ...blob.namespaceId
// ]).toString('base64'),
// data: Buffer.from(blob.data).toString('base64'),
// share_version: blob.shareVersion,
// commitment: Buffer.from(blob.shareCommitment).toString('base64')
// }
// ]
// ]
// };

// const response = await lightNodeRpc.post('', request);

// // error handle
// if (response?.data === undefined || response.data?.result === undefined) {
// const timeoutError = 'timed out waiting for tx to be included in a block';
// const mempoolError = 'tx already in mempool';
// // if got timeout error, retry
// if (
// response?.data?.error &&
// (errorInclude(response, timeoutError) ||
// errorInclude(response, mempoolError))
// ) {
// await delay(1000);
// return submitPayForBlob(data);
// }

// let reason: any = '';

// // in case response.data is undefined
// if (response?.data === undefined) {
// reason = response;
// }

// if (response.data?.result === undefined) {
// throw Error(
// `Failed to SubmitPayForBlob. Reason: ${
// response.data?.error
// ? JSON.stringify(response.data.error)
// : JSON.stringify(response.data)
// }`
// );
// }

// throw Error(
// `Failed to SubmitPayForBlob. Reason: ${JSON.stringify(reason)}`
// );
// }

// const height = response.data.result.height;
// const commitment = Buffer.from(blob.shareCommitment).toString('base64');
// return `${height}::${commitment}`;
// }

function errorInclude(response: any, message: string): boolean {
return (
response?.data?.error &&
JSON.stringify(response.data.error).indexOf(message) !== -1
);
}

function defaultEstimateGas(blobSizes: number[]) {
return estimateGas(
blobSizes,
Expand Down Expand Up @@ -195,15 +94,6 @@ function sparseSharesNeeded(sequenceLen: number): number {
return 1;
}

// let sharesNeeded = 1;
// let bytesAvailable = firstSparseShareContentSize;
// while (bytesAvailable < sequenceLen) {
// bytesAvailable += continuationSparseShareContentSize;
// sharesNeeded++;
// }

// return sharesNeeded;

return (
1 +
Math.floor(
Expand Down
5 changes: 3 additions & 2 deletions bots/src/worker/batchSubmitter/batchSubmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ export class BatchSubmitter {
output.startBlockNumber,
output.endBlockNumber
);
let batchInfo: string[] = await this.publishBatch(batch);

const batchInfo: string[] = await this.publishBatch(batch);
await this.saveBatchToDB(
manager,
batchInfo,
Expand All @@ -104,14 +104,15 @@ export class BatchSubmitter {
if (!bulk) {
throw new Error(`Error getting block bulk from L2`);
}

const commit: RawCommit | null = await this.rpcClient.getRawCommit(
end.toString()
);
if (!commit) {
throw new Error(`Error getting commit from L2`);
}

let reqStrings = bulk.blocks.concat(commit.commit);
const reqStrings = bulk.blocks.concat(commit.commit);
return compress(reqStrings);
}

Expand Down

0 comments on commit 67ff46d

Please sign in to comment.