zoukankan      html  css  js  c++  java
  • 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()
      })
    })
  • 相关阅读:
    PHP数据采集curl常用的5个例子
    【荐】PHP采集工具curl快速入门教程
    PHP常用正则表达式
    JavaScript方法call、apply、caller、callee、bind的使用详解及区别
    axf、elf文件转换成bin、hex脚本工具
    为什么数据须要做爱
    POJ 3978 Primes(求范围素数个数)
    Facebook Hacker Cup 2015 Round 1--Homework(筛选法求素数)
    后台进程整理
    Nginx优化具体,应对高并发
  • 原文地址:https://www.cnblogs.com/wanghui-garcia/p/10101287.html
Copyright © 2011-2022 走看看