Skip to content

Commit

Permalink
Merge pull request ethereumjs#63 from ethereumjs/addTests
Browse files Browse the repository at this point in the history
Add tests for increased coverage
  • Loading branch information
holgerd77 authored Feb 6, 2020
2 parents 485cddb + 7a0ef2e commit 6bbeb01
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 46 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/account-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
26 changes: 0 additions & 26 deletions .travis.yml

This file was deleted.

9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
Expand Down Expand Up @@ -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",
Expand Down
14 changes: 7 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default class Account {
* ]
*
* var data = {
* nonce: '',
* nonce: '0x0',
* balance: '0x03e7',
* stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
* codeHash: '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470',
Expand Down Expand Up @@ -136,7 +136,7 @@ export default class Account {
* )
*
* let raw = {
* nonce: '',
* nonce: '0x0',
* balance: '0x03e7',
* stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
* codeHash: '0xb30fb32201fe0486606ad451e1a61e2ae1748343cd3d411ed992ffcc0774edd4',
Expand Down Expand Up @@ -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',
Expand All @@ -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.`)
* })
Expand All @@ -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()
})
Expand Down
115 changes: 109 additions & 6 deletions test/index.ts → test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
})
Expand All @@ -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()
})
})

0 comments on commit 6bbeb01

Please sign in to comment.