Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

Commit

Permalink
bing bong
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdevbear committed Jan 17, 2024
1 parent 5bc992a commit 84d501e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 41 deletions.
1 change: 0 additions & 1 deletion cosmos/x/evm/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,5 @@ func (k *Keeper) EndBlock(ctx context.Context) error {
func (k *Keeper) PrepareCheckState(ctx context.Context) error {
k.spf.SetLatestQueryContext(ctx)
k.chain.PrimePlugins(ctx)
k.chain.EmitHeadEvent()
return nil
}
21 changes: 21 additions & 0 deletions eth/core/chain_genesis.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2023, Berachain Foundation. All rights reserved.
// Use of this software is govered by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

package core

import (
"errors"

"github.com/berachain/polaris/eth/core/state"

ethtypes "github.com/ethereum/go-ethereum/core/types"
)

Expand Down
28 changes: 14 additions & 14 deletions eth/core/chain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func (bc *blockchain) CurrentHeader() *ethtypes.Header {
if block == nil || !ok {
return nil
}
bc.blockNumCache.Add(block.Number().Uint64(), block)
bc.blockHashCache.Add(block.Hash(), block)
// bc.blockNumCache.Add(block.Number().Uint64(), block)
// bc.blockHashCache.Add(block.Hash(), block)
return block.Header()
}

Expand All @@ -74,8 +74,8 @@ func (bc *blockchain) CurrentBlock() *ethtypes.Header {
if block == nil || !ok {
return nil
}
bc.blockNumCache.Add(block.Number().Uint64(), block)
bc.blockHashCache.Add(block.Hash(), block)
// bc.blockNumCache.Add(block.Number().Uint64(), block)
// bc.blockHashCache.Add(block.Hash(), block)
return block.Header()
}

Expand All @@ -91,8 +91,8 @@ func (bc *blockchain) CurrentFinalBlock() *ethtypes.Header {
if fb == nil || !ok {
return nil
}
bc.blockNumCache.Add(fb.Number().Uint64(), fb)
bc.blockHashCache.Add(fb.Hash(), fb)
// bc.blockNumCache.Add(fb.Number().Uint64(), fb)
// bc.blockHashCache.Add(fb.Hash(), fb)
return fb.Header()
}

Expand All @@ -115,7 +115,7 @@ func (bc *blockchain) GetBlock(hash common.Hash, number uint64) *ethtypes.Block
func (bc *blockchain) GetBlockByHash(hash common.Hash) *ethtypes.Block {
// check the block hash cache
if block, ok := bc.blockHashCache.Get(hash); ok {
bc.blockNumCache.Add(block.Number().Uint64(), block)
// bc.blockNumCache.Add(block.Number().Uint64(), block)
return block
}

Expand All @@ -133,16 +133,16 @@ func (bc *blockchain) GetBlockByHash(hash common.Hash) *ethtypes.Block {
}

// Cache the found block for next time and return
bc.blockNumCache.Add(block.Number().Uint64(), block)
bc.blockHashCache.Add(hash, block)
// bc.blockNumCache.Add(block.Number().Uint64(), block)
// bc.blockHashCache.Add(hash, block)
return block
}

// GetBlock retrieves a block from the database by hash and number, caching it if found.
func (bc *blockchain) GetBlockByNumber(number uint64) *ethtypes.Block {
// check the block number cache
if block, ok := bc.blockNumCache.Get(number); ok {
bc.blockHashCache.Add(block.Hash(), block)
// bc.blockHashCache.Add(block.Hash(), block)
return block
}

Expand Down Expand Up @@ -170,8 +170,8 @@ func (bc *blockchain) GetBlockByNumber(number uint64) *ethtypes.Block {
}

// Cache the found block for next time and return
bc.blockNumCache.Add(number, block)
bc.blockHashCache.Add(block.Hash(), block)
// bc.blockNumCache.Add(number, block)
// bc.blockHashCache.Add(block.Hash(), block)
return block
}

Expand Down Expand Up @@ -203,7 +203,7 @@ func (bc *blockchain) GetReceiptsByHash(blockHash common.Hash) ethtypes.Receipts
}

// cache the found receipts for next time and return
bc.receiptsCache.Add(blockHash, receipts)
// bc.receiptsCache.Add(blockHash, receipts)
derived, err := bc.deriveReceipts(receipts, blockHash)
if err != nil {
bc.logger.Error("failed to derive receipts", "err", err)
Expand Down Expand Up @@ -239,7 +239,7 @@ func (bc *blockchain) GetTransactionLookup(
}

// cache the found transaction for next time and return
bc.txLookupCache.Add(hash, txLookupEntry)
// bc.txLookupCache.Add(hash, txLookupEntry)
return txLookupEntry
}

Expand Down
40 changes: 14 additions & 26 deletions eth/core/chain_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ type ChainWriter interface {
InsertBlock(block *ethtypes.Block) error
InsertBlockAndSetHead(block *ethtypes.Block) error
SetFinalizedBlock() error
EmitHeadEvent() error
WriteBlockAndSetHead(block *ethtypes.Block, receipts []*ethtypes.Receipt, logs []*ethtypes.Log,
state state.StateDB, emitHeadEvent bool) (status core.WriteStatus, err error)
}
Expand Down Expand Up @@ -149,6 +148,20 @@ func (bc *blockchain) WriteBlockAndSetHead(
bc.receiptsCache.Add(block.Hash(), receipts)
}

// In theory, we should fire a ChainHeadEvent when we inject
// a canonical block, but sometimes we can insert a batch of
// canonical blocks. Avoid firing too many ChainHeadEvents,
// we will fire an accumulated ChainHeadEvent and disable fire
// event here.
if emitHeadEvent {
// Fire off the feeds.
bc.chainFeed.Send(core.ChainEvent{Block: block, Hash: block.Hash(), Logs: logs})
if len(logs) > 0 {
bc.logsFeed.Send(logs)
}
bc.chainHeadFeed.Send(core.ChainHeadEvent{Block: block})
}

return core.CanonStatTy, nil
}

Expand Down Expand Up @@ -221,28 +234,3 @@ func (bc *blockchain) SetFinalizedBlock() error {
}
return nil
}

func (bc *blockchain) EmitHeadEvent() error {
block := bc.finalizedBlock.Load()
receipts, ok := bc.receiptsCache.Get(block.Hash())
if !ok {
return errors.New("receipts not found")
}

logs := make([]*ethtypes.Log, 0)
for _, receipt := range receipts {
logs = append(logs, receipt.Logs...)
}

// In theory, we should fire a ChainHeadEvent when we inject
// a canonical block, but sometimes we can insert a batch of
// canonical blocks. Avoid firing too many ChainHeadEvents,
// we will fire an accumulated ChainHeadEvent and disable fire
// event here.
bc.chainFeed.Send(core.ChainEvent{Block: block, Hash: block.Hash(), Logs: logs})
if len(logs) > 0 {
bc.logsFeed.Send(logs)
}
bc.chainHeadFeed.Send(core.ChainHeadEvent{Block: block})
return nil
}

0 comments on commit 84d501e

Please sign in to comment.