-
Notifications
You must be signed in to change notification settings - Fork 701
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "proposed" optional flag to
getValidatorsAt
(#3531)
Signed-off-by: Ian Suvak <[email protected]> Co-authored-by: Stephen Buttolph <[email protected]>
- Loading branch information
1 parent
480add4
commit ae6b476
Showing
9 changed files
with
215 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package api | ||
|
||
import ( | ||
"errors" | ||
"math" | ||
|
||
avajson "github.com/ava-labs/avalanchego/utils/json" | ||
) | ||
|
||
type Height avajson.Uint64 | ||
|
||
const ( | ||
ProposedHeightFlag = "proposed" | ||
ProposedHeight = math.MaxUint64 | ||
) | ||
|
||
var errInvalidHeight = errors.New("invalid height") | ||
|
||
func (h Height) MarshalJSON() ([]byte, error) { | ||
if h == ProposedHeight { | ||
return []byte(`"` + ProposedHeightFlag + `"`), nil | ||
} | ||
return avajson.Uint64(h).MarshalJSON() | ||
} | ||
|
||
func (h *Height) UnmarshalJSON(b []byte) error { | ||
if string(b) == ProposedHeightFlag { | ||
*h = ProposedHeight | ||
return nil | ||
} | ||
err := (*avajson.Uint64)(h).UnmarshalJSON(b) | ||
if err != nil { | ||
return errInvalidHeight | ||
} | ||
// MaxUint64 is reserved for proposed height | ||
// return an error if supplied numerically | ||
if uint64(*h) == ProposedHeight { | ||
*h = 0 | ||
return errInvalidHeight | ||
} | ||
return nil | ||
} | ||
|
||
func (h Height) IsProposed() bool { | ||
return h == ProposedHeight | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package api | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMarshallHeight(t *testing.T) { | ||
require := require.New(t) | ||
h := Height(56) | ||
bytes, err := h.MarshalJSON() | ||
require.NoError(err) | ||
require.Equal(`"56"`, string(bytes)) | ||
|
||
h = Height(ProposedHeight) | ||
bytes, err = h.MarshalJSON() | ||
require.NoError(err) | ||
require.Equal(`"proposed"`, string(bytes)) | ||
} | ||
|
||
func TestUnmarshallHeight(t *testing.T) { | ||
require := require.New(t) | ||
|
||
var h Height | ||
|
||
require.NoError(h.UnmarshalJSON([]byte("56"))) | ||
require.Equal(Height(56), h) | ||
|
||
require.NoError(h.UnmarshalJSON([]byte(ProposedHeightFlag))) | ||
require.Equal(Height(ProposedHeight), h) | ||
require.True(h.IsProposed()) | ||
|
||
err := h.UnmarshalJSON([]byte("invalid")) | ||
require.ErrorIs(err, errInvalidHeight) | ||
require.Equal(Height(0), h) | ||
|
||
err = h.UnmarshalJSON([]byte(`"` + strconv.FormatUint(uint64(ProposedHeight), 10) + `"`)) | ||
require.ErrorIs(err, errInvalidHeight) | ||
require.Equal(Height(0), h) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters