Skip to content

Commit

Permalink
Merge pull request #173 from skalenetwork/enhancement/SKALE-4340-gett…
Browse files Browse the repository at this point in the history
…ers-storage-space

Enhancement/skale 4340 getters storage space
  • Loading branch information
DmytroNazarenko authored Sep 16, 2021
2 parents 79af5d8 + 04bed23 commit 5d260b9
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 11 deletions.
70 changes: 69 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ Reserve space for certain address in Filestorage in bytes.
filestorage.reserveSpace(ownerAddress, addressToReserve, reservedSpace, privateKey);
```

Create directory using external signing (Metamask etc):
Reserve space using external signing (Metamask etc):

```javascript
filestorage.reserveSpace(ownerAddress, addressToReserve, reservedSpace);
Expand All @@ -274,6 +274,74 @@ filestorage.reserveSpace(ownerAddress, addressToReserve, reservedSpace);
| `String` reservedSpace | Reserved space in bytes |
| `String` privateKey | _(optional)_ sChain owner private key, to sign transactions |

#### Get reserved space

Get information about reserved space for account in bytes.

```javascript
filestorage.getReservedSpace(address);
```

##### Parameters

| Parameter | Description |
| ------------------------- | ----------------------------------------------------------- |
| `String` address | Account address |

##### Returns

| Parameter | Description |
| ---------------------- | ------------------------------------------------ |
| `Number` reservedSpace | Reserved space in bytes |

#### Get occupied space

Get information about occupied (actually used) space for account in bytes.

```javascript
filestorage.getOccupiedSpace(address);
```

##### Parameters

| Parameter | Description |
| ------------------------- | ----------------------------------------------------------- |
| `String` address | Account address |

##### Returns

| Parameter | Description |
| ---------------------- | ------------------------------------------------ |
| `Number` occupiedSpace | Occupied space in bytes |

#### Get total space in Filestorage

Get information about total space in Filestorage in bytes.

```javascript
filestorage.getTotalSpace();
```

##### Returns

| Parameter | Description |
| ---------------------- | ------------------------------------------------ |
| `Number` space | Total space in Filestorage in bytes |

#### Get total reserved space

Get information about total reserved space (sum of all reserved spaces) in bytes.

```javascript
filestorage.getTotalReservedSpace();
```

##### Returns

| Parameter | Description |
| ---------------------- | ------------------------------------------------ |
| `Number` reservedSpace | Total reserved space in bytes |

## Development

### Install
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@skalenetwork/filestorage.js",
"version": "1.0.0",
"version": "1.0.1",
"description": "SKALE Filestorage client tools",
"keywords": [
"SKALE",
Expand Down Expand Up @@ -29,7 +29,7 @@
"version": "node -e \"console.log(require('./package.json').version);\""
},
"dependencies": {
"@skalenetwork/filestorage": "1.0.0-develop.2",
"@skalenetwork/filestorage": "1.0.0-develop.5",
"streamsaver": "^1.0.1",
"web3": "1.2.6"
},
Expand Down
2 changes: 1 addition & 1 deletion scripts/generate_config.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=$(npm view @skalenetwork/filestorage version)
VERSION=$(node -p "require('@skalenetwork/filestorage/package.json').version")
ARTIFACTS_PATH=$PWD/test/data/artifacts.json
curl -LJO https://github.com/skalenetwork/filestorage/releases/download/$VERSION/artifacts.json
mv artifacts.json $ARTIFACTS_PATH
Expand Down
48 changes: 48 additions & 0 deletions src/FilestorageContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,54 @@ class FilestorageContract {
let txData = this.contract.methods.reserveSpace(addressToReserve, reservedSpace);
return await transactions.send(this.web3, ownerAddress, privateKey, txData, constants.STANDARD_GAS);
}

/**
* Javascript wrapper for solidity function getReservedSpace. Get information about reserved space for account
*
* @function getReservedSpace
*
* @param {string} address - Account address
* @returns {number} Reserved space in bytes
*/
async getReservedSpace(address) {
return await this.contract.methods.getReservedSpace(address).call();
}

