小编And*_*ang的帖子

web3 websocket连接可防止节点进程退出

我有一个节点js进程,它创建一个web3 websocket连接,如下所示:

web3 = new Web3('ws://localhost:7545')
Run Code Online (Sandbox Code Playgroud)

当进程完成时(我发送一个SIGTERM),它不会退出,而是永远挂起而没有控制台输出.

我注册了SIGINT和SIGTERM一个听众在什么处理过程中具有突出以观察process._getActiveRequests()process._getActiveHandles(),我看到以下内容:

 Socket {
    connecting: false,
    _hadError: false,
    _handle: 
     TCP {
       reading: true,
       owner: [Circular],
       onread: [Function: onread],
       onconnection: null,
       writeQueueSize: 0 },
    <snip>
    _peername: { address: '127.0.0.1', family: 'IPv4', port: 7545 },
    <snip>
}
Run Code Online (Sandbox Code Playgroud)

为了完整性,这里是监听信号的代码:

async function stop() {
  console.log('Shutting down...')

  if (process.env.DEBUG) console.log(process._getActiveHandles())

  process.exit(0)
}

process.on('SIGTERM', async () => {
  console.log('Received SIGTERM')
  await stop()
})

process.on('SIGINT', async () => {
  console.log('Received SIGINT')
  await stop()
})
Run Code Online (Sandbox Code Playgroud)

看起来web3正在打开一个套接字,这是有道理的,因为我从未告诉它关闭连接.通过文档和谷歌搜索,它看起来不像web3对象的close或end方法.

手动关闭stop …

javascript websocket ethereum solidity web3-donotuse

9
推荐指数
1
解决办法
1003
查看次数

web3.eth.abi.decodeLog 返回错误的日志参数值

我有一个以太坊合约,其事件定义如下:

event Apple(address indexed a, address b, address c);
Run Code Online (Sandbox Code Playgroud)

该事件被触发,我可以在交易收据中看到日志。

通过 web3,当我尝试解析收据中的日志时,我能够检索事件参数,但看起来 的值a始终相同。

// compiled is the built contract. address is the contract address
const contract = new web3.eth.Contract(compiled.abi, address)

const eventJsonInterface = _.find(
  contract._jsonInterface,
  o => o.name === 'Apple' && o.type === 'event',
)

const log = _.find(
    receipt.logs,
    l => l.topics.includes(eventJsonInterface.signature)
)

web3.eth.abi.decodeLog(eventJsonInterface.inputs, log.data, log.topics)
Run Code Online (Sandbox Code Playgroud)

我最终得到的是:

Result {
  '0': '0x42087b16F33E688a9e73BFeef94F8F2bd2BfC98f',
  '1': '0xfc36bFe712f30F75DF0BA9A60A109Ad51ac7Ca38',
  '2': '0x6915d2f3D512F7CfEF968f653D1cA3ed4489798C',
  __length__: 3,
  a: '0x42087b16F33E688a9e73BFeef94F8F2bd2BfC98f',
  b: '0xfc36bFe712f30F75DF0BA9A60A109Ad51ac7Ca38',
  c: '0x6915d2f3D512F7CfEF968f653D1cA3ed4489798C' }
Run Code Online (Sandbox Code Playgroud)

其中a触发的事件之间始终是相同的地址。我为每笔交易生成一个新合约,a …

javascript ethereum solidity web3js

5
推荐指数
1
解决办法
4621
查看次数