Skip to content

Commit

Permalink
refactor name of function
Browse files Browse the repository at this point in the history
  • Loading branch information
huyngopt1994 committed Aug 26, 2024
1 parent a11b1d5 commit b493978
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/listener/chain_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (l *ChainListener) getFinalizedBlockNumber(rpcEndpoint string) (uint64, err
return 0, err
}

number, err := utils.ConvertHexaStringToIntWithBuffer(rs.Result.Number, BufferBlock)
number, err := utils.ParseBlockNumberWithOptionalDelay(rs.Result.Number, BufferBlock)
return number, err
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ func Max(a, b uint64) uint64 {
return b
}

func ConvertHexaStringToIntWithBuffer(s string, buffer uint64) (uint64, error) {
// 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") {
Expand All @@ -61,8 +62,8 @@ func ConvertHexaStringToIntWithBuffer(s string, buffer uint64) (uint64, error) {
if err != nil {
return 0, err
}
if number < buffer {
if number < delay {
return 0, fmt.Errorf("invalid hexa string %s", s)
}
return number - buffer, nil
return number - delay, nil
}
5 changes: 3 additions & 2 deletions tests/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestMax(t *testing.T) {
}
}

func TestConvertHexaStringToIntWithBuffer(t *testing.T) {
func TestParseBlockNumberWithOptionalDelay(t *testing.T) {
const (
bufferBlock = 2
)
Expand All @@ -75,6 +75,7 @@ func TestConvertHexaStringToIntWithBuffer(t *testing.T) {
{"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"},
Expand All @@ -83,7 +84,7 @@ func TestConvertHexaStringToIntWithBuffer(t *testing.T) {
}

for _, tc := range testCases {
result, err := utils.ConvertHexaStringToIntWithBuffer(tc.input, tc.buffer)
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)
}
Expand Down

0 comments on commit b493978

Please sign in to comment.