Remix

地址不存在合約

  • January 20, 2020

我已經設置了一個私有 Quorum 網路,並使用 Visual Studio Code 和 remix 成功部署了合約並執行了合約功能。

當我嘗試使用新創建的帳戶使用 Web3.js 手動部署完全相同的契約時,我看到了奇怪的行為。執行部署程式碼(粘貼在末尾)不會在節點中顯示任何錯誤,實際上它會按預期返回事務雜湊。如果我檢查 TransactionReceipt 的雜湊值,它也會顯示contractAddress(下面的螢幕截圖)。 在此處輸入圖像描述

問題

  1. 當我在 remix 中的“Quorum network”擴展中提供該合約地址時,它顯示一個錯誤,指出該合約不存在(下面的螢幕截圖)。 在此處輸入圖像描述
  2. 當我在 remix 中的“部署和執行事務”擴展中提供該契約地址時,它不會給出任何錯誤並成功執行 get 函式什麼都不返回。但是當我嘗試設置功能時,它會要求我輸入密碼(下面的螢幕截圖)。我不知道這個密碼是什麼……我嘗試了部署時用來簽署交易的相同私鑰,但它不起作用。 在此處輸入圖像描述
  3. 如果我再次嘗試部署合約,它會給我一個“nonce too low”錯誤(下面的螢幕截圖)。 在此處輸入圖像描述
  4. 我只有一個 RPC,所以我不可能錯誤地使用錯誤的 RPC。我沒有對隨機數進行硬編碼,所以沒有風險。web3.eth.getTransactionCount 仍然給我 0,這表明尚未從該帳戶發送任何交易。但這沒有意義,因為我可以看到我發送的第一筆交易的 TransactionReceipt。我確實注意到 TransactionReceipt 顯示了一個我不認識的“發件人”地址。它不是我用來簽署交易的私鑰的帳戶。這個神秘的“發件人”地址有一個 transactionCountof 1 : 在此處輸入圖像描述

我在這裡超出了我的深度……如果有人能給我一些關於這個神秘地址是如何存在的想法,或者契約是否真的被部署或其他任何一點……請做。

程式碼

const Web3 = require('web3')

const rpcURL = "https://<myRPCURL>"

const web3 = new Web3(rpcURL)

const EthereumTx = require('ethereumjs-tx').Transaction

const account = '0x841Be8B07475dDE4e67b247Cf8F6dc59Ab71C93b'

const privateKey = Buffer.from('<myPivateKey>', 'hex')

var accountNonce

web3.eth.getTransactionCount(account, (err, txCount) => {
   if (err!=null) {console.log('err: ', err)}
   console.log(txCount)
   accountNonce = txCount
})

const contractByteCode = '0x608060405234801561001057600080fd5b50610970806100206000396000f3fe60806040526004361061006d576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063144f9ac71461007257806346428adc146101025780638eb5234c1461028b578063e319990c14610306578063f56228c814610396575b600080fd5b34801561007e57600080fd5b50610087610499565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100c75780820151818401526020810190506100ac565b50505050905090810190601f1680156100f45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561010e57600080fd5b506102896004803603608081101561012557600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803590602001909291908035906020019064010000000081111561016c57600080fd5b82018360208201111561017e57600080fd5b803590602001918460018302840111640100000000831117156101a057600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192908035906020019064010000000081111561020357600080fd5b82018360208201111561021557600080fd5b8035906020019184600183028401116401000000008311171561023757600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929050505061053b565b005b34801561029757600080fd5b506102c4600480360360208110156102ae57600080fd5b810190808035906020019092919050505061067b565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34801561031257600080fd5b5061031b6106b9565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561035b578082015181840152602081019050610340565b50505050905090810190601f1680156103885780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b3480156103a257600080fd5b506103ab610757565b604051808481526020018060200180602001838103835285818151815260200191508051906020019080838360005b838110156103f55780820151818401526020810190506103da565b50505050905090810190601f1680156104225780820380516001836020036101000a031916815260200191505b50838103825284818151815260200191508051906020019080838360005b8381101561045b578082015181840152602081019050610440565b50505050905090810190601f1680156104885780820380516001836020036101000a031916815260200191505b509550505050505060405180910390f35b606060058054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156105315780601f1061050657610100808354040283529160200191610531565b820191906000526020600020905b81548152906001019060200180831161051457829003601f168201915b5050505050905090565b816005908051906020019061055192919061089f565b5060008060008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000209050838160000181905550828160010190805190602001906105b592919061089f565b50818160020190805190602001906105ce92919061089f565b508360026000018190555082600260010190805190602001906105f292919061089f565b508160028001908051906020019061060b92919061089f565b506001808690806001815401808255809150509060018203906000526020600020016000909192909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050505050505050565b60018181548110151561068a57fe5b906000526020600020016000915054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60058054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561074f5780601f106107245761010080835404028352916020019161074f565b820191906000526020600020905b81548152906001019060200180831161073257829003601f168201915b505050505081565b6002806000015490806001018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156107f75780601f106107cc576101008083540402835291602001916107f7565b820191906000526020600020905b8154815290600101906020018083116107da57829003601f168201915b505050505090806002018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156108955780601f1061086a57610100808354040283529160200191610895565b820191906000526020600020905b81548152906001019060200180831161087857829003601f168201915b5050505050905083565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106108e057805160ff191683800117855561090e565b8280016001018555821561090e579182015b8281111561090d5782518255916020019190600101906108f2565b5b50905061091b919061091f565b5090565b61094191905b8082111561093d576000816000905550600101610925565b5090565b9056fea165627a7a72305820845c427085709af3cb79b879eac302fec884aed75e0f8515afbbdbfac1a9f8990029'

const txObject = {
   nonce: web3.utils.toHex(accountNonce),
   gasLimit: web3.utils.toHex(1000000),
   gasPrice: web3.utils.toHex(web3.utils.toWei('0', 'gWei')),
   data: contractByteCode
}

const tx = new EthereumTx(txObject)
tx.sign(privateKey)

const serializedTransaction = tx.serialize()
const raw = '0x' + serializedTransaction.toString('hex')

web3.eth.sendSignedTransaction(raw,(err, txHash) => {
   if (err!=null) {console.log('err: ', err)}
   console.log('txHash:', txHash)
})

admin.nodeInfo.protocols 在此處輸入圖像描述

我在這裡粘貼解決方案,這是我從鬆弛頻道獲得的回复,幾乎是逐字記錄。

預設情況下,EthereumJS-Tx 為乙太坊主網簽名。為自定義網路進行的交易有一些價值變化,在簽署交易時需要考慮到這一點。這裡發生的情況是我們使用了一些錯誤的值對交易進行了簽名,因此在恢復簽名者地址(from 欄位)時,我們得到了不同的值。為了簽署交易(至少,公共交易,這似乎是你在這裡所做的),那麼我們需要將這些值設置為它們應該的值。我從您的程式碼片段和 EthereumJS-Tx 為自定義鏈提供的說明中獲取了以下程式碼,稍作調整,因為他們的說明似乎假設在 Node.js 中使用 ES 模組,而我沒有這樣做。

const Web3 = require('web3');
const rpcURL = "http://localhost:22000";
const web3 = new Web3(rpcURL);
const EthereumTx = require('ethereumjs-tx').Transaction;
const account = '0x841Be8B07475dDE4e67b247Cf8F6dc59Ab71C93b';
const privateKey = Buffer.from('<privateKey>', 'hex');
var accountNonce;
web3.eth.getTransactionCount(account, (err, txCount) => {
   if (err != null) {
       console.log('err: ', err)
   }
   console.log(txCount);
   accountNonce = txCount
});
const contractByteCode = '0x6060604052341561000f57600080fd5b604051602080610149833981016040528080519060200190919050505b806000819055505b505b610104806100456000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680632a1afcd914605157806360fe47b11460775780636d4ce63c146097575b600080fd5b3415605b57600080fd5b606160bd565b6040518082815260200191505060405180910390f35b3415608157600080fd5b6095600480803590602001909190505060c3565b005b341560a157600080fd5b60a760ce565b6040518082815260200191505060405180910390f35b60005481565b806000819055505b50565b6000805490505b905600a165627a7a72305820d5851baab720bba574474de3d09dbeaabc674a15f4dd93b974908476542c23f00029';
const txObject = {
   nonce: web3.utils.toHex(accountNonce),
   gasLimit: web3.utils.toHex(1000000),
   gasPrice: web3.utils.toHex(web3.utils.toWei('0', 'gWei')),
   data: contractByteCode
};
const Common = require('ethereumjs-common').default;
const customCommon = Common.forCustomChain(
   'mainnet',
   {
       name: 'my-network',
       chainId: 10,
   },
   'constantinople',
);
const tx = new EthereumTx(txObject, { common: customCommon });
tx.sign(privateKey);
const serializedTransaction = tx.serialize();
const raw = '0x' + serializedTransaction.toString('hex');
web3.eth.sendSignedTransaction(raw, (err, txHash) => {
   if (err != null) {
       console.log('err: ', err)
   }
   console.log('txHash:', txHash)
});

主要區別在於我們定義了一個名為 customCommon 的變數並將其傳遞給 EthereumTx 建構子,因此它知道簽名時要使用哪些參數。我的網路的 chainId 為 10,但您應該將其更改為您的值,即 2226 - 在 geth 的 admin.node.protocols 命令中可見。

其次,簡要介紹一下 Javascript 和 Promises/Callbacks。我看到您正在獲取帳戶 nonce,然後將其分配給全域變數以供以後使用。這是行不通的,因為回調是在稍後的某個未確定的時間執行的。這意味著其餘程式碼在回調返回之前執行,並且當您創建 txObject 變數時,帳戶 nonce 始終設置為 0。因此,您得到了 nonce 太低的錯誤,因為您的交易的 nonce 也是 0,這是被採用的。這裡有3個解決方案:

  1. 將其餘程式碼放在傳遞給 getTransactionCount 的回調函式中。這意味著您不需要全域變數,您可以使用傳遞給回調的變數。
  2. 使用承諾 - 實際上只是回調,但使用不同的語法,非常流行
  3. 使用 async/await 函式,這意味著您可以等待函式返回值,而不是執行回調。這 3 個選項中的每一個都是通用的 Javascript 學習點,與 Quorum 沒有直接關係,所以我會讓你去查找它們的含義!

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