Skip to content

Conversation

@StephenButtolph
Copy link
Contributor

Why this should be merged

These are just minor suggestions that I feel would improve readability. None of these changes should (meaningfully) change any logic.

How this works

Small changes.

How this was tested

CI

// nodes.
func (te *TestEnvironment) GetRandomNodeURI() string {
r := rand.New(rand.NewSource(time.Now().Unix())) //nolint:gosec
r := rand.New(rand.NewSource(time.Now().Unix())) //#nosec G404
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Rather than skipping the whole linter - I think it's better to be explicit on which rule we are silencing.

pChainClient := platformvm.NewClient(nodeURI)
ctx, cancel := context.WithTimeout(context.Background(), e2e.DefaultTimeout)
validatorIDs, err := pChainClient.SampleValidators(ctx, ids.ID{}, 1)
validatorIDs, err := pChainClient.SampleValidators(ctx, constants.PrimaryNetworkID, 1)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Named constants ❤️

// will succeed against a persistent network.
randomShortID, _ := ids.ToShortID(utils.RandomBytes(20))
validatorID := ids.NodeID(randomShortID)
validatorID, err := ids.ToNodeID(utils.RandomBytes(ids.NodeIDLen))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think ideally we should be able to use ids.GenerateTestNodeID(). Generation of IDs should result in different results for different runs imo.

Copy link
Contributor

Choose a reason for hiding this comment

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

I tried using that, but since the ids from is.GenerateTestNodeID() are generated sequentially the same ids will be generated on successive test runs. This was not compatible with a persistent testnet fixture, though if there's a better way to avoid collision I'm open to suggestions.


// Synchronizes access to test data
lock sync.Mutex
TestData
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's a general best practice to put the lock over the fields that it is locking. I don't recall where I read this... But I think it makes sense... It isn't as important here... But in cases where the struct gets larger then it can improve readability.

// Type used to marshal/unmarshal a set of test keys for transmission over http.
type keysDocument struct {
Keys []*secp256k1.PrivateKey
Keys []*secp256k1.PrivateKey `json:"keys"`
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know the marshaller defaults to the name of the variable... But I think explicitly tagging makes it very clear that we are json marshalling this. Also we typically use camelCase for json rather than CamelCase.

}

// Allocate the requested number of keys
remainingKeys := len(s.FundedKeys) - keyCount
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just found the code a lot easier to understand once remainingKeys was a var

if resp.Body != nil {
defer resp.Body.Close()
}
defer resp.Body.Close()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Body is guaranteed to never be nil (and if it wasn't, then the below io.ReadAll(resp.Body) would panic anyway)

Comment on lines +68 to +74
require.ErrorIs(err, tc.expectedError)

addresses := make([]ids.ShortID, len(keys))
for i, key := range keys {
addresses[i] = key.Address()
}
require.Equal(tc.expectedAddresses, addresses)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a bit odd as it enforces that keys is empty if an error is returned... If we want to remove this requirement than I'd prefer adding a check after the require.ErrorIs(err, tc.expectedError) with if err != nil { return }

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think it matters.

@StephenButtolph StephenButtolph requested a review from marun August 14, 2023 23:25
Comment on lines +11 to +19
// ZeroSlice sets all values of the provided slice to the type's zero value.
//
// This can be useful to ensure that the garbage collector doesn't hold
// references to values that are no longer desired.
func ZeroSlice[T any](s []T) {
for i := range s {
s[i] = *new(T)
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if we could've used this in the past (typically we zero out the slice 1 element at a time)... But it seems generally useful.

"testing"

ginkgo "github.com/onsi/ginkgo/v2"

Copy link
Contributor

Choose a reason for hiding this comment

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

So I'm clear, the preference is for separating every 3rd party import package?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah - that's how it's done through the rest of the repo... Really all I want is some super strict linter that we can get behind... but for now consistency is all I can get

@StephenButtolph StephenButtolph merged commit 9c18ba4 into e2e-address-allocation Aug 14, 2023
@StephenButtolph StephenButtolph deleted the e2e-address-allocation-nits branch August 14, 2023 23:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects
Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants