基于区块链的毕业设计Tips – 提示

本文提供基于区块链的毕业设计国外最新区块链项目源码下载,包括solidity,eth,fabric等blockchain区块链,基于区块链的毕业设计Tips – 提示 是一篇很好的国外资料

Tips

Comments

The standard in the Solidity community is to use a format called natspec https://docs.soliditylang.org/en/develop/natspec-format.html

Gas Fees

Normally there’s no benefit to using these sub-types because Solidity reserves 256 bits of storage regardless of the uint size. For example, using uint8 instead of uint (uint256) won’t save you any gas.

But there’s an exception to this: inside structs.

If you have multiple uints inside a struct, using a smaller-sized uint when possible will allow Solidity to pack these variables together to take up less storage. For example:

struct NormalStruct { uint a; uint b; uint c; }

struct MiniMe { uint32 a; uint32 b; uint c; }

// mini will cost less gas than normal because of struct packing NormalStruct normal = NormalStruct(10, 20, 30); MiniMe mini = MiniMe(10, 20, 30); For this reason, inside a struct you’ll want to use the smallest integer sub-types you can get away with.

You’ll also want to cluster identical data types together (i.e. put them next to each other in the struct) so that Solidity can minimize the required storage space. For example, a struct with fields uint c; uint32 a; uint32 b; will cost less gas than a struct with fields uint32 a; uint c; uint32 b; because the uint32 fields are clustered together.

Generate random number

https://ethereum.stackexchange.com/questions/191/how-can-i-securely-generate-a-random-number-in-my-smart-contract

The best source of randomness we have in Solidity is the keccak256 hash function.

We could do something like the following to generate a random number:

// Generate a random number between 1 and 100: uint randNonce = 0; uint random = uint(keccak256(abi.encodePacked(now, msg.sender, randNonce))) % 100; randNonce++; uint random2 = uint(keccak256(abi.encodePacked(now, msg.sender, randNonce))) % 100; What this would do is take the timestamp of now, the msg.sender, and an incrementing nonce (a number that is only ever used once, so we don’t run the same hash function with the same input parameters twice).

It would then “pack” the inputs and use keccak to convert them to a random hash. Next, it would convert that hash to a uint, and then use % 100 to take only the last 2 digits. This will give us a totally random number between 0 and 99.

This method is vulnerable to attack by a dishonest node In Ethereum, when you call a function on a contract, you broadcast it to a node or nodes on the network as a transaction. The nodes on the network then collect a bunch of transactions, try to be the first to solve a computationally-intensive mathematical problem as a “Proof of Work”, and then publish that group of transactions along with their Proof of Work (PoW) as a block to the rest of the network.

Once a node has solved the PoW, the other nodes stop trying to solve the PoW, verify that the other node’s list of transactions are valid, and then accept the block and move on to trying to solve the next block.

This makes our random number function exploitable.

Let’s say we had a coin flip contract — heads you double your money, tails you lose everything. Let’s say it used the above random function to determine heads or tails. (random >= 50 is heads, random < 50 is tails).

If I were running a node, I could publish a transaction only to my own node and not share it. I could then run the coin flip function to see if I won — and if I lost, choose not to include that transaction in the next block I’m solving. I could keep doing this indefinitely until I finally won the coin flip and solved the next block, and profit.

So how do we generate random numbers safely in Ethereum? Because the entire contents of the blockchain are visible to all participants, this is a hard problem, and its solution is beyond the scope of this tutorial. You can read this StackOverflow thread for some ideas. One idea would be to use an oracle to access a random number function from outside of the Ethereum blockchain.


提示

评论

Solidity社区的标准是使用一种称为natspec的格式https://docs.soliditylang.org/en/develop/natspec-format.html

煤气费

通常情况下,使用这些子类型没有任何好处,因为Solidity保留256位的存储空间,而不考虑uint的大小。例如,使用uint8代替uint(uint256)不会节省任何汽油。但有一个例外:结构内部。

如果在一个结构中有多个uint,那么尽可能使用较小的uint将允许Solidity将这些变量打包在一起以占用较少的存储空间。例如:

struct NormalStruct{uint a;uint b;uint c;}

struct MiniMe{uint32 a;uint32 b;uint c;}

//由于struct packing NormalStruct normal=NormalStruct(10,20,30);MiniMe mini=MiniMe(10,20,30);因此,在一个结构中,您需要使用最小的整数子类型,你可以摆脱。

您还需要将相同的数据类型聚集在一起(即,将它们放在结构中的相邻位置),这样,Solidity就可以最大限度地减少所需的存储空间。例如,具有uint32 c;uint32 a;uint32 b;字段的结构将比具有uint32 a;uint32 c;uint32 b;字段的结构花费更少的天然气,因为uint32字段聚集在一起。

https://ethereum.stackexchange.com/questions/191/how-can-i-security-generate-a-random-number-in-my-smart-contract

生成随机数

我们在Solidity中拥有的最好的随机性来源是keccak256散列函数。

我们可以执行以下操作来生成随机数:

//生成一个介于1和100之间的随机数:uint randonce=0;uint random=uint(keccak256(abi.1包装(现在,邮件发件人,随机数))%100;随机数++;uint random2=uint(keccak256(abi.1包装(现在,邮件发件人,随机数))%100;这样做的目的是获取现在的时间戳邮件发件人,以及一个递增的nonce(一个只使用过一次的数字,因此我们不会对相同的输入参数运行相同的哈希函数两次)。

然后它将“打包”输入并使用keccak将其转换为随机散列。接下来,它将把散列转换成uint,然后使用%100只取最后2位数字。这将给我们一个0到99之间的完全随机数。

此方法易受以太坊eth中不诚实节点的攻击,当您调用合约上的函数时,您会将其作为事务广播到网络上的一个或多个节点。然后,网络上的节点收集一组事务,尝试首先解决一个计算密集型的数学问题,作为“工作证明”,然后将该组事务及其工作证明(PoW)作为一个块发布到网络的其余部分。

一旦一个节点解算了PoW,其他节点就停止尝试解算PoW,验证其他节点的事务列表是否有效,然后接受该块并继续尝试解算下一个块。

这使得我们的随机数函数可以利用。假设我们有一个掷硬币的合同——正面你的钱加倍,反面你失去了一切。假设它使用了上面的随机函数来确定头部或尾部。(random&gt;=50表示正面,random&lt;50表示反面)。

如果我运行的是节点,则只能将事务发布到自己的节点,而不能共享它。然后我可以运行投币功能,看看我是否赢了-如果我输了,选择不包括在下一块我正在解决的交易。我可以无限期地这样做,直到我最终赢得了投币,解决了下一个街区,并获利。那么我们如何在以太坊eth中安全地生成随机数呢?因为区块链blockchain的全部内容对所有参与者都是可见的,所以这是一个困难的问题,其解决方案超出了本教程的范围。你可以阅读这个StackOverflow线程来获得一些想法。一个想法是使用甲骨文从以太坊eth区块链blockchain外部访问随机数函数。

Let’s say we had a coin flip contract — heads you double your money, tails you lose everything. Let’s say it used the above random function to determine heads or tails. (random >= 50 is heads, random < 50 is tails).

If I were running a node, I could publish a transaction only to my own node and not share it. I could then run the coin flip function to see if I won — and if I lost, choose not to include that transaction in the next block I’m solving. I could keep doing this indefinitely until I finally won the coin flip and solved the next block, and profit.

So how do we generate random numbers safely in Ethereum? Because the entire contents of the blockchain are visible to all participants, this is a hard problem, and its solution is beyond the scope of this tutorial. You can read this StackOverflow thread for some ideas. One idea would be to use an oracle to access a random number function from outside of the Ethereum blockchain.

部分转自网络,侵权联系删除区块链源码网

www.interchains.cc

https://www.interchains.cc/20110.html

区块链毕设网(www.interchains.cc)全网最靠谱的原创区块链毕设代做网站 部分资料来自网络,侵权联系删除! 最全最大的区块链源码站 ! QQ3039046426
区块链知识分享网, 以太坊dapp资源网, 区块链教程, fabric教程下载, 区块链书籍下载, 区块链资料下载, 区块链视频教程下载, 区块链基础教程, 区块链入门教程, 区块链资源 » 基于区块链的毕业设计Tips – 提示

提供最优质的资源集合

立即查看 了解详情