Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Include fractions in epoch number representations #1585

Merged
merged 6 commits into from
Sep 19, 2019
Merged

feat: Include fractions in epoch number representations #1585

merged 6 commits into from
Sep 19, 2019

Conversation

xxuejie
Copy link
Collaborator

@xxuejie xxuejie commented Sep 12, 2019

This change introduce fractions in 2 places where epoch numbers might
be used:

  • The epoch field in the header
  • Cell input's since part when using epoch values

Here we use a rational number format to represent epoch number.
The lower 54 bits of the epoch number field are split into 3
parts(listed in the order from higher bits to lower bits):

  • The highest 16 bits represent the epoch length
  • The next 16 bits represent the current block index in the epoch
  • The lowest 24 bits represent the current epoch number

Different from previous solution, with a rational number format the
precision can be guaranteed.

Assuming we are at block number 11555, epoch 50, and epoch 50 starts
from block 11000, has a length of 1000. The epoch number for this
particular block will then be 9326559282, which is calculated
in the following way:

50 | ((11555 - 11000) << 24) | (1000 << 16)

This way, we can alleviate the trouble that CKB uses variable epoch
length. The actual contract or dapps can just read this epoch number
with fractions and perform numerical operations as normal.

@xxuejie xxuejie requested review from a team September 12, 2019 02:15
util/types/src/core/extras.rs Outdated Show resolved Hide resolved
util/types/src/core/extras.rs Outdated Show resolved Hide resolved
@doitian doitian added this to 👀 Awaiting review in CKB - Pull Requests Sep 16, 2019
util/types/src/core/extras.rs Outdated Show resolved Hide resolved
);

let median_time_context = MockMedianTime::new(vec![0; 11]);
assert!(verify_since(&rtx, &median_time_context, 5, 1).is_ok(),);
Copy link
Contributor

@u2 u2 Sep 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it a valid or invalid since? For the test name test_invalid_zero_length_since, it's an invalid since, but verify_since is is_ok().

@u2
Copy link
Contributor

u2 commented Sep 18, 2019

The lower 54 bits of the epoch number field are split into 3

56 bits?

50 | ((11555 - 11000) << 24) | (1000 << 16)

50 | ((11555 - 11000) << 24) | (1000 << 40)?

@u2 u2 added the s:waiting-on-reviewers Status: Waiting for Review label Sep 18, 2019
CKB - Pull Requests automation moved this from 👀 Awaiting review to ✅ Reviewer approved Sep 19, 2019
This change introduce fractions in 2 places where epoch numbers might
be used:

* The epoch field in the header
* Cell input's since part when using epoch values

Specifically, the new epoch number format works as follows:

* Only the lower 56 bits of the full 64-bit value are used
* The upper 40 bits are used to represent the current epoch number
* The lower 16 bits represent the already elapsed fractions of current
  epoch(not including current block)

Assuming we are at block number 11555, epoch 50, and epoch 50 starts
from block 11000, has a length of 1000. The epoch number for this
particular block will then be 3313172, which is calculated in the
following way:

```
(50 << 16) | (((2 ** 16) * (11555 - 11000) / 1000) & ((2 ** 16) - 1))
```

Note the formula uses normal integer division which rounded to the
floor.

This way, we can alleviate the trouble that CKB uses variable epoch
length. The actual contract or dapps can just read this epoch number
with fractions and perform numerical operations as normal.
This commits changes the epoch number format in the following way:

* The lower 48 bits of the full 64-bit value represents the current
  epoch number with fractions
* The upper 32 bits contain the current epoch number
* The lower 16 bits represents the already elapsed fractions of
  current epoch(not including current block)

Depending on where we are using the epoch number, the unused higher 16
bits are put to different uses:

* In block header's epoch field, the higher 16 bits contains the
  current epoch length
* In cell input's since field, the highest 8 bits contains since flag,
  while the remaining 8 bits are unused.

Assuming we are at block number 11555, epoch 50, and epoch 50 starts
from block 11000, has a length of 1000. The epoch number for this
particular block will then be 281474976713969172, which is calculated
in the following way:

```
(1000 << 48) | (50 << 16) | ((2 ** 16) * (11555 - 11000) / 1000)
```
This commit changes the epoch number to a rational number based
format. The lower 54 bits of the epoch number field are split into 3
parts(listed in the order from higher bits to lower bits):

* The highest 16 bits represent the epoch length
* The next 16 bits represent the current block index in the epoch
* The lowest 24 bits represent the current epoch number

Different from previous solution, with a rational number format the
precision can be guaranteed.

Assuming we are at block number 11555, epoch 50, and epoch 50 starts
from block 11000, has a length of 1000. The epoch number for this
particular block will then be 9326559282, which is calculated
in the following way:

```
50 | ((11555 - 11000) << 24) | (1000 << 16)
```
@doitian doitian moved this from ✅ Reviewer approved to 🔴 High priority in CKB - Pull Requests Sep 19, 2019
@xxuejie
Copy link
Collaborator Author

xxuejie commented Sep 19, 2019

bors r+

@nervos-bot nervos-bot bot added the s:ready-to-merge Status: Waiting to be merged. label Sep 19, 2019
bors bot added a commit that referenced this pull request Sep 19, 2019
1585: feat: Include fractions in epoch number representations r=xxuejie a=xxuejie

This change introduce fractions in 2 places where epoch numbers might
be used:

* The epoch field in the header
* Cell input's since part when using epoch values

Here we use a rational number format to represent epoch number.
The lower 54 bits of the epoch number field are split into 3
parts(listed in the order from higher bits to lower bits):

* The highest 16 bits represent the epoch length
* The next 16 bits represent the current block index in the epoch
* The lowest 24 bits represent the current epoch number

Different from previous solution, with a rational number format the
precision can be guaranteed.

Assuming we are at block number 11555, epoch 50, and epoch 50 starts
from block 11000, has a length of 1000. The epoch number for this
particular block will then be 9326559282, which is calculated
in the following way:

```
50 | ((11555 - 11000) << 24) | (1000 << 16)
```

This way, we can alleviate the trouble that CKB uses variable epoch
length. The actual contract or dapps can just read this epoch number
with fractions and perform numerical operations as normal.

Co-authored-by: Xuejie Xiao <xxuejie@gmail.com>
@bors
Copy link
Contributor

bors bot commented Sep 19, 2019

Build succeeded

  • continuous-integration/travis-ci/push

@bors bors bot merged commit e1634a0 into nervosnetwork:develop Sep 19, 2019
CKB - Pull Requests automation moved this from 🔴 High priority to Done Sep 19, 2019
@xxuejie xxuejie deleted the include-fractions-in-epoch-numbers branch July 6, 2020 04:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
s:ready-to-merge Status: Waiting to be merged. s:waiting-on-reviewers Status: Waiting for Review
Projects
Development

Successfully merging this pull request may close these issues.

None yet

5 participants