Private-Blockchain

Quorum - 無法從另一個公共契約中讀取一個公共契約的狀態

  • March 22, 2019

我正在執行 truffle 的 7nodes 仲裁範例,範例功能按預期工作。0但是,當我創建一個公共契約來訪問另一個公共契約的狀態時,即使不是這樣,我也會得到一個返回值。奇怪的是,合約能夠設置被呼叫合約的狀態。我在下面添加了詳細資訊。有人可以告訴我在這裡做錯了什麼嗎?

geth version: 1.7.2-stable truffle version: v4.1.7 solc-js version: v0.4.23

公共契約 1:

pragma solidity ^0.4.17;

contract PublicStorage {
 uint public storedData;

 function PublicStorage(uint initVal) public {
   storedData = initVal;
 }

 function set(uint x) public {
   storedData = x;
 }

 function get() constant public returns (uint retVal) {
   return storedData;
 }
}

公共契約 2:

pragma solidity ^0.4.17;

import './PublicStorage.sol';

contract PubStorageReader {
 uint public storedData;
 PublicStorage publicStorage;

 function PubStorageReader(uint initVal, address publStorageAddr) public {
   storedData = initVal;
   publicStorage = PublicStorage(publStorageAddr);
 }

 function setPublic(uint x) public {
   publicStorage.set(x);
 }

 function getPublic() constant public returns (uint publicRetVal) {
   return publicStorage.get();
 }
}

部署腳本:

deployer.deploy(PublicStorage, 102)
       .then(function() {
           return deployer.deploy(PubStorageReader, 202, PublicStorage.address);
       });

呼叫:

> PublicStorage.deployed().then(function(instance) { return instance.get(); })
{ [String: '102'] s: 1, e: 2, c: [ 102 ] }

> PubStorageReader.deployed().then(function(instance) { return instance.getPublic(); })
{ [String: '0'] s: 1, e: 0, c: [ 0 ] }

> PubStorageReader.deployed().then(function(instance) { return instance.setPublic(2); })
{ ... }

> PublicStorage.deployed().then(function(instance) { return instance.get(); })
{ [String: '2'] s: 1, e: 0, c: [ 2 ] }

現在在 testrpc 上執行相同的程式碼會按預期讀取狀態。這有什麼錯誤?

我試過最新的松露(v5.0.9)和solc v0.5.0,它適用於那些:

MacBook-Pro:truffle-workspace satpal$ truffle version
Truffle v5.0.9 (core: 5.0.9)
Solidity v0.5.0 (solc-js)
Node v8.11.4
Web3.js v1.0.0-beta.37


MacBook-Pro:truffle-workspace satpal$ truffle compile

Compiling your contracts...
===========================
> Compiling ./contracts/PubStorageReader.sol
> Compiling ./contracts/PublicStorage.sol
> Artifacts written to /Users/satpal/Documents/truffle-workspace/build/contracts
> Compiled successfully using:
  - solc: 0.5.0+commit.1d4f565a.Emscripten.clang


MacBook-Pro:truffle-workspace satpal$ truffle console
truffle(development)> migrate --reset

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.

Running migration: 1_initial_migration.js
Replacing Migrations...
Migrations: 0x10ae69385C79eF3Eb815AC008A7013D6878f1d38
Saving successful migration to network...
Saving artifacts...
Running migration: 7_public_storage.js
Deploying PublicStorage...
PublicStorage: 0xF597e754F2dab6aDB6A048ade0d9a2c38dc237b3
Deploying PubStorageReader...
PubStorageReader: 0x685e0b659C3be1c465d5BB37C03E6263EfcAE25B
Saving successful migration to network...
Saving artifacts...


truffle(development)> PublicStorage.deployed().then(function(instance) { return instance.get(); })
<BN: 66>                   //NB: truffle v5.0.9 returns hex result 0x66==102
truffle(development)> PubStorageReader.deployed().then(function(instance) { return instance.getPublic(); })
<BN: 66>


truffle(development)> PubStorageReader.deployed().then(function(instance) { return instance.setPublic(2); })
{ tx: '0x6b27ff734ef52f479118cb52f9497e7bb11393c434c52d2e0784552d843caa8a',
 receipt:
  { blockHash: '0x8daf2888fd33d342d599079badc74a8131163f1628b4287b517b58913d531f95',
    blockNumber: 54,
    contractAddress: null,
    cumulativeGasUsed: 28786,
    from: '0xed9d02e382b34818e88b88a309c7fe71e65f419d',
    gasUsed: 28786,
    logs: [],
    logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
    status: true,
    to: '0x685e0b659c3be1c465d5bb37c03e6263efcae25b',
    transactionHash: '0x6b27ff734ef52f479118cb52f9497e7bb11393c434c52d2e0784552d843caa8a',
    transactionIndex: 0,
    rawLogs: [] },
 logs: [] }
truffle(development)> PublicStorage.deployed().then(function(instance) { return instance.get(); })
<BN: 2>

完全相同的問題。

我還注意到,如果您使函式“getPublic()”不是常量/視圖,則執行會耗盡氣體。

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