Skip to content

Commit

Permalink
Merge pull request #2178 from CortexFoundation/dev
Browse files Browse the repository at this point in the history
side chain event header
  • Loading branch information
ucwong authored Oct 23, 2024
2 parents bcd7f5e + a0a027c commit cf17465
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 32 deletions.
12 changes: 6 additions & 6 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,16 @@ type BlockChain struct {
processor Processor // Block transaction processor interface
vmConfig vm.Config

shouldPreserve func(*types.Block) bool // Function used to determine whether should preserve the given block.
writeLegacyJournal bool // Testing flag used to flush the snapshot journal in legacy format.
shouldPreserve func(*types.Header) bool // Function used to determine whether should preserve the given block.
writeLegacyJournal bool // Testing flag used to flush the snapshot journal in legacy format.
utcNow int64
Viper bool
}

// NewBlockChain returns a fully initialised block chain using information
// available in the database. It initialises the default Cortex Validator and
// Processor.
func NewBlockChain(db ctxcdb.Database, cacheConfig *CacheConfig, chainConfig *params.ChainConfig, engine consensus.Engine, vmConfig vm.Config, shouldPreserve func(block *types.Block) bool, txLookupLimit *uint64) (*BlockChain, error) {
func NewBlockChain(db ctxcdb.Database, cacheConfig *CacheConfig, chainConfig *params.ChainConfig, engine consensus.Engine, vmConfig vm.Config, shouldPreserve func(block *types.Header) bool, txLookupLimit *uint64) (*BlockChain, error) {
if cacheConfig == nil {
cacheConfig = defaultCacheConfig
}
Expand Down Expand Up @@ -1431,7 +1431,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
} else if block.NumberU64() == currentBlock.NumberU64() {
var currentPreserve, blockPreserve bool
if bc.shouldPreserve != nil {
currentPreserve, blockPreserve = bc.shouldPreserve(currentBlock), bc.shouldPreserve(block)
currentPreserve, blockPreserve = bc.shouldPreserve(currentBlock.Header()), bc.shouldPreserve(block.Header())
}
reorg = !currentPreserve && (blockPreserve || mrand.Float64() < 0.5)
}
Expand Down Expand Up @@ -1468,7 +1468,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
bc.chainHeadFeed.Send(ChainHeadEvent{Block: block})
}
} else {
bc.chainSideFeed.Send(ChainSideEvent{Block: block})
bc.chainSideFeed.Send(ChainSideEvent{Header: block.Header()})
}
return status, nil
}
Expand Down Expand Up @@ -2123,7 +2123,7 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Header) error {
return errInvalidOldChain // Corrupt database, mostly here to avoid weird panics
}
// Also send event for blocks removed from the canon chain.
bc.chainSideFeed.Send(ChainSideEvent{Block: block})
bc.chainSideFeed.Send(ChainSideEvent{Header: block.Header()})

// Collect deleted logs for notification
if logs := bc.collectLogs(block, true); len(logs) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion core/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type ChainEvent struct {
}

type ChainSideEvent struct {
Block *types.Block
Header *types.Header
}

type ChainHeadEvent struct{ Block *types.Block }
8 changes: 4 additions & 4 deletions ctxc/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,10 @@ func (s *Cortex) Coinbase() (eb common.Address, err error) {
return common.Address{}, fmt.Errorf("coinbase must be explicitly specified")
}

func (s *Cortex) isLocalBlock(block *types.Block) bool {
author, err := s.engine.Author(block.Header())
func (s *Cortex) isLocalBlock(block *types.Header) bool {
author, err := s.engine.Author(block)
if err != nil {
log.Warn("Failed to retrieve block author", "number", block.NumberU64(), "hash", block.Hash(), "err", err)
log.Warn("Failed to retrieve block author", "number", block.Number.Uint64(), "hash", block.Hash(), "err", err)
return false
}
// Check whether the given address is etherbase.
Expand All @@ -453,7 +453,7 @@ func (s *Cortex) isLocalBlock(block *types.Block) bool {
// shouldPreserve checks whether we should preserve the given block
// during the chain reorg depending on whether the author of block
// is a local account.
func (s *Cortex) shouldPreserve(block *types.Block) bool {
func (s *Cortex) shouldPreserve(block *types.Header) bool {
// The reason we need to disable the self-reorg preserving for clique
// is it can be probable to introduce a deadlock.
//
Expand Down
2 changes: 1 addition & 1 deletion miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ type Miner struct {
}

// New create an instance of Miner
func New(ctxc Backend, config *Config, chainConfig *params.ChainConfig, mux *event.TypeMux, engine consensus.Engine, isLocalBlock func(block *types.Block) bool) *Miner {
func New(ctxc Backend, config *Config, chainConfig *params.ChainConfig, mux *event.TypeMux, engine consensus.Engine, isLocalBlock func(block *types.Header) bool) *Miner {
miner := &Miner{
ctxc: ctxc,
mux: mux,
Expand Down
40 changes: 20 additions & 20 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,10 @@ type worker struct {

wg sync.WaitGroup

current *environment // An environment for current running cycle.
localUncles map[common.Hash]*types.Block // A set of side blocks generated locally as the possible uncle blocks.
remoteUncles map[common.Hash]*types.Block // A set of side blocks as the possible uncle blocks.
unconfirmed *unconfirmedBlocks // A set of locally mined blocks pending canonicalness confirmations.
current *environment // An environment for current running cycle.
localUncles map[common.Hash]*types.Header // A set of side blocks generated locally as the possible uncle blocks.
remoteUncles map[common.Hash]*types.Header // A set of side blocks as the possible uncle blocks.
unconfirmed *unconfirmedBlocks // A set of locally mined blocks pending canonicalness confirmations.

mu sync.RWMutex // The lock used to protect the coinbase and extra fields
coinbase common.Address
Expand All @@ -195,7 +195,7 @@ type worker struct {
noempty atomic.Bool

// External functions
isLocalBlock func(block *types.Block) bool // Function used to determine whether the specified block is mined by local miner.
isLocalBlock func(block *types.Header) bool // Function used to determine whether the specified block is mined by local miner.

// Test hooks
newTaskHook func(*task) // Method to call upon receiving a new sealing task.
Expand All @@ -205,7 +205,7 @@ type worker struct {
checkpoint uint64
}

func newWorker(config *Config, chainConfig *params.ChainConfig, engine consensus.Engine, ctxc Backend, mux *event.TypeMux, isLocalBlock func(*types.Block) bool, init bool) *worker {
func newWorker(config *Config, chainConfig *params.ChainConfig, engine consensus.Engine, ctxc Backend, mux *event.TypeMux, isLocalBlock func(*types.Header) bool, init bool) *worker {
worker := &worker{
config: config,
chainConfig: chainConfig,
Expand All @@ -214,8 +214,8 @@ func newWorker(config *Config, chainConfig *params.ChainConfig, engine consensus
mux: mux,
chain: ctxc.BlockChain(),
isLocalBlock: isLocalBlock,
localUncles: make(map[common.Hash]*types.Block),
remoteUncles: make(map[common.Hash]*types.Block),
localUncles: make(map[common.Hash]*types.Header),
remoteUncles: make(map[common.Hash]*types.Header),
unconfirmed: newUnconfirmedBlocks(ctxc.BlockChain(), miningLogAtDepth),
pendingTasks: make(map[common.Hash]*task),
txsCh: make(chan core.NewTxsEvent, txChanSize),
Expand Down Expand Up @@ -484,23 +484,23 @@ func (w *worker) mainLoop() {

case ev := <-w.chainSideCh:
// Short circuit for duplicate side blocks
if _, exist := w.localUncles[ev.Block.Hash()]; exist {
if _, exist := w.localUncles[ev.Header.Hash()]; exist {
continue
}
if _, exist := w.remoteUncles[ev.Block.Hash()]; exist {
if _, exist := w.remoteUncles[ev.Header.Hash()]; exist {
continue
}
// Add side block to possible uncle block set depending on the author.
if w.isLocalBlock != nil && w.isLocalBlock(ev.Block) {
w.localUncles[ev.Block.Hash()] = ev.Block
if w.isLocalBlock != nil && w.isLocalBlock(ev.Header) {
w.localUncles[ev.Header.Hash()] = ev.Header
} else {
w.remoteUncles[ev.Block.Hash()] = ev.Block
w.remoteUncles[ev.Header.Hash()] = ev.Header
}
// If our mining block contains less than 2 uncle blocks,
// add the new uncle block if valid and regenerate a mining block.
if w.isRunning() && w.current != nil && w.current.uncles.Cardinality() < 2 {
start := time.Now()
if err := w.commitUncle(w.current, ev.Block.Header()); err == nil {
if err := w.commitUncle(w.current, ev.Header); err == nil {
var uncles []*types.Header
w.current.uncles.Each(func(hash common.Hash) bool {
uncle, exist := w.localUncles[hash]
Expand All @@ -510,7 +510,7 @@ func (w *worker) mainLoop() {
if !exist {
return false
}
uncles = append(uncles, uncle.Header())
uncles = append(uncles, uncle)
return false
})
w.commit(uncles, nil, true, start)
Expand Down Expand Up @@ -764,7 +764,7 @@ func (w *worker) updateSnapshot() {
if !exist {
return false
}
uncles = append(uncles, uncle.Header())
uncles = append(uncles, uncle)
return false
})

Expand Down Expand Up @@ -954,22 +954,22 @@ func (w *worker) commitNewWork(interrupt *atomic.Int32, noempty bool, timestamp
env := w.current
// Accumulate the uncles for the current block
uncles := make([]*types.Header, 0, 2)
commitUncles := func(blocks map[common.Hash]*types.Block) {
commitUncles := func(blocks map[common.Hash]*types.Header) {
// Clean up stale uncle blocks first
for hash, uncle := range blocks {
if uncle.NumberU64()+staleThreshold <= header.Number.Uint64() {
if uncle.Number.Uint64()+staleThreshold <= header.Number.Uint64() {
delete(blocks, hash)
}
}
for hash, uncle := range blocks {
if len(uncles) == 2 {
break
}
if err := w.commitUncle(env, uncle.Header()); err != nil {
if err := w.commitUncle(env, uncle); err != nil {
log.Trace("Possible uncle rejected", "hash", hash, "reason", err)
} else {
log.Debug("Committing new uncle to block", "hash", hash)
uncles = append(uncles, uncle.Header())
uncles = append(uncles, uncle)
}
}
}
Expand Down

0 comments on commit cf17465

Please sign in to comment.