Skip to content

Commit

Permalink
Handle overflow when getting finalized block (#23)
Browse files Browse the repository at this point in the history
* Handle overflow when getting finalized block

* refactor name of function
  • Loading branch information
huyngopt1994 authored Aug 28, 2024
1 parent 2798dc6 commit 289b289
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
10 changes: 3 additions & 7 deletions pkg/listener/chain_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"time"

"github.com/ethereum/go-ethereum/ethclient"
Expand All @@ -15,6 +14,7 @@ import (
"github.com/ronin-chain/ronin-random-beacon/pkg/contract"
"github.com/ronin-chain/ronin-random-beacon/pkg/db"
"github.com/ronin-chain/ronin-random-beacon/pkg/event"
"github.com/ronin-chain/ronin-random-beacon/pkg/utils"
)

const (
Expand Down Expand Up @@ -119,12 +119,8 @@ func (l *ChainListener) getFinalizedBlockNumber(rpcEndpoint string) (uint64, err
return 0, err
}

number, err := strconv.ParseUint(rs.Result.Number[2:], 16, 64)
if err != nil {
return 0, err
}

return number - BufferBlock, nil // Add a little buffer 2 blocks for avoiding reorg in RequestRandom.
number, err := utils.ParseBlockNumberWithOptionalDelay(rs.Result.Number, BufferBlock)
return number, err
}

// Syncs latest block number.
Expand Down
23 changes: 23 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"fmt"
"math/big"
"strconv"
"strings"

"github.com/ethereum/go-ethereum/crypto"
Expand Down Expand Up @@ -44,3 +45,25 @@ func Max(a, b uint64) uint64 {
}
return b
}

// Convert HexString to Uin64 with optional delay, delay is subtracted from the result.
func ParseBlockNumberWithOptionalDelay(s string, delay uint64) (uint64, error) {
s = strings.TrimSpace(s)
// Strip the "0x" prefix if present
if strings.HasPrefix(s, "0x") {
s = s[2:]
}

if len(s) == 0 {
return 0, fmt.Errorf("invalid hexa string %s", s)
}

number, err := strconv.ParseUint(s, 16, 64)
if err != nil {
return 0, err
}
if number < delay {
return 0, fmt.Errorf("invalid hexa string %s", s)
}
return number - delay, nil
}
Binary file removed ronin-random-beacon
Binary file not shown.
42 changes: 42 additions & 0 deletions tests/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tests

import (
"math/big"
"strings"
"testing"

"github.com/ronin-chain/ronin-random-beacon/pkg/utils"
Expand Down Expand Up @@ -58,3 +59,44 @@ func TestMax(t *testing.T) {
}
}
}

func TestParseBlockNumberWithOptionalDelay(t *testing.T) {
const (
bufferBlock = 2
)
testCases := []struct {
input string
buffer uint64
expected uint64
errorMessage string
}{
{"0x0", bufferBlock, 0, "invalid hexa string"},
{"0x11", bufferBlock, 15, ""},
{"0x1234", bufferBlock, 4658, ""},
{" 0x1234 ", bufferBlock, 4658, ""},
{" 1234", bufferBlock, 4658, ""},
{"0x1234", 0, 4660, ""},
{"0x1", bufferBlock, 0, "invalid hexa string"},
{"0x", bufferBlock, 0, "invalid hexa string"},
{" ", bufferBlock, 0, "invalid hexa string"},
{" ", bufferBlock, 0, "invalid hexa string"},
{"", bufferBlock, 0, "invalid hexa string"},
}

for _, tc := range testCases {
result, err := utils.ParseBlockNumberWithOptionalDelay(tc.input, tc.buffer)
if result != tc.expected {
t.Errorf("ConvertHexaStringToIntWithBuffer(%q, %v) = %v; want %v", tc.input, tc.buffer, result, tc.expected)
}

if tc.errorMessage == "" {
if err != nil {
t.Errorf("ConvertHexaStringToIntWithBuffer(%q, %v) got error %v; want nil ", tc.input, tc.buffer, err)
}
} else {
if !strings.Contains(err.Error(), tc.errorMessage) {
t.Errorf("ConvertHexaStringToIntWithBuffer(%q, %v) got error %v; want %v ", tc.input, tc.buffer, err, tc.errorMessage)
}
}
}
}

0 comments on commit 289b289

Please sign in to comment.