Skip to content

Commit

Permalink
fix integrating with archiver
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgao001 committed Jul 18, 2024
1 parent 1c6cc55 commit bd8e075
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 24 deletions.
5 changes: 2 additions & 3 deletions core/blockarchiver/block_archiver_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,14 @@ func (c *BlockArchiverService) getBlockByNumber(number uint64) (*types.Body, *ty
// fetch the bundle range
log.Info("fetching bundle of blocks", "number", number)
start, end, err := c.client.GetBundleBlocksRange(number)
log.Debug("bundle of blocks", "start", start, "end", end, "err", err)
if err != nil {
return nil, nil, err
}

// add lock to avoid concurrent fetching of the same bundle of blocks
c.requestLock.AddRange(start, end)
defer c.requestLock.RemoveRange(start, end)

//todo can fetch the bundle by request SP directly and extract blocks instead of calling the block archiver service if bundle name is known.
blocks, err := c.client.GetBundleBlocksByBlockNum(number)
if err != nil {
return nil, nil, err
Expand All @@ -147,7 +147,6 @@ func (c *BlockArchiverService) getBlockByNumber(number uint64) (*types.Body, *ty
header = block.Header()
}
}

return body, header, nil
}

Expand Down
13 changes: 3 additions & 10 deletions core/blockarchiver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func New(blockHubHost string) (*Client, error) {
}

func (c *Client) GetBlockByHash(hash common.Hash) (*Block, error) {
payload := preparePayload("eth_getBlockByHash", []interface{}{hash.String()})
payload := preparePayload("eth_getBlockByHash", []interface{}{hash.String(), "true"})
body, err := c.postRequest(payload)
if err != nil {
return nil, err
Expand All @@ -49,7 +49,7 @@ func (c *Client) GetBlockByHash(hash common.Hash) (*Block, error) {
}

func (c *Client) GetBlockByNumber(number uint64) (*Block, error) {
payload := preparePayload("eth_getBlockByNumber", []interface{}{Int64ToHex(int64(number))})
payload := preparePayload("eth_getBlockByNumber", []interface{}{Int64ToHex(int64(number)), "true"})
body, err := c.postRequest(payload)
if err != nil {
return nil, err
Expand All @@ -63,14 +63,7 @@ func (c *Client) GetBlockByNumber(number uint64) (*Block, error) {
}

func (c *Client) GetLatestBlock() (*Block, error) {
payload := map[string]interface{}{
"jsonrpc": "2.0",
"method": "eth_getBlockByNumber",
// todo mock the latest 2000
"Params": []interface{}{"0x7D0"},
"id": 1,
}

payload := preparePayload("eth_getBlockByNumber", []interface{}{"latest", "true"})
body, err := c.postRequest(payload)
if err != nil {
return nil, err
Expand Down
14 changes: 3 additions & 11 deletions core/blockarchiver/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ type Range struct {
type RequestLock struct {
rangeMap map[uint64]Range
lookupMap map[uint64]bool
mu sync.Mutex
mu sync.RWMutex
}

// NewRequestLock creates a new RequestLock
Expand All @@ -127,8 +127,8 @@ func NewRequestLock() *RequestLock {

// IsWithinAnyRange checks if the number is within any of the cached ranges
func (rc *RequestLock) IsWithinAnyRange(num uint64) bool {
rc.mu.Lock()
defer rc.mu.Unlock()
rc.mu.RLock()
defer rc.mu.RUnlock()
_, exists := rc.lookupMap[num]
return exists
}
Expand All @@ -137,11 +137,7 @@ func (rc *RequestLock) IsWithinAnyRange(num uint64) bool {
func (rc *RequestLock) AddRange(from, to uint64) {
rc.mu.Lock()
defer rc.mu.Unlock()

// Add the range to the rangeMap
rc.rangeMap[from] = Range{from, to}

// Update the lookupMap for fast lookup
for i := from; i <= to; i++ {
rc.lookupMap[i] = true
}
Expand All @@ -151,11 +147,7 @@ func (rc *RequestLock) AddRange(from, to uint64) {
func (rc *RequestLock) RemoveRange(from, to uint64) {
rc.mu.Lock()
defer rc.mu.Unlock()

// Remove the range from the rangeMap
delete(rc.rangeMap, from)

// Update the lookupMap for fast lookup
for i := from; i <= to; i++ {
delete(rc.lookupMap, i)
}
Expand Down
1 change: 1 addition & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3193,5 +3193,6 @@ func (bc *BlockChain) updateCurrentHeader() error {
headBlockGauge.Update(int64(header.Number.Uint64()))

bc.hc.tdCache.Add(block.Hash(), block.TotalDifficulty)
log.Info("update current header", "number", header.Number, "hash", header.Hash())
return nil
}

0 comments on commit bd8e075

Please sign in to comment.