From 477703cda0b41de310d6a7ce897f17906325f1da Mon Sep 17 00:00:00 2001 From: Matthew Lam Date: Fri, 25 Aug 2023 17:12:09 +0000 Subject: [PATCH] deleting extra file and add missing pkg --- go.mod | 2 +- relayer/canonical_validator_client.go | 43 ---------------------- temp/README.md | 1 + temp/temp.go | 51 +++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 44 deletions(-) delete mode 100644 relayer/canonical_validator_client.go create mode 100644 temp/README.md create mode 100644 temp/temp.go diff --git a/go.mod b/go.mod index bf1a013c..51be6a68 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/spf13/viper v1.16.0 github.com/stretchr/testify v1.8.4 go.uber.org/zap v1.25.0 + golang.org/x/exp v0.0.0-20230206171751-46f607a40771 ) require ( @@ -27,7 +28,6 @@ require ( github.com/spf13/cast v1.5.1 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/subosito/gotenv v1.4.2 // indirect - golang.org/x/exp v0.0.0-20230206171751-46f607a40771 // indirect gopkg.in/ini.v1 v1.67.0 // indirect ) diff --git a/relayer/canonical_validator_client.go b/relayer/canonical_validator_client.go deleted file mode 100644 index 5a2e369d..00000000 --- a/relayer/canonical_validator_client.go +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright (C) 2023, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package relayer - -import ( - "context" - - "github.com/ava-labs/avalanchego/ids" - "github.com/ava-labs/avalanchego/snow/validators" - "github.com/ava-labs/avalanchego/vms/platformvm" -) - -// CanonicalValidatorClient wraps platformvm.Client and implements validators.State -type CanonicalValidatorClient struct { - client platformvm.Client -} - -func NewCanonicalValidatorClient(client platformvm.Client) *CanonicalValidatorClient { - return &CanonicalValidatorClient{ - client: client, - } -} - -func (v *CanonicalValidatorClient) GetMinimumHeight(ctx context.Context) (uint64, error) { - return v.client.GetHeight(ctx) -} - -func (v *CanonicalValidatorClient) GetCurrentHeight(ctx context.Context) (uint64, error) { - return v.client.GetHeight(ctx) -} - -func (v *CanonicalValidatorClient) GetSubnetID(ctx context.Context, chainID ids.ID) (ids.ID, error) { - return v.client.ValidatedBy(ctx, chainID) -} - -func (v *CanonicalValidatorClient) GetValidatorSet( - ctx context.Context, - height uint64, - subnetID ids.ID, -) (map[ids.NodeID]*validators.GetValidatorOutput, error) { - return v.client.GetValidatorsAt(ctx, subnetID, height) -} diff --git a/temp/README.md b/temp/README.md new file mode 100644 index 00000000..3d658cec --- /dev/null +++ b/temp/README.md @@ -0,0 +1 @@ +This package contains functions and types that are implemented in various dev branches. They are copied here to aid in concurrent development, and to keep this repo's dependencies limited to tagged releases \ No newline at end of file diff --git a/temp/temp.go b/temp/temp.go new file mode 100644 index 00000000..9c378bb8 --- /dev/null +++ b/temp/temp.go @@ -0,0 +1,51 @@ +// (c) 2023, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package temp + +import ( + "fmt" + + "github.com/ava-labs/avalanchego/utils" + "github.com/ava-labs/avalanchego/utils/math" + "github.com/ava-labs/avalanchego/vms/platformvm" + "github.com/ava-labs/avalanchego/vms/platformvm/warp" + "golang.org/x/exp/maps" +) + +// Keep this function here until we're able to get a validators.State object from the P-Chain API. +// Then, we can call warp.GetCanonicalValidatorSet +func GetCanonicalValidatorSet(vdrSet []platformvm.ClientPermissionlessValidator) ([]*warp.Validator, uint64, error) { + var ( + vdrs = make(map[string]*warp.Validator, len(vdrSet)) + totalWeight uint64 + err error + ) + for _, vdr := range vdrSet { + totalWeight, err = math.Add64(totalWeight, vdr.Weight) + if err != nil { + return nil, 0, fmt.Errorf("%w: %s", warp.ErrWeightOverflow, err) + } + pk := vdr.Signer.Key() + if pk == nil { + continue + } + pkBytes := pk.Serialize() + uniqueVdr, ok := vdrs[string(pkBytes)] + if !ok { + uniqueVdr = &warp.Validator{ + PublicKey: pk, + PublicKeyBytes: pkBytes, + } + vdrs[string(pkBytes)] = uniqueVdr + } + uniqueVdr.Weight += vdr.Weight // Impossible to overflow here + uniqueVdr.NodeIDs = append(uniqueVdr.NodeIDs, vdr.NodeID) + + } + + // Sort validators by public key + vdrList := maps.Values(vdrs) + utils.Sort(vdrList) + return vdrList, totalWeight, nil +}