英文:
Which is the more efficient way to hold contract data on the Ethereum block chain?
问题
我正在使用一个私有的以太坊区块链,并且对实现一些智能合约很感兴趣。然而,由于这是区块链的较新实现,信息非常有限。
举个例子,假设我想要一个保存有关个人信息的合约。在效率上,是为每个人创建一个新的合约更高效,还是在同一个合约中保存所有用户的信息更高效?
伪代码中,这两个选项看起来像这样。
选项1(为每个人实例化一个新合约):
contract = // 合约代码
ethereum.newContract(contract, userInfo);
选项2(在一个合约中保存所有用户的信息):
contract = {
var users = [];
// 其他合约代码
}
ethereum.newContract(contract, userInfo);
以下是我们如何量化这种情况的"效率":
- 每次实例化一个新合约时,我们必须为合约挖掘区块,然后挖掘用户对合约的任何交易。然而,如果我们只实例化一个合约,我们只需要挖掘一次合约部署,然后挖掘之后的任何交易,但是...
- 如果我们选择在一个合约中存储所有用户的所有信息,那么只有合约数据的"差异"(即所有用户的"数组")会作为一个区块存储,还是每个区块都存储整个数据集?或者...
- 如果我们选择"每个用户一个合约"的选项,如果我们多次存储整个合约定义,是否会"浪费空间"(并且是否值得进行可能多余的挖掘)?
希望我的问题表达清楚,如果有任何不清楚的地方,请告诉我。我相信这个问题涉及到一些"权衡"。
(关于标签的说明--我正在使用以太坊的golang实现,并使用JavaScript API与之交互。)
英文:
I am playing with a private Ethereum blockchain, and I am interested in implementing some smart contracts. However, information is very limited since this is a newer implementation of the blockchain.
Just as an example, say I want a contract that holds information about a person. Is it more efficient to create a new contract for each person, or simply hold information about all users in the same contract?
In pseudo-code, the two options look like this.
Option 1 (instantiate a new contract for each person):
contract = // contract code
ethereum.newContract(contract, userInfo);
Option 2 (hold info of all users in one contract):
contract = {
var users = [];
// other contract code
}
ethereum.newContract(contract, userInfo);
Here's how we can quantify "efficiency" in this case:
- Each time a new contract is instantiated, we have to mine the block for the contract, and then mine any transactions the user makes to the contract. If we only instantiate one contract, however, we only mine the contract deployment once, and then any transactions thereafter, but...
- If we go with the option of storing all info for all users in one contract, is only the "diff" of the contract data (the "array" of all users) stored as a block, or is the entire set of data stored in every block? Or...
- If we go with the option of "contract per user", does it "waste space" if we're storing the entire contract definition multiple times (and is it worth the possibly redundant mining that needs to take place)?
Hopefully I was clear in my question, but if not, please let me know. I believe this question is one of "trade offs".
(Re: the tags -- I'm using the golang implementation of Ethereum, and a JavaScript API to interact with it.)
答案1
得分: 3
-
是的。然而,每次您想要添加一个用户,您都需要发送一个事务来添加一个新的记录到现有的合约中。
-
区块由事务组成。每次您添加一个用户,您都需要为相应的函数调用创建一个事务。然而,您只需要这样做一次,数据不会被冗余地复制到未来的区块中。
-
是的。
从您提出问题的方式来看,似乎您应该再次了解一下事务和区块之间的区别。
英文:
-
Yes. However, each time you want to add a user you will have to send a transaction for adding a new record to the existing contract.
-
Blocks consist of transactions. Each time you add a user, you will have to create a transaction for the corresponding function call. However, you will only have to do this once and not the data will not be redundantly copied to future blocks.
-
Yes.
From the way your question is structured, it seems you should read up again on the difference between transactions and blocks.
答案2
得分: 0
一个合同对于所有用户来说应该足够了,只要你的用户对象不是太大。
从上面的代码中,建议使用一个map
,它将比数组更高效地获取用户记录。将记录按任意字符串键入,比如名字和姓氏。
mapping(string => user_struct) public users;
英文:
A single contract for all users should be sufficient, given that your user objects are not too large.
From the code above, suggest using a map
which will allow more efficient fetching of user records than an array. Key the records by any string, such as first+last name.
mapping(string => user_struct) public users;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论