Skip to content

Commit

Permalink
Merge pull request #7 from IoBuilders/feature/KiB-representation
Browse files Browse the repository at this point in the history
KiB representation
  • Loading branch information
Ferparishuertas authored Nov 11, 2020
2 parents c500ab5 + 368b2f8 commit 93459b5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ truffle run contract-size --contracts ExampleContract1 ExampleContract2

### Check maximum contract sizes

The plugin can be used to check that the smart contracts aren't bigger than the allowed maximum contract size of the Ethereum Mainnet (24 kb). For example this can be used, to make a CI/CD pipeline fail, if a contract is bigger than allowed.
The plugin can be used to check that the smart contracts aren't bigger than the allowed maximum contract size of the Ethereum Mainnet (24 KiB = 24576 bytes). For example this can be used, to make a CI/CD pipeline fail, if a contract is bigger than allowed.

```bash
truffle run contract-size --checkMaxSize
```

If another size limit than the default one should be checked, it can be given as argument to the option. For example to set the maximum to 48 kb the following command can be used:
If another size limit than the default one should be checked, it can be given as argument to the option. For example to set the maximum to 48 KiB the following command can be used:

```bash
truffle run contract-size --checkMaxSize 48
Expand Down
28 changes: 14 additions & 14 deletions 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": "truffle-contract-size",
"version": "2.0.0",
"version": "2.0.1",
"description": "Displays the size of a truffle contracts in kilobytes",
"main": "src/index.js",
"scripts": {
Expand Down
17 changes: 9 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const readDir = util.promisify(fs.readdir)
const Table = require('cli-table')
const yargs = require('yargs')

const DEFAULT_MAX_CONTRACT_SIZE_IN_KB = 24
// 1 KiB = 1.024 bytes
const DEFAULT_MAX_CONTRACT_SIZE_IN_KIB = 24

/**
* Outputs the size of a given smart contract
Expand Down Expand Up @@ -43,7 +44,7 @@ module.exports = async (config, done) => {
done(`Error: deployedBytecode not found in ${contract.file} (it is not a contract json file)`)
}

const byteCodeSize = computeByteCodeSizeInKb(contractFile.deployedBytecode)
const byteCodeSize = computeByteCodeSizeInKiB(contractFile.deployedBytecode)

table.push([
contract.name,
Expand All @@ -56,11 +57,11 @@ module.exports = async (config, done) => {
console.log(table.toString())

if (checkMaxSize) {
const maxSize = checkMaxSize === true ? DEFAULT_MAX_CONTRACT_SIZE_IN_KB : checkMaxSize
const maxSize = checkMaxSize === true ? DEFAULT_MAX_CONTRACT_SIZE_IN_KIB : checkMaxSize

table.forEach(row => {
if (Number.parseFloat(row[1]) > maxSize) {
done(`Contract ${row[0]} is bigger than ${maxSize} kb`)
done(`Contract ${row[0]} is bigger than ${maxSize} KiB`)
}
})
}
Expand All @@ -70,7 +71,7 @@ module.exports = async (config, done) => {

function configureArgumentParsing () {
yargs.option('contracts', { describe: 'Only display certain contracts', type: 'array' })
yargs.option('checkMaxSize', { describe: 'Returns an error exit code if a contract is bigger than the optional size in kb (default: 24). Must be an integer value' })
yargs.option('checkMaxSize', { describe: 'Returns an error exit code if a contract is bigger than the optional size in KiB (default: 24). Must be an integer value' })
yargs.option('ignoreMocks', { describe: 'Ignores all contracts which names end with "Mock"', type: 'boolean' })

// disable version parameter
Expand All @@ -88,15 +89,15 @@ function isValidCheckMaxSize (checkMaxSize) {
return checkMaxSize === true || !Number.isNaN(checkMaxSize)
}

function computeByteCodeSizeInKb (byteCode) {
function computeByteCodeSizeInKiB (byteCode) {
// -2 to remove 0x from the beginning of the string
// /2 because one byte consists of two hexadecimal values
// /1024 to convert to size from byte to kilo byte
// /1024 to convert to size from byte to kibibytes
return (byteCode.length - 2) / 2 / 1024
}

function formatByteCodeSize (byteCodeSize) {
return `${byteCodeSize.toFixed(2)} kb`
return `${byteCodeSize.toFixed(2)} KiB`
}

async function checkFile (filePath, done) {
Expand Down

0 comments on commit 93459b5

Please sign in to comment.