← COMP5565 全部讲次
Q13

期末练习|C 长题

期末自拟练习,英文题干,逐题折叠中文解析、英文参考答案与课件页码。

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

使用边界:期末自拟练习,非官方材料。没有往年期末卷可对照,题型、分值和时长只是练习安排;正式范围、题型、时长和开闭卷要求以讲师通知为准。题干全部自拟;课件与官方文档说法不同时按课件作答,并在解析里注明差异。

复习资料:范围与以太坊、Solidity 要点 · Fabric 与选型要点 · 必背与闪卡

先做题再看解析:A 单选题 · B 短题 · C 长题

统一约定:Solidity 为 0.8.x,默认检查算术溢出;Fabric 题按课件的读写集与版本模型。页码一律用 PDF 页序。

练习安排:4 题共 50 分,建议 60 分钟。

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

来源代号:L02 = Lecture2.pdf;L03 = Lecture3.pdf;L04b = Lecture 4.1.pdf;L05 = Lecture 5.pdf;L06 = Lecture 6 - v3.pdf;L07 = Lecture 7.pdf;L08 = Lecture 8.pdf。来源为对应逐讲注解。

C · 长题

共 50 分。先写结论再展开;Fabric 题画出版本变化。

C1 · Complete a Solidity contract · 12 marks

A club keeps a shared fund. Members deposit ether; only the treasurer can pay it out. Fill in blanks (1)–(6).

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract ClubFund {
    /* (1) declare the treasurer address, readable by anyone */
    mapping(address => uint256) public deposits;

    error NotTreasurer(address caller);
    event Deposited(address indexed member, uint256 amount);
    event Paid(address indexed to, uint256 amount);

    constructor() {
        /* (2) the deployer becomes treasurer */
    }

    modifier onlyTreasurer() {
        /* (3) reject other callers with the custom error, then continue */
    }

    function deposit() external /* (4) */ {
        require(msg.value > 0, "zero deposit");
        /* (5) record the deposit and log it */
    }

    function pay(address payable to, uint256 amount) external onlyTreasurer {
        require(amount <= address(this).balance, "insufficient balance");
        /* (6) send amount to `to` with call, and stop if it fails */
        emit Paid(to, amount);
    }

    receive() external payable {
        deposits[msg.sender] += msg.value;
        emit Deposited(msg.sender, msg.value);
    }
}
  1. Blank (1). (1)
  2. Blank (2). (1)
  3. Blank (3). (2)
  4. Blank (4), and what happens without it. (2)
  5. Blank (5). (2)
  6. Blank (6), and why the return value matters. (2)
  7. Bob, who is not the treasurer, calls pay. What happens to state and to the ether? Why is a custom error preferred over a long require string? (2)
C1 · 查看答案与解析

推理过程:逐空对应课件语法点。(1) 状态变量要 public 才自动生成 getter。(2) 构造器只运行一次,部署者就是 msg.sender。(3) 修饰器里先检查,_; 代表被修饰函数体。(4) 不加 payable 的函数拒收 ether,带 value 的调用整体回退。(5) 记账加 emit 事件,DApp 才能监听到存款。(6) 推荐用 call 转账,它失败时只返回 false、不自动回滚,所以必须检查返回值。(7) Bob 调用时修饰器 revert:本次调用的状态全部回滚,ether 不动,已消耗的 gas 不退。custom error 比长 require 字符串省 gas,还能带参数。

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract ClubFund {
    address public treasurer;                                          // (1)
    mapping(address => uint256) public deposits;

    error NotTreasurer(address caller);
    event Deposited(address indexed member, uint256 amount);
    event Paid(address indexed to, uint256 amount);

    constructor() {
        treasurer = msg.sender;                                        // (2)
    }

    modifier onlyTreasurer() {
        if (msg.sender != treasurer) revert NotTreasurer(msg.sender);  // (3)
        _;
    }

    function deposit() external payable {                              // (4)
        require(msg.value > 0, "zero deposit");
        deposits[msg.sender] += msg.value;                             // (5)
        emit Deposited(msg.sender, msg.value);
    }

    function pay(address payable to, uint256 amount) external onlyTreasurer {
        require(amount <= address(this).balance, "insufficient balance");
        (bool ok, ) = to.call{value: amount}("");                      // (6)
        require(ok, "transfer failed");
        emit Paid(to, amount);
    }

    receive() external payable {
        deposits[msg.sender] += msg.value;
        emit Deposited(msg.sender, msg.value);
    }
}

English: payable lets a function receive ether. A low-level call returns a success flag that must be checked. A revert undoes all state changes of the call. Custom errors are cheaper than revert strings and can carry parameters.

自查分值:按题面分值;第 (3) 空缺 _; 扣 1 分。回查:L04b p.9–10、p.26、p.29–34、p.43–44、p.55–62、p.73。

C2 · A Fabric transaction from proposal to commit · 12 marks

Channel trade has peers of Org1, Org2 and Org3. An asset-transfer chaincode is installed on the peers of Org1 and Org2 only. The endorsement policy is AND('Org1MSP.peer','Org2MSP.peer'). A client application of Org1 submits TransferAsset(asset7, Bob).

  1. Describe the five steps from proposal to commit, naming the component that acts in each step. (5)
  2. State two things the ordering service does and two things it does not do, according to the slides. (2)
  3. The chaincode reads the local clock, so Org2’s peer returns a different write value from Org1’s peer. What happens? Would a single-endorser policy change the outcome? (2)
  4. How can Org3’s peer validate and commit this transaction without the chaincode installed? (2)
  5. How does the client application learn that the transaction has been committed? (1)
C2 · 查看答案与解析

推理过程:按「谁签、谁算、谁排、谁验」四个角色走一遍。

  1. Proposal:Org1 的客户端构造提案(链码名、函数、参数),用自己的身份签名,发给 Org1 和 Org2 的背书 peer。
  2. Endorse:背书 peer 检查客户端签名,在本地世界状态上模拟执行,不改账本,生成读写集并签名返回。
  3. Submit:客户端核对两份结果一致、满足 AND 策略,把提案、读写集和背书打包成交易,提交给排序服务。
  4. Order:排序服务把所有客户端的交易排成全序,切块,分发给通道里所有 peer,包括 Org3。
  5. Validate and commit:每个 peer 对每笔交易做背书策略检查和版本检查,标记 valid/invalid,把区块追加到区块链,只把有效交易的写集应用到世界状态。

第 2 问:排序服务做全序、打包、分发;不执行链码、不检查读写集和背书、不保存世界状态。课件 vs 官方:课件说排序服务 stateless、不检查交易内容;官方文档里它会按通道的写入策略核对提交者身份。

第 3 问:两家结果不同,背书不匹配,凑不出满足 AND 的一致背书,交易不会被提交。若策略只要一个背书方,它的结果被直接采用,因为提交方不重新执行。

第 4 问:验证只需要背书签名、背书策略和读写集版本,不需要重跑链码;Org3 信任背书方的模拟结果。

第 5 问:peer 提交后向客户端发出事件通知。

English: The client signs a proposal; endorsing peers simulate and sign rw-sets; the client submits the endorsed transaction to the orderer; the orderer orders, batches and distributes blocks; every peer checks the endorsement policy and read-set versions, then commits. Peers without the chaincode can still validate.

自查分值:第 1 问每步 1 分,其余按题面。回查:L06 p.49–54、p.56、p.58、p.73、p.86–89。

C3 · Read-write set validation · 14 marks

Versions follow the slides’ counter model: a valid write raises the key’s version by 1, and a new key starts at version 1. Initial world state (key, version, value):

Key Version Value
k1 3 100
k2 1 20
k3 2 7
k4 5 40

All six transactions were simulated on this initial state and are ordered into one block as T1 → T6.

Tx Read set (key, version) Write set
T1 (k1, 3) k1 → 110
T2 (k1, 3), (k2, 1) k2 → 30
T3 see part 1 see part 1
T4 none k5 → 50
T5 (k3, 2), (k4, 5) k4 → 41
T6 (k4, 5) delete k2

T3’s chaincode runs, in order: PutState(k3, 8), PutState(k3, 9), GetState(k2), GetState(k3).

  1. Write T3’s read set and write set. What value does the final GetState(k3) return? (3)
  2. Decide whether each of T1–T6 is valid or invalid, with a reason. (6)
  3. Give the final world state as (key, version, value). (4)
  4. Are the invalid transactions recorded in the ledger? (1)
C3 · 查看答案与解析

推理过程:第 1 问:两次 PutState 只保留最后一次,写集是 k3 → 9。课件不支持 read-your-writes,写之后再读 k3 返回已提交的值 7,并把 (k3, 2) 记进读集。T3 读集为 (k2, 1)、(k3, 2)。

第 2 问按区块顺序推,每笔交易拿读集版本和「当时」的世界状态比:

Tx 判定 原因 之后的变化
T1 valid k1 版本仍是 3 k1 → (4, 110)
T2 invalid k1 已被 T1 改成版本 4 无
T3 valid k2 仍是 1(T2 无效没写成),k3 仍是 2 k3 → (3, 9)
T4 valid 没有读集,不可能冲突 新键 k5 → (1, 50)
T5 invalid k3 已被 T3 改成版本 3 无,k4 不变
T6 valid k4 仍是 5(T5 无效没写成) 删除 k2

第 3 问最终世界状态:(k1, 4, 110)、(k3, 3, 9)、(k4, 5, 40)、(k5, 1, 50);k2 已删除。

第 4 问:T2 和 T5 仍记在区块里,被标为 invalid,只是不改世界状态。

易错点:无效交易的写不算数,所以 T3、T6 不受 T2、T5 影响;同一区块里顺序决定结果。课件 vs 官方:本题版本按课件 +1 计数;真实 Fabric 的版本号是写入该键的 (块号, 交易号),判定逻辑相同。

English: A transaction is valid only if every key in its read set still has the version it read. Valid writes update the world state in block order; invalid transactions stay in the block without changing state. Chaincode does not see its own uncommitted writes.

自查分值:第 1 问 3 分;第 2 问每笔 1 分;第 3 问每个键 1 分;第 4 问 1 分。回查:L06 p.67–72、p.77、p.81–83;L08 p.45 的同类题。

C4 · Choosing a platform · 12 marks

Twelve universities in a region plan a shared system for degree records. Facts:

  • Records must be stored and updated (issued, later possibly revoked).
  • Every university writes records for its own graduates.
  • The universities refuse to hand the registry to any single operator; no always-online third party is acceptable to all of them.
  • Only accredited universities may write, and each is identified by a certificate.
  • The universities do not fully trust one another’s records, because a writer could backdate or alter an entry.
  • Employers in any country must be able to verify a degree without joining the group.
  1. Walk through the six-question flow, answering each question with a reason, and give the final result. (6)
  2. A working group scores the ACT-IAC questions as follows. Compute the total and interpret it using the slides. (4)
Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 Q11 Q12 Q13
5 3 4 -2 4 3 2 1 4 3 2 -3 0
  1. A government launches an always-online national registry that every university and employer trusts. Q12 becomes -5 and Q4 becomes -5. Give the new total and say what the six-question flow now concludes. (2)
C4 · 查看答案与解析

推理过程:第 1 问逐问走:

  1. Store state? Yes,要保存并更新学位记录。
  2. Multiple writers? Yes,十二所大学各自写入。
  3. Always-online TTP? No,没有各方都接受的第三方。
  4. All writers known? Yes,只有持证书的认证大学能写。
  5. All writers trusted? No,写入方可能倒签或篡改。
  6. Public verifiability? Yes,任何国家的雇主都要能验证。

结论:Public Permissioned Blockchain。

第 2 问:正分题合计 5 + 3 + 4 + 4 + 3 + 2 + 1 + 4 + 3 + 2 = 31,负分题合计 -2 - 3 + 0 = -5,总分 26,落在 21–40。按课件 p.18:可以用区块链支持,但不是压倒性的天然候选,需要更彻底的分析。

第 3 问:Q4 从 -2 变 -5、Q12 从 -3 变 -5,总分 26 - 3 - 2 = 21,仍在 21–40 的下沿。六问流程在第三问就变成 yes,结论改为 Don’t use。两种工具给出的结论可以不同:六问遇到可信第三方直接走到 Don’t use,打分只是把这一条算作几个负分项。课件没有规定两者冲突时听谁的,作答时把两个结论分别写出并各给理由。课件 vs 官方:课件只给 21–40 一个区间,20 分以下与 41 分以上的解读来自 ACT-IAC playbook 补充。

English: Yes, yes, no, yes, no, yes leads to a public permissioned blockchain. An ACT-IAC score between 21 and 40 means blockchain can support the case but is not an overwhelmingly natural fit, so more thorough analysis is needed. An acceptable always-online trusted third party ends the six-question flow with “don’t use a blockchain”.

自查分值:第 1 问每问 1 分;第 2 问总分 2 分、解读 2 分;第 3 问 2 分。回查:L08 p.4–5、p.8–18。

作答后怎么复盘

先对结论,再对推导:代码题逐空核对语法与回滚后果;读写集题重画版本表;选型题把六问和打分各走一遍,说明两者结论为何可能不同。

这组材料已读完,可以回课程页选择配套资料或练习:COMP5565 课程页。

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