Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
193 commits
Select commit Hold shift + click to select a range
5394aef
exploratory nit
abi87 Aug 28, 2023
0ed3974
no naked access to ids.ID[:]. Use Bytes attribute instead.
abi87 Aug 28, 2023
8ce752b
some more ids package cleanup
abi87 Aug 28, 2023
4feb4e1
nits
abi87 Aug 28, 2023
58be8e6
nit
abi87 Aug 28, 2023
b128954
ids copy UTs
abi87 Aug 28, 2023
1ef12a3
reverted ctors changes
abi87 Aug 29, 2023
b5bdc6a
Merge branch 'dev' into ids_package_cleanup
abi87 Sep 6, 2023
4182792
nits
abi87 Sep 6, 2023
c430ce9
introduced GenericNodeID struct
abi87 Sep 7, 2023
85bb0a2
validators: replacing NodeID with GenericNodeID
abi87 Sep 7, 2023
536e573
validators: replacing NodeID with GenericNodeID p2
abi87 Sep 8, 2023
7f29f23
Merge branch 'dev' into ids_package_cleanup
abi87 Sep 11, 2023
0c585f6
validators: replacing NodeID with GenericNodeID p3
abi87 Sep 11, 2023
2af164e
validators: replacing NodeID with GenericNodeID p4
abi87 Sep 11, 2023
3a4a6bd
validators: replacing NodeID with GenericNodeID p5
abi87 Sep 11, 2023
a664d35
Merge branch 'dev' into ids_package_cleanup
abi87 Sep 12, 2023
f2af3f5
validators: fix P-chain keys serialization
abi87 Sep 12, 2023
ac4a517
Merge branch 'ids_package_cleanup' into genericNodeID
abi87 Sep 12, 2023
aafd74d
simplified GenericNodeID implementation
abi87 Sep 12, 2023
9d7127f
network package: replacing NodeID with GenericNodeID
abi87 Sep 12, 2023
87e850b
network package: cleaned up throttling package
abi87 Sep 12, 2023
a828b8c
network package: keep on cleaning up
abi87 Sep 12, 2023
0296b44
network package: more cleanups
abi87 Sep 12, 2023
f6dfdd6
network package: some more cleanup
abi87 Sep 12, 2023
c2bb72a
network package: cleaned up peer subpackage
abi87 Sep 12, 2023
7a2f437
network package: cleaned up network package
abi87 Sep 12, 2023
248c659
router cleanup
abi87 Sep 12, 2023
02df155
node and chain manager cleanup
abi87 Sep 13, 2023
4186d05
nit
abi87 Sep 13, 2023
be4365a
message package: some cleanup
abi87 Sep 13, 2023
a0094bc
fixed fuzz test
abi87 Sep 13, 2023
9a42c63
polls cleanup
abi87 Sep 13, 2023
73fbcd3
common sender package: some cleanup
abi87 Sep 13, 2023
0958003
common sender package: some more cleanup
abi87 Sep 14, 2023
95b7cde
common sender package: some more cleanup
abi87 Sep 14, 2023
43e4e47
cut coreth dependencies + common sender package cleanup
abi87 Sep 14, 2023
e9dbe07
engine interface cleanup
abi87 Sep 14, 2023
0f341fd
engine interface more cleanup
abi87 Sep 14, 2023
0d634e2
engine interface: some more cleanup
abi87 Sep 14, 2023
2afccb3
engine interface: more cleanup
abi87 Sep 14, 2023
2fe7f1c
network.Send cleanup
abi87 Sep 15, 2023
5b854cb
snow context cleanup
abi87 Sep 15, 2023
5168d70
nits
abi87 Sep 15, 2023
e1352fa
nits + sync clients UTs fixed
abi87 Sep 15, 2023
7f455e9
nits
abi87 Sep 15, 2023
3186ed3
some more nits
abi87 Sep 15, 2023
765a716
Merge branch 'dev' into genericNodeID
abi87 Sep 15, 2023
eca8960
Merge branch 'dev' into ids_package_cleanup
abi87 Sep 15, 2023
0e1e84c
wip: re-inlining coreth
abi87 Sep 15, 2023
b16831c
Merge branch 'ids_package_cleanup' into genericNodeID
abi87 Sep 15, 2023
147cfd6
nit
abi87 Sep 15, 2023
c9e167b
wip:renaming GenericNodeID
abi87 Sep 15, 2023
a0d4c66
cut coreth out
abi87 Sep 15, 2023
0d2c1d3
renamed GenericNodeID to NodeID
abi87 Sep 15, 2023
53c4302
reintroducing coreth
abi87 Sep 15, 2023
e62fd9f
nits + reduced diffs
abi87 Sep 15, 2023
f5347a0
reduced diffs
abi87 Sep 15, 2023
edf2b45
fixed NodeID fuzzing tests
abi87 Sep 18, 2023
07b6edc
Merge branch 'dev' into ids_package_cleanup
abi87 Sep 18, 2023
ab7c0ac
Merge branch 'ids_package_cleanup' into genericNodeID
abi87 Sep 18, 2023
eb668c2
drop Writable methods
abi87 Sep 18, 2023
2f6c082
some more dropping of Writable methods
abi87 Sep 18, 2023
879d8ff
reduced diff
abi87 Sep 18, 2023
e4fbea3
Merge branch 'ids_package_cleanup' into genericNodeID
abi87 Sep 18, 2023
62f05d8
Merge branch 'dev' into ids_package_cleanup
abi87 Sep 20, 2023
391596c
temporarily removed coreth dependency
abi87 Sep 20, 2023
89a4282
Merge branch 'dev' into genericNodeID
abi87 Sep 20, 2023
5b93a39
bumped coreth dependency
abi87 Sep 20, 2023
c7d3db1
Merge branch 'ids_package_cleanup' into genericNodeID
abi87 Sep 20, 2023
9856159
leftover
abi87 Sep 20, 2023
ed0f956
Merge branch 'dev' into ids_package_cleanup
abi87 Sep 21, 2023
8721f2e
Merge branch 'ids_package_cleanup' into genericNodeID
abi87 Sep 21, 2023
1cb0495
dropped NodeID.Equal
abi87 Sep 21, 2023
457b1da
reduced diff
abi87 Sep 21, 2023
0bbf29b
cleanup NodeID ctor
abi87 Sep 21, 2023
54ddc4b
Merge branch 'dev' into ids_package_cleanup
abi87 Sep 25, 2023
43f1eb5
Merge branch 'ids_package_cleanup' into genericNodeID
abi87 Sep 25, 2023
849f1ff
Merge branch 'dev' into ids_package_cleanup
abi87 Sep 27, 2023
238312d
nit
abi87 Sep 27, 2023
1350547
Merge branch 'ids_package_cleanup' into genericNodeID
abi87 Sep 27, 2023
82a8769
fixed NodeID usage in tests
abi87 Sep 27, 2023
09d2c08
dropped ids.ID.Bytes
abi87 Oct 2, 2023
431c72b
nit
abi87 Oct 2, 2023
c679b02
nits
abi87 Oct 2, 2023
8fe07d9
nit
abi87 Oct 3, 2023
72ca6cb
nit
abi87 Oct 3, 2023
7740122
fixed NodeID unmarshalling
abi87 Oct 3, 2023
8f6039c
changed NodeID struct
abi87 Oct 3, 2023
31a3741
ids serialization UTs
abi87 Oct 3, 2023
16671fb
cleanup
abi87 Oct 3, 2023
c8d1857
Merge branch 'dev' into genericNodeID
abi87 Oct 3, 2023
a0252ed
temporarily removed coreth dependencies
abi87 Oct 3, 2023
7a7e357
bumped coreth
abi87 Oct 3, 2023
8993e72
nit
abi87 Oct 3, 2023
63eb8a1
Replace property testing with fuzz testing
StephenButtolph Oct 3, 2023
fd29f04
cleanup
StephenButtolph Oct 3, 2023
ffc4908
cleanup
StephenButtolph Oct 3, 2023
bad7d13
nits
StephenButtolph Oct 3, 2023
feaebba
cleanup
StephenButtolph Oct 3, 2023
7bb4505
reduce diff
StephenButtolph Oct 4, 2023
3efdd6e
reduce diff
StephenButtolph Oct 4, 2023
76c2a25
nit
StephenButtolph Oct 4, 2023
8e05c51
reduce diff
StephenButtolph Oct 4, 2023
78cabfa
reduce diff
StephenButtolph Oct 4, 2023
358152a
fixes EmptyNodeID to EmptyShortNodeID mapping
abi87 Oct 4, 2023
4fffa3d
fixed NodeID from bytes
abi87 Oct 4, 2023
d05caec
p-chain apis, replaced ShortNodeID with NodeID
abi87 Oct 4, 2023
1ae79d4
nits
abi87 Oct 4, 2023
2a2f801
fixes
abi87 Oct 4, 2023
1acba39
Merge branch 'dev' into genericNodeID
abi87 Oct 4, 2023
85ac064
Merge branch 'dev' into genericNodeID
abi87 Oct 5, 2023
a667456
Merge branch 'dev' into genericNodeID
abi87 Oct 6, 2023
113eede
Merge branch 'dev' into genericNodeID
abi87 Oct 11, 2023
5b1dcdb
Merge branch 'dev' into genericNodeID
abi87 Oct 27, 2023
52a9768
temporarily dropped coreth dependency
abi87 Oct 27, 2023
3d76349
bumped coreth version
abi87 Oct 27, 2023
05d303d
Merge branch 'dev' into genericNodeID
abi87 Oct 27, 2023
ea7e361
Merge branch 'dev' into genericNodeID
abi87 Oct 30, 2023
bc15253
temporarily cut coreth dependency
abi87 Oct 30, 2023
10dae4c
bumped coreth version
abi87 Oct 30, 2023
da1609f
Merge branch 'dev' into genericNodeID
abi87 Nov 6, 2023
7981dcb
nits
abi87 Nov 6, 2023
31c56f1
some more nits
abi87 Nov 6, 2023
5145b26
some more nits
abi87 Nov 6, 2023
694ab1a
nit
abi87 Nov 6, 2023
807bf28
temporarily cut coreth dependency
abi87 Oct 30, 2023
6e3b435
bumped coreth version
abi87 Nov 6, 2023
8b315d8
Merge branch 'dev' into genericNodeID
abi87 Nov 9, 2023
43043d4
temporarily cut coreth dependency
abi87 Oct 30, 2023
0ac8972
bumped coreth version
abi87 Nov 9, 2023
f27c18c
improved usage of NodeIDs methods
abi87 Nov 9, 2023
6f35ae0
nit
abi87 Nov 9, 2023
c05c853
improved hideous function signature
abi87 Nov 9, 2023
fc57aef
introduced BuildTestNodeID
abi87 Nov 9, 2023
911ae39
spread usage of BuildTestNodeID
abi87 Nov 9, 2023
eea5821
nit
abi87 Nov 9, 2023
346088f
leftovers
abi87 Nov 9, 2023
c65c623
Merge branch 'ids_cleanup' into genericNodeID
abi87 Nov 9, 2023
6e6a5fe
nits
abi87 Nov 9, 2023
ee7fd40
Merge commit '6e6a5feb4' into genericNodeID
abi87 Nov 9, 2023
865765f
reduced diff
abi87 Nov 9, 2023
92e8edf
genesis validators placeholder types
abi87 Nov 9, 2023
e2867e3
improved naming
abi87 Nov 9, 2023
46597bf
Merge branch 'genesis_validators_cleanup' into genericNodeID
abi87 Nov 9, 2023
eca0488
Merge branch 'dev' into genericNodeID
abi87 Nov 9, 2023
19c527c
Merge branch 'dev' into genericNodeID
abi87 Nov 9, 2023
e37b649
Merge branch 'dev' into genericNodeID
abi87 Nov 9, 2023
839678e
reduced diffs
abi87 Nov 10, 2023
6a129e2
update txs builders to reduce diffs
abi87 Nov 10, 2023
d196285
Merge branch 'dev' into genericNodeID
abi87 Nov 10, 2023
68b857f
cleaned up nodeID generation in some UTs
abi87 Nov 10, 2023
94d252f
more cleanup
abi87 Nov 10, 2023
b1c77b4
some more cleanup
abi87 Nov 10, 2023
4ee3b32
introducing genesisNodeIDs in UTs
abi87 Nov 10, 2023
46fba82
some more clenaup
abi87 Nov 10, 2023
a70b1a1
some more cleanup
abi87 Nov 10, 2023
af13438
some more cleanup
abi87 Nov 10, 2023
9c95f87
nit
abi87 Nov 10, 2023
ab7db24
hardened UT
abi87 Nov 10, 2023
e5fd7fe
Merge branch 'pchain_nodeID_cleanup_in_UTs' into genericNodeID
abi87 Nov 10, 2023
1a5bd9c
nit
abi87 Nov 10, 2023
529aabc
changes from code review
abi87 Nov 13, 2023
8837b98
nit
abi87 Nov 13, 2023
05e4fa9
Merge branch 'pchain_nodeID_cleanup_in_UTs' into genericNodeID
abi87 Nov 13, 2023
8ea46cd
nit
abi87 Nov 13, 2023
a32d343
nit
abi87 Nov 13, 2023
0dcdcc5
nit: fixed api
abi87 Nov 13, 2023
092ce06
nit
abi87 Nov 13, 2023
4b3619f
reduced diffs
abi87 Nov 13, 2023
ff4fdb2
nits
abi87 Nov 13, 2023
1f194fa
reverted faulty API changes
abi87 Nov 13, 2023
26a757a
Merge branch 'dev' into pchain_nodeID_cleanup_in_UTs
Nov 13, 2023
a22fd65
nit
abi87 Nov 13, 2023
4203d2e
nit
abi87 Nov 13, 2023
22843b4
Merge branch 'dev' into pchain_nodeID_cleanup_in_UTs
abi87 Nov 14, 2023
132bb3b
Merge branch 'pchain_nodeID_cleanup_in_UTs' into genericNodeID
abi87 Nov 14, 2023
130b4df
Merge branch 'dev' into genericNodeID
abi87 Nov 17, 2023
7918c58
temporarily cut coreth dependency
abi87 Oct 30, 2023
dcfe530
bumped coreth version
abi87 Nov 9, 2023
ab4ab97
Merge branch 'dev' into genericNodeID
abi87 Nov 17, 2023
b8c01ff
Merge branch 'dev' into genericNodeID
abi87 Nov 18, 2023
d8ca7fb
Merge branch 'dev' into genericNodeID
abi87 Nov 21, 2023
1620e46
Merge branch 'dev' into genericNodeID
abi87 Nov 22, 2023
e334182
Merge branch 'dev' into genericNodeID
abi87 Nov 23, 2023
77b6c15
bumped coreth version
abi87 Nov 23, 2023
2ea4b0e
Merge branch 'dev' into genericNodeID
abi87 Nov 28, 2023
0d2e8f2
Merge branch 'dev' into genericNodeID
abi87 Nov 29, 2023
f0b6705
Merge branch 'dev' into genericNodeID
abi87 Dec 3, 2023
176b5a4
bumped coreth version
abi87 Dec 3, 2023
95d228a
Merge branch 'dev' into genericNodeID
abi87 Jan 2, 2024
44bf435
bumped coreth version
abi87 Jan 2, 2024
892be8e
Merge branch 'dev' into genericNodeID
abi87 Jan 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion genesis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (a Allocation) Compare(other Allocation) int {
}

type Staker struct {
NodeID ids.NodeID `json:"nodeID"`
NodeID ids.ShortNodeID `json:"nodeID"`
RewardAddress ids.ShortID `json:"rewardAddress"`
DelegationFee uint32 `json:"delegationFee"`
Signer *signer.ProofOfPossession `json:"signer,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion genesis/unparsed_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (ua UnparsedAllocation) Parse() (Allocation, error) {
}

type UnparsedStaker struct {
NodeID ids.NodeID `json:"nodeID"`
NodeID ids.ShortNodeID `json:"nodeID"`
RewardAddress string `json:"rewardAddress"`
DelegationFee uint32 `json:"delegationFee"`
Signer *signer.ProofOfPossession `json:"signer,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/DataDog/zstd v1.5.2
github.com/Microsoft/go-winio v0.5.2
github.com/NYTimes/gziphandler v1.1.1
github.com/ava-labs/coreth v0.12.10-rc.2
github.com/ava-labs/coreth v0.12.9-rc.9.0.20240102131324-9fd7093cec0b
github.com/ava-labs/ledger-avalanche/go v0.0.0-20231102202641-ae2ebdaeac34
github.com/btcsuite/btcd/btcutil v1.1.3
github.com/cockroachdb/pebble v0.0.0-20230209160836-829675f94811
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/ava-labs/coreth v0.12.10-rc.2 h1:+2YK7PzStcLCHtsBb1VQjw6DyMl1sMZatReZAyIy7w8=
github.com/ava-labs/coreth v0.12.10-rc.2/go.mod h1:RIbv14KMyWSj4hB1danS+vEPCUsgArZUEo99SaP5nW8=
github.com/ava-labs/coreth v0.12.9-rc.9.0.20240102131324-9fd7093cec0b h1:vYPVa6G6pc4BGzG6EvrIPS9CaLsKBghYdq5vmegVN7w=
github.com/ava-labs/coreth v0.12.9-rc.9.0.20240102131324-9fd7093cec0b/go.mod h1:J63sud3GlfNeVFu/8qW55Othc+bISLK1OlfkJ0OtmGs=
github.com/ava-labs/ledger-avalanche/go v0.0.0-20231102202641-ae2ebdaeac34 h1:mg9Uw6oZFJKytJxgxnl3uxZOs/SB8CVHg6Io4Tf99Zc=
github.com/ava-labs/ledger-avalanche/go v0.0.0-20231102202641-ae2ebdaeac34/go.mod h1:pJxaT9bUgeRNVmNRgtCHb7sFDIRKy7CzTQVi8gGNT6g=
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
Expand Down
123 changes: 123 additions & 0 deletions ids/marshalling_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package ids

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"
)

func FuzzNodeIDMarshallUnmarshalInversion(f *testing.F) {
f.Fuzz(func(t *testing.T, buf string) {
var (
require = require.New(t)
input = NodeID{buf: buf}
output = new(NodeID)
)

// json package marshalling
b, err := json.Marshal(input)
require.NoError(err)

require.NoError(json.Unmarshal(b, output))
require.Equal(input, *output)

// MarshalJson/UnmarshalJson
output = new(NodeID)
b, err = input.MarshalJSON()
require.NoError(err)

require.NoError(output.UnmarshalJSON(b))
require.Equal(input, *output)
})
}

func FuzzShortNodeIDMarshallUnmarshalInversion(f *testing.F) {
f.Fuzz(func(t *testing.T, buf []byte) {
if len(buf) != ShortNodeIDLen {
return
}

var (
require = require.New(t)
input = ShortNodeID(buf)
output = new(ShortNodeID)
)

// json package marshalling
b, err := json.Marshal(input)
require.NoError(err)

require.NoError(json.Unmarshal(b, output))
require.Equal(input, *output)

// MarshalJson/UnmarshalJson
output = new(ShortNodeID)
b, err = input.MarshalJSON()
require.NoError(err)

require.NoError(output.UnmarshalJSON(b))
require.Equal(input, *output)
})
}

func FuzzShortIDMarshallUnmarshalInversion(f *testing.F) {
f.Fuzz(func(t *testing.T, buf []byte) {
if len(buf) != ShortIDLen {
return
}

var (
require = require.New(t)
input = ShortID(buf)
output = new(ShortID)
)

// json package marshalling
b, err := json.Marshal(input)
require.NoError(err)

require.NoError(json.Unmarshal(b, output))
require.Equal(input, *output)

// MarshalJson/UnmarshalJson
output = new(ShortID)
b, err = input.MarshalJSON()
require.NoError(err)

require.NoError(output.UnmarshalJSON(b))
require.Equal(input, *output)
})
}

func FuzzIDMarshallUnmarshalInversion(f *testing.F) {
f.Fuzz(func(t *testing.T, buf []byte) {
if len(buf) != IDLen {
return
}

var (
require = require.New(t)
input = ID(buf)
output = new(ID)
)

// json package marshalling
b, err := json.Marshal(input)
require.NoError(err)

require.NoError(json.Unmarshal(b, output))
require.Equal(input, *output)

// MarshalJson/UnmarshalJson
output = new(ID)
b, err = input.MarshalJSON()
require.NoError(err)

require.NoError(output.UnmarshalJSON(b))
require.Equal(input, *output)
})
}
100 changes: 70 additions & 30 deletions ids/node_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,60 @@
package ids

import (
"bytes"
"errors"
"fmt"
"strings"

"github.com/ava-labs/avalanchego/staking"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/cb58"
"github.com/ava-labs/avalanchego/utils/hashing"
)

const (
NodeIDPrefix = "NodeID-"
NodeIDLen = ShortIDLen

NodeIDLen = 32
)

var (
EmptyNodeID = NodeID{}

errShortNodeID = errors.New("insufficient NodeID length")
ErrBadNodeIDLength = errors.New("bad nodeID length")

_ utils.Sortable[NodeID] = NodeID{}
)

type NodeID ShortID

func (id NodeID) String() string {
return ShortID(id).PrefixedString(NodeIDPrefix)
// NodeID embeds a string, rather than being a type alias for a string
// to be able to use custom marshaller for json encoding.
// See https://github.com/golang/go/blob/go1.20.8/src/encoding/json/encode.go#L1004-L1026
// which checks for the string type first, then checks to see if a custom marshaller exists,
// then checks if any other of the primitive types are provided.
type NodeID struct {
buf string
}
Comment on lines +36 to 38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we should explicitly link or document why type NodeID string doesn't work. (ref)

func (w *reflectWithString) resolve() error {
	if w.k.Kind() == reflect.String {
		w.ks = w.k.String()
		return nil
	}
	if tm, ok := w.k.Interface().(encoding.TextMarshaler); ok {
		if w.k.Kind() == reflect.Pointer && w.k.IsNil() {
			return nil
		}
		buf, err := tm.MarshalText()
		w.ks = string(buf)
		return err
	}
	switch w.k.Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		w.ks = strconv.FormatInt(w.k.Int(), 10)
		return nil
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		w.ks = strconv.FormatUint(w.k.Uint(), 10)
		return nil
	}
	panic("unexpected map key type")
}

For some reason this function checks for the string type first, then checks to see if a custom marshaller exists, then checks if any other of the primitive types are provided.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment with the ref you found.


func (id NodeID) Bytes() []byte {
return id[:]
func (n NodeID) Bytes() []byte {
return []byte(n.buf)
}

func (id NodeID) MarshalJSON() ([]byte, error) {
return []byte(`"` + id.String() + `"`), nil
func (n NodeID) String() string {
// We assume that the maximum size of a byte slice that
// can be stringified is at least the length of an ID
str, _ := cb58.Encode([]byte(n.buf))
return NodeIDPrefix + str
}

func (id NodeID) MarshalText() ([]byte, error) {
return []byte(id.String()), nil
func (n NodeID) MarshalJSON() ([]byte, error) {
return []byte(`"` + n.String() + `"`), nil
}

func (id *NodeID) UnmarshalJSON(b []byte) error {
func (n *NodeID) UnmarshalJSON(b []byte) error {
str := string(b)
if str == nullStr { // If "null", do nothing
return nil
} else if len(str) <= 2+len(NodeIDPrefix) {
}
if len(str) <= 2+len(NodeIDPrefix) {
return fmt.Errorf("%w: expected to be > %d", errShortNodeID, 2+len(NodeIDPrefix))
}

Expand All @@ -58,35 +67,66 @@ func (id *NodeID) UnmarshalJSON(b []byte) error {
}

var err error
*id, err = NodeIDFromString(str[1:lastIndex])
*n, err = NodeIDFromString(str[1:lastIndex])
return err
}

func (id *NodeID) UnmarshalText(text []byte) error {
return id.UnmarshalJSON(text)
func (n NodeID) MarshalText() ([]byte, error) {
return []byte(n.String()), nil
}

func (n *NodeID) UnmarshalText(text []byte) error {
return n.UnmarshalJSON(text)
Comment on lines +74 to +79
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should explicitly document why these are not symmetric

}

func (id NodeID) Compare(other NodeID) int {
return bytes.Compare(id[:], other[:])
func (n NodeID) Compare(other NodeID) int {
return strings.Compare(n.buf, other.buf)
}

// ToNodeID attempt to convert a byte slice into a node id
func ToNodeID(bytes []byte) (NodeID, error) {
nodeID, err := ToShortID(bytes)
return NodeID(nodeID), err
func ToNodeID(src []byte) (NodeID, error) {
switch {
case len(src) == 0:
return EmptyNodeID, nil

Comment on lines +88 to +90
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we can get away with not supporting this parsing here (treating the zero value of NodeID as invalid rather than trying to maintain mappings between the existing EmptyNodeID and the new EmptyNodeID).

case len(src) == ShortIDLen || len(src) == NodeIDLen:
return NodeID{
buf: string(src),
}, nil

default:
return EmptyNodeID, fmt.Errorf("%w: expected %d or %d bytes but got %d", ErrBadNodeIDLength, ShortNodeIDLen, NodeIDLen, len(src))
}
}

func NodeIDFromShortNodeID(nodeID ShortNodeID) NodeID {
if nodeID == EmptyShortNodeID {
return EmptyNodeID
}
return NodeID{
buf: string(nodeID.Bytes()),
}
}

func NodeIDFromCert(cert *staking.Certificate) NodeID {
return hashing.ComputeHash160Array(
hashing.ComputeHash256(cert.Raw),
)
return NodeID{
buf: string(hashing.ComputeHash160(
hashing.ComputeHash256(cert.Raw),
)),
}
}

// NodeIDFromString is the inverse of NodeID.String()
func NodeIDFromString(nodeIDStr string) (NodeID, error) {
asShort, err := ShortFromPrefixedString(nodeIDStr, NodeIDPrefix)
s, found := strings.CutPrefix(nodeIDStr, NodeIDPrefix)
if !found {
return EmptyNodeID, fmt.Errorf("ID: %s is missing the prefix: %s", nodeIDStr, NodeIDPrefix)
}

bytes, err := cb58.Decode(s)
if err != nil {
return NodeID{}, err
return EmptyNodeID, err
}
return NodeID(asShort), nil
return NodeID{
buf: string(bytes),
}, nil
}
Loading