Web3js

發送多個交易時出錯

  • May 11, 2020

我對下面的程式碼有一個奇怪的問題,有時它可以工作,有時它不能。TRANSACTIONS設置為更大的數字時似乎更明顯。我讓它失敗通過TRANSACTIONS設置為 20。令我難以置信的是,如果我設置from:為總是accounts[0]它似乎每次都有效,即使我設置TRANSACTIONS為 100。錯誤被拋出executePromises(),我在下面顯示錯誤但是這是一個Error: gas required exceeds allowance (8000029) or always failing transaction錯誤。此外,我觀察到,當它失敗時,它確實會發出一些被提取的交易,例如它為賬戶 1-5 發出交易,但不為第 6 或第 7 等發送交易。

有人可以對此有所了解嗎?

async function multipleTx(
 TRANSACTIONS,
 GAS_PRICE,
) {
 let accounts = await web3.eth.getAccounts()
 let tripKey = 0
 var sendBlockNumber = await web3.eth.getBlockNumber()
 let bookingPromiseArr = []
 let txStartTime = Date.now()
 for (let i = 0; i < TRANSACTIONS; i++) {
   let promise = ''
   promise = instance.methods.bookTrip(tripKey).send({
     from: accounts[i],
     gasPrice: GAS_PRICE,
     value: 1,
   })
   bookingPromiseArr.push(promise)
 }
 var receipts = await executePromises(bookingPromiseArr)

 /* more code below ...*/
}

async function executePromises(promisesArr) {
 let res = await Promise.all(promisesArr)
   .then(async (receipts) => {
     return receipts
   })
   .catch((error) => {
     console.log('executePromises():', error)
     process.exit(1)
   })
 return res
}

智能合約功能

   function bookTrip(uint256 key) external payable requireTrip(key) {
       Trip storage trip = trips[key];
       trip.passengers[msg.sender] = trip.price;
       trip.passengerCount++;
       emit LogNewTripPassenger(msg.sender, key, trip.price);
   }

錯誤日誌:

executePromises(): Error: gas required exceeds allowance (8000029) or always fai
ling transaction
   at C:\Users\Diddi\Documents\mjukvaruteknik\examensarbete\tqdt33\node_modules
\@truffle\hdwallet-provider\node_modules\web3-provider-engine\subproviders\provi
der.js:18:36
   at XMLHttpRequest.request.onreadystatechange (C:\Users\Diddi\Documents\mjukv
aruteknik\examensarbete\tqdt33\node_modules\web3-providers-http\src\index.js:96:
13)
   at XMLHttpRequestEventTarget.dispatchEvent (C:\Users\Diddi\Documents\mjukvar
uteknik\examensarbete\tqdt33\node_modules\xhr2-cookies\xml-http-request-event-ta
rget.ts:44:13)
   at XMLHttpRequest._setReadyState (C:\Users\Diddi\Documents\mjukvaruteknik\ex
amensarbete\tqdt33\node_modules\xhr2-cookies\xml-http-request.ts:219:8)
   at XMLHttpRequest._onHttpResponseEnd (C:\Users\Diddi\Documents\mjukvarutekni
k\examensarbete\tqdt33\node_modules\xhr2-cookies\xml-http-request.ts:345:8)
   at IncomingMessage.<anonymous> (C:\Users\Diddi\Documents\mjukvaruteknik\exam
ensarbete\tqdt33\node_modules\xhr2-cookies\xml-http-request.ts:311:39)
   at IncomingMessage.emit (events.js:228:7)
   at endReadableNT (_stream_readable.js:1185:12)
   at processTicksAndRejections (internal/process/task_queues.js:81:21)

更新

我接受了@LauriPeltonen 給出的建議,並儘可能地精簡了所有內容,並查看了@goodvibration 關於以不正確的方式混契約步和非同步執行的建議。我仍然得到同樣的錯誤。下面的程式碼顯示Transaction設置為 50。在 etherscan 上,我可以看到我的智能合約收到了假定的 50 筆交易中的 20 筆。然而,在第二次嘗試中,它成功發送了所有 50 個。

const web3 = require('./web3.js')
const DeRail = require('../../build/contracts/DeRail.json')
const address = require('../../address.json')
const instance = new web3.eth.Contract(DeRail.abi, address.address, {
 gasPrice: '20000000000',
})

async function multipleTx(TRANSACTIONS = 50, GAS_PRICE = 1000000000) {
 let accounts = await web3.eth.getAccounts()
 let tripKey = 19
 console.log(accounts.length)
 let bookingPromiseArr = []
 for (let i = 0; i < TRANSACTIONS; i++) {
   let promise = instance.methods.bookTrip(tripKey).send({
     from: accounts[i],
     gasPrice: GAS_PRICE,
     value: 1,
   })
   bookingPromiseArr.push(promise)
 }
 var receipts = await executePromises(bookingPromiseArr)
 process.exit(0)
}

async function executePromises(promisesArr) {
 try {
   let res = await Promise.all(promisesArr)
   return res
 } catch (error) {
   console.log('executePromises():', error.message)
   process.exit(1)
 }
}

multipleTx()

這似乎完全是 Infura 的問題。我懷疑這是關於他們的速率限制。儘管如此,我們現在已經使用 geth 建立了自己的乙太坊節點,並且能夠發出我們想要的任意數量的交易,幾乎沒有問題!

引用自:https://ethereum.stackexchange.com/questions/83111