Skip to content

Commit

Permalink
Merge pull request #333 from uprendis/feature/add-zero-block-api
Browse files Browse the repository at this point in the history
Add zero block into API
  • Loading branch information
uprendis committed Jun 26, 2022
2 parents 52613a1 + a7a3387 commit d136014
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gossip/ethapi_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumbe
if number == rpc.PendingBlockNumber {
number = rpc.LatestBlockNumber
}
// Otherwise resolve and return the block
// Otherwise, resolve and return the block
var blk *evmcore.EvmBlock
if number == rpc.LatestBlockNumber {
blk = b.state.CurrentBlock()
Expand Down
20 changes: 20 additions & 0 deletions gossip/store_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"

"github.com/Fantom-foundation/go-opera/evmcore"
"github.com/Fantom-foundation/go-opera/inter"
)

Expand All @@ -24,6 +25,14 @@ func (s *Store) GetGenesisID() *hash.Hash {
return &val
}

func (s *Store) fakeGenesisHash() hash.Event {
fakeGenesisHash := hash.Event(*s.GetGenesisID())
for i := range fakeGenesisHash[:8] {
fakeGenesisHash[i] = 0
}
return fakeGenesisHash
}

func (s *Store) SetGenesisID(val hash.Hash) {
err := s.table.Genesis.Put([]byte("g"), val.Bytes())
if err != nil {
Expand All @@ -41,6 +50,13 @@ func (s *Store) SetBlock(n idx.Block, b *inter.Block) {

// GetBlock returns stored block.
func (s *Store) GetBlock(n idx.Block) *inter.Block {
if n == 0 {
// fake genesis block for compatibility with web3
return &inter.Block{
Time: evmcore.FakeGenesisTime - 1,
Atropos: s.fakeGenesisHash(),
}
}
// Get block from LRU cache first.
if c, ok := s.cache.Blocks.Get(n); ok {
return c.(*inter.Block)
Expand Down Expand Up @@ -98,6 +114,10 @@ func (s *Store) GetBlockIndex(id hash.Event) *idx.Block {
s.Log.Crit("Failed to get key-value", "err", err)
}
if buf == nil {
if id == s.fakeGenesisHash() {
zero := idx.Block(0)
return &zero
}
return nil
}
n := idx.BytesToBlock(buf)
Expand Down

0 comments on commit d136014

Please sign in to comment.