Skip to content

fix: use flag-anchored positioning for MULTIPLE commission decode#26

Open
alphafitz11 wants to merge 1 commit intomainfrom
fix/multiple-commission-decode-with-extra-data
Open

fix: use flag-anchored positioning for MULTIPLE commission decode#26
alphafitz11 wants to merge 1 commit intomainfrom
fix/multiple-commission-decode-with-extra-data

Conversation

@alphafitz11
Copy link
Collaborator

Summary

  • MULTIPLE commission(3-8 推荐人)解析从"基于 calldata 末尾偏移定位 middle block"改为"基于 flag 锚点位置向后推算定位 middle block",适配合约侧 ERC-8021 兼容性改造后 calldata 末尾可能拼接任意长度额外数据的场景

Background

合约侧正在推进 ERC-8021 兼容性方案(docs/20260304-erc8021-compatibility/),核心变更是将 _getCommissionAndTrimInfo() 中的解析锚点从 calldatasize() 改为通过后向 flag 扫描定位的 effectiveEnd,以容忍 calldata 末尾拼接的任意长度后缀数据。

Decode 脚本需要同步适配:当 calldata 末尾存在额外数据时,解析不应依赖 calldata 总长度。

Problem

MULTIPLE commission 解析中,middle block 的定位使用了 calldataHex.length:

// 旧代码
const middleStart = calldataHex.length - (BYTE_SIZE.BLOCK * 2);

这假定 middle block 始终位于 calldata 倒数第二个 bytes32 位置。当 calldata 末尾拼接额外数据后,该偏移量指向错误位置,导致解析失败。

SINGLE/DUAL commission 和 SINGLE/DUAL trim 的解析已经是基于 flag 位置锚定的,不受影响。

Fix

改为基于 flag 位置向后推算 middle block:

  1. 通过 indexOf 找到 MULTIPLE flag(commissionN 的起始位置)
  2. 遍历候选 referrerNum(3~8),计算每种情况下 middle block 的预期位置:flagIndex + (tryNum - 1) * BYTE_SIZE.BLOCK
  3. 读取该位置的 middle block,校验其中编码的 referrerNum 是否与候选值一致
  4. 匹配成功后按原有逻辑提取所有 block

Layout: [commissionN][commission_{N-1}]...[commission_2][middle][commission_1]
^flagIndex +64 ... +(N-2)*64 +(N-1)64 +N64

Test Plan

  • 验证 MULTIPLE commission(3~8 推荐人)在无额外数据时解析结果与修改前一致
  • 验证 MULTIPLE commission 在 calldata 末尾拼接任意长度额外数据时仍能正确解析
  • 验证 SINGLE/DUAL commission 和 SINGLE/DUAL trim 解析不受影响
  • npm run build 构建通过

The MULTIPLE commission parser previously used calldataHex.length to
locate the middle block, which breaks when arbitrary data is appended
after the commission data. Now derives middle block position from the
flag index and validates referrerNum by trying each candidate (3-8).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant