如何在私人连锁店免费发送交易

时间:2019-08-09 01:30:54

标签: blockchain ethereum solidity web3

我想免费发送交易。

我建立了一条私人连锁店,从汽油价格为0开始,如下图所示。

geth --datadir node1/ --syncmode 'full' --port 30311 --rpc --rpcaddr '0.0.0.0' --rpcport 8545 --rpccorsdomain "*" --rpcvhosts "*" --rpcapi 'personal,db,eth,net,web3,txpool,miner'  --networkid 1515 --gasprice '0'

但是,它本来不需要加油,但是错误消息显示intrinsic gas too low。 我的代码如下所示

const customCommon = Common.forCustomChain(
      'mainnet',
      {
        name: 'privatechain',
        networkId: 1515,
        chainId: 1515,
      },
      'petersburg',
    )
    const functionAbi = await this.state.contract.methods.setGreeting(this.state.text).encodeABI()
    console.log(this.state.nonce)
    var details = await {
      nonce : this.state.nonce,
      gasPrice : 0,
      gas : 0,
      gasLimit: 0,
      from : this.state.web3.eth.coinbase,
      to: this.state.address,
      value : 0,
      data : functionAbi,
    };
    const transaction = await new EthereumTx(details, { common: customCommon },);  
    await transaction.sign(this.state.pk)

    var rawdata = await '0x' + transaction.serialize().toString('hex');
    console.log(rawdata)

    await this.state.web3.eth.sendSignedTransaction(rawdata)
    .on('transactionHash', function(hash){
      console.log(['transferToStaging Trx Hash:' + hash]);
    })
    .on('receipt', function(receipt){
      console.log(['transferToStaging Receipt:', receipt]);
    })
    .on('error', console.error);

我的代码有问题吗?请给我任何建议吗?

1 个答案:

答案 0 :(得分:1)

在没有可用块的情况下,事务不能包含在块中。您必须激活挖掘,以便可以开采区块并可以包含已发送的交易。

您必须添加--mine --mine.threads 1 这将激活挖掘并创建1个线程来挖掘新块。 还必须使用--unlock来解锁您的帐户(在本例中为帐户0,这是硬币基础)。 为了成功解锁帐户,您必须在.sec文件中提供帐户0的密码。该文件仅包含密码,没有任何空格或换行。 创建文件后,添加以下内容: --password <path of the .sec file> 如果您正在使用私人链,请添加--allow-insecure-unlock,因为否则解锁将不起作用。您可以根据需要在另一篇文章中查找原因。

因此,您应该总共添加以下内容:

--mine --miner.threads 1 --unlock --allow-insecure-unlock --password <path of the .sec file>

在查看“ geth帮助”时,选项“ gasprice”被标记为已弃用。 您应该使用--miner.gasprice '0'