Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov committed Oct 18, 2024
2 parents f468916 + ea2ca63 commit f4be99d
Show file tree
Hide file tree
Showing 14 changed files with 748 additions and 122 deletions.
25 changes: 0 additions & 25 deletions .github/workflows/qa-rpc-integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -191,31 +191,8 @@ jobs:
trace_replayTransaction/test_23.tar,\
trace_replayTransaction/test_24.json,\
trace_replayTransaction/test_29.tar,\
ots_searchTransactionsAfter/test_01.json,\
ots_searchTransactionsAfter/test_03.json,\
ots_searchTransactionsAfter/test_04.json,\
ots_searchTransactionsAfter/test_06.json,\
ots_searchTransactionsAfter/test_08.json,\
ots_searchTransactionsAfter/test_09.json,\
ots_searchTransactionsAfter/test_10.json,\
ots_searchTransactionsAfter/test_11.json,\
ots_searchTransactionsAfter/test_12.json,\
ots_searchTransactionsAfter/test_13.json,\
ots_searchTransactionsBefore/test_01.json,\
ots_searchTransactionsBefore/test_03.json,\
ots_searchTransactionsBefore/test_04.json,\
ots_searchTransactionsBefore/test_06.json,\
ots_searchTransactionsBefore/test_09.json,\
ots_searchTransactionsBefore/test_10.json,\
ots_searchTransactionsBefore/test_11.json,\
ots_searchTransactionsBefore/test_12.json,\
admin_nodeInfo/test_01.json,\
admin_peers/test_01.json,\
erigon_getBlockByTimestamp/test_03.json,\
erigon_getBlockByTimestamp/test_04.json,\
erigon_getBlockByTimestamp/test_05.json,\
erigon_getBlockByTimestamp/test_06.json,\
erigon_getBlockByTimestamp/test_07.json,\
erigon_nodeInfo/test_1.json,\
eth_coinbase/test_01.json,\
eth_feeHistory/test_01.json,\
Expand Down Expand Up @@ -265,9 +242,7 @@ jobs:
ots_getBlockTransactions/test_03.json,\
ots_getBlockTransactions/test_04.json,\
ots_getBlockTransactions/test_05.json,\
ots_searchTransactionsAfter/test_14.json,\
ots_searchTransactionsBefore/test_13.json,\
ots_searchTransactionsBefore/test_14.json,\
trace_call/test_05.json,\
trace_call/test_07.json,\
trace_call/test_12.json,\
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/qa-snap-download.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: QA - Snapshot Download

on:
schedule:
- cron: '0 0 * * 1-6' # Run every night at 00:00 AM UTC except Sunday
- cron: '0 22 * * 1-6' # Run every night at 22:00 (10:00 PM) UTC except Sunday
workflow_dispatch: # Run manually

jobs:
Expand Down
3 changes: 1 addition & 2 deletions cmd/devnet/services/polygon/proofgenerator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ func (rg *requestGenerator) GetTransactionReceipt(ctx context.Context, hash libc
}
defer tx.Rollback()

_, _, _, ibs, _, err := transactions.ComputeTxEnv(ctx, engine, block, chainConfig, reader, rawdbv3.TxNums, tx, 0)

ibs, _, _, _, _, err := transactions.ComputeBlockContext(ctx, engine, block.HeaderNoCopy(), chainConfig, reader, rawdbv3.TxNums, tx, 0)
if err != nil {
return nil, err
}
Expand Down
6 changes: 5 additions & 1 deletion eth/stagedsync/stage_polygon_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -1514,7 +1514,11 @@ func (e *polygonSyncStageExecutionEngine) UpdateForkChoice(ctx context.Context,
case <-ctx.Done():
return common.Hash{}, ctx.Err()
case result := <-resultCh:
return result.latestValidHash, result.validationErr
err := result.validationErr
if err != nil {
err = fmt.Errorf("%w: %w", polygonsync.ErrForkChoiceUpdateBadBlock, err)
}
return result.latestValidHash, err
}
}

Expand Down
110 changes: 107 additions & 3 deletions polygon/sync/canonical_chain_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ type CanonicalChainBuilder interface {
Tip() *types.Header
Root() *types.Header
HeadersInRange(start uint64, count uint64) []*types.Header
Prune(newRootNum uint64) error
PruneRoot(newRootNum uint64) error
PruneNode(hash libcommon.Hash) error
Connect(ctx context.Context, headers []*types.Header) (newConnectedHeaders []*types.Header, err error)
LowestCommonAncestor(a, b libcommon.Hash) (*types.Header, bool)
}

type producerSlotIndex uint64
Expand Down Expand Up @@ -156,9 +158,9 @@ func (ccb *canonicalChainBuilder) HeadersInRange(start uint64, count uint64) []*
return headers[offset : offset+count]
}

func (ccb *canonicalChainBuilder) Prune(newRootNum uint64) error {
func (ccb *canonicalChainBuilder) PruneRoot(newRootNum uint64) error {
if (newRootNum < ccb.root.header.Number.Uint64()) || (newRootNum > ccb.Tip().Number.Uint64()) {
return errors.New("canonicalChainBuilder.Prune: newRootNum outside of the canonical chain")
return errors.New("canonicalChainBuilder.PruneRoot: newRootNum outside of the canonical chain")
}

newRoot := ccb.tip
Expand All @@ -170,6 +172,35 @@ func (ccb *canonicalChainBuilder) Prune(newRootNum uint64) error {
return nil
}

func (ccb *canonicalChainBuilder) PruneNode(hash libcommon.Hash) error {
if ccb.root.headerHash == hash {
return errors.New("canonicalChainBuilder.PruneNode: can't prune root node")
}

var exists bool
ccb.enumerate(func(node *forkTreeNode) bool {
if node.headerHash != hash {
return true
}

for idx, parentChild := range node.parent.children {
if parentChild.headerHash == hash {
exists = true
delete(node.parent.children, idx)
break
}
}

return false
})
if !exists {
return errors.New("canonicalChainBuilder.PruneNode: could not find node to prune")
}

ccb.tip = ccb.recalcTip() // tip may have changed after prunning, re-calc
return nil
}

// compareForkTreeNodes compares 2 fork tree nodes.
// It returns a positive number if the chain ending at node1 is "better" than the chain ending at node2.
// The better node belongs to the canonical chain, and it has:
Expand All @@ -195,6 +226,23 @@ func (ccb *canonicalChainBuilder) updateTipIfNeeded(tipCandidate *forkTreeNode)
}
}

func (ccb *canonicalChainBuilder) recalcTip() *forkTreeNode {
var tip *forkTreeNode
ccb.enumerate(func(node *forkTreeNode) bool {
if tip == nil {
tip = node
return true
}

if compareForkTreeNodes(tip, node) < 0 {
tip = node
}

return true
})
return tip
}

// Connect connects a list of headers to the canonical chain builder tree.
// Returns the list of newly connected headers (filtering out headers that already exist in the tree)
// or an error in case the header is invalid or the header chain cannot reach any of the nodes in the tree.
Expand Down Expand Up @@ -291,3 +339,59 @@ func (ccb *canonicalChainBuilder) Connect(ctx context.Context, headers []*types.

return headers, nil
}

func (ccb *canonicalChainBuilder) LowestCommonAncestor(a, b libcommon.Hash) (*types.Header, bool) {
pathA := ccb.pathToRoot(a)
if len(pathA) == 0 {
// 'a' doesn't exist in the tree
return nil, false
}

pathB := ccb.pathToRoot(b)
if len(pathB) == 0 {
// 'b' doesn't exist in the tree
return nil, false
}

heightA := pathA[0].header.Number.Uint64()
heightB := pathB[0].header.Number.Uint64()
for heightA != heightB {
if heightA < heightB {
pathB = pathB[1:]
heightB = pathB[0].header.Number.Uint64()
} else if heightA > heightB {
pathA = pathA[1:]
heightA = pathA[0].header.Number.Uint64()
}
}

for i := 0; i < len(pathA); i++ {
if pathA[i].headerHash == pathB[i].headerHash {
return pathA[i].header, true
}
}

return nil, false
}

func (ccb *canonicalChainBuilder) pathToRoot(from libcommon.Hash) []*forkTreeNode {
path := make([]*forkTreeNode, 0, ccb.Tip().Number.Uint64()-ccb.Root().Number.Uint64())
pathToRootRec(ccb.root, from, &path)
return path
}

func pathToRootRec(node *forkTreeNode, from libcommon.Hash, path *[]*forkTreeNode) bool {
if node.headerHash == from {
*path = append(*path, node)
return true
}

for _, child := range node.children {
if pathToRootRec(child, from, path) {
*path = append(*path, node)
return true
}
}

return false
}
101 changes: 89 additions & 12 deletions polygon/sync/canonical_chain_builder_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f4be99d

Please sign in to comment.