/**
* Javascript wrapper for solidity function getOccupiedSpace. Get information about occupied space for account
*
* @function getOccupiedSpace
*
* @param {string} address - Account address
* @returns {string} Occupied space in bytes
*/
async getOccupiedSpace(address) {
return await this.contract.methods.getOccupiedSpace(address).call();
}

/**
* Javascript wrapper for solidity function getOccupiedSpace. Get information about total allocated space for
* Filestorage
*
* @function getTotalSpace
*
* @returns {string} Total space in Filestorage in bytes
*/
async getTotalSpace() {
return await this.contract.methods.getTotalStorageSpace().call();
}

/**
* Javascript wrapper for solidity function getOccupiedSpace. Get information about total reserved space in
* Filestorage
*
* @function getTotalReservedSpace
*
* @returns {string} Total reserved space in Filestorage in bytes
*/
async getTotalReservedSpace() {
return await this.contract.methods.getTotalReservedSpace().call();
}
}

module.exports = FilestorageContract;
50 changes: 50 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,56 @@ class FilestorageClient {
if (this.enableLogs) console.log('Space was allocated');
}

/**
* Get information about reserved space for account
*
* @function getReservedSpace
*
* @param {string} address - Account address
* @returns {number} Reserved space in bytes
*/
async getReservedSpace(address) {
let space = await this.contract.getReservedSpace(address);
return Number(space);
}

/**
* Get information about occupied space for account
*
* @function getOccupiedSpace
*
* @param {string} address - Account address
* @returns {number} Occupied space in bytes
*/
async getOccupiedSpace(address) {
let space = await this.contract.getOccupiedSpace(address);
return Number(space);
}

/**
* Get information about total allocated space for Filestorage
*
* @function getTotalSpace
*
* @returns {number} Total space in Filestorage in bytes
*/
async getTotalSpace() {
let space = await this.contract.getTotalSpace();
return Number(space);
}

/**
* Get information about total reserved space in Filestorage
*
* @function getTotalReservedSpace
*
* @returns {number} Total reserved space in Filestorage in bytes
*/
async getTotalReservedSpace() {
let space = await this.contract.getTotalReservedSpace();
return Number(space);
}

async _downloadFile(storagePath, stream) {
let ptrPosition = 0;
let i = 0;
Expand Down
45 changes: 45 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,4 +443,49 @@ describe('Test FilestorageClient', function () {
});
});
});

describe('getters', function () {
it('should return total storage space', async function () {
let space = await filestorage.getTotalSpace();
assert.isNumber(space);
assert(space > 0);
});

it('should return total reserved storage space', async function () {
let space = await filestorage.getTotalReservedSpace();
assert.isNumber(space);
assert(space > 0);
});

it('should return reserved storage space for account', async function () {
let owner = testHelper.getAddress(process.env.SCHAIN_OWNER_PK);
await filestorage.reserveSpace(
owner,
testConstants.SPACE_TEST_ADDRESS,
200,
process.env.SCHAIN_OWNER_PK
);
let space = await filestorage.getReservedSpace(testConstants.SPACE_TEST_ADDRESS);
assert.isNumber(space);
assert(space === 200);
await filestorage.reserveSpace(
owner,
testConstants.SPACE_TEST_ADDRESS,
300,
process.env.SCHAIN_OWNER_PK
);
space = await filestorage.getReservedSpace(testConstants.SPACE_TEST_ADDRESS);
assert(space === 300);
});

it('should return total occupied storage space', async function () {
let initSpace = await filestorage.getOccupiedSpace(address);
assert.isNumber(initSpace);
let fileName = 'test_' + randomstring.generate();
let data = Buffer.from(fileName);
await filestorage.uploadFile(address, fileName, data, privateKey);
let afterSpace = await filestorage.getOccupiedSpace(address);
assert(afterSpace - initSpace === data.length);
});
});
});

0 comments on commit 5d260b9

Please sign in to comment.