From 7a0ef2ecaebbdd1da8bc76c80a0bfbcf04855917 Mon Sep 17 00:00:00 2001 From: Ryan Ghods Date: Wed, 5 Feb 2020 15:43:54 -0800 Subject: [PATCH] add tests for increased coverage --- .github/workflows/account-coverage.yml | 4 +- .travis.yml | 26 ------ package.json | 9 +- src/index.ts | 14 +-- test/{index.ts => index.spec.ts} | 115 +++++++++++++++++++++++-- 5 files changed, 122 insertions(+), 46 deletions(-) delete mode 100644 .travis.yml rename test/{index.ts => index.spec.ts} (52%) diff --git a/.github/workflows/account-coverage.yml b/.github/workflows/account-coverage.yml index ce49081782..e8be0aec70 100644 --- a/.github/workflows/account-coverage.yml +++ b/.github/workflows/account-coverage.yml @@ -10,10 +10,10 @@ jobs: - uses: actions/checkout@v1 - run: npm install + - run: npm run coverage - - run: npm run coveralls - - name: Post coverage data to coveralls.io for analysis + - name: Post coverage to Coveralls uses: coverallsapp/github-action@master with: github-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 54f3ecc05f..0000000000 --- a/.travis.yml +++ /dev/null @@ -1,26 +0,0 @@ -language: node_js -node_js: - - "6" - - "7" - - "8" -env: - - CXX=g++-4.8 -addons: - apt: - sources: - - ubuntu-toolchain-r-test - packages: - - g++-4.8 -env: - matrix: - - CXX=g++-4.8 TEST_SUITE=test -matrix: - fast_finish: true - include: - - os: linux - node_js: "6" - env: CXX=g++-4.8 TEST_SUITE=coveralls - - os: linux - node_js: "6" - env: CXX=g++-4.8 TEST_SUITE=lint -script: npm run $TEST_SUITE diff --git a/package.json b/package.json index e0cca74488..b8446ade62 100644 --- a/package.json +++ b/package.json @@ -14,8 +14,7 @@ "scripts": { "build": "ethereumjs-config-build", "prepublishOnly": "npm run test && npm run build", - "coverage": "ethereumjs-config-coverage", - "coveralls": "ethereumjs-config-coveralls", + "coverage": "npx nyc npm run test", "docs:build": "typedoc --out docs --mode file --readme none --theme markdown --mdEngine github --excludeNotExported src", "format": "ethereumjs-config-format", "format:fix": "ethereumjs-config-format-fix", @@ -24,7 +23,7 @@ "tsc": "ethereumjs-config-tsc", "lint": "ethereumjs-config-lint", "lint:fix": "ethereumjs-config-lint-fix", - "test": "ts-node node_modules/tape/bin/tape ./test/index.ts" + "test": "npx tape -r ts-node/register test/*.spec.ts" }, "husky": { "hooks": { @@ -54,9 +53,9 @@ "@types/bn.js": "^4.11.3", "@types/node": "^11.9.4", "@types/tape": "^4.2.33", - "coveralls": "^3.0.0", "husky": "^2.1.0", - "nyc": "^13.2.0", + "merkle-patricia-tree": "^3.0.0", + "nyc": "^15.0.0", "prettier": "^1.15.3", "tape": "^4.10.1", "ts-node": "^7.0.1", diff --git a/src/index.ts b/src/index.ts index fc9c82f26a..a9c3c23998 100644 --- a/src/index.ts +++ b/src/index.ts @@ -52,7 +52,7 @@ export default class Account { * ] * * var data = { - * nonce: '', + * nonce: '0x0', * balance: '0x03e7', * stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', * codeHash: '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', @@ -136,7 +136,7 @@ export default class Account { * ) * * let raw = { - * nonce: '', + * nonce: '0x0', * balance: '0x03e7', * stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', * codeHash: '0xb30fb32201fe0486606ad451e1a61e2ae1748343cd3d411ed992ffcc0774edd4', @@ -190,10 +190,10 @@ export default class Account { * ~~~ * // Requires manual merkle-patricia-tree install * const SecureTrie = require('merkle-patricia-tree/secure') - * const Account = require('./index.js').default + * const Account = require('./index.js').default * * let raw = { - * nonce: '', + * nonce: '0x0', * balance: '0x03e7', * stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', * codeHash: '0xb30fb32201fe0486606ad451e1a61e2ae1748343cd3d411ed992ffcc0774edd4', @@ -203,7 +203,7 @@ export default class Account { * let key = Buffer.from('0000000000000000000000000000000000000000', 'hex') * let value = Buffer.from('01', 'hex') * - * account.setStorage(trie, key, value, function(err, value) { + * account.setStorage(trie, key, value, function(err) { * account.getStorage(trie, key, function(err, value) { * console.log(`Value ${value.toString('hex')} set and retrieved from trie.`) * }) @@ -215,11 +215,11 @@ export default class Account { * @param val * @param cb */ - setStorage(trie: Trie, key: Buffer | string, val: Buffer | string, cb: () => void) { + setStorage(trie: Trie, key: Buffer | string, val: Buffer | string, cb: TriePutCb) { const t = trie.copy() t.root = this.stateRoot t.put(key, val, (err: any) => { - if (err) return cb() + if (err) return cb(err) this.stateRoot = t.root cb() }) diff --git a/test/index.ts b/test/index.spec.ts similarity index 52% rename from test/index.ts rename to test/index.spec.ts index ec43b9a45f..20669c6d57 100644 --- a/test/index.ts +++ b/test/index.spec.ts @@ -1,5 +1,7 @@ import * as tape from 'tape' +import * as rlp from 'rlp' import Account from '../src/index' +const SecureTrie = require('merkle-patricia-tree/secure') tape('empty constructor', function(tester) { const it = tester.test @@ -90,12 +92,19 @@ tape('constructor with RLP', function(tester) { tape('serialize', function(tester) { const it = tester.test it('should work', function(t) { - const account = new Account( - 'f84602820384a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', - ) - t.equal( - account.serialize().toString('hex'), - 'f84602820384a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', + const raw = { + nonce: '0x01', + balance: '0x0042', + stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', + codeHash: '0xc5d2461236f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', + } + const account = new Account(raw) + t.equals( + Buffer.compare( + account.serialize(), + rlp.encode([raw.nonce, raw.balance, raw.stateRoot, raw.codeHash]), + ), + 0, ) t.end() }) @@ -122,3 +131,97 @@ tape('isContract', function(tester) { t.end() }) }) + +tape('setCode && getCode', tester => { + const it = tester.test + it('should set and get code', t => { + const code = Buffer.from( + '73095e7baea6a6c7c4c2dfeb977efac326af552d873173095e7baea6a6c7c4c2dfeb977efac326af552d873157', + 'hex', + ) + + const raw = { + nonce: '0x0', + balance: '0x03e7', + stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', + codeHash: '0xb30fb32201fe0486606ad451e1a61e2ae1748343cd3d411ed992ffcc0774edd4', + } + const account = new Account(raw) + const trie = new SecureTrie() + + account.setCode(trie, code, function(err, codeHash) { + account.getCode(trie, function(err, codeRetrieved) { + t.equals(Buffer.compare(code, codeRetrieved!), 0) + t.end() + }) + }) + }) + it('should not get code if is not contract', t => { + const raw = { + nonce: '0x0', + balance: '0x03e7', + } + const account = new Account(raw) + const trie = new SecureTrie() + account.getCode(trie, function(err, code) { + t.equals(Buffer.compare(code!, Buffer.alloc(0)), 0) + t.end() + }) + }) + it('should set empty code', t => { + const raw = { + nonce: '0x0', + balance: '0x03e7', + } + const account = new Account(raw) + const trie = new SecureTrie() + const code = Buffer.alloc(0) + account.setCode(trie, code, function(err, codeHash) { + t.equals(Buffer.compare(codeHash, Buffer.alloc(0)), 0) + t.end() + }) + }) +}) + +tape('setStorage && getStorage', tester => { + const it = tester.test + it('should set and get storage', t => { + const raw = { + nonce: '0x0', + balance: '0x03e7', + stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', + codeHash: '0xb30fb32201fe0486606ad451e1a61e2ae1748343cd3d411ed992ffcc0774edd4', + } + const account = new Account(raw) + const trie = new SecureTrie() + const key = Buffer.from('0000000000000000000000000000000000000000', 'hex') + const value = Buffer.from('01', 'hex') + + account.setStorage(trie, key, value, err => { + account.getStorage(trie, key, (err, valueRetrieved) => { + t.equals(Buffer.compare(value, valueRetrieved!), 0) + t.end() + }) + }) + }) +}) + +tape('isEmpty', tester => { + const it = tester.test + it('should return true for an empty account', t => { + const account = new Account() + t.ok(account.isEmpty()) + t.end() + }) + it('should return false for a non-empty account', t => { + const raw = { + nonce: '0x01', + balance: '0x0042', + stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', + codeHash: '0xc5d2461236f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', + } + const account = new Account(raw) + t.notOk(account.isEmpty()) + t.end() + }) +})