Ethereum 使用块菌核算以太坊合同中的交易费用

Ethereum 使用块菌核算以太坊合同中的交易费用,ethereum,truffle,Ethereum,Truffle,我正在为以太坊使用块菌测试框架(v4.0.1)。我不明白为什么对于以下简单合同,交易费加起来不等于gasPrice*gasUsed: contract MinTest { function run() public returns(bool) { return true; } } 我使用的摩卡测试是: contract('Minimum Test', function (accounts) { it("min test", function () {

我正在为以太坊使用块菌测试框架(v4.0.1)。我不明白为什么对于以下简单合同,交易费加起来不等于
gasPrice*gasUsed

contract MinTest {
    function run() public returns(bool) {
        return true;
    }
}
我使用的摩卡测试是:

contract('Minimum Test', function (accounts) {

  it("min test", function () {
    var initial = web3.eth.getBalance(accounts[1]);
    var final;

    return MinTest.deployed().then(function(instance) {
        return instance.run({from: accounts[1]});
    }).then(function(result) {
        final =  web3.eth.getBalance(accounts[1]);

        var gasPrice = new BigNumber(web3.eth.gasPrice);
        var gasUsed = new BigNumber(result.receipt.gasUsed);
        var gasCost = gasPrice.times(gasUsed);

        console.log("gasPrice       : " + gasPrice);
        console.log("gasUsed        : " + gasUsed);
        console.log("gasCost        : " + gasCost);
        console.log("initial        : " + initial);
        console.log("initial-gasCost: " + initial.minus(gasCost));
        console.log("final          : " + final);
        console.log("unaccounted    : " + initial.minus(gasCost).minus(final));
    });
  });

});
上述测试产生以下输出:

gasPrice       :            20000000000
gasUsed        :                  21478
gasCost        :        429560000000000
initial        :  100000000000000000000
initial-gasCost:   99999570440000000000
final          :   99997852200000000000
unaccounted    :       1718240000000000

我希望对
MinTest.run
函数的调用会导致
账户[1]
被借记的金额正好等于
gasPrice*gassused
,但本例中的情况并非如此。有一笔额外的
1718240000000000
wei借记,我无法解释。为什么要在此处借记额外的
1718240000000000
wei?

web3.eth.gasPrice
不是您的交易通知中指定的价格。从文档中:

此属性为只读,返回当前的天然气价格。煤气 价格由x个最新区块的中间天然气价格确定

它用于告诉您其他人正在支付什么,以便您可以动态确定“现行费率”。如果您想随时间改变交易的天然气价格,您可以使用此选项。我猜,
testrpc
刚刚设置了20000000

另一方面,当您在事务调用中未指定
gasPrice
时,它默认为1000000000。下面是一个更新的测试用例,其中传入了
gasPrice
,并输出了结果(我在测试中使用了15GWEI)

编辑-web3js文档确实说,gasPrice的默认值应该是相同的:

gasPrice:Number | String | BigNumber-(可选,默认值: (待定)本次交易的天然气价格为:, 默认为平均网络天然气价格

那可能是松露里的虫子。无论如何,如果你把自己的汽油价格算进去,数字就会出来

contract('Minimum Test', function (accounts) {

  it("min test", function () {
    var initial = web3.eth.getBalance(accounts[1]);
    var final;
    var gasPrice = new BigNumber(15000000000);

    return MinTest.deployed().then(function(instance) {
      return instance.run({from: accounts[1], gasPrice: gasPrice});
    }).then(function(result) {
      final =  web3.eth.getBalance(accounts[1]);

      var gasUsed = new BigNumber(result.receipt.gasUsed);
      var gasCost = gasPrice.times(gasUsed);

      console.log("gasPrice       : " + gasPrice);
      console.log("gasUsed        : " + gasUsed);
      console.log("gasCost        : " + gasCost);
      console.log("initial        : " + initial);
      console.log("initial-gasCost: " + initial.minus(gasCost));
      console.log("final          : " + final);
      console.log("unaccounted    : " + initial.minus(gasCost).minus(final));
    });
  });

});
  Contract: Minimum Test
gasPrice       : 15000000000
gasUsed        : 21431
gasCost        : 321465000000000
initial        : 100000000000000000000
initial-gasCost: 99999678535000000000
final          : 99999678535000000000
unaccounted    : 0
    √ min test (773ms)


  1 passing (922ms)