Skip to content

Commit

Permalink
Sanitize eth_getBlockByNumber unmarshal method
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Jun 26, 2024
1 parent c7d6dfb commit 22794c2
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions l1/eth_subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package l1

import (
"context"
"encoding/json"
"fmt"
"math/big"
"strconv"
"strings"
"time"

"github.com/NethermindEth/juno/l1/contract"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/rpc"
Expand Down Expand Up @@ -54,28 +54,21 @@ func (s *EthSubscriber) ChainID(ctx context.Context) (*big.Int, error) {
}

func (s *EthSubscriber) FinalisedHeight(ctx context.Context) (uint64, error) {
finalisedBlock := make(map[string]any, 0)
if err := s.client.CallContext(ctx, &finalisedBlock, "eth_getBlockByNumber", "finalized", false); err != nil { //nolint:misspell
var raw json.RawMessage
if err := s.client.CallContext(ctx, &raw, "eth_getBlockByNumber", "finalized", false); err != nil { //nolint:misspell

Check warning on line 58 in l1/eth_subscriber.go

View check run for this annotation

Codecov / codecov/patch

l1/eth_subscriber.go#L57-L58

Added lines #L57 - L58 were not covered by tests
return 0, fmt.Errorf("get finalised Ethereum block: %w", err)
}

number, ok := finalisedBlock["number"]
if !ok {
return 0, fmt.Errorf("number field not present in Ethereum block")
var head *types.Header
if err := json.Unmarshal(raw, &head); err != nil {
return 0, err

Check warning on line 64 in l1/eth_subscriber.go

View check run for this annotation

Codecov / codecov/patch

l1/eth_subscriber.go#L62-L64

Added lines #L62 - L64 were not covered by tests
}

numberString, ok := number.(string)
if !ok {
return 0, fmt.Errorf("block number is not a string: %v", number)
if head == nil {
return 0, fmt.Errorf("finalised block not found")

Check warning on line 68 in l1/eth_subscriber.go

View check run for this annotation

Codecov / codecov/patch

l1/eth_subscriber.go#L67-L68

Added lines #L67 - L68 were not covered by tests
}

numberString = strings.TrimPrefix(numberString, "0x")
numberUint, err := strconv.ParseUint(numberString, 16, 64)
if err != nil {
return 0, fmt.Errorf("parse block number: %s", numberString)
}

return numberUint, nil
return head.Number.Uint64(), nil

Check warning on line 71 in l1/eth_subscriber.go

View check run for this annotation

Codecov / codecov/patch

l1/eth_subscriber.go#L71

Added line #L71 was not covered by tests
}

func (s *EthSubscriber) Close() {
Expand Down

0 comments on commit 22794c2

Please sign in to comment.