Skip to content

Commit

Permalink
Fix block hex parse to number
Browse files Browse the repository at this point in the history
  • Loading branch information
pablomendezroyo committed Dec 11, 2024
1 parent ff6192e commit 99cac64
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions internal/adapters/execution/execution_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package execution

import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"net/http"

"github.com/ethereum/go-ethereum/common/hexutil"
)

// ExecutionAdapter interacts with the Ethereum execution client to fetch block information.
Expand Down Expand Up @@ -54,13 +57,24 @@ func (e *ExecutionAdapter) GetMostRecentBlockNumber() (uint64, error) {
return 0, fmt.Errorf("failed to decode response from execution client: %w", err)
}

// Convert the hexadecimal block number to uint64
var blockNumber uint64
_, err = fmt.Sscanf(result.Result, "0x%x", &blockNumber)
// Convert the hexadecimal block number to uint64 using hexutil.Decode
decodedBytes, err := hexutil.Decode(result.Result)
if err != nil {
return 0, fmt.Errorf("failed to parse block number from result %s: %w", result.Result, err)
return 0, fmt.Errorf("failed to decode block number from result %s: %w", result.Result, err)
}

// Ensure the decoded byte slice does not exceed 8 bytes for uint64
if len(decodedBytes) > 8 {
return 0, fmt.Errorf("block number too large to fit in uint64: length %d bytes", len(decodedBytes))
}

// Convert the byte slice to uint64
var blockNumber uint64
// If the byte slice is less than 8 bytes, pad it with leading zeros
paddedBytes := make([]byte, 8)
copy(paddedBytes[8-len(decodedBytes):], decodedBytes)
blockNumber = binary.BigEndian.Uint64(paddedBytes)

return blockNumber, nil
}

Expand Down

0 comments on commit 99cac64

Please sign in to comment.