【葵花宝典】区块链技术面试必考题

资讯 2024-06-20 阅读:53 评论:0
作者 | John Baker 译者 | GuoxiThe author is John Baker ˂br', the translator of Guoxi. 话说,区块链行业对人才的缺口越来越大,但由于区块链涉及的知识领...
美化布局示例

欧易(OKX)最新版本

【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   全球官网 大陆官网

币安(Binance)最新版本

币安交易所app【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

火币HTX最新版本

火币老牌交易所【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

作者 | John Baker
译者 | Guoxi

The author is John Baker

话说,区块链行业对人才的缺口越来越大,但由于区块链涉及的知识领域较为广泛,能找到真正有用的人才对每个企业来说都非常不易。

In other words, there is a growing gap in talent in the block chain industry, but given the wide range of areas of knowledge involved in the block chain, it is very difficult for each enterprise to find a truly useful talent.

另一方面,由于区块链行业「待遇高」、「前途好」,很多人以次充好,做了几天开发就敢自成专家。那么真正的区块链人才究竟应该具备哪些知识?

On the other hand, because of the “good treatment” and “good futures” of the block chain industry, many people are second-guessing and develop themselves for a few days. What should really be the knowledge of the block chain?

作为想要在区块链领域发展的你,又该如何通过区块链企业的技术面试?俗话说知己知彼百战不殆,了解了企业的面试套路,才能迈出区块链的决胜的第一步。

How can you, as you want to develop in the area of the block chain, go through a technical interview with the block chain firm? It is common to say that know-it-it-it-it-it-all, know-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-it-

本篇文章「套路满满」,营长将从各方了解到的企业常见的面试问题着手,从「区块链」、「智能合约」、「Solidity」、「DApp」、「Web 3.0」、「核心技术」、「函数」等几个方面,列出在面试技术岗位中最常问的问题,并给出标准的技术解答

This article, lists the most frequently asked questions in interview technology posts and gives standard technical answers

是月薪八千,还是百万年薪,就看你能记住多少了,营长只能帮你到这了~

Eight thousand a month, or a million a year, depending on how much you can remember.

image

(1)以太坊常见问题

(1) >. . >. .....................................................................................................

问:在以太坊中,Wei和Ether(以太币)有什么区别?

asks: What's the difference between Wei and Ether in Ether?

答:Wei是以太币的最小面值,就好比说人民币的最小面值是分,英镑的最小面值是便士。 其换算关系为1以太币=10^18Wei。

Wei is the smallest face value of the Tai currency, as if the lowest face value of the renminbi was a fraction and the lowest face value of the pound was a penny.

问:以太坊的平均区块间隔时间是多少?

asks: What is the average block interval in Etheria?

答:平均区块间隔时间为14秒,当然了这只是理论值,你可以在Etherscan

Answer:

https://etherscan.io/chart/blocktime

中查到每日的平均区块时间间隔。

An average time interval of blocks per day can be found.

问:以太坊的平均区块大小是多少?

asks: What is the average size of the block in Ethio?

答:区块大小视情况而定,大约是2KB。不同于比特币使用区块大小来规定区块的交易量上限,以太坊使用燃料(gas)限制。燃料限制决定了每个区块中处理的交易量、存储/带宽的上限,因为交易和智能合约中函数的执行都要根据指令的复杂度多少来付出相应的燃料,所以使用燃料限制来约束区块大小是可行的。

Answer: blocks, depending on their size, are about 2KB. Unlike Bitcoin, which uses block sizes to cap the amount of transactions in blocks, fuel (gas) is used to limit the amount of transactions, storage/bandwidths processed in each block. Fuel restrictions determine the maximum amount of transactions, storage/bandwidths that are processed in each block, as it is feasible to use fuel limits to limit block sizes because the performance of functions in trade and smart contracts is based on the complexity of the instructions.

燃料限制会随网络的波动和矿工的意愿变化,每个矿工都可以选择自己愿意接受的燃料价格。燃料价格就像是比特币中的交易费,只是这里的价格是最小单位燃料的价格,而不是每笔交易的价格。

Fuel restrictions vary with the volatility of the network and the willingness of miners, and each miner can choose the price of the fuel he or she wishes to accept. The price of fuel is like a transaction fee in bitcoin, but the price here is the price of the smallest unit of fuel, not the price of each transaction.

想要算出一个区块中可以容纳多少笔交易,你不需要清楚地知道燃料的价格,只需知道平均每笔交易使用多少燃料并用整个燃料限制除以它即可。

If you want to figure out how many transactions can be accommodated in a block, you do not need to know the price of the fuel well enough to know how much fuel is used per transaction and to divide it with the whole fuel.

去年以太猫的发行造成了以太坊网络的大拥堵,整个网络中充斥着大量未被处理的交易。在这种情况下矿工有两种选择。他们可以投票提高燃料限制来处理更多交易,也可以开始提高自己的燃料价格标准并拒绝处理燃料费用过低的交易。

In this case, miners have two options. They can vote to increase fuel restrictions to deal with more transactions, or they can start raising their own fuel price standards and refusing to deal with low fuel costs.

与比特币一样,即使燃料价格很低的交易也可能会被处理加入区块链中,但矿工肯定会先处理完燃料价格高的交易再处理它。如果你的交易并没有那么紧急,设置一个很低的燃料价格也不是不可以,就像我们现实生活中的“花时间来节省金钱”。

Like Bitcoin, even low-priced transactions may be processed into block chains, but miners will certainly deal with high-priced transactions before they do. If your deal is not so urgent, it is not impossible to set a low-price fuel, as we actually say, "take time to save money."

如果有恶意用户持续地发起海量交易堵塞网络,全网的交易成本就会越来越高,直到这个恶意用户用完资金或者矿工赚足了交易费并决定扩大网络容量。

If a malicious user continues to launch a big deal to block the network, the cost of a network-wide transaction will increase until the malicious user runs out of money or the miner earns the transaction fee and decides to expand the network capacity.

问:以太币是如何产生的?

asks: How did the Ethercopics come out?

答:2014年以太坊项目以众筹的形式创建并预售了6000万个以太币,除此之外,矿工挖矿也会生成新的以太币。

Answer: In addition to the 60 million NTs created and pre-sold in 2014 in the form of popular funding by the , miners will also generate new NTs.

问:以太坊中的节点是什么?

asks: What's the node in Etheria?

答:从本质上来说,节点是一台连接到区块链、可以处理交易的计算机。

Answer: is essentially a computer connected to a block chain that handles transactions.

问:以太坊都有哪些网络?

asks: What are the networks in Etheria?

答:以太坊共有三种类型的网络:以太坊主链(就是我们平时用的以太坊)、以太坊测试网络(如Ropsten和Rinkeby,供开发人员的学习和测试)和以太坊私有链(也叫以太坊私有网络,任何人都能用以太坊的代码部署自己的私有链)。

. There are three types of networks: , , , , , , , , , (e.g. Ropsten and Rinkeby, for developers to study and test) and , , >, , ,

问:与以太坊网络交互都哪些方法?

asks: What are the ways to interact with Ethernonet?

答:你可以使用电子钱包或DApp。

Answer: You can use electronic wallets or Dapps.

问:你是否能在以太坊中“隐藏”一笔交易?

asks: Can you “hidden” a deal in Etheria?

答:不能。以太坊区块链中所有的交易都是公开可见的。

Answer: cannot.

问:以太坊的交易记录存放在哪里?

asks: Where's Ethio's record of transactions?

答:在公共可见的账本中,这个帐本通常被称为区块链。

问:以太坊主链已经很强大了,为什么还要使用以太坊私有链?

asks: Why do you use the etai private chain? >

答:原因有很多,主要是因为数据涉及隐私,将数据库去中心化,权限控制和测试。

Answer: for a number of reasons, mainly because of the privacy of the data, the centralization of the database, access control and testing.

问:如何查看一笔交易或一个区块的详细信息?

asks: How to view details of a transaction or a block?

答:你可以使用区块链浏览器,如etherscan.io或live.ether.camp。

Answer: You can use block chain browsers such as eitherscan.io or Live.other.camp.

问:如何查看私有链中一笔交易或一个区块的详细信息?

asks: How to view details of a transaction or a block in a private chain?

答:一些开源的区块链浏览器满足这种需求,如etherparty推出的区块链浏览器

Answer: some open-source block-chain browsers, such as block-chain browsers launched by each

https://github.com/etherparty/explorer)。

问:区块链的共识过程是什么?

asks: What is the consensus process in the block chain?

答:共识是按照特定的协议(如以太坊的协议)验证交易,将交易打包进区块并加入区块链的过程。

Answer: Consensus is the process of authenticating transactions according to specific agreements (e.g. the Ethernos agreement), packing them into blocks and adding them to the block chain.

问:以太坊挖矿操作的工作原理是什么呢?

asks: What is the working principle of mine-mining in the tata?

答:从原理上说,以太坊中的挖矿操作几乎与比特币相同。

Answer: is theoretically almost the same as Bitcoin.

image

“套路”

“Setline”

简单地说,就是对于每个包含交易的区块,矿工使用计算机反复且非常快速地试验谜题的答案,直到有一个矿工猜对谜题。

Simply put, for each block containing a transaction, miners use computers to test the answer to the puzzle repeatedly and very quickly until one miner guesses the puzzle.

更具体地说,矿工将当前区块唯一的区块头元数据(包括时间戳和软件版本)和一个随机数(nonce value)作为哈希函数的输入,函数将返回一个固定长度、看起来像是由数字和字母随机组成的乱码,叫做哈希值。哈希函数的特性是不同输入对应不同的哈希值,因此矿工仅需改变随机数的值,就会得到一个完全不同的哈希值。

More specifically, the miners use the only block head metadata of the current block (including time stampes and software versions) and a random number (nonce value) as input for the Hashi function, which returns a fixed length that looks like a random code consisting of numbers and letters, called Hashi value. The Hashi function is characterized by different input values, so that miners simply change the value of a random number and get a completely different Hashi value.

如果算出来的哈希值小于当前的目标值(挖矿难度),则这个矿工就挖出了一个区块,他会获得一些以太币奖励,然后通过向全网络广播该区块,其他节点可以验证该区块中的交易,验证通过后将该区块加入到本地区块链的副本中。也就是说,如果矿工B算出了一个哈希值,矿工A将立刻停止当前区块的哈希值计算,把B挖出的区块加入区块链中并开始新一轮的哈希值计算。

If the Hashi value is less than the current target value (the difficulty of mining), then the miner digs out a block, receives some tyats, then reports the block to the entire network, other nodes verify the trade in the block, and then adds the block to a copy of the chain in the area. That is, if Miner B calculates a hashi value, Miner A immediately stops the Hashi value of the current block, adds the dug B block to the chain and starts a new Hashi calculation.

矿工们很难在这场算力竞争中作弊。为了得到迷题的答案,除了一个个试没有更好的办法,也没有伪造这些计算工作的可能,这就是该解谜方法被称为“工作量证明”的原因。

It is difficult for miners to cheat in this arithmetic competition. In order to get the answer to the puzzle, there is no better way to try it, nor is there any possibility of falsifying these calculations, which is why the puzzle is called “the proof of the workload”.

从另一方面来说,用户不需要来验证哈希值是否正确,因为每个节点都已验证过。

On the other hand, the user does not need to verify whether the Hashi value is correct, as each node has been verified.

一般来说,每12到15秒就会有一个矿工挖出一个新区块。如果矿工解谜的时间开始出现更快或更慢的倾向,算法会自动调整问题的难度,以使矿工解谜的时间稳定在14秒左右。

Normally, a miner digs up a new block every 12 to 15 seconds. If the miners start to tend to solve the puzzle faster or slower, the algorithm automatically adjusts the difficulty of the problem in order to stabilize the miner’s puzzle in about 14 seconds.

矿工有一定几率能挖到新区块赚取以太币奖励,他们的赚钱能力取决于运气和他们投入的计算能力。

The ability of miners to make money depends on luck and the computing power they invest in.

以太坊使用的工作量证明算法被称为“ethash”,它被设计的需要更多内存,从而增大了使用昂贵的ASIC矿机挖矿的难度,因为ASIC矿机的出现严重压榨了使用其他设备矿工的收益,以至于在比特币中唯一能盈利的挖矿形式就是使用这种定制化的芯片。

As proof of the volume of work used by the Taiku, the algorithm is known as “ethash”, which is designed to require more memory, thus making it more difficult to dig with an expensive ASIC miner, because the emergence of an ASIC miner has severely squeezed the proceeds of miners using other equipment to the extent that the only profitable form of mining in bitco is the use of this customized chip.

从某种意义上来说,ethash可能已经成功实现了这一目标,因为专用的ASIC矿机不能用于挖掘以太坊(至少目前还没有)。

In a sense, ethash may have succeeded in achieving this goal, as the dedicated ASIC mine machine cannot be used to excavate the Ether (at least not yet).

此外,由于以太坊将要从工作量证明挖矿逐步过渡到权益证明挖矿,因而购买ASIC矿机可能不是一个明智的选择,因为一旦以太坊转向权益证明它必将被淘汰。

Moreover, since the Ethermill is about to gradually transition from workload proof to equity proof mining, the purchase of an ASIC miner may not be a wise option, as it is bound to be phased out once the Etherium has turned to equity.

问:区块链中最常用的两种共识协议是什么?

asks: What are the two most common consensus agreements in the block chain?

答:工作量证明(PoW)和权益证明(PoS),业界也在不断涌现新的共识协议,比如说活动证明(Proof-of-Activity,PoA)。

Answer: workload certification (PoW) and certificate of interest (PoS) and industry is also emerging with new consensus agreements, such as activity certification (Proof-of-Activity, PoA).

问:请简述权益证明的工作原理。

Ask: Please describe briefly the working principles of the certificate of entitlement.

答:权益证明就是根据持有货币的数量和时间随机选择区块的创建者,它不是计算密集型的挖矿形式。

Answer:

问:以太坊使用哪种共识协议呢?

asks: What kind of consensus agreement does Ethio use?

答:截至2018年初,以太坊使用工作量证明建立共识,但在未来它将转向权益证明。

Answer: up to the beginning of 2018, evidence of consensus-building with the use of workload by the Tai shop, but in the future it will turn to evidence of equity.

活动证明在比特币的工作量证明中引入了一种新形式的激励结构,这种混合结构结合了工作量证明和权益证明。为避免系统出现恶性通货膨胀,应该停发新的加密货币,也就是说矿工的挖矿行为不再能拿到挖矿奖励。

The activity proves that a new form of incentive structure has been introduced in Bitcoin’s workload certificate, which combines both workload and equity. In order to avoid a system of hyperinflation, the release of new encrypted currency should be stopped, meaning that miners no longer receive mining incentives.

这样系统中的加密货币数量就恒定了。在活动证明中,矿工使用工作量证明产生区块,加密货币的持有者通过权益证明签署区块,交易费将按比例分给矿工和签署该块的验证人。

In the certificate of activity, the miners use the workload to prove the creation of blocks, the holders of the encrypted currency sign blocks by means of an equity certificate, and the transaction fee is distributed proportionately to the miners and the certifiers who sign the blocks.

问:签署一笔交易需要用到什么工具?

asks: What tools do you need to sign a transaction?

答:用户的私钥。

问:在私钥丢失后,用户是否还可以恢复以太坊帐户?

asks: Can the user restore the Etherium account after the missing private key?

答:是的,用户可以使用12字助记词恢复。

Answer: Yes, users can restore with 12 words.

问:用什么方法可以连接到以太坊节点?

asks: How can you connect to the Etheria node?

答:IPC-RPC,JSON-RPC和WS-RPC(RPC指Remote Procedure Call,远程过程调用)。

answer: IPC-RPC, JSON-RPC and WS-RPC (RPC refers to Remote Process Call, remote process call).

问:以太坊中异常火爆的Geth是什么呢?

asks: what is the unusually hot Geth in the Etheria?

答:Geth是以太坊的一个命令行客户端。

: Geth is a command line client in Taiku.

问:连接到Geth客户端的默认方式是什么?

asks: What is the default way to connect to the Geth client?

答:默认情况下使用IPC-RPC,禁用其他所有的RPC。

Answer: uses IPC-RPC by default and disables all other RPCs.

image

问:Geth客户端中都有哪些API(Application Programming Interface,应用程序编程接口)?

asks: What are the APIs on the Geth client? (Application Programing Interface, Application Programming Interface)

答:Admin(管理员)、 eth(以太币)、web3、miner(矿工)、net(网络)、personal(个人)、shh、debug(调试)和 txpool(工具)。

Answer: Admin (Manager), eth (Ethan), web3, miner (miners), net (network), personal (individual), shh, debug (debug) and txpol (tools).

问:你可以使用哪些RPC通过网络连接到Geth客户端?

asks: Which RPCs can be used to connect to the Geth client?

答:你可以使用JSON-RPC或WS-RPC通过网络连接到Geth客户端。 IPC-RPC只能用来连接本地部署的Geth客户端。

Answer: You can use JSON-RPC or WS-RPC to connect to a Geth client via a network. IPC-RPC can only connect to a Geth client that is deployed locally.

问:如果你输入命令“--rpc”,启用的是哪一个RPC?

asks: If you type the command '--rpc', which RPC?

答:JSON-RPC。

Response: JSON-RPC.

问:****默认情况下哪些RPC API是启用的?

asks: *** * Which RPC APIs are enabled under default?

答:eth(以太币)、 web3和net(网络)。

answer: eth (Ethanese), web3 and net (network).

问:如何为JSON-RPC启用admin api?

asks: How to activate the admin api for JSON-RPC?

答:输入命令“--rpcapi”。

Answer: Enter the command " --rpcapi".

问:命令“--datadir”有什么功能?

Ask: What is the function of the command " --datadir"?

答:它指定了区块链的存储位置。

: specifies the storage location of the block chain.

问:Geth的“快速”同步是什么,为什么它更快速呢?

asks: "What's the fast sync? Why is it faster?"

答: “快速”同步仅下载收款交易所在的区块,并拉取(pull)整个最近状态数据库,而不是像普通同步一样,下载整个区块链的数据并重放所有发生的交易。

Answer: "quick" downloads only the blocks in the receiving exchange and pulls (pull) the entire state-of-the-art database, instead of downloading data from the entire block chain and replaying all transactions that occur, as is normally the case.

问:命令“--testnet”有什么功能?

Ask: What is the function of the command "--testnet"?

答:它将客户端连接到以太坊Ropsten测试网络。

: connects the client to the Ether Ropsten testing network.

问:启动Geth客户端会在屏幕上打印大量的输出信息,如果不想被这些繁杂信息干扰该怎么办?

asks: Start the Geth client will print a lot of output information on the screen, and what if you don't want to be disturbed by these complicated information?

答:使用“--verbosity”命令调低输出信息复杂度的值(默认值为3)。

answer: uses the "-verbosity" command to lower the value of output information complexity (default value 3).

问:如何使用IPC-RPC连接两个Geth客户端?

asks: How can IPC-RPC be used to connect two Geth clients?

答:首先启动一个Geth客户端,复制其管道位置(pipe location),然后使用相同的数据文档存储目录(datadir)启动另一个Geth客户端,并使用”--attach”命令传递复制的管道位置。

Answer: starts with a Geth client, copying its pipe location, then starts another Geth client with the same data document storage directory (datadir) and uses the "-attach " command to transmit the copied piping position.

问:如何将自定义javascript文件加载到Geth控制台?

asks: How can a custom javascript file be loaded to the Getth Console?

答:输入”--preload”命令和文件的路径即可。

Answer: Enter' - path to preload' commands and files is sufficient.

问:Geth客户端中帐户存储在哪里?

asks: Where is the account stored in the Geth client?

答:存储在密钥库(keystore)目录中。

Answer: is stored in the Keystore directory.

问:如何使用给定的账户发起一笔交易?

asks: How can a given account be used to initiate a transaction?

答:首先你要在“--unlock”命令中传入帐户地址或索引来解锁账户。然后你需要使用“--password”命令指定一个此账户的密码文件。

: first you will enter the account address or index to unlock the account in the command -unlock. Then you will need to specify a password file for this account using the command -password.

问:我们刚才说到了有关索引的内容。账户的索引取决于什么?

asks: We just talked about the index. The account index depends on what?

答:取决于你添加帐户的顺序。

: depends on the order in which you add the account.

问:Geth客户端是否能用来挖矿?

asks:

答:是的,输入“--mine”命令即可。

Answer: Yes, enter the "-mine" command.

问:挖矿选项中的“etherbase”是什么?

asks: What's "etherbase" in the mining option?

答:这是接受挖矿奖励的帐户地址,该帐户的索引为0。

Answer: is the address of the account that received the incentive to dig, indexed to 0.

(2)智能合约常见问题

(2) Smart Contract Common Problems

问:ABI是什么?

Ask: What's ABI?

答:简单来说,“ABI”就是低级别的“API”。

Answer: Simply put, “ABI” is a lower level of “API”.

ABI(Application Binary Interface)应用程序二进制接口,从本质上讲就是你调用智能合约中的函数并获取它返回值的方式。

The ABI application binary interface is essentially the way you call a function in a smart contract and get it back to value.

ABI中定义了如何调用智能合约中的函数以及应该选用哪种二进制格式将信息从一个程序组件传递到下一个程序组件的详细信息。

ABI defines how to call a function in a smart contract and what binary format should be selected to transmit the information from one program component to the next.

以太坊智能合约以字节码的形式部署在以太坊区块链上,一个智能合约中可能会有多个函数。所以说,有了ABI你才可以指定调用智能合约中的哪个函数,才能保证函数的返回值是你期望的格式。

You can then specify which function in the smart contract to use in order to ensure the return value of the function is the format you want.

这是一个来自以太坊ABI规范的例子:

This is an example of Ether's ABI norm:


如果我们想使用参数“69”和“真”调用函数baz(),我们总共会传递68字节的数据,整个过程可以分解为:

If we want to use the parameters "69" and "real" call function baz(), we will collectively transmit 68 bytes of data and the whole process can be broken down to:

0xcdcd77c0:baz()函数的ID。这是函数baz(uint32,bool)以ASCII形式编码的Keccak-256哈希值的前4个字节。

This is the first four bytes of the Keccak-256 Hashi value, which is coded by function baz (uint32, bool) in the form of ASCII.

0x0000000000000000000000000000000000000000000000000000000000000045

传入函数baz()的第一个参数,32位无符号整数69被填充为32个字节(10进制中的69换算成16进制为45)。

The first parameter of the input function baz(), 32 unsigned integer 69 is filled with 32 bytes (69 in 10 converted to 16 in 45).

0x00000000000000000000000000000000000000000000000000000000000000000001

传入函数baz()的第二个参数,布尔值真,也就是1,被填充为32个字节。

Enter the second parameter of the baz() function, the boolean value is real, i.e. 1, filled with 32 bytes.

这68个字节会存放在交易的数据字段(data),需要注意的是,一定要仔细检查交易数据字段中添加的内容,因为在将其传递给智能合约时可能会产生意外的,甚至可能是恶意的副作用。)

These 68 bytes will be stored in the transaction data field (data) and it is important to note that the additions to the transaction data fields must be carefully checked, as they may have unintended or even malicious side effects when passed to an intelligent contract.

为了避免出现生成函数ID时的常见错误,在此过程中必须使用规范的数据类型,就比如说使用标准的256位无符号整型(uint256)而不是无符号整型(uint)。

In order to avoid common errors in the generation of function ID, the regulated data type must be used in this process, such as standard 256 unsigned integer (uint256) instead of unsigned integer (uint).

在Solidity中计算上述sam()函数ID的的代码如下:

The code for calculating the above sam() function ID in Solidity is as follows:


在这里可以使用诸如web3.js等高级程序库来抽象大部分的细节,不过提供给web3.js的JSON格式ABI是必不可少的。

Advanced libraries, such as web3.js, can be used here to abstract most of the details, although the JSON format ABI provided to web3.js is essential.

注意:******ABI是一个抽象,它并不是以太坊核心协议的一部分。任何人都可以为自己的智能合约定义专属的ABI,这些智能合约的任何调用者都必须遵守该ABI的规定才能得到有意义的调用结果。但是,对于所有开发人员来说,使用Solidity,Serpent和web3.js更为简单,这些也都符合ABI的规定。

Note: ***ABI is an abstract one that is not part of the Tails Core Agreement . Anyone can define their intellectual contracts as exclusive ABI, and any caller of these smart contracts must comply with the ABI requirements in order to obtain meaningful call results. But, for all developers, it is easier to use Solidity, Serpent and Web3.js, which also meet ABI requirements.

问:智能合约是什么?

asks: What's a smart contract?

答:从本质上说,智能合约就是用多种语言编写的计算机代码。智能合约存在于区块链网络上,它们按照自身嵌入的规则执行相关操作,可以看做是参与者之间的契约。

: is essentially a computer code written in multiple languages. Smart contracts exist on a block chain network, and they perform related operations according to their own embedded rules, which can be seen as a contract between the participants.

问:智能合约可以用哪种语言编写?

asks: What language can smart contracts be written?

答:最常用的语言是Solidity,除此之外还有一些小众的语言,比如说Serpent和类Lisp语言。

Answer: most commonly used language is Solidity, in addition to a number of smaller languages, such as Serpent and Lisp-type languages.

问:智能合约都有什么样的使用场景呢?

asks: What's the use of smart contracts?

答:举一个简单的商品买卖场景,买方在以太坊中部署一个智能合约并向其中存入资金,卖方看到存入的资金后发送商品,买方收到商品后通过智能合约支付货款。这里的智能合约可以看作是一个保障淘宝交易的去中心化的支付宝。

Answer: takes a simple view of the sale of goods, where the buyer deploys and deposits a smart contract in Etheria, where the seller sends the goods when it sees the money deposited, and where the buyer receives the goods and pays for them through a smart contract.

问:什么是MetaMask?

Ask: What's MetaMask?

答:Metamask是一款插件类型的以太坊钱包,可以帮助用户在浏览器中与以太坊网络进行交互。

: Metamask is an Ether wallet of the plugin type that helps users interact with the Ethernet in the browser.

image

问:Metamask使用什么节点?

asks: What node does Metamask use?

答:它使用infura.io。

: it uses infra.io.

问:相比于传统以太坊钱包,有哪些功能是Metamask不支持的?

asks: What are the functions of Metamask that are not supported by the traditional Ether wallet?

答:它不支持采矿和部署智能合约。

: does not support mining and deployment smart contracts.

问:智能合约的执行是免费的吗?

asks: is the execution of a smart contract free of charge?

答:不,只能通过执行交易来调用智能合约,而交易需要燃料费用。

Answer: does not only call smart contracts through the execution of transactions, which require fuel costs.

问:查看智能合约的状态是免费的吗?

Ask: Is viewing the status of a smart contract free of charge?

答:是的,查询状态不需要执行交易。

: Yes, query status does not require transaction execution.

问:谁来执行智能合约?

asks: Who will execute the smart contract?

答:矿工。

Answer: Miners.

问:为什么调用智能合约中的函数需要花钱?

asks: Why do you need money to call a function in a smart contract?

答:除了一些不会改变智能合约状态,除了返回值之外没有其他逻辑的函数之外,调用智能合约中的函数都是需要花钱的。

Answer: requires money to call a function in a smart contract, except for some functions that do not change the status of a smart contract and do not have any logical function other than to return the value.

这笔花费中,除了向智能合约中转入以太币执行调用之外,调用改变智能合约状态的函数需要花费燃料来执行。

In addition to the transfer to an intelligent contract to perform a call in Chinese currency, the function calling for a change in the status of an intelligent contract requires the cost of fuel.

问:为什么以太坊中要引入燃料费用呢?

asks: Why do you want to introduce fuel costs in the taupulega?

答:因为矿工使用自己的计算机(矿机)执行智能合约代码,矿工如果能收回购买机器的成本并获得盈利才能保证整个系统生态的安全性,所以以太坊设计使得矿工可以通过执行调用者请求的代码来赚取燃料费用,从而维持一个健康的生态。

Answer: allows miners to earn fuel by performing the code requested by their callers, thereby maintaining a healthy ecology by using their own computers (miners) to execute smart contract codes that enable miners to ensure the ecological safety of the entire system if they recover the cost of buying machines and earn a profit.

问:燃料价格是否能决定交易被处理的时间?

asks: Does the price of fuel determine when the transaction is processed?

答:是的,也不全是。一般来说,你支付的燃料价格越高,交易越有可能被加入区块链。尽管如此,燃料价格并不能保证交易更快地被处理。

Answer: Yes, not all. Generally, the higher the price of fuel you pay, the more likely it is that the transaction will be added to the block chain. Nevertheless, fuel prices do not guarantee that the transaction will be processed more quickly.

问:交易中的燃料使用量取决于什么?

asks: What does fuel use in a transaction depend on?

答:燃料使用量取决于存储的用量,指令(操作码)的类型和数量。每一条以太坊虚拟机的操作码都明确规定了所需燃料的数量。

Answer: fuel use depends on the quantity of fuel stored, type of command (operational code) and quantity.

问:交易费该如何计算?

asks: How do you calculate the transaction fee?

答:****交易费=使用的燃料数量 * 燃料价格(燃料价格由交易者指定)。

Answer: *** Transaction fee = quantity of fuel used * Fuel price (fuel price specified by the dealer).

问:如果执行智能合约的花费少于交易者支付的燃料费用,他是否会获得退款?

asks: will he get a refund if the cost of implementing a smart contract is less than the cost of fuel paid by the dealer?

答:是的。

answer: Yes.

问:如果执行智能合约的花费超过了交易者支付的燃料费用,这时会发生什么?

asks: What happens if the cost of implementing an intelligent contract exceeds the cost of fuel paid by the dealer?

答:用户不会获得退款,并且一旦所有燃料耗尽执行就会停止,智能合约的状态就不会改变。

answer: users will not receive refunds and will stop once all fuel is spent and the status of the intelligent contract will not change.

问:谁来支付调用智能合约的费用?

asks: Who will pay for calling a smart contract?

答:调用智能合约的用户。

answer: calls the user of the smart contract.

问:节点在哪里运行智能合约的代码呢?

asks: Where does the node run the code of the smart contract?

答:节点在以太坊虚拟机(EVM)中运行智能合约的代码。以太坊虚拟机规范是以太坊协议的一部分。以太坊虚拟机只是节点运行的一个进程。

Answer: nodes run the code for smart contracts in the Etheraya Virtual Machine (EVM). The Etheraya Virtual Machine Code is part of the Taiyo Protocol. The Ether Virtual Machine is only a process for running the node.

问:以太坊虚拟机需要什么工具来运行智能合约?

asks: What tools do you need to run the smart contracts with the Taiwan virtual machine?

答:它需要智能合约的字节码,它由高级别语言(如Solidity)编译生成。

: requires a byte code for a smart contract, which is compiled in high-level languages (e.g. Solidity).

问:以太坊虚拟机都包含哪些部分?

asks: What are the parts of the Etherno Virtual Machine?

答:内存区域,堆栈和执行引擎。

Answer: Memory area, stacking and execution engine.

问:Remix是什么?

Ask: What's Remix?

答:Remix是一个用于开发,测试和部署智能合约的在线工具。它非常适合快速构建和测试轻量级的智能合约,但不适用于复杂的智能合约。

Remix is an online tool for developing, testing and deploying smart contracts. It is well suited to quickly build and test lightweight smart contracts, but it does not apply to complex smart contracts.

问:在Remix中,你可以连接哪些节点?

asks: What nodes can you connect to in Remix?

答:你可以使用Metamask钱包连接到公共节点,使用Geth钱包连接到本地节点以及使用Javascript虚拟机连接到内存中模拟的节点。

Answer: you can connect the public node with the Metamask wallet, the local node with the Getth wallet and the simulated node with the Javascript virtual machine.

问:什么是DApp,它与普通App有什么不同?

asks: What's a Dapp, what's it like to be a regular App?

答:应用程序通常包含一个与某些中心化资源(由组织拥有)交互的客户端,通常有一个与中心化数据层相连的中间层。如果中心化数据层中的数据丢失,则无法(轻松)恢复。

Answer: The application usually contains a client that interacts with certain centralized resources (owned by the organization) and usually has an intermediate layer connected to the centralized data layer. If data in the centralized data layer are lost, it is not possible (explicit) to recover.

DApp表示去中心化的应用程序。DApp通过智能合约与区块链网络交互,它们使用的数据驻留在智能合约的实例中,与中心化的数据相比,去中心化的数据安全性更高。

DApp represents a decentralised application. DApp interacts with block-chain networks through smart contracts, where data are stored in intelligent contract examples and decentralised data security is higher than centralized data.

(3)Solidity常见问题

(3) Common Problems of Solidity

问:Solidity是静态类型语言(类型的检查是在运行前做的,如编译阶段)还是动态类型语言(类型的检查是在运行时做的)?

asks: Is Solidity a static type language (the type of examination is done before running, as in the compilation phase) or a dynamic type language (the type of examination is performed at running time)?

答:Solidity是静态类型语言,这意味着类型在编译阶段是已知的。

Answer: Solidity is a static type of language, which means that the type is known at the compilation stage.

问:Solidity中的什么结构与Java中的类(Class)相对应?

asks: What structure in Solidity corresponds to the class in Java?

答:智能合约。

answer: Smart Contract.

问:智能合约的实例是什么?

asks: What are the examples of smart contracts?

答:智能合约的实例就是在区块链上部署的一个智能合约。

: An example of a smart contract is a smart contract deployed on a block chain.

问:Java和Solidity之间存在哪些差异。

asks what the difference is between Java and Solidity.

答:相比于Java,Solidity支持多继承(multiple inheritance),但不支持方法重载(Overloading)。

Answer: supports multi-succession (multiple inheritance) but does not support method reloading (Overloading).

问:在Solidity文件中需要指定的第一个参数是什么?

asks: What is the first parameter that needs to be specified in the Solidity file?

答:第一个参数是Solidity编译器的版本,需要指定为^ 0.4.8。不要小看了这一步,因为它可以避免出现在使用其他版本编译器进行编译时引入的不兼容错误。

Answer: The first parameter of is the version of the Solidity compiler, which needs to be specified as 0.4.8. Do not underestimate this step, as it avoids the incompatibilities introduced by using other versions of the compiler.

问:一个智能合约包含什么?

asks: What does a smart contract contain?

答:智能合约主要由存储变量,函数和事件组成。

Answer: Smart contracts consist mainly of stored variables, functions and events.

问:智能合约中都有哪些类型的函数?

asks: What are the types of functions in smart contracts?

答:有构造函数(constructor),回退函数(fallback function),常量函数(constant functions)和修改智能合约状态的函数。

answer: has a construction function (constructor), a fallback function, a constant function (constant functions) and a function to modify the status of an intelligent contract.

问:如果我将多个智能合约定义放入单个Solidity文件中,会出现什么样的错误?

asks: What mistakes would happen if I put multiple smart contract definitions in a single Solidity file?

答:将多个智能合约定义放入单个Solidity文件中是完全可行的。

Answer: it is entirely feasible to place multiple smart contract definitions in a single Solidity file.

问:两个智能合约可以通过哪些方式进行交互?

asks: How can two smart contracts interact?

答:一个智能合约可以调用、创建和继承另一个智能合约。

Answer: A smart contract can call, create and inherit another smart contract.

问:当你尝试部署具有多个智能合约的文件时会发生什么?

asks: What happens when you try to deploy files with multiple smart contracts?

答:编译器只会部署该文件中的最后一个智能合约,也就是说,其他所有智能合约都被忽略了。

Answer: compilers will only deploy the last smart contract in the document, that is, all other smart contracts are ignored.

问:如果我有一个庞大的项目,我是否需要将所有相关的智能合约保存在一个文件中?

asks: If I have a huge project, do I need to keep all the relevant smart contracts in one file?

答:不需要,你可以使用import语句导入文件,如下所示:

Answer: does not need, and you can import documents using an import statement as follows:


问:我是否只能导入本地文件?

asks: Do I have to import only local files?

答:不,你也可以使用HTTP导入文件(甚至是Github上的文件),如下所示:

Answer: No, you can also import files (even on Github) using HTTP, as follows:


问:以太坊虚拟机的内存都有哪些部分?

asks: What's the memory of the Etherno Virtual Machine?

答:存储(Storage)、内存(Memory)和Calldata。

Answer: Storage (Storage), Memory (Memory) and Calldata.

问:请解释一下存储(Storage)。

Ask: Please explain the store.

答:可以把存储想象成一个数据库。每个智能合约都管理自己的存储变量,从而得到一个键值对数据库(256位的键和值)。存储与普通数据库的唯一区别就是,由于存在燃料费用的问题它读取和写入操作的成本更高。

Answer: imagines storage as a database. Each smart contract manages its own storage variable, thereby obtaining a key to the database (256-digit keys and values). The only difference between storage and the normal database is that it is more costly to read and write to because of fuel costs.

问:请解释一下内存(Memory)。

Ask: Please explain your memory (Memory).

答:内存是一个临时性的存储。当函数调用执行完毕后,内存中的数据将会被释放。你可以在内存中分配各种复杂的数据类型,如数组和结构体。

Answer: Memory is a temporary storage. When the function is used, the data in the memory is released. You can assign complex data types, such as arrays and structures, to the memory.

问:请解释一下Calldata。

Ask: Please explain Calldata.

答:Calldata可以理解为一个函数调用堆栈(Callstack)。它是临时的,不可修改的,它存储着以太坊虚拟机的执行数据。

Answer: Calldata can be understood as calling stacks for a function. It is temporary, unalterable, and it stores the execution data of the Taiwan Virtual Machine.

问:存储区和内存区分别存储了哪些变量?

asks: What variables are stored in the storage area and in the memory area?

答:状态变量和局部变量(通常局部变量都是对状态变量的引用)位于存储区中,而函数的参数位于内存区中。

Answer: State variables and local variables (usually local variables are references to status variables) are located in the storage area, while the parameters of the function are located in the memory area.

问:阅读以下代码,请解释代码的哪一部分对应哪个内存区域:

asks: Read the following code and explain which part of the code corresponds to which memory area:


答:第1部分 - 存储。

answer: Part 1 - Storage.


问:我是否可以这样定义一个函数:

asks if I can define a function like this:


答:是的,你可以强制函数的参数为存储类型。在这种情况下,如果你没有向函数中传入存储引用,编译器将会抱怨。

Answer: Yes, you can force the parameter of the function to the storage type. In this case, the compiler will complain if you do not insert the memory reference into the function.

问:EVM调用和非EVM调用之间有什么区别呢?

asks: What's the difference between EVM and non-EVM calls?

答:

answer:

  • EVM调用是智能合约中的函数调用,它触发函数执行并需要燃料。

    The EVM call is a function called in a smart contract that triggers the function and requires fuel.

  • 非EVM调用读取公开可见的数据,不需要燃料。

    Non-EVM calls to read publicly available data and does not require fuel.

问:如何设定智能合约的以太币余额限制,如果向有余额限制的智能合约中发送超额的以太币会发生什么?

asks: How do you set an Ether balance limit for smart contracts, and what happens if you send an excess Ether to an Ether contract with a balance limit?

答:

answer:


问:如何在智能合约帐户中设置msg.val的值?

asks: How can the value of msg.val be set in an intelligent contract account?

答:msg.val=> msg.value=

answer: msg.val=andgt; msg.value=

随消息发送的wei的个数。

Send the number of wei with the message.

(4)DApp和web3.0常见问题

(4)DApp and web3.0 common problems

问:DApp是什么?

asks: What's a Dapp?

答:去中心化应用程序(DApp)是在点对点网络的计算机上而不是单台计算机上运行的应用程序。去中心化应用程序自点对点网络出现以来就已经存在,它是一种旨在以不受任何实体控制的方式存在于Internet上的软件工具。

Answer: is an application running on a point-to-point network computer, not on a single computer. A de-centre application exists since the point-to-point network appeared, a software tool designed to be available on the Internet in a manner not controlled by any entity.

  • 去中心化应用程序不一定需要在区块链网络上运行。 BitTorrent(去中心化下载软件)、Popcorn Time(去中心化视频软件)、BitMessage(去中心化通信协议)、Tor(洋葱路由)都是在点对点网络上运行的传统去中心化应用程序,它们都没有在区块链上运行(区块链是一种特殊的点对点网络)。

    The on the block chain network. BitTorrent (decentralized downloading software), Popcorn Time (decentralized video software), BitMessage (decentralized communication protocol), Tor (onion route) are traditional decentralized applications operating on the point-to-point network, none of which operate on the block chain (a special point-to-point network).

  • 与简单的智能合约相反,在比特币的经典使用场景,即用户A向用户B汇款,去中心化应用程序的参与者可以是市场中的任意一个人。

    , contrary to simple smart contracts , in the classic use of Bitcoin, where user A remits to user B, the participant in a decentralised application may be any person in the market.

问:DApp与智能合约有何不同?

asks: What's the difference between DApp and the smart contract?

答:去中心化应用程序是一个基于区块链的网站,其中智能合约允许它连接到区块链中。说起来有点绕,为了更好地理解去中心化应用程序和智能合约,让我们先来了解传统网站的工作方式。

Answer: The decentralised application is a block-based web site in which smart contracts allow it to be connected to the block chain. It's a bit of a twist. In order to better understand decentralized applications and smart contracts, let's start with how traditional sites work.

  • 传统的Web(网络)应用程序在前端使用HTML,CSS和Javascript来呈现页面,在后端它需要利用API从数据库中获取一些详细信息,比如用户的用户名和头像。当你进入网页版的新浪微博时,该页面将调用API来获取你的个人数据并将其显示在页面上。也就是说,传统网站的工作方式为:前端→API→数据库。

    The traditional Web application uses HTML, CSS and Javascript at the front end, where it needs to use API to get some details from the database, such as user names and headers. When you enter the web page, the page calls API to get your personal data and show it on the page. That is, the traditional website works in the following way: the API database.

  • 去中心化应用程序类似于传统的Web应用程序。它在前端使用完全相同的技术来呈现页面。一个关键的区别是,去中心化应用程序不使用API来连接到数据库,而是使用智能合约连接到区块链。所以说,基于去中心化应用程序的网站的工作方式为:前端→智能合约→区块链。

    Decentralizing applications is similar to the traditional Web applications. It uses exactly the same technology to present pages at the front end. A key difference is that decentralizing applications do not use API to connect to databases, but rather to use smart contracts to connect to block chains. So, the way the website based on decentralizing applications works is: front-end smart contracts to block chains.

在传统的中心化应用程序中,后端代码在中心化的服务器上运行。而去中心化应用程序与之相反,它的后端代码在去中心化的点对点网络上运行。去中心化应用程序包括从后端到前端的整个软件包,智能合约只是去中心化应用程序的一部分。

In traditional centralised applications, backends run on centralised servers. Contrary to decentralized applications, backends run on decentralised point-to-point networks. Decentralized applications include the entire package from back to front. Smart contracts are only part of decentralized applications.

也就是说,DApp包括:

In other words, Dapp includes:

  • 前端(你能看到的页面),和

    frontend (pages you can see), and

  • 后端(应用程序后台的逻辑)。

    backend (application backstage logic).

另一方面,智能合约仅包含后端,通常只是整个DApp的一小部分。这意味着如果你想在智能合约系统上创建去中心化的应用程序,你必须与多个智能合约相结合并依赖第三方系统作为前端

On the other hand, smart contracts contain only backends, usually only a fraction of the whole DApp. means that if you want to create decentralised applications on smart contract systems, you must combine multiple smart contracts and rely on third-party systems as frontends .

问:前端使用什么工具连接到后端的智能合约?

asks: What tools are used to connect to backend smart contracts?

答:Web3 API程序库。

: Web3 API Library.

问:请列几个你所知道的DApp。

Ask: Please list some of the Dapps you know.

答:Golem(单词含义:傀儡),Golem项目旨在创建业界首个闲置计算机算力的全球市场;Augur(单词含义:预兆),Augur项目旨在将预测市场与去中心化网络相结合,创建一个预测工具,以获得潜在的收益;Aragon Network,它旨在充当一个方便用户的数字化管理组织。

Answer: Golem (word meaning: puppet), the Golem project aims to create the first global market for idle computer computing in industry; Augur (word meaning: omens), the Augur project aims to combine forecasting markets with decentralized networks and create a forecasting tool to obtain potential benefits; Aragon Network, it aims to serve as a user-friendly digital management organization.

问:你需要什么工具与DApp的智能合约进行交互?

asks: What tools do you need to interact with Dapp's smart contract?

答:需要智能合约的ABI和字节码。

: requires smart contract ABI and bytes.

问:ABI的作用是什么?

asks: What's the role of ABI?

答:ABI是智能合约公共接口的描述,DApp用这个公共接口来调用智能合约。

ABI is a description of the public interface for smart contracts, which Dapp uses to call for smart contracts.

问:字节码的作用是什么?

asks: What's the role of bytes?

答:每个节点上的以太坊虚拟机都需要智能合约的字节码来运行智能合约。

: Every node of

问:为什么要使用大数运算程序库(BigNumber library)?

asks: Why use BigNumber library?

答:因为Javascript无法正确处理大数字。

: due to Javascript's inability to process large numbers correctly.

问:为什么要始终检查DApp代码的开头是否设置了web3提供程序(provider)?

asks: Why always check if the beginning of the DApp code has a web3 provision program in place?

答:因为如果不这样,Metamask会用自己的web3提供程序覆盖掉它。

: because otherwise Metamask would have covered it with his own web3 program.

问:为什么使用web3 js的1.x版本而不是0.2x.x版本?

asks: Why do we use version 1.x of web3 js instead of version 0.2x.x?

答:主要是因为1.x版本的异步调用使用promise对象(承诺将来会执行,比回调对象更合理和更强大)而不是回调对象,promise对象也是javascript中的首选。

Answer: is mainly due to the use of promise objects for the atrophy of version 1.x (a promise that it will be implemented in the future, more rational and stronger than the echoes) rather than the return objects, which are also the preferred ones in Javascript.

问:在web3 1.x版本中如何列出所有帐户?

asks: How do all accounts are listed in version 1.x of web3?

答:使用命令:“web3.eth.getAccounts

answer: uses command: “web3.eth.getAccounds

问:“.call”和“.send”有什么区别?

asks: “.call” and “.send”?

答:“.send”发起交易并且产生费用,而“.call”仅查询智能合约的状态不产生费用。

answer: '.send' initiates the transaction and incurs costs, while “.call” merely inquires about the status of the smart contract at no cost.

问:是否可以通过这样的命令“.send({value:1})”发送一个以太币?

asks if it's possible to send a talisman by such an order'.send ({value: )'?

答:不可以,实际上这样你只送了1wei(1以太币=10^18Wei)。交易中的单位是wei,而不是以太币。

Answer: can't, actually so you send only 1 wei (1 taels = 10 18 Wei). The unit in the transaction is wei, not tai.

问:那是否意味着,为了发送一个以太币,我需要将值设置为10^18?

asks: Does that mean that I need to set the value to 10:18?

答:不用,这样太麻烦。你可以使用util方法,即:

Answer: No, it's too much trouble. You can use the util method, which is:

web3.utils.toWei(1,'ether')

问:调用“.send()”时我需要指定什么?

asks: What do I need to specify when calling'.send()'?

答:你必须指定其中的发件人地址“from”。其他一切都是可选的。

: You must specify the sender's address, "from." Everything else is optional.

问:将以太币发送到特定地址的函数是否只有

asks if only functions will be sent to a particular address in a tusk.

web3.eth.sendTransaction()一个?

web3.eth.sendTransation() One?

答:不,你还可以调用智能合约中的函数。

Answer: No, you can also call functions in an intelligent contract.

问:以太坊的可扩展性问题有什么解决方案?

asks: What are the solutions to Ether's scalable problems?

答:解决可拓展性问题的思路是使用链下交易机制。具体的解决方案有状态通道(state channels)和plasma。

Answer: addresses the problem of scalability by using a chain-based trading mechanism.

02

面试核心要点梳理

(1)Solidity文件的布局

(1) Layout of Solidity File

源文件中可能包含任意数量的智能合约定义,包括指令和编译指示(pragma directives)。

The source document may contain any number of smart contract definitions, including instructions and compile instructions (pragma directties).

Pragma版本(Version Pragma)

Pragma version (Version Pragma)

Pragma版本用来声明代码应该使用的Solidity编译器版本。

The Pragma version is used to state that the code should be used for the Solidity compiler version.


注意:有了这条指令,源文件不会使用早于版本0.4.0的编译器进行编译,也不会使用晚于版本0.5.0的编译器进行编译。

Note: With this instruction, the source document will not be compiled using a compiler earlier than version 0.4.0 and will not be compiled using a compiler later than version 0.5.0.

导入其他源文件

Imports other source files

以太坊的Solidity语言支持与JavaScript非常相似的导入语句(import),不过Solidity中没有“默认导出(default export)”的概念。

The import statement (import) that is very similar to JavaScript is supported in the Tails' Solidity language, but there is no concept of “default export” in Solidity.

在全局级别,你可以使用以下的导入语句:

At the global level, you can use the following import statements:


上述代码将“filename(你所指定的文件名)”中所有全局符号导入到当前的全局范围。

The code above imports all global symbols in Filename (the filename you specify) to the current global scope.


上述代码创建了一个新的全局符号symbolName,其成员是“filename”中所有的全局符号。

The code above creates a new global symbol symbol Name, which consists of all global symbols in " filename ".

注释

和大多数编程语言一样,Solidity也可以使用单行注释和多行注释。

Like most programming languages, Solidity can also use one-line notes and multi-line notes.


(2)以太坊存储区域

(2) to store area

在我们进一步深入了解Solidity之前,你应该知道以太坊有三个存储区域。

You should know that there are three storage areas in Etheria before we get to know more about Solarity.

  1. 存储(Storage):所有智能合约状态变量所在的位置。每个智能合约都有自己的存储,并且只有函数调用才能改变存储中的状态变量。

    Storage: The location where all smart contract status variables are located. Each smart contract has its own storage and only a function call can change the status variable in storage.

  2. 内存(Memory):用于保存外部调用的临时数据并在调用结束之后释放数据,相比之下,使用内存中的数据价格更低。

    Memory (Memory): The data in the memory are less expensive than the data in the memory, which is used to save temporary data called externally and to release data after the call has been completed.

  3. 堆栈(Stack):用于保存小的局部变量。堆栈几乎是免费使用的,但只能保存有限数量的值。

    Stack: Saves small local variables. Stacks are almost free of charge, but only a limited number of values can be saved.

对于几乎所有的类型,你都无法指定它们存储的位置,因为每次使用时它们都会在这三种类型间互相复制。

For almost all types, you cannot specify where they are stored, because each time they are used, they are copied between the three types.

在了解了以太坊Solidity中的存储位置之后,我们来学习Solidity中的数据类型。

After learning about the storage location in the Etherm Solidity, we came to learn the type of data in Solidity.

(3)Solidity中的数据类型

(3) Data type in Solidity

以下的数据类型通常也被称为值类型,因为这些类型的数据始终按值传递。

The following data types are also commonly referred to as value types because they are always transmitted by value.


Solidity中的数据类型

Type of data in Solidity

布尔类型

Boolean Type

关键词:Bool

Keyword: Bool

布尔类型可能的取值是固定的,即“真”或“假”。

The possible value for the Boolean type is fixed, i.e., “real” or “false”.

整型

integer

关键字:int / uint(从uint8(8位无符号整型)到uint256(256位无符号整型),从int8(8位整型)到int256(256位整型))

Keywords: int/ uint (from uint8 (8 unsigned integer) to uint256 (256 unsigned integer), from int8 (8 integer) to int256 (256 integer))

整型就是各种大小的有符号或无符号整数。

Integer is a symbol or unsigned integer of all sizes.

我们可以按以下代码来定义整型:

We can define the integer by the following code:


在上面的代码中,我们创建了一个名为InsignedInt的无符号整型(uint)并将它的值设置为50。

In the code above, we created an unsigned integer (uint) called InsignedInt and set its value to 50.

地址类型

address type

关键字:address

Keyword: address

地址类型用于保存长度为20字节的值(以太坊地址的长度)。地址类型有其内部的数据成员,是所有智能合约的基础。

The address type is used to save a value of 20 bytes in length (the length of the Taiwan address). The address type has its internal data member, which is the basis for all smart contracts.

地址的数据成员:余额(balance)与转账(transfer)

Address data member: balance (balance) and transfer (transfer)

你可以使用余额属性查询某个地址中的余额,也可以使用转账函数将以太币发送到某个地址,如下所示:

You can use the balance attribute to search for the balance in an address or the transfer function to send it to an address in too much currency, as follows:


字符串类型

string type

关键字:字符串中的文字通常用双引号或单引号引出,例如“foo”,“bar”。

Keywords: Text in a string is usually quoted in double quotes or single quotes, e.g. "foo", "bar".

字符串类型可以是任意长度的UTF数据,如下所示:

String types may be UTF data of any length, as follows:


这些值类型可以在包含运算符的表达式中相互交互。接下来,我们就来说说各种运算符。

These value types can interact with each other in expressions that include operators. Next, let's talk about operators.

(4)运算符

(4) Operator

Solidity中的运算符与JavaScript相同,有四种类型的运算符:

The operators in Solidity are the same as JavaScript and have four types of operators:


Solidity中的运算符

Operator in Solidity

算术运算符

Arithmetic Operator

与大多数编程语言类似,Solidity语言中包含一些非常简单的数学运算:

Similar to most programming languages, the Solidity language contains very simple mathematical calculations:

  • 加法:x + y

    Add: x + y

  • 减法:x - y

    Subtraction: x-y

  • 乘法:x * y

    Multiplication: x* y

  • 除法:x / y

    Division: x/ y

  • 求模/求余:x%y

    Modified/residual: x%y

如下所示,Solidity中也支持指数运算符:

Index operators are also supported in Solidity as follows:



增量运算符

incremental operator

Solidity中的增量运算符包括:a ++,a - ,++ a,-a,a +=1,a=a + 1

Increment operators in Solidity include: a ++, a -, ++ a, -a, a + = 1, a=a + 1

Solidity中增量运算符的运算规则与其他编程语言类似。

The operating rules for incremental operators in Solidity are similar to those for other programming languages.

位运算符

bit operator

Solidity中的位运算符包括:按位或“ | ”,按位异或“ ^ ”,按位取反“ ~ ”,按位右移“ >> ”,按位左移“ << ”。

The bits operators in Solidity include: bit by bit or "bang", bit by bit or "", bit by counter "~", bit by right "> >" and bit by left "< <".

逻辑运算符

Logical Operator

Solidity中的逻辑运算符包括:逻辑非“ ! ”,逻辑与“ && ”,逻辑或“ || ”,相等“==”,不相等“ !=”。

Logical operators in Solidity include: logic is not "!", logic is " & &", logic or "pull", equivalent "==" and not equal "!=".

用法请看以下示例:

Please see the following examples for use:


在复杂的智能合约中仅仅使用这些数据类型不能满足需求。为此,Solidity还提供了数据结构。

The data structure is also available for this purpose.

(5)Solidity中的数据结构

(5) Data structure in Solidity

Solidity提供三种类型的数据结构:

Solidity provides three types of data structure:


Solidity中的数据结构

Data structure in Solidity

结构体

Structure

Solidity提供了一种以结构体定义新数据类型的方法。 结构体是自定义类型,其中可以包含多个变量。我们可以按如下方式定义结构体:

Solidity provides a way to define new data types by structure. Structures are self-defined types that can contain multiple variables. We can define structures in the following manner:


注意:结构体类型中最多只能有16个成员,数量超标后可能会发生“堆栈太深(Stack too Deep)”错误。

Note: A maximum of 16 members of the structure type can be exceeded by an error of " Stack too Deep ".

结构体类型可以用来创建功能更完善、更复杂的数据类型。

Structure types can be used to create more functional and complex data types.

结构体类型很强大,但如果要处理多个相同种类的数据,比如说要存储多个以太坊地址,该怎么办呢?与大多数编程语言类似,Solidity中也支持数组。

The type of structure is strong, but what if you want to process more than one type of data, for example, storage of more than one Ether address? Similar to most programming languages, clusters are supported in Solidity.

数组

array

Solidity中的数组可以是固定长度数组,也可以是动态数组。

The arrays in Solidity may be fixed length arrays or dynamic arrays.


如果数组中的元素为结构体,那么我们可以得到一个结构体数组。如下所示,使用刚才创建的Voter结构体创建一个结构体数组:

If the element in the array is a structure, we can get a structure array. As shown below, a structure array is created using the Voter structure that has just been created.


注意:将数组声明为公开(public)将自动为其创建getter方法。

N.B. Declarations of arrays as public will automatically create a getter method for them.


映射

map

映射可以看作是元素被虚拟初始化的哈希表,即每个键值对中的键被初始化,键映射的值用默认值0代替。

Maps can be seen as the Hashi table where elements are virtually initialized, i.e. the keys in each key pair are initialized and the values in the key map are replaced by the default value of zero.

映射的声明方式如下所示:

The mode of presentation of the map is as follows:


注意:“_Keytype”可以是除了动态数组,智能合约,枚举类型和结构体类型之外的任何类型。

Note: “_Keytype” may be any type other than dynamic arrays, smart contracts, listing types and structure types.

请看以下示例:

Please see the following examples:



(6)程序控制结构

(6) procedural control structure

除了switch和goto之外,Solidity支持JavaScript中的大多数程序控制结构,也就是说Solidity支持以下的程序控制结构:if,else,while,do,for,break,continue,return,? :,它们的用法与C语言和JavaScript中的用法相同。

With the exception of switch and goto, Solidity supports most of the procedural control structures in JavaScript, i.e. Solidity supports the following procedural control structures: if, else, while, do, for, break, continue, return?: they are used in the same way as in C and JavaScript.

注意:Solidity不能像C语言和JavaScript那样将非布尔类型数据转换成布尔类型数据。

Note: Solidity cannot convert non-bullet type data into Boolean type data as in C and JavaScript.

现在让我们看看如何在Solidity中使用这些程序控制结构:

Now let's see how these program control structures can be used in Solidity:


将数据类型与程序控制结构相结合就得到了程序的代码。接下来让我们谈谈智能合约中可执行的代码单元,也就是我们刚才提到的函数。

When you combine the data type with the program control structure, you get the program code. Let's talk about the implementable code units in the smart contract, which is the function that we just mentioned.

(7)函数

function (7)

如何在Solidity中声明一个函数呢?请看以下示例:

How to declare a function in Solidity? Please see the following examples:


上面的代码中声明了一个空函数,它有两个参数:一个是字符串和一个是无符号整型(uint)。

The code above states an empty function with two parameters: a string and an unsigned integer (uint).

你可以通过以下命令调用这个函数:

You can call this function with the following command:


除了函数之外,Solidity中还提供函数修改器(Function Modifiers)。

In addition to functions, there is a function changer (Function Modifiers) in Solidity.

(8)函数修改器

function changer

函数修改器可以轻易的修改一个函数的功能。只要提前在智能合约的函数定义中声明,即使在执行函数调用之前也可以进行修改。

function changer can easily change the function of a function. You can simply declare in the function definition of an intelligent contract that you can change it even before you call for an executable function.

如果你想要调用一个只有函数的所有者或创建者才能调用的销毁智能合约函数killContract(),代码如下所示:

If you want to call a destruction contract function that can only be called by the owner or creator of a function, the code is as follows:



(9)继承

(9) Succession

Solidity可以通过复制包含多态(接口的不同实现方式)的代码来支持多继承(一个子类可以有多个父类,它继承了多个父类的特性)。

Solidity can support multiple inheritance (a subcategory can have more than one parent, which inherits more than one parent) by copying the code containing multiple forms (different ways of achieving the interface).


原文链接:

Original link:

https://medium.com/coinmonks/blockchain-interview-questions-the-collection-38de299ce44d

本文转载自年薪百万的技术面试都问啥?来收下这份必考题葵花宝典吧|套路,感谢翻译。

. What's the technical interview from

美化布局示例

欧易(OKX)最新版本

【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   全球官网 大陆官网

币安(Binance)最新版本

币安交易所app【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

火币HTX最新版本

火币老牌交易所【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址
文字格式和图片示例

注册有任何问题请添加 微信:MVIP619 拉你进入群

弹窗与图片大小一致 文章转载注明

分享:

扫一扫在手机阅读、分享本文

发表评论
平台列表
美化布局示例

欧易(OKX)

  全球官网 大陆官网

币安(Binance)

  官网

火币(HTX)

  官网

Gate.io

  官网

Bitget

  官网

deepcoin

  官网
热门文章
  • DDO在新加坡上市真假,欧意交易所能交易数字期权吗?

    DDO在新加坡上市真假,欧意交易所能交易数字期权吗?
    然而,对于DDO在新加坡的列名是否真实存在争议。 据报道,有人质疑DDO的数字选项是否真的上市,其背景是否经过了彻底调查。 首先,有报告说,DDO数字选项清单仅仅是一种宣传手段,没有找到DDO交易的平台,这就对DDO的真正清单提出了疑问。 其次,一些媒体调查了DDO数字选项的背景。 报告显示DDO数字选项发行人声称其数字选项是国家赞助的,但实际上,根据中国人民银行,虚拟货币相关业务是非法金融活动。 此外,DDO数字选项在视频号码等平台上广为传播,吸引了许多信徒的注意,然...
  • Griffin Gaming Partners计划为其第三支基金筹集5亿美元

    Griffin Gaming Partners计划为其第三支基金筹集5亿美元
    Pitchbook引用的6月8日《快链头条新闻》作为监管文件, 指出游戏风险投资公司Griffin赌博伙伴计划为其第三个旗舰基金筹集5亿美元, 比第二个基金少33%。 2021年,格里芬赌博伙伴从Web3游戏开发者Forte获得A回合资金1.85亿美元,并于2022年筹集了第二个旗舰基金,金额达7.5亿美元,此时风险资本家对Web3和加密游戏的热情达到顶峰。...
  • 加密货币之王重回王位:比特币飙升至 71,000 美元,还能再涨多少?

    加密货币之王重回王位:比特币飙升至 71,000 美元,还能再涨多少?
    比特币是市场上最大的加密货币,它再次打破了重要的7万美元门槛。 在短短的四舍五入(67,000美元到69,000美元之间)之后,价格在这一水平上遇到了强烈的抵制。 然而,势头的不断增强表明,比特币可能形成一个超过70 000美元的板块,为重新测试下一个抵抗阵地71 300美元和3月份可能攀升到历史最高点73 700美元铺平了道路。 问题仍然是:比特币能否维持预期的上升趋势并继续大幅上升?    分析家预计比特币价格将上升到74,400美元。 加密货币分析师Ali M...
  • 比过山车还狠!比特币价格再次暴跌

    比过山车还狠!比特币价格再次暴跌
      上周日,比特币的价格创造了3000美元的历史新高,随后就开始各种高台跳水了。Last Sunday, the price of Bitcoin created a record high of $3,000, and then began to dive on all the high platforms.   据外媒报道,本周一,比特币价格一度下跌到2526.4美元,最高跌幅高达14.5%,这创造了2015年1月以来最大跌幅。According to external...
  • 几张图看懂区块链技术到底是什么?https://www.cnblogs.com/behindman/p/8873191.html

    几张图看懂区块链技术到底是什么?https://www.cnblogs.com/behindman/p/8873191.html
    “区块链”的概念可以说是异常火爆,好像互联网金融峰会上没人谈一谈区块链技术就out了,BAT以及各大银行还有什么金融机构都在开始自己的区块链研究工作,就连IBM最近也成立了自己的区块链研究实验室,但其实区块链到底是什么?大家或许并不清楚,停留在雾里看花的状态。从今天开始,就让我们一起走进区块链,揭开区块链的神秘面纱吧!The concept of a block chain can be described as an unusually hot one, as if no...
标签列表