期中模拟卷 B|升级 状态与钱包 22-24
现有材料范围内的非官方复习,含英文术语、成立条件与自拟练习解析。
手机上宽表格可左右滑动;自测答案默认收起,点击题目下方按钮查看。
非官方自拟模拟卷 B,正式考试范围、题型、时长和开闭卷要求以讲师通知为准。 按现有期中前密码学基础、L01-L05 与 Lab 1 编写,借鉴所提供往年期末卷的可见设问结构;不加入期中后专题。本卷不是往年期中真题或押题。
全卷 100 分,建议练习 100 分钟。Part A 为自拟单选 15 题共 30 分;Part B 共 70 分。原参考文件没有选择题正文,不能据此预测实际选择规则。每题答案和给分点默认收起,自评分不等于教师评分。
统一前提:Solidity 0.8.30、默认 checked arithmetic;采用普通 EOA 交易模型,不讨论账户代码委托。代码仅用于教学;除题目说明外,gas 充足、无隐藏操作、无额外费用。计算按题面精确数值。
读题词:attacker = 攻击者;justify = 给出原因;at least = 至少;strictly = 严格;resulting state = 结果状态。问题中的“攻击者”不是哈希性质的名称。
来源代号:C01 是密码学基础;L01-L05 对应逐讲注解。答案回查页码均为原 PDF 页序。英文同义表达与机制正确的替代答案可以成立。
B 卷导航:单选题 1-8 · 单选题 9-15 · 机制与应用 16-20 · 重入推演 21 · 升级 状态与钱包 22-24
升级 状态与钱包 22-24
B22 · Q22 Preparing for an upgrade [3 marks]
A team wants users to keep the same contract address and balances when a bug is fixed later. A colleague proposes: “Deploy the application normally now; if a bug appears, edit its deployed bytecode.”
Describe a suitable proxy-based alternative, including what must be arranged at deployment and what must be checked when upgrading. Identify where application state lives and who is permitted to change the implementation.
B22 · 查看答案与解析
Q22 · Proxy deployment and upgrade [3 marks]
推理过程:先安排一个稳定入口 Proxy(代理合约),把余额等状态保存在它那里,再让 Implementation(实现合约)提供执行代码。修复时让代理改用新实现,用户仍访问同一代理地址。因为旧数据还在,必须核对新代码按相容布局读取;因为换实现能改变业务行为,必须限制谁有权升级。初始化要设置的也是代理自己的状态。
English: Deploy a proxy and an implementation before users depend on the application address; initialize the application’s state in the proxy. Users call the proxy, which executes implementation code by delegatecall while retaining its own state. A permitted upgrade changes the implementation reference, not the already deployed bytecode. Enforce upgrade authorization and verify compatible storage layout so old balances retain their meaning.
给分:部署 proxy/implementation 并初始化代理状态 1;delegatecall、proxy 保留状态与地址 1;升级权限和布局兼容两个检查均点出 1。implementation 的 constructor 不自动初始化 proxy 的 storage。回查:L05 p.3-25。
B23 · Q23 Ordered transactions, nonce and rollback [11 marks]
Initially, Alice is an ordinary EOA with 3 ETH and transaction nonce 12. Vault has storage variable x = 4 and holds 0 ETH. Both functions below are payable: add(k) increases x by k; addThenRevert(k) increases x by k and then executes an uncaught revert. Neither function sends ETH elsewhere.
A block producer examines the following candidates in the listed order, against the evolving state. It includes every valid candidate and skips any invalid candidate; skipped candidates are not reconsidered. All signatures, chain IDs, intrinsic-gas requirements and other admission conditions are satisfied except for any nonce mismatch shown. Alice can afford every transaction’s maximum upfront cost. The block has sufficient capacity.
For every included transaction, the block’s base fee is 12 gwei/gas, maxFeePerGas is 16 gwei/gas, and maxPriorityFeePerGas is 7 gwei/gas. Each gas limit is 80,000. Listed gas used is the final charged amount; do not apply another refund. T3 is not assigned gas used in advance.
| Candidate | Nonce | Operation | Attached value | Charged gas used if included |
|---|---|---|---|---|
| T1 | 12 | add(3) |
0.2 ETH | 50,000 |
| T2 | 13 | addThenRevert(9) |
0.4 ETH | 30,000 |
| T3 | 13 | add(1) |
0.1 ETH | Determine admission first |
| T4 | 14 | add(5) |
0.3 ETH | 45,000 |
(a) [9 marks] For each candidate, state whether it is (i) included and successful, (ii) included but reverted, or (iii) not included because invalid at that point. Give a reason and the resulting Alice nonce and x after that candidate is considered. Explain the difference in fee treatment between T2 and T3.
(b) [2 marks] Calculate the total fee charged to Alice and the final ETH balances of Alice and Vault. Show your effective gas-price calculation and enough working to distinguish fees from transferred value.
| Candidate | Outcome and reason | Alice nonce after | x after |
|---|---|---|---|
| T1 | |||
| T2 | |||
| T3 | |||
| T4 |
B23 · 查看答案与解析
Q23(a) · Consider candidates against evolving state [9 marks]
推理过程:按候选顺序更新两条记录:交易 nonce(交易序号)与合约 x。T1 序号 12 匹配,x 从 4 加到 7,下一序号变 13。T2 序号 13 匹配,x 暂变 16 又回滚为 7,但已入块交易消耗序号,下一号变 14。T3 仍拿 13,执行前就不匹配,因此跳过;T4 拿 14 匹配,x 从 7 加到 12,下一号变 15。
| Candidate | Outcome and reason | Alice nonce after | x after |
|---|---|---|---|
| T1 | Included, successful; nonce 12 matches | 13 | 7 |
| T2 | Included, reverted; nonce 13 matches, then execution reverts | 14 | 7 |
| T3 | Not included; nonce 13 is stale after T2 | 14 | 7 |
| T4 | Included, successful; nonce 14 matches | 15 | 12 |
English: An included reverted transaction still consumes its transaction nonce and pays for used gas. T2’s increment and 0.4 ETH transfer are rolled back, but its nonce consumption and gas charge remain. T3 is invalid before execution here: it has no included receipt and pays no on-chain execution fee in this scenario.
给分:每行 outcome + 原因各 1 分,共 4;每行 nonce/x 均对 1 分,共 4;准确区分 T2 与 T3 手续费边界 1。不要把 T3 写成“receipt status = 0”:那是已纳入交易的执行失败,不是本题的未纳入。
Q23(b) · Fee and balances [2 marks]
推理过程:先列真正入块的 T1、T2、T4;T3 未执行,不编造 gas 用量。总价封顶 16,基础价占 12,小费实际只能给 4,所以计费单价是 16。再分两本账:三笔入块交易均付已消耗 gas;本金只有成功的 0.2 与 0.3 留在 Vault,T2 的 0.4 回滚。最后从 Alice 的 3 ETH 同时扣本金与费用。
Effective tip = min(7, 16 - 12) = 4 gwei/gas. Effective price = 12 + 4 = 16 gwei/gas.
| Included transaction | Charged gas | Fee / ETH | Value retained by Vault / ETH |
|---|---|---|---|
| T1 | 50,000 | 0.000800 | 0.2 |
| T2 | 30,000 | 0.000480 | 0 |
| T4 | 45,000 | 0.000720 | 0.3 |
| Total | 125,000 | 0.002000 | 0.5 |
Final: Alice = 3 - 0.5 - 0.002 = 2.498 ETH; Vault = 0.5 ETH. 给分:正确费用计算 1;Alice、Vault 各 0.5。检查用:销毁 0.0015 ETH,小费 0.0005 ETH,相加等于 0.002;gas limit 不是 gas used。回查:L02 p.42-59;L04b p.55-59。
B24 · Q24 Wallet recovery and public content [4 marks]
(a) [2 marks] A wallet was created using a BIP39 mnemonic and a nonempty passphrase. After reinstalling the wallet, a user enters the correct mnemonic but a different passphrase and sees unrelated addresses. Explain why this need not cause an “invalid password” error, and name one additional recovery setting that must match.
(b) [2 marks] A DApp stores a document’s IPFS CID on-chain. Later, the document cannot be downloaded from any available provider. Explain why this does not by itself show that content hashing failed, and give one measure to improve availability.
B24 · 查看答案与解析
Q24 · Recovery and availability [4 marks]
推理过程:(a)恢复是重新计算:助记词 + passphrase(可选附加口令)生成 seed(种子),再按路径生成密钥。输入另一附加口令仍可算出结果,只是通常不再是原钱包,所以不保证弹出“密码错误”。(b)CID(内容标识符)供你拿到内容后核对;无人提供文件时,问题在可用性。Pinning(固定保留)与独立副本增加取到内容的机会,不是永久可用的无条件保证。
(a) English: The passphrase is an input to seed derivation, not a password checked against one stored account. A different passphrase normally derives a different wallet rather than reporting an authentication error. The derivation path/account index must also match the intended recovery settings.
给分:不同 passphrase 派生不同 seed 而非保证报错 1;正确派生路径/账户索引等设置 1。助记词正确不等于恢复参数全对。回查:L03 p.41-56 及 BIP39 补充。
(b) English: A CID supports checking the identity/integrity of retrieved content; it does not force anyone to keep serving the bytes. Pinning and maintaining replicated copies with independent providers improves availability, without making it an unconditional guarantee.
给分:完整性与可用性的区分 1;持续 pin/复制/独立存储服务等措施 1。回查:L04a p.8-18、p.30-34。
个人整理的学习笔记,不是官方材料;数字与结论以课件和讲师为准。