可靠性:父合同可以查看子合同的数据更新吗? [英] Solidity: Can a Parent contract see data updates from a Child contract?

查看:88
本文介绍了可靠性:父合同可以查看子合同的数据更新吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以太坊游戏的过早实现.我将代码分为两个合同,将游戏"功能与管理员调用的功能分开.

I have a premature implementation of an Ethereum game. I have divided my code in two contracts, separating "game" functions from functions called by the Admin.

Admin.sol继承自Game.sol,如下所示.在Admin中创建了一个struct对象,但是Game无法看到它. (Getter函数什么也不返回)

Admin.sol inherits from Game.sol as seen below. A struct object is created in Admin, but Game cannot see it. (Getter function returns nothing)

import "./Game.sol";

contract Admin is Game

即使没有分成两个合同,相同的代码也可以正常工作.

The same code, if not divided in two contracts, works perfectly.

创建对象的Admin.sol中的函数头:

Header of function in Admin.sol that creates the object:

function createJob(string memory _jname, uint _reward, uint _application_period, uint _job_duration) public {

Game.sol中getter函数的标题:

Header of the getter function in Game.sol:

function getJob(uint _jID) public view returns (string memory, uint, uint, uint, uint)

我从吸气剂中得到的是:

What I get from the getter is:

结果{'0':'','1':,'2':,'3':,'4':}

Result { '0': '', '1': , '2': , '3': , '4': }

很明显,这只是在映射中向我显示了一个空"点.

Which makes it clear it is just showing me an "empty" spot in the mapping.

是否可以让Game.sol看到在Admin.sol中所做的数据更改?如果是,该怎么做?

Is it possible to have the data changes made in Admin.sol seen by Game.sol? If yes, how is it done?

感谢您的帮助.

推荐答案

是可以的.首先您需要了解的几件事是,SmartContract只能访问在EVM中分配的自己的存储,因此,一个SmartContract不能访问另一个SmartContract的变量,除非它们被继承.在您的情况下,您可能要分别部署它们,并且在更新值时使用的是不同的实例.

Yes it is possible. Few things you have to understand first is that SmartContract can only access its own storage allocated in the EVM so one smartcontract can not access the variables of another SmartContract until they are inherit. In your case you may be deploying they seprately and while you are updating the values you are using different instances.

您要做的是仅部署一个具有继承性的合同(在下面的示例中为ContractB).您也可以在哪里使用父合同的功能.这是示例

What you have to do is to deploy only one contract contract with inheritence (in below example its ContractB). Where as you can use the function of the parent contract too. Here is the sample

pragma solidity >=0.4.22 <0.6.0;

contract ContractA {

      int a;

      function setA(int _a)  public {
           a = _a;
      }
      function getA() view public returns(int){
           return a;
      }
}

合同B就是这样

pragma solidity >=0.4.22 <0.6.0;
import"./ContractA.sol";

contract ContractB is ContractA {
       function getContractAvalue() pure public returns(int){
           // For reference that you can also access the function of ContractA in ContractB
           return ContractA.a;
       }
}

因此,如果仅部署合同B,则可以访问合同A的功能,因此只需一个实例即可进行更改,并且它将存储在EVM的相同存储空间中,可以使用正确的值进行访问.

So if you deploy the contract B only, you can access the function of Contract A so with one instance you can do the changes and it will be store in same space of storage in EVM, which can be access with correct values.

您也可以仅通过部署ContractB在混音上对其进行检查,并且可以看到Contract A的功能.

You can check it on remix too by just deploying the ContractB and you can see the functions of Contract A.

这篇关于可靠性:父合同可以查看子合同的数据更新吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