Skip to content

Commit

Permalink
Merge pull request #22 from casper-ecosystem/maciej/deploy-size
Browse files Browse the repository at this point in the history
Validate the deploy size.
  • Loading branch information
Maciej Zieliński authored Mar 31, 2021
2 parents a69eb5b + b381928 commit 8f01b1b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to casper-client-sdk.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.0.35

### Changed

- Validate the size of the `Deploy`. Now `CasperServiceByJsonRPC.deploy` throws an error if the size of the deploy is larger then 1 megabyte.`

## 1.0.34

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion 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 package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "casper-client-sdk",
"version": "1.0.34",
"version": "1.0.35",
"license": "Apache 2.0",
"description": "SDK to interact with the Casper blockchain",
"main": "dist/lib.node.js",
Expand Down
11 changes: 11 additions & 0 deletions src/lib/DeployUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -978,3 +978,14 @@ export const addArgToDeploy = (

return makeDeploy(deployParams, session, deploy.payment);
};

export const deploySizeInBytes = (deploy: Deploy): number => {
const hashSize = deploy.hash.length;
const bodySize = serializeBody(deploy.payment, deploy.session).length;
const headerSize = serializeHeader(deploy.header).length;
const approvalsSize = deploy.approvals.map(approval => {
return (approval.signature.length + approval.signer.length) / 2;
}).reduce((a, b) => a + b, 0);

return hashSize + headerSize + bodySize + approvalsSize;
}
9 changes: 9 additions & 0 deletions src/services/CasperServiceByJsonRPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,15 @@ export class CasperServiceByJsonRPC {
}

public async deploy(signedDeploy: DeployUtil.Deploy) {
const oneMegaByte = 1048576;
const size = DeployUtil.deploySizeInBytes(signedDeploy);
if(size > oneMegaByte) {
throw Error(
`Deploy can not be send, because it's too large: ${size} bytes. ` +
`Max size is 1 megabyte.`
);
}

return await this.client.request({
method: 'account_put_deploy',
params: deployToJson(signedDeploy)
Expand Down
36 changes: 32 additions & 4 deletions test/nctl/RPC.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { assert } from 'chai';
import { CasperServiceByJsonRPC } from '../../src/services';
import { Keys, DeployUtil, RuntimeArgs } from '../../src/index';

let client = new CasperServiceByJsonRPC(
'http://127.0.0.1:40101/rpc',
);

describe.skip('RPC', () => {
it('should return correct block by number', async () => {
describe('RPC', () => {
xit('should return correct block by number', async () => {
let check = async (height: number) => {
let result = await client.getBlockInfoByHeight(height);
assert.equal(result.block?.header.height, height);
Expand All @@ -17,7 +18,7 @@ describe.skip('RPC', () => {
}
});

it('should return correct block by hash', async () => {
xit('should return correct block by hash', async () => {
let check = async (height: number) => {
let block_by_height = await client.getBlockInfoByHeight(height);
let block_hash = block_by_height.block?.hash!;
Expand All @@ -29,4 +30,31 @@ describe.skip('RPC', () => {
await check(i);
}
});
});

it('should not allow to send deploy larger then 1 megabyte.', async () => {
// moduleBytes need to have length of (1 megabyte - 169 bytes) to produce
// a deploy with the size of (1 megabyte + 1 byte).
const oneMegaByte = 1048576;
const moduleBytes = Uint8Array.from(Array(oneMegaByte - 169).fill(0));

let deployParams = new DeployUtil.DeployParams(
Keys.Ed25519.new().publicKey, 'test');
let session = DeployUtil.ExecutableDeployItem.newModuleBytes(
moduleBytes, RuntimeArgs.fromMap({})
);
let payment = DeployUtil.standardPayment(100000);
let deploy = DeployUtil.makeDeploy(deployParams, session, payment);

assert.equal(DeployUtil.deploySizeInBytes(deploy), oneMegaByte + 1);
await client.deploy(deploy)
.then(_ => {
assert.fail("client.deploy should't throw an error.");
})
.catch(err => {
let expectedMessage =
`Deploy can not be send, because it's too large: ${oneMegaByte + 1} bytes. ` +
`Max size is 1 megabyte.`
assert.equal(err.message, expectedMessage);
});
});
});

0 comments on commit 8f01b1b

Please sign in to comment.