Skip to content

Forked ChainManager #4593

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

Merged
merged 11 commits into from
Apr 15, 2025
Merged

Forked ChainManager #4593

merged 11 commits into from
Apr 15, 2025

Conversation

envestcc
Copy link
Member

@envestcc envestcc commented Mar 26, 2025

Description

Changes for ChainManager:

  • implemented its own MintNewBlock method and caches the minted blocks.
  • can fork a chain at specifying the block hash.

base #4590 and #4581

Type of change

Please delete options that are not relevant.

  • [] Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • [] Code refactor or improvement
  • [] Breaking change (fix or feature that would cause a new or changed behavior of existing functionality)
  • [] This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • [] make test
  • [] fullsync
  • [] Other test (please specify)

Test Configuration:

  • Firmware version:
  • Hardware:
  • Toolchain:
  • SDK:

Checklist:

  • [] My code follows the style guidelines of this project
  • [] I have performed a self-review of my code
  • [] I have commented my code, particularly in hard-to-understand areas
  • [] I have made corresponding changes to the documentation
  • [] My changes generate no new warnings
  • [] I have added tests that prove my fix is effective or that my feature works
  • [] New and existing unit tests pass locally with my changes
  • [] Any dependent changes have been merged and published in downstream modules

@envestcc envestcc requested review from CoderZhi, dustinxie, Liuhaai and a team as code owners March 26, 2025 03:35
@envestcc envestcc mentioned this pull request Mar 26, 2025
1 task
@envestcc envestcc marked this pull request as draft March 26, 2025 03:51
@envestcc envestcc mentioned this pull request Mar 26, 2025
@envestcc envestcc mentioned this pull request Mar 28, 2025
@envestcc envestcc marked this pull request as ready for review March 31, 2025 09:53
d.mu.Lock()
defer d.mu.Unlock()
for _, b := range d.nodes {
if b.PrevHash() == prevHash && b.Timestamp().Equal(timestamp) {
Copy link
Member

@dustinxie dustinxie Apr 9, 2025

Choose a reason for hiding this comment

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

from the logic here, it's possible to have 2 blocks with same prevHash and diff time, is that right?

b1.PrevHash() = 0x1234, b1.Time() = 17:56:32
b2.PrevHash() = 0x1234, b2.Time() = 17.56.35 (3 seconds later)

see comments at L55

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, but in practice it should be relatively rare, which requires all 24 delegates to fail.

// it must be a new tip of any fork, or make a new fork
prevHash := blk.PrevHash()
if _, ok := d.leaves[prevHash]; ok {
delete(d.leaves, prevHash)
Copy link
Member

Choose a reason for hiding this comment

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

if trying to add 2 blocks with same prevHash, then only the first will be added successfully, say

b1.PrevHash() = 0x1234, b1.Time() = 17:56:32
b2.PrevHash() = 0x1234, b2.Time() = 17.56.35 (3 seconds later)

only b1 will be added (which deletes d.leaves[0x1234], b2 won't be added into the pool
then situation at L126 won't happend? see comments at L126

Copy link
Member Author

Choose a reason for hiding this comment

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

No, b1 and b2 are both included as two forks in your case. It won't go L55, b/c the tip is b1 instead of b1.prev.

log.L().Debug("remove block from draft pool", log.Hex("hash", ha[:]), zap.Uint64("height", b.Height()), zap.Time("timestamp", b.Timestamp()))
delete(d.nodes, b.HashBlock())
}
}
Copy link
Member

Choose a reason for hiding this comment

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

let's sync offline for this part of logic

start = blk
}
for b := start; b != nil; b = d.nodes[b.PrevHash()] {
ha := b.HashBlock()
log.L().Debug("remove block from draft pool", log.Hex("hash", ha[:]), zap.Uint64("height", b.Height()), zap.Time("timestamp", b.Timestamp()))
delete(d.nodes, b.HashBlock())
}
if !has || blk.HashBlock() == leaf {
leavesToDelete = append(leavesToDelete, leaf)
Copy link
Member

Choose a reason for hiding this comment

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

so just to confirm, for the case we discussed, this will delete A, B, 3, 4, and 1, 2 remain as 2 leaves?

Copy link

@CoderZhi CoderZhi merged commit 16601d6 into iotexproject:master Apr 15, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants