Skip to content

Commit

Permalink
make injected function private, removed xplugeth impororts
Browse files Browse the repository at this point in the history
  • Loading branch information
Akohjesse committed Aug 26, 2024
1 parent 304b90f commit 5e4ae14
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
12 changes: 6 additions & 6 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1643,7 +1643,7 @@ func (bc *BlockChain) writeKnownBlock(block *types.Block) error {
func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.Receipt, logs []*types.Log, state *state.StateDB) ([]*types.Log, error) {
//begin PluGeth injection
var interval time.Duration
_ = PluginSetTrieFlushIntervalClone(interval) // this is being called here to engage a testing scenario
_ = pluginSetTrieFlushIntervalClone(interval) // this is being called here to engage a testing scenario
//end PluGeth injection

// Calculate the total difficulty of the block
Expand Down Expand Up @@ -1741,7 +1741,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
// If we exceeded time allowance, flush an entire trie to disk

// begin PluGeth code injection
flushInterval = PluginSetTrieFlushIntervalClone(flushInterval)
flushInterval = pluginSetTrieFlushIntervalClone(flushInterval)
// end PluGeth code injection

if bc.gcproc > flushInterval {
Expand Down Expand Up @@ -1830,7 +1830,7 @@ func (bc *BlockChain) writeBlockAndSetHead(ctx context.Context, block *types.Blo

if status == CanonStatTy {
// begin plugeth injection
PluginNewHead(block, block.Hash(), logs, externTd)
pluginNewHead(block, block.Hash(), logs, externTd)
// end plugeth injection
bc.chainFeed.Send(ChainEvent{Block: block, Hash: block.Hash(), Logs: logs})

Expand Down Expand Up @@ -1858,7 +1858,7 @@ func (bc *BlockChain) writeBlockAndSetHead(ctx context.Context, block *types.Blo
}
} else {
//begin PluGeth injection
PluginNewSideBlock(block, block.Hash(), logs)
pluginNewSideBlock(block, block.Hash(), logs)
//end PluGeth injection
bc.chainSideFeed.Send(ChainSideEvent{Block: block})

Expand Down Expand Up @@ -2693,7 +2693,7 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
logFn = log.Warn
}
//begin PluGeth code injection
PluginReorg(commonBlock, oldChain, newChain)
pluginReorg(commonBlock, oldChain, newChain)
//end PluGeth code injection
logFn(msg, "number", commonBlock.Number(), "hash", commonBlock.Hash(),
"drop", len(oldChain), "dropfrom", oldChain[0].Hash(), "add", len(newChain), "addfrom", newChain[0].Hash())
Expand Down Expand Up @@ -2929,7 +2929,7 @@ func (bc *BlockChain) SetCanonical(head *types.Block) (common.Hash, error) {
if ptd != nil {
externTd = new(big.Int).Add(head.Difficulty(), ptd)
}
PluginNewHead(head, head.Hash(), logs, externTd)
pluginNewHead(head, head.Hash(), logs, externTd)
// end PluGeth code injection
bc.chainHeadFeed.Send(ChainHeadEvent{Block: head})

Expand Down
32 changes: 20 additions & 12 deletions core/xplugeth_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ import (
)

type newHeadPlugin interface {
PluginNewHead(block *types.Block, hash common.Hash, logs []*types.Log, td *big.Int)
NewHead(block *types.Block, hash common.Hash, logs []*types.Log, td *big.Int)
}

type newSideBlockPlugin interface {
PluginNewSideBlock(block *types.Block, hash common.Hash, logs []*types.Log)
NewSideBlock(block *types.Block, hash common.Hash, logs []*types.Log)
}

type reorgPlugin interface {
PluginReorg(commonBlock *types.Block, oldChain, newChain types.Blocks)
Reorg(commonBlock common.Hash, oldChain, newChain []common.Hash)
}

type setTrieFlushIntervalClonePlugin interface {
PluginSetTrieFlushIntervalClone(flushInterval time.Duration) time.Duration
SetTrieFlushIntervalClone(flushInterval time.Duration) time.Duration
}

func init() {
Expand All @@ -34,32 +34,40 @@ func init() {
xplugeth.RegisterHook[setTrieFlushIntervalClonePlugin]()
}

func PluginNewHead(block *types.Block, hash common.Hash, logs []*types.Log, td *big.Int) {
func pluginNewHead(block *types.Block, hash common.Hash, logs []*types.Log, td *big.Int) {
for _, m := range xplugeth.GetModules[newHeadPlugin]() {
m.PluginNewHead(block, hash, logs, td)
m.NewHead(block, hash, logs, td)
}
}

func PluginNewSideBlock(block *types.Block, hash common.Hash, logs []*types.Log) {
func pluginNewSideBlock(block *types.Block, hash common.Hash, logs []*types.Log) {
for _, m := range xplugeth.GetModules[newSideBlockPlugin]() {
m.PluginNewSideBlock(block, hash, logs)
m.NewSideBlock(block, hash, logs)
}
}

func PluginReorg(commonBlock *types.Block, oldChain, newChain types.Blocks) {
func pluginReorg(commonBlock *types.Block, oldChain, newChain types.Blocks) {
oldChainHashes := make([]common.Hash, len(oldChain))
for i, block := range oldChain {
oldChainHashes[i] = block.Hash()
}
newChainHashes := make([]common.Hash, len(newChain))
for i, block := range newChain {
newChainHashes[i] = block.Hash()
}
for _, m := range xplugeth.GetModules[reorgPlugin]() {
m.PluginReorg(commonBlock, oldChain, newChain)
m.Reorg(commonBlock.Hash(), oldChainHashes, newChainHashes)
}
}

func PluginSetTrieFlushIntervalClone(flushInterval time.Duration) time.Duration {
func pluginSetTrieFlushIntervalClone(flushInterval time.Duration) time.Duration {
m := xplugeth.GetModules[setTrieFlushIntervalClonePlugin]()
var snc sync.Once
if len(m) > 1 {
snc.Do(func() { log.Warn("The blockChain flushInterval value is being accessed by multiple plugins") })
}
for _, m := range m {
flushInterval = m.PluginSetTrieFlushIntervalClone(flushInterval)
flushInterval = m.SetTrieFlushIntervalClone(flushInterval)
}
return flushInterval
}

0 comments on commit 5e4ae14

Please sign in to comment.