Skip to content
This repository has been archived by the owner on Nov 9, 2022. It is now read-only.

feat: optimize Godwoken finality mechanism #132

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

keroro520
Copy link
Contributor

@keroro520 keroro520 commented Sep 29, 2022

Motivation & Overview

https://talk.nervos.org/t/optimize-godwoken-finality-and-on-chain-cost/6739

Implementation Note

Summary

The finality mechanism determines whether an entity has been finalized or not.

Entities that require finality checking must contain an attribution indicating their occuration time, which can be found in godwoken.mol:

Entity Occuration Attribution
custodian_state_cell deposit_block_number
withdrawal_state_cell withdrawal_block_number
stake_state_cell stake_block_number

Convention

In order to denote entities' occurrences, we define a compatible type, Timepoint:

// Layout of Timepoint:
//   - the highest 1 bit represent whether the time point is block-number-based
//     or timestamp-based
//   - the rest 63 bits represent the value of time point
pub enum Timepoint {
	BlockNumber(/* block_number: */ u64),
	Timestamp(/* timestamp: */ u64),
}

impl Timepoint {
    const MASK: u64 = 1 << 63;

    pub fn from_full_value(full_value: u64) -> Self {
        let is_block_number = (Self::MASK & full_value) == 0;
        if is_block_number {
            Timepoint::BlockNumber(full_value)
        } else {
            Timepoint::Timestamp(Self::MASK ^ full_value)
        }
    }

    pub fn full_value(&self) -> u64 {
        match self {
            Timepoint::BlockNumber(block_number) => *block_number,
            Timepoint::Timestamp(timestamp) => Self::MASK | *timestamp,
        }
    }
}

On-Chain Scripts

  • Rollup_state_cell.global_state.tip_block_timestamp equals to l2block.timestamp, same as before.
    • Assert rawl2block.withdrawal.withdrawal_block_number == l2block's timepoint
    • Assert custodian_state_cell.deposit_block_number == l2block's timepoint
    • Assert stake_block_number == l2block's timepoint
  • Rollup_state_cell.global_state.last_finalized_block_number
    • is block-number-based, equals to l2block.number - CONSTANT when global_state.version < 2;
    • is timestamp-based, equals to the max timestamp of header_deps, when global_state.version >= 2.
  • Determine finality of these entities by comparing their occuration attributions to global_state's last_finalized_block_number if the entity is timestamp-based, otherwise by comparing to global_state's block.count.

@keroro520 keroro520 force-pushed the new-finality-rule-based-on-timestamp branch 2 times, most recently from e61136e to 1fdb028 Compare September 30, 2022 04:36
@keroro520 keroro520 marked this pull request as ready for review September 30, 2022 04:40
@Flouse Flouse changed the title feat: impl new finality rule feat: optimize Godwoken finality mechanism Oct 7, 2022
@keroro520 keroro520 self-assigned this Oct 8, 2022
@keroro520 keroro520 changed the base branch from master to breaking-change-next-upgrade October 13, 2022 10:08
@keroro520 keroro520 force-pushed the new-finality-rule-based-on-timestamp branch from 1fdb028 to 0b45e20 Compare October 13, 2022 10:10
@keroro520 keroro520 marked this pull request as draft October 13, 2022 10:12
@keroro520 keroro520 force-pushed the new-finality-rule-based-on-timestamp branch from 0b45e20 to 2836684 Compare October 25, 2022 12:46
@keroro520 keroro520 marked this pull request as ready for review October 25, 2022 13:08
@keroro520
Copy link
Contributor Author

Ready for review @zeroqn @jjyr

@Flouse Flouse requested review from blckngm and magicalne October 27, 2022 06:45
@keroro520 keroro520 force-pushed the new-finality-rule-based-on-timestamp branch from 74ebd43 to 47e3bda Compare November 2, 2022 11:06
@keroro520 keroro520 changed the base branch from breaking-change-next-upgrade to master November 3, 2022 07:33
c-uint256-tests/build.rs Show resolved Hide resolved
contracts/gw-utils/src/finality.rs Outdated Show resolved Hide resolved
contracts/gw-utils/src/finality.rs Outdated Show resolved Hide resolved
contracts/gw-utils/src/finality.rs Outdated Show resolved Hide resolved
contracts/gw-utils/src/finality.rs Show resolved Hide resolved
contracts/gw-utils/src/finality.rs Show resolved Hide resolved
contracts/gw-utils/src/finality.rs Show resolved Hide resolved
@keroro520 keroro520 force-pushed the new-finality-rule-based-on-timestamp branch from 0c759e8 to 5cc20fe Compare November 7, 2022 13:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants