solidity:从另一个合约调用函数时出错



对我来说遇到了一个非常不清楚的问题。有两个简单的合约:

contract Test1 {
int128 public val;    
function getVal() view public returns(int128) {
return val;
}    
function setVal( int128 _val ) public {
val = _val;
}
}
contract Test2 {
address public the1;    
function setTest1( address _adr ) public {
the1 = _adr;
}    
function setVal( int128 _val ) public {
Test1( the1 ).setVal( _val );
}    
function getVal() view public returns(int128) {
return Test1( the1 ).getVal();
}    
}

字段 Test1.val 的值可以更改为在 Test1 合约中调用函数setVal 并在 Test2 中调用相同的函数(当然是在第二个 Test2.setTest1 中设置第一个合约的地址之后((。

在混音和测试(甘纳许(中 - 一切都按预期工作。但是在专用网络(通过 Geth 实现(上,我遇到了麻烦:当我调用 Test2.setVal 时 – 值发生了变化;当我调用Test2.getVal时 - 不起作用。我通过web3j拨打电话

test2.setVal( BigInteger.valueOf(30)).send();
result = test2.getVal().send(); // (1)

在第(1(点中有一个例外:

ContractCallException: Emtpy value (0x) returned from contract.

我不知道这有什么问题。从另一个合约调用函数的机制非常简单。但我无法理解我做错了什么。

我试图调用合约的函数抛出geth-console。在这种情况下没有错误,只是 Test2.getVal (( 返回 0。

我将不胜感激任何想法!

更新。这是测试(我使用了@Ferit的测试(

const TEST_1 = artifacts.require('Test1.sol');
const TEST_2 = artifacts.require('Test2.sol'); 
contract('Ferit Test1', function (accounts) {
let test1;
let test2;
beforeEach('setup contract for each test case', async () => {
test1 = await TEST_1.at("…");
test2 = await TEST_2.at("…");   
})
it('test1', async () => {
await test1.setVal(333);
let result = await test1.getVal();
console.log( "-> test1.getVal=" + result );   
assert(result.toNumber(), 333 );
})
it('test2', async () => {
await test2.setVal(444);
let result = await test2.getVal(); // (!!!) return 0
console.log( "-> test2.getVal=" + result );   
assert(result.toNumber(), 444);
})
})

问题 1.send()应删除。

问题 2:是否确定已将测试 1 实例的地址传递给测试 2?

问题 3:需要异步调用它们。在您的测试文件中,我没有看到任何异步/等待或任何承诺条款。

我所做的更改:

  • 将合约移动到相应的文件(Test1.sol 和 Test2.sol(。
  • 通过删除.send()修复了测试文件中的问题 1
  • 通过将 Test1 实例的地址传递给 Test2 修复了测试文件中的问题 2
  • 通过使用异步/等待语法修复了测试文件中的问题 3。

修复了测试文件,如下所示:

const TEST_1 = artifacts.require('Test1.sol');
const TEST_2 = artifacts.require('Test2.sol');

contract('Test1', function (accounts) {
let test1;
let test2;
beforeEach('setup contract for each test case', async () => {
test1 = await TEST_1.new({from: accounts[0]});
test2 = await TEST_2.new({from: accounts[0]});
await test2.setTest1(test1.address); // Problem 2
})
it('should let owner to send funds', async () => {
await test2.setVal(30); // Problem 1 and 3
result = await test2.getVal(); // Problem 1 and 3
assert(result.toNumber(), 30);
})
})

欢迎来到堆栈溢出!

我找到了问题的原因。

@Adam-Kipnis关于文件生成的请求让我想到尝试使用不同的参数启动另一个专用网络。 我从这里拿走了它们。 测试奏效了!

不幸的是,我不记得我在哪里为我的专用网络获取了创世文件。homesteadBlock、eip155Block、eip158Block、byzantiumBlock中的值可能存在问题。 我将尝试部署剩余的合约并对其进行测试。我会写关于结果的文章。

非常感谢大家的参与!您的报价对于找到解决方案非常有用!

最新更新