Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support putBlobs #11

Merged
merged 4 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ const data = await ethStorage.read(key);
Batch upload blob data.

```js
const count = 6;
const data = Buffer.from("test data");
await ethStorage.putBlobs(count, data);
await ethStorage.putBlobs(data);
```


Expand Down
5 changes: 2 additions & 3 deletions spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,15 @@ const data = await ethStorage.read("example.txt");
**Description**: Batch upload blob data to the EthStorage network.

**Parameters**
- `number` (number): Number of blobs.
- `data` (Buffer): Blob content to be written.
- `data` (Buffer): Blob content to be written. The size not exceed 6 times the blob size.

**Returns**
- `status` (Promise<boolean>): A Promise that resolves to the execution result. `true|false`

**Example**
```javascript
const blobData = Buffer.from("some data");
const status = await ethStorage.putBlobs(number, blobData);
const status = await ethStorage.putBlobs(blobData);
```

<p id="FlatDirectory"></p>
Expand Down
30 changes: 25 additions & 5 deletions src/ethstorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,41 @@ export class EthStorage {
return ethers.getBytes(data);
}

async putBlobs(number, data) {
async putBlobs(data) {
iteyelmp marked this conversation as resolved.
Show resolved Hide resolved
if (!data) {
throw new Error(`EthStorage: Invalid data.`);
}
data = Buffer.from(data);
if (data.length < 0 || data.length > BLOB_DATA_SIZE) {
throw new Error(`EthStorage: the length of data(Buffer) should be > 0 && < ${6 * BLOB_DATA_SIZE}.`);
}

const blobs = encodeBlobs(data);
const blobLength = blobs.length;
const blobDataSize = BLOB_DATA_SIZE;

const keys = [];
const ids = [];
const lengths = [];
for (let i = 0; i < blobLength; i ++) {
const key = ethers.keccak256(stringToHex(Date.now() + "_" + i + "_" +this.#wallet.address));
keys.push(key);
ids.push(i);
if (i === blobLength - 1) {
lengths.push(data.length - blobDataSize * (blobLength - 1));
} else {
lengths.push(blobDataSize);
}
}

const contract = new ethers.Contract(this.#contractAddr, EthStorageAbi, this.#wallet);
try {
const storageCost = await contract.upfrontPayment();
const tx = await contract.putBlobs.populateTransaction(number, {
value: storageCost * BigInt(number),
const tx = await contract.putBlobs.populateTransaction(keys, ids, lengths, {
value: storageCost * BigInt(blobLength),
});

const blobs = encodeBlobs(data);
let txRes = await this.#blobUploader.sendTx(tx, [blobs[0]]);
let txRes = await this.#blobUploader.sendTx(tx, blobs);
console.log(`EthStorage: Tx hash is ${txRes.hash}`)
txRes = await txRes.wait();
return txRes.status;
Expand Down
2 changes: 1 addition & 1 deletion src/param/abi.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

export const EthStorageAbi = [
'function putBlobs(uint256 num) public payable',
'function putBlobs(bytes32[] memory _keys, uint256[] memory _blobIdxs, uint256[] memory _lengths)',
'function putBlob(bytes32 _key, uint256 _blobIdx, uint256 _length) public payable',
'function get(bytes32 _key, uint8 _decodeType, uint256 _off, uint256 _len) public view returns (bytes memory)',
'function size(bytes32 _key) public view returns (uint256)',
Expand Down
22 changes: 9 additions & 13 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const dotenv = require("dotenv")
dotenv.config()
const privateKey = process.env.pk;

const filePath = '/Users/lmp/Downloads/dist/img123.jpeg';
const filePath = '/Users/lmp/Downloads/dist/img122.jpeg';
const name = filePath.substring(filePath.lastIndexOf("/") + 1);

const saveFile = (data) => {
Expand All @@ -19,8 +19,8 @@ const saveFile = (data) => {

async function EthStorageTest() {
const es = await EthStorage.create({
rpc: 'http://142.132.154.16:8545',
ethStorageRpc: 'http://65.108.230.142:9545',
rpc: 'http://65.109.20.29:8545',
ethStorageRpc: 'http://65.109.115.36:9540',
privateKey
})

Expand All @@ -38,14 +38,10 @@ async function EthStorageTest() {
console.log(p)

// put blobs
// const es = await EthStorage.create({
// rpc: 'http://88.99.30.186:8545',
// privateKey
// })
// const status = await es.putBlobs(3, content);
// console.log(status);
status = await es.putBlobs(content);
console.log(status);
}
// EthStorageTest();
EthStorageTest();
iteyelmp marked this conversation as resolved.
Show resolved Hide resolved

async function FlatDirectoryTest() {
const fd = await FlatDirectory.create({
Expand All @@ -68,7 +64,7 @@ async function FlatDirectoryTest() {
onFail: (err) => {
console.log(err);
},
onSuccess: (info) => {
onFinish: (info) => {
console.log(info);
}
});
Expand All @@ -88,7 +84,7 @@ async function FlatDirectoryTest() {
onFail: (err) => {
console.log(err);
},
onSuccess: (info) => {
onFinish: (info) => {
console.log(info);
}
});
Expand All @@ -110,4 +106,4 @@ async function FlatDirectoryTest() {
}
})
}
FlatDirectoryTest();
// FlatDirectoryTest();