forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
subnet.go
36 lines (30 loc) · 1.02 KB
/
subnet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package verify
import (
"context"
"errors"
"fmt"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/snow"
)
var (
ErrSameChainID = errors.New("same chainID")
ErrMismatchedSubnetIDs = errors.New("mismatched subnetIDs")
)
// SameSubnet verifies that the provided [ctx] was provided to a chain in the
// same subnet as [peerChainID], but not the same chain. If this verification
// fails, a non-nil error will be returned.
func SameSubnet(ctx context.Context, chainCtx *snow.Context, peerChainID ids.ID) error {
if peerChainID == chainCtx.ChainID {
return ErrSameChainID
}
subnetID, err := chainCtx.ValidatorState.GetSubnetID(ctx, peerChainID)
if err != nil {
return fmt.Errorf("failed to get subnet of %q: %w", peerChainID, err)
}
if chainCtx.SubnetID != subnetID {
return fmt.Errorf("%w; expected %q got %q", ErrMismatchedSubnetIDs, chainCtx.SubnetID, subnetID)
}
return nil
}