-
Notifications
You must be signed in to change notification settings - Fork 702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Log critical consensus values during health checks #2609
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package common | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ava-labs/avalanchego/ids" | ||
) | ||
|
||
func TestRequestJSONMarshal(t *testing.T) { | ||
requestMap := map[Request]ids.ID{ | ||
{ | ||
NodeID: ids.GenerateTestNodeID(), | ||
RequestID: 12345, | ||
}: ids.GenerateTestID(), | ||
} | ||
_, err := json.Marshal(requestMap) | ||
require.NoError(t, err) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,21 @@ | |
|
||
package bimap | ||
|
||
import "github.com/ava-labs/avalanchego/utils" | ||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
|
||
"github.com/ava-labs/avalanchego/utils" | ||
) | ||
|
||
var ( | ||
_ json.Marshaler = (*BiMap[int, int])(nil) | ||
_ json.Unmarshaler = (*BiMap[int, int])(nil) | ||
|
||
nullBytes = []byte("null") | ||
errNotBijective = errors.New("map not bijective") | ||
) | ||
|
||
type Entry[K, V any] struct { | ||
Key K | ||
|
@@ -100,3 +114,28 @@ func (m *BiMap[K, V]) DeleteValue(val V) (K, bool) { | |
func (m *BiMap[K, V]) Len() int { | ||
return len(m.keyToValue) | ||
} | ||
|
||
func (m *BiMap[K, V]) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(m.keyToValue) | ||
} | ||
|
||
func (m *BiMap[K, V]) UnmarshalJSON(b []byte) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is the custom unmarshaller used? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or is this just moreso ensuring if we add marshal, we should also have unmarshal? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It isn't used. I can remove it if you'd prefer not to have it. Although I felt like implementing json marshal on a normal data structure and not supporting unmarshaling was a bit weird. |
||
if bytes.Equal(b, nullBytes) { | ||
return nil | ||
} | ||
var keyToValue map[K]V | ||
if err := json.Unmarshal(b, &keyToValue); err != nil { | ||
return err | ||
} | ||
valueToKey := make(map[V]K, len(keyToValue)) | ||
for k, v := range keyToValue { | ||
valueToKey[v] = k | ||
} | ||
if len(keyToValue) != len(valueToKey) { | ||
return errNotBijective | ||
} | ||
|
||
m.keyToValue = keyToValue | ||
m.valueToKey = valueToKey | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️