← COMP5565 全部讲次
Q03

期中复习题|C 计算题

英文题干,逐题折叠中文解析、英文参考答案与适用条件。

手机上宽表格可左右滑动;自测答案默认收起,点击题目下方按钮查看。

使用边界:2026-10-13 期中复习,非官方材料;正式范围、题型、时长和开闭卷要求以讲师通知为准。保留课件版本、适用条件与补充标注。

复习资料:密码学、账户与 Gas · 共识、钱包与代币 · Solidity、调用与代理 · DApp 签名与英文作答

先做题再看解析:A 判断题 · B 单选题 · C 计算题 · D 代码推演 · E 简答与 X 选做补练

统一约定:除题目另说,Solidity 为 0.8.30、默认检查算术溢出;账户按普通 EOA 发起交易的课堂模型,不讨论账户代码委托。代码是隔离的教学片段,不是生产合约;假设有足够 gas,且不存在题面未给出的外部操作。金额计算忽略题面之外的费用。页码回查一律使用 PDF 页序。

自查分值仅供练习,不代表教师评分标准;先作答,再展开每题答案。

来源代号:C01 = Lecture crypto foundation.pdf;L01 = Lecture1.pdf;L02 = Lecture2.pdf;L03 = Lecture3.pdf;L04a = Lecture 4.0.pdf;L04b = Lecture 4.1.pdf;L05 = Lecture 5.pdf。页码都是 PDF 页序。来源为对应逐讲注解;标“补充”的部分另列标准文档。所有题干均为自拟。

C · 计算题

每题 6 分。写公式、单位和条件,再核对每一步。

C1 · EIP-1559 fee cap · 6 marks

Morgan sends 0.25 ETH to Riley in a successful transaction. Its gas limit is 80,000 and its charged gas used is 50,000. In the inclusion block, the base fee is 30 gwei/gas. The transaction specifies maxFeePerGas = 32 gwei and maxPriorityFeePerGas = 5 gwei.

  1. Calculate the effective tip and effective gas price. (2)
  2. Calculate the total fee, burned amount, and tip amount in ETH. (2)
  3. State Morgan’s total ETH debit and Riley’s credit. (1)
  4. If only the max fee were changed to 29 gwei, could this transaction be included validly in the same block? Explain. (1)

Use 1 ETH=10⁹ gwei. Do not assume that a fee cap or gas limit is fully spent.

C1 · 查看答案与解析

C1 · 实付单价被 max fee 限制

推理过程:① 把 80,000 标为用量上限,把 50,000 标为最终计费用量。② 总单价最多 32,基础费占 30,只剩 2 可给小费;愿给最多 5 也只能实际给 2。③ 用 50,000 乘实付单价得到 gwei,再除以 10⁹ 换 ETH。④ 手续费与转账本金分开,最后才合计付款者扣款。

先确认 max fee ≥ base fee。对这笔有效交易:

tip=min(5,32-30)=2 gwei/gas;price=30+2=32 gwei/gas。

项目 计算 结果
总手续费 50000 × 32/10⁹ 0.0016 ETH
销毁 50000 × 30/10⁹ 0.0015 ETH
执行层小费总额 50000 × 2/10⁹ 0.0001 ETH
Morgan 总扣款 0.25 + 0.0016 0.2516 ETH
Riley 收款 题定转账金额 0.25 ETH

这里的小费进入区块指定的执行层费用接收地址,与共识层奖励分开。

如果 max fee 改为 29,而当前块 base fee 为 30,这笔交易不能有效纳入该块;不要硬算出负小费。将来 base fee 降低是另一个条件,不能说此交易永远不可能入块。

English: The effective tip is 2 gwei per gas and the total fee is 0.0016 ETH. A fee cap below the block’s base fee makes inclusion in that block invalid.

自查分值:tip 与 price 各 1;总费 1、拆分 1;扣款与收款合计 1;29 < 30 的判断及条件 1。易错:乘 80,000;直接加 30 + 5;把 max fee 当成小费。回查 L02 p.54-59。课件 p.55 写了 gas units (limit),本题明确用 charged gas used;这与上限、预留资金不是同一个量。

C2 · Mnemonic, entropy and seed · 6 marks

A BIP39 mnemonic is generated from 160 bits of entropy. Each word represents an 11-bit index. The checksum length is ENT / 32.

  1. Calculate the checksum length and number of words. (2)
  2. State the output length in bits of BIP39’s mnemonic-to-seed step. Is it the same quantity as entropy length? (2)
  3. The user enters the correct mnemonic but a different optional passphrase. Must this be rejected as a mnemonic-checksum error? What is the practical consequence? (2)
C2 · 查看答案与解析

C2 · 三种长度不要混

推理过程:按流程分三段:① 160 位熵加校验和,决定助记词有多少词;② 助记词再与附加口令参与种子派生,输出长度另按 BIP39 规则计算;③ 题目只改附加口令,没有改助记词本身,所以不能据此判断助记词校验和错误。不同输入产生另一个钱包,并不等于软件认出了“输错密码”。

  1. CS=160/32=5 bits;总长度 160+5=165 bits;165/11=15 words。(2 分)
  2. Mnemonic-to-seed 输出 512 bits = 64 bytes,不是本题 160 位的原始熵,也不是一个私钥。(2 分)
  3. 不必报 mnemonic-checksum 错误:助记词自身未改变,passphrase 在后续 seed 派生中使用。不同 passphrase 通常导向不同 seed/钱包;输错不保证有错误提示,可能看到空钱包。(2 分)

English: The mnemonic has 15 words. BIP39 derives a 512-bit seed; a different passphrase normally produces a different wallet rather than a mnemonic-checksum error.

回查 L03 p.51-53;补充 BIP39。用词:checksum 校验和,entropy 原始熵,seed 种子,passphrase 可选附加口令;不是四种叫法描述同一串数据。

C3 · Storage packing and addressing · 6 marks

Consider this contract’s state declarations, with no inheritance or custom storage base:

uint128 a = 1;
uint64 b = 2;
bool c = true;
uint256 d = 9;
uint256[] values;
mapping(address => uint256) credit;
  1. Give the slot number and byte offset of a, b, c, and d. Offsets are measured from the low-order end of a slot. (2)
  2. Give the main slots reserved for values and credit. What is stored in the main slot of values? (2)
  3. Give the storage-slot expressions for values[1] and credit[Alice]. You do not need to compute a hash digest. (2)
C3 · 查看答案与解析

C3 · 先算宽度,再排槽

推理过程:slot(存储槽)每个容纳 32 字节;byte offset(字节偏移)从低位端数。① 将类型位数除以 8:uint128 占 16,uint64 占 8,bool 占 1。② 按声明顺序填入同槽,累计得到偏移 0、16、24。③ uint256 需要完整 32 字节,剩余 7 字节放不下,就从新槽开始。④ 再给动态数组和 mapping 安排主槽,元素位置另算。

变量 slot 从低位起 byte offset 字节数
a 0 0 16
b 0 16 8
c 0 24 1
d 1 0 32
values 主槽 2 0 32
credit 主槽 3 0 32

Slot 0 用了 25 bytes,余下 7 bytes 容不下 d,所以 d 从 slot 1 开始。values 主槽存动态数组长度;credit 主槽保留,不直接存某个用户余额。

values[1] 在 uint256(keccak256(abi.encode(uint256(2)))) + 1 所指的槽,槽地址按 256 位空间计算。这里元素是 uint256,每个一整槽;若换小类型,需要重查打包规则。

credit[Alice] 在 keccak256(abi.encode(Alice, uint256(3))) 对应的槽。地址 key 和槽号各编码成 32 bytes,不能用未填充的 20-byte 地址直接拼接代替本式。

English: Packing starts at the lower-order end. Dynamic-array elements and mapping entries use hashed locations rather than expanding the declaration’s main slot.

自查分值:四个普通变量的位置共 2;两个主槽与长度含义共 2;两条寻址式各 1。回查 L05 p.7-15;补充 Solidity storage layout。

C4 · Weighted votes and slashing boundaries · 6 marks

For this calculation only, a checkpoint link requires at least two-thirds of total active effective balance to support it. The total active effective balance is 320 ETH. Twelve validators participate; eight supporters together represent 200 ETH. All votes are otherwise valid.

  1. Does this meet the stated weight threshold? Show the comparison. Does “eight out of twelve” settle it? (2)
  2. A validator signs two distinct attestations with epoch intervals (source, target) = (2, 8) and (4, 6). Identify the slashable relation. (2)
  3. Replace the intervals by (2, 8) and (2, 6). Do they satisfy either the double-vote or strict-surround condition solely from these intervals? Explain. (2)

This is a threshold exercise, not a complete implementation of Ethereum fork choice or finalization.

C4 · 查看答案与解析

C4 · 按权重,不按人数

推理过程:先分开“支持者人数”与“支持者有效余额权重”。题目门槛针对后者,所以比较 200 与总权重 320 的三分之二;交叉相乘可避免小数舍入。检查罚没时再另开一步:double vote(双重投票)比 target 是否相等;surround vote(包围投票)比起点和终点是否都严格包住,不能只比其中一端。

  1. 3 × 200=600<2 × 320=640;200 / 320 = 62.5%,不足题定的 2/3。八人占十二人的 2/3 不代表权重达标。(2 分)
  2. Surround vote:2<4<6<8,外层区间严格包围内层区间;不是 double vote,因为 target 分别为 8、6。(2 分)
  3. source 都为 2,不满足严格 source 不等式;target 不同,也不满足 double vote。因此仅凭给出的区间不符合这两条条件;这不是对所有其他有效性规则的证明。(2 分)

English: The threshold is stake-weighted. Surround voting requires strict containment; equality at the source boundary is not enough.

回查 L02 p.100-103、p.109-110 与概念校订;补充 共识规范。本题给定门槛只是局部条件,不等于“达到票数就立即最终确定一个块”。

作答后怎么复盘

先记录错题号,再分成 概念 / 英文限定词 / 算术与单位 / 调用帧与状态 / 版本与条件 五类。展开对应题目的解析看错因,遮住答案后换一组数字或条件再答一次。不要只背 T/F 和选项字母。

下一页 →
Q04 期中复习题|D 代码推演

个人整理的学习笔记,不是官方材料;数字与结论以课件和讲师为准。