ethereumjs/ethereumjs-account-2-test

ethereumjs-account/test/index.js

const Account = require('../index.js')
const tape = require('tape')

tape('empty constructor', function (tester) {
  var it = tester.test
  it('should work', function (t) {
    var account = new Account() //创建一个空账户
    t.equal(account.nonce.toString('hex'), '')
    t.equal(account.balance.toString('hex'), '')
    t.equal(account.stateRoot.toString('hex'), '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421') //即null的RLP编码的Keccak-256 hash值,ethUtil.SHA3_RLP_S,现在更名为KECCAK256_RLP_S
    t.equal(account.codeHash.toString('hex'), 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470') //即空字符串hash值,ethUtil.SHA3_NULL_S
    t.end()
  })
})

tape('constructor with Array', function (tester) {
  var it = tester.test
  it('should work', function (t) {
    var raw = [ //数组的格式初始化
      '0x02', // nonce
      '0x0384', // balance
      '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', // stateRoot,是null的RLP编码的Keccak-256 hash值
      '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470' // codeHash,是空字符串hash值
    ]
    var account = new Account(raw)
    t.equal(account.nonce.toString('hex'), '02')
    t.equal(account.balance.toString('hex'), '0384')
    t.equal(account.stateRoot.toString('hex'), '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421')
    t.equal(account.codeHash.toString('hex'), 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470')
    t.end()
  })
})

tape('constructor with Object', function (tester) {
  var it = tester.test
  it('should work', function (t) {
    var raw = { //对象的格式初始化
      nonce: '0x02',
      balance: '0x0384',
      stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',// stateRoot,是null的RLP编码的Keccak-256 hash值
      codeHash: '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470' // codeHash,是空字符串hash值
    }
    var account = new Account(raw)
    t.equal(account.nonce.toString('hex'), '02')
    t.equal(account.balance.toString('hex'), '0384')
    t.equal(account.stateRoot.toString('hex'), '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421')
    t.equal(account.codeHash.toString('hex'), 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470')
    t.end()
  })
})

tape('constructor with RLP', function (tester) {
  var it = tester.test
  it('should work', function (t) {//RLP的格式初始化
    var account = new Account('f84602820384a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470')
    t.equal(account.nonce.toString('hex'), '02')
    t.equal(account.balance.toString('hex'), '0384')
    t.equal(account.stateRoot.toString('hex'), '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421')
    t.equal(account.codeHash.toString('hex'), 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470')
    t.end()
  })
})

tape('serialize', function (tester) {
  var it = tester.test
  it('should work', function (t) {//account.serialize()得到的就是RLP的值
    var account = new Account('f84602820384a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470')
    t.equal(account.serialize().toString('hex'), 'f84602820384a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470')
    t.end()
  })
})

tape('isContract', function (tester) {
  var it = tester.test
  it('should return false', function (t) {
    var account = new Account('f84602820384a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470')
    t.equal(account.isContract(), false)//这个不是合约账户,因为codeHash为空字符串hash值
    t.end()
  })
  it('should return true', function (t) {
    var raw = {
      nonce: '0x01',
      balance: '0x0042',
      stateRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
      codeHash: '0xc5d2461236f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'//不是空字符串hash值了
    }
    var account = new Account(raw)
    t.equal(account.isContract(), true)//所以就是合约账户了
    t.end()
  })
})

猜你喜欢

转载自www.cnblogs.com/wanghui-garcia/p/10101287.html