Skip to content

Commit

Permalink
fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
JekaMas committed May 5, 2022
1 parent 2cb82c9 commit aa3004b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
10 changes: 1 addition & 9 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,6 @@ Loop:

// handleWhitelistCheckpoint handles the checkpoint whitelist mechanism.
func (s *Ethereum) handleWhitelistCheckpoint() error {
var m sync.Mutex

ethHandler := (*ethHandler)(s.handler)

if !ethHandler.chain.Engine().(*bor.Bor).WithoutHeimdall {
Expand All @@ -626,14 +624,8 @@ func (s *Ethereum) handleWhitelistCheckpoint() error {
return err
}

m.Lock()
// Update the checkpoint whitelist map.
ethHandler.downloader.EnqueueCheckpointWhitelist(endBlockNum, endBlockHash)
// If size of checkpoint whitelist map is greater than 10, remove the oldest entry.
if len(ethHandler.downloader.GetCheckpointWhitelist()) > 10 {
ethHandler.downloader.DequeueCheckpointWhitelist()
}
m.Unlock()
ethHandler.downloader.ProcessCheckpoint(endBlockNum, endBlockHash)
}

return nil
Expand Down
1 change: 1 addition & 0 deletions eth/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ type Downloader struct {
// interface for whitelist service
type ChainValidator interface {
IsValidChain(remoteHeader *types.Header, fetchHeadersByNumber func(number uint64, amount int, skip int, reverse bool) ([]*types.Header, []common.Hash, error)) (bool, error)
ProcessCheckpoint(endBlockNum uint64, endBlockHash common.Hash)
}

// LightChain encapsulates functions required to synchronise a light chain.
Expand Down
21 changes: 19 additions & 2 deletions eth/downloader/whitelist/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package whitelist

import (
"errors"
"sync"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand All @@ -12,16 +13,19 @@ var (
ErrCheckpointMismatch = errors.New("checkpoint mismatch")
)

// Checkpoint whitelist
type Service struct {
// Checkpoint whitelist
m sync.RWMutex
checkpointWhitelist map[uint64]common.Hash // Checkpoint whitelist, populated by reaching out to heimdall
checkpointOrder []uint64 // Checkpoint order, populated by reaching out to heimdall
maxCapacity uint
}

func NewService() *Service {
func NewService(maxCapacity uint) *Service {
return &Service{
checkpointWhitelist: make(map[uint64]common.Hash),
checkpointOrder: []uint64{},
maxCapacity: maxCapacity,
}
}

Expand Down Expand Up @@ -61,6 +65,18 @@ func (w *Service) IsValidChain(remoteHeader *types.Header, fetchHeadersByNumber
return false, ErrCheckpointMismatch
}

func (w *Service) ProcessCheckpoint(endBlockNum uint64, endBlockHash common.Hash) {
w.m.Lock()
defer w.m.Unlock()

w.EnqueueCheckpointWhitelist(endBlockNum, endBlockHash)
// If size of checkpoint whitelist map is greater than 10, remove the oldest entry.

if len(w.GetCheckpointWhitelist()) > int(w.maxCapacity) {
w.DequeueCheckpointWhitelist()
}
}

// PurgeWhitelistMap purges data from checkpoint whitelist map
func (w *Service) PurgeWhitelistMap() error {
for k := range w.checkpointWhitelist {
Expand All @@ -73,6 +89,7 @@ func (w *Service) PurgeWhitelistMap() error {
func (w *Service) EnqueueCheckpointWhitelist(key uint64, val common.Hash) {
if _, ok := w.checkpointWhitelist[key]; !ok {
log.Debug("Enqueing new checkpoint whitelist", "block number", key, "block hash", val)

w.checkpointWhitelist[key] = val
w.checkpointOrder = append(w.checkpointOrder, key)
}
Expand Down
4 changes: 3 additions & 1 deletion eth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/ethereum/go-ethereum/core/forkid"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/eth/downloader/whitelist"
"github.com/ethereum/go-ethereum/eth/fetcher"
"github.com/ethereum/go-ethereum/eth/protocols/eth"
"github.com/ethereum/go-ethereum/eth/protocols/snap"
Expand Down Expand Up @@ -200,7 +201,8 @@ func newHandler(config *handlerConfig) (*handler, error) {
// Construct the downloader (long sync) and its backing state bloom if snap
// sync is requested. The downloader is responsible for deallocating the state
// bloom when it's done.
h.downloader = downloader.New(h.checkpointNumber, config.Database, h.eventMux, h.chain, nil, h.removePeer, success)
// todo: it'd better to extract maxCapacity into config
h.downloader = downloader.New(h.checkpointNumber, config.Database, h.eventMux, h.chain, nil, h.removePeer, success, whitelist.NewService(10))

// Construct the fetcher (short sync)
validator := func(header *types.Header) error {
Expand Down

0 comments on commit aa3004b

Please sign in to comment.