Skip to content

Commit

Permalink
Merge pull request #92 from ava-labs/gstuart/chainID-to-blockchainID
Browse files Browse the repository at this point in the history
rename chainID to blockchainID
  • Loading branch information
geoff-vball authored Dec 1, 2023
2 parents 2d31d5e + fdcf1e0 commit ffccd45
Show file tree
Hide file tree
Showing 25 changed files with 334 additions and 334 deletions.
64 changes: 32 additions & 32 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type MessageProtocolConfig struct {
}
type SourceSubnet struct {
SubnetID string `mapstructure:"subnet-id" json:"subnet-id"`
ChainID string `mapstructure:"chain-id" json:"chain-id"`
BlockchainID string `mapstructure:"blockchain-id" json:"blockchain-id"`
VM string `mapstructure:"vm" json:"vm"`
APINodeHost string `mapstructure:"api-node-host" json:"api-node-host"`
APINodePort uint32 `mapstructure:"api-node-port" json:"api-node-port"`
Expand All @@ -54,7 +54,7 @@ type SourceSubnet struct {

type DestinationSubnet struct {
SubnetID string `mapstructure:"subnet-id" json:"subnet-id"`
ChainID string `mapstructure:"chain-id" json:"chain-id"`
BlockchainID string `mapstructure:"blockchain-id" json:"blockchain-id"`
VM string `mapstructure:"vm" json:"vm"`
APINodeHost string `mapstructure:"api-node-host" json:"api-node-host"`
APINodePort uint32 `mapstructure:"api-node-port" json:"api-node-port"`
Expand All @@ -74,8 +74,8 @@ type Config struct {
ProcessMissedBlocks bool `mapstructure:"process-missed-blocks" json:"process-missed-blocks"`

// convenience fields to access the source subnet and chain IDs after initialization
sourceSubnetIDs []ids.ID
sourceChainIDs []ids.ID
sourceSubnetIDs []ids.ID
sourceBlockchainIDs []ids.ID
}

func SetDefaultConfigValues(v *viper.Viper) {
Expand Down Expand Up @@ -135,7 +135,7 @@ func BuildConfig(v *viper.Viper) (Config, bool, error) {
// Otherwise, check for private keys suffixed with the chain ID and set it for that subnet
// Since the key is dynamic, this is only possible through environment variables
for i, subnet := range cfg.DestinationSubnets {
subnetAccountPrivateKey := os.Getenv(fmt.Sprintf("%s_%s", accountPrivateKeyEnvVarName, subnet.ChainID))
subnetAccountPrivateKey := os.Getenv(fmt.Sprintf("%s_%s", accountPrivateKeyEnvVarName, subnet.BlockchainID))
if subnetAccountPrivateKey != "" {
optionOverwritten = true
cfg.DestinationSubnets[i].AccountPrivateKey = utils.SanitizeHexString(subnetAccountPrivateKey)
Expand Down Expand Up @@ -182,26 +182,26 @@ func (c *Config) Validate() error {
if err := s.Validate(); err != nil {
return err
}
if destinationChains.Contains(s.ChainID) {
if destinationChains.Contains(s.BlockchainID) {
return fmt.Errorf("configured destination subnets must have unique chain IDs")
}
destinationChains.Add(s.ChainID)
destinationChains.Add(s.BlockchainID)
}

// Validate the source chains and store the source subnet and chain IDs for future use
sourceChains := set.NewSet[string](len(c.SourceSubnets))
sourceBlockchains := set.NewSet[string](len(c.SourceSubnets))
var sourceSubnetIDs []ids.ID
var sourceChainIDs []ids.ID
var sourceBlockchainIDs []ids.ID
for _, s := range c.SourceSubnets {
// Validate configuration
if err := s.Validate(&destinationChains); err != nil {
return err
}
// Verify uniqueness
if sourceChains.Contains(s.ChainID) {
if sourceBlockchains.Contains(s.BlockchainID) {
return fmt.Errorf("configured source subnets must have unique chain IDs")
}
sourceChains.Add(s.ChainID)
sourceBlockchains.Add(s.BlockchainID)

// Save IDs for future use
subnetID, err := ids.FromString(s.SubnetID)
Expand All @@ -210,15 +210,15 @@ func (c *Config) Validate() error {
}
sourceSubnetIDs = append(sourceSubnetIDs, subnetID)

chainID, err := ids.FromString(s.ChainID)
blockchainID, err := ids.FromString(s.BlockchainID)
if err != nil {
return fmt.Errorf("invalid subnetID in configuration. error: %v", err)
}
sourceChainIDs = append(sourceChainIDs, chainID)
sourceBlockchainIDs = append(sourceBlockchainIDs, blockchainID)
}

c.sourceSubnetIDs = sourceSubnetIDs
c.sourceChainIDs = sourceChainIDs
c.sourceBlockchainIDs = sourceBlockchainIDs

return nil
}
Expand All @@ -227,13 +227,13 @@ func (s *SourceSubnet) GetSupportedDestinations() set.Set[ids.ID] {
return s.supportedDestinations
}

// Validates the source subnet configuration, including verifying that the supported destinations are present in destinationChainIDs
func (s *SourceSubnet) Validate(destinationChainIDs *set.Set[string]) error {
// Validates the source subnet configuration, including verifying that the supported destinations are present in destinationBlockchainIDs
func (s *SourceSubnet) Validate(destinationBlockchainIDs *set.Set[string]) error {
if _, err := ids.FromString(s.SubnetID); err != nil {
return fmt.Errorf("invalid subnetID in source subnet configuration. Provided ID: %s", s.SubnetID)
}
if _, err := ids.FromString(s.ChainID); err != nil {
return fmt.Errorf("invalid chainID in source subnet configuration. Provided ID: %s", s.ChainID)
if _, err := ids.FromString(s.BlockchainID); err != nil {
return fmt.Errorf("invalid blockchainID in source subnet configuration. Provided ID: %s", s.BlockchainID)
}
if _, err := url.ParseRequestURI(s.GetNodeWSEndpoint()); err != nil {
return fmt.Errorf("invalid relayer subscribe URL in source subnet configuration: %v", err)
Expand Down Expand Up @@ -267,9 +267,9 @@ func (s *SourceSubnet) Validate(destinationChainIDs *set.Set[string]) error {
for _, blockchainIDStr := range s.SupportedDestinations {
blockchainID, err := ids.FromString(blockchainIDStr)
if err != nil {
return fmt.Errorf("invalid chainID in configuration. error: %v", err)
return fmt.Errorf("invalid blockchainID in configuration. error: %v", err)
}
if !destinationChainIDs.Contains(blockchainIDStr) {
if !destinationBlockchainIDs.Contains(blockchainIDStr) {
return fmt.Errorf("configured source subnet %s has a supported destination blockchain ID %s that is not configured as a destination blockchain",
s.SubnetID,
blockchainID)
Expand All @@ -284,8 +284,8 @@ func (s *DestinationSubnet) Validate() error {
if _, err := ids.FromString(s.SubnetID); err != nil {
return fmt.Errorf("invalid subnetID in source subnet configuration. Provided ID: %s", s.SubnetID)
}
if _, err := ids.FromString(s.ChainID); err != nil {
return fmt.Errorf("invalid chainID in source subnet configuration. Provided ID: %s", s.ChainID)
if _, err := ids.FromString(s.BlockchainID); err != nil {
return fmt.Errorf("invalid blockchainID in source subnet configuration. Provided ID: %s", s.BlockchainID)
}
if _, err := url.ParseRequestURI(s.GetNodeRPCEndpoint()); err != nil {
return fmt.Errorf("invalid relayer broadcast URL: %v", err)
Expand All @@ -308,7 +308,7 @@ func (s *DestinationSubnet) Validate() error {
return nil
}

func constructURL(protocol string, host string, port uint32, encrypt bool, chainIDStr string, subnetIDStr string) string {
func constructURL(protocol string, host string, port uint32, encrypt bool, blockchainIDStr string, subnetIDStr string) string {
var protocolPathMap = map[string]string{
"http": "rpc",
"ws": "ws",
Expand All @@ -324,15 +324,15 @@ func constructURL(protocol string, host string, port uint32, encrypt bool, chain
}
subnetID, _ := ids.FromString(subnetIDStr) // already validated in Validate()
if subnetID == constants.PrimaryNetworkID {
chainIDStr = cChainIdentifierString
blockchainIDStr = cChainIdentifierString
}
return fmt.Sprintf("%s://%s%s/ext/bc/%s/%s", protocol, host, portStr, chainIDStr, path)
return fmt.Sprintf("%s://%s%s/ext/bc/%s/%s", protocol, host, portStr, blockchainIDStr, path)
}

// Constructs an RPC endpoint for the subnet.
// If the RPCEndpoint field is set in the configuration, returns that directly.
// Otherwise, constructs the endpoint from the APINodeHost, APINodePort, and EncryptConnection fields,
// following the /ext/bc/{chainID}/rpc format.
// following the /ext/bc/{blockchainID}/rpc format.
func (s *DestinationSubnet) GetNodeRPCEndpoint() string {
if s.RPCEndpoint != "" {
return s.RPCEndpoint
Expand All @@ -344,7 +344,7 @@ func (s *DestinationSubnet) GetNodeRPCEndpoint() string {
s.APINodeHost,
s.APINodePort,
s.EncryptConnection,
s.ChainID,
s.BlockchainID,
s.SubnetID,
)
return s.RPCEndpoint
Expand All @@ -353,7 +353,7 @@ func (s *DestinationSubnet) GetNodeRPCEndpoint() string {
// Constructs an RPC endpoint for the subnet.
// If the RPCEndpoint field is set in the configuration, returns that directly.
// Otherwise, constructs the endpoint from the APINodeHost, APINodePort, and EncryptConnection fields,
// following the /ext/bc/{chainID}/rpc format.
// following the /ext/bc/{blockchainID}/rpc format.
func (s *SourceSubnet) GetNodeRPCEndpoint() string {
if s.RPCEndpoint != "" {
return s.RPCEndpoint
Expand All @@ -365,7 +365,7 @@ func (s *SourceSubnet) GetNodeRPCEndpoint() string {
s.APINodeHost,
s.APINodePort,
s.EncryptConnection,
s.ChainID,
s.BlockchainID,
s.SubnetID,
)
return s.RPCEndpoint
Expand All @@ -374,7 +374,7 @@ func (s *SourceSubnet) GetNodeRPCEndpoint() string {
// Constructs a WS endpoint for the subnet.
// If the WSEndpoint field is set in the configuration, returns that directly.
// Otherwise, constructs the endpoint from the APINodeHost, APINodePort, and EncryptConnection fields,
// following the /ext/bc/{chainID}/ws format.
// following the /ext/bc/{blockchainID}/ws format.
func (s *SourceSubnet) GetNodeWSEndpoint() string {
if s.WSEndpoint != "" {
return s.WSEndpoint
Expand All @@ -386,7 +386,7 @@ func (s *SourceSubnet) GetNodeWSEndpoint() string {
s.APINodeHost,
s.APINodePort,
s.EncryptConnection,
s.ChainID,
s.BlockchainID,
s.SubnetID,
)
return s.WSEndpoint
Expand All @@ -413,5 +413,5 @@ func (s *DestinationSubnet) GetRelayerAccountInfo() (*ecdsa.PrivateKey, common.A

// GetSourceIDs returns the Subnet and Chain IDs of all subnets configured as a source
func (c *Config) GetSourceIDs() ([]ids.ID, []ids.ID) {
return c.sourceSubnetIDs, c.sourceChainIDs
return c.sourceSubnetIDs, c.sourceBlockchainIDs
}
60 changes: 30 additions & 30 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (
)

var (
testSubnetID string = "2TGBXcnwx5PqiXWiqxAKUaNSqDguXNh1mxnp82jui68hxJSZAx"
testChainID string = "S4mMqUXe7vHsGiRAma6bv3CKnyaLssyAxmQ2KvFpX1KEvfFCD"
testChainID2 string = "291etJW5EpagFY94v1JraFy8vLFYXcCnWKJ6Yz9vrjfPjCF4QL"
testAddress string = "0xd81545385803bCD83bd59f58Ba2d2c0562387F83"
primarySubnetID string = "11111111111111111111111111111111LpoYY"
testValidConfig = Config{
testSubnetID string = "2TGBXcnwx5PqiXWiqxAKUaNSqDguXNh1mxnp82jui68hxJSZAx"
testBlockchainID string = "S4mMqUXe7vHsGiRAma6bv3CKnyaLssyAxmQ2KvFpX1KEvfFCD"
testBlockchainID2 string = "291etJW5EpagFY94v1JraFy8vLFYXcCnWKJ6Yz9vrjfPjCF4QL"
testAddress string = "0xd81545385803bCD83bd59f58Ba2d2c0562387F83"
primarySubnetID string = "11111111111111111111111111111111LpoYY"
testValidConfig = Config{
LogLevel: "info",
NetworkID: 1337,
PChainAPIURL: "http://test.avax.network",
Expand All @@ -32,7 +32,7 @@ var (
{
APINodeHost: "http://test.avax.network",
APINodePort: 0,
ChainID: testChainID,
BlockchainID: testBlockchainID,
SubnetID: testSubnetID,
VM: "evm",
EncryptConnection: false,
Expand All @@ -47,7 +47,7 @@ var (
{
APINodeHost: "http://test.avax.network",
APINodePort: 0,
ChainID: testChainID,
BlockchainID: testBlockchainID,
SubnetID: testSubnetID,
VM: "evm",
EncryptConnection: false,
Expand All @@ -71,40 +71,40 @@ func TestGetDestinationRPCEndpoint(t *testing.T) {
EncryptConnection: false,
APINodeHost: "127.0.0.1",
APINodePort: 9650,
ChainID: testChainID,
BlockchainID: testBlockchainID,
SubnetID: testSubnetID,
},
expectedResult: fmt.Sprintf("http://127.0.0.1:9650/ext/bc/%s/rpc", testChainID),
expectedResult: fmt.Sprintf("http://127.0.0.1:9650/ext/bc/%s/rpc", testBlockchainID),
},
{
name: "Encrypt connection",
s: DestinationSubnet{
EncryptConnection: true,
APINodeHost: "127.0.0.1",
APINodePort: 9650,
ChainID: testChainID,
BlockchainID: testBlockchainID,
SubnetID: testSubnetID,
},
expectedResult: fmt.Sprintf("https://127.0.0.1:9650/ext/bc/%s/rpc", testChainID),
expectedResult: fmt.Sprintf("https://127.0.0.1:9650/ext/bc/%s/rpc", testBlockchainID),
},
{
name: "No port",
s: DestinationSubnet{
EncryptConnection: false,
APINodeHost: "api.avax.network",
APINodePort: 0,
ChainID: testChainID,
BlockchainID: testBlockchainID,
SubnetID: testSubnetID,
},
expectedResult: fmt.Sprintf("http://api.avax.network/ext/bc/%s/rpc", testChainID),
expectedResult: fmt.Sprintf("http://api.avax.network/ext/bc/%s/rpc", testBlockchainID),
},
{
name: "Primary subnet",
s: DestinationSubnet{
EncryptConnection: false,
APINodeHost: "127.0.0.1",
APINodePort: 9650,
ChainID: testChainID,
BlockchainID: testBlockchainID,
SubnetID: primarySubnetID,
},
expectedResult: "http://127.0.0.1:9650/ext/bc/C/rpc",
Expand All @@ -115,7 +115,7 @@ func TestGetDestinationRPCEndpoint(t *testing.T) {
EncryptConnection: false,
APINodeHost: "127.0.0.1",
APINodePort: 9650,
ChainID: testChainID,
BlockchainID: testBlockchainID,
SubnetID: testSubnetID,
RPCEndpoint: "https://subnets.avax.network/mysubnet/rpc", // overrides all other settings used to construct the endpoint
},
Expand Down Expand Up @@ -144,43 +144,43 @@ func TestGetSourceSubnetEndpoints(t *testing.T) {
EncryptConnection: false,
APINodeHost: "127.0.0.1",
APINodePort: 9650,
ChainID: testChainID,
BlockchainID: testBlockchainID,
SubnetID: testSubnetID,
},
expectedWsResult: fmt.Sprintf("ws://127.0.0.1:9650/ext/bc/%s/ws", testChainID),
expectedRpcResult: fmt.Sprintf("http://127.0.0.1:9650/ext/bc/%s/rpc", testChainID),
expectedWsResult: fmt.Sprintf("ws://127.0.0.1:9650/ext/bc/%s/ws", testBlockchainID),
expectedRpcResult: fmt.Sprintf("http://127.0.0.1:9650/ext/bc/%s/rpc", testBlockchainID),
},
{
name: "Encrypt connection",
s: SourceSubnet{
EncryptConnection: true,
APINodeHost: "127.0.0.1",
APINodePort: 9650,
ChainID: testChainID,
BlockchainID: testBlockchainID,
SubnetID: testSubnetID,
},
expectedWsResult: fmt.Sprintf("wss://127.0.0.1:9650/ext/bc/%s/ws", testChainID),
expectedRpcResult: fmt.Sprintf("https://127.0.0.1:9650/ext/bc/%s/rpc", testChainID),
expectedWsResult: fmt.Sprintf("wss://127.0.0.1:9650/ext/bc/%s/ws", testBlockchainID),
expectedRpcResult: fmt.Sprintf("https://127.0.0.1:9650/ext/bc/%s/rpc", testBlockchainID),
},
{
name: "No port",
s: SourceSubnet{
EncryptConnection: false,
APINodeHost: "api.avax.network",
APINodePort: 0,
ChainID: testChainID,
BlockchainID: testBlockchainID,
SubnetID: testSubnetID,
},
expectedWsResult: fmt.Sprintf("ws://api.avax.network/ext/bc/%s/ws", testChainID),
expectedRpcResult: fmt.Sprintf("http://api.avax.network/ext/bc/%s/rpc", testChainID),
expectedWsResult: fmt.Sprintf("ws://api.avax.network/ext/bc/%s/ws", testBlockchainID),
expectedRpcResult: fmt.Sprintf("http://api.avax.network/ext/bc/%s/rpc", testBlockchainID),
},
{
name: "Primary subnet",
s: SourceSubnet{
EncryptConnection: false,
APINodeHost: "127.0.0.1",
APINodePort: 9650,
ChainID: testChainID,
BlockchainID: testBlockchainID,
SubnetID: primarySubnetID,
},
expectedWsResult: "ws://127.0.0.1:9650/ext/bc/C/ws",
Expand All @@ -192,7 +192,7 @@ func TestGetSourceSubnetEndpoints(t *testing.T) {
EncryptConnection: false,
APINodeHost: "127.0.0.1",
APINodePort: 9650,
ChainID: testChainID,
BlockchainID: testBlockchainID,
SubnetID: testSubnetID,
WSEndpoint: "wss://subnets.avax.network/mysubnet/ws", // overrides all other settings used to construct the endpoint
RPCEndpoint: "https://subnets.avax.network/mysubnet/rpc", // overrides all other settings used to construct the endpoint
Expand Down Expand Up @@ -335,14 +335,14 @@ func TestGetRelayerAccountPrivateKey_set_pk_with_subnet_env(t *testing.T) {
configModifier: func(c Config) Config {
// Add a second destination subnet. This PK should NOT be overwritten
newSubnet := c.DestinationSubnets[0]
newSubnet.ChainID = testChainID2
newSubnet.BlockchainID = testBlockchainID2
newSubnet.AccountPrivateKey = testPk1
c.DestinationSubnets = append(c.DestinationSubnets, newSubnet)
return c
},
envSetter: func() {
// Overwrite the PK for the first subnet using an env var
varName := fmt.Sprintf("%s_%s", accountPrivateKeyEnvVarName, testValidConfig.DestinationSubnets[0].ChainID)
varName := fmt.Sprintf("%s_%s", accountPrivateKeyEnvVarName, testValidConfig.DestinationSubnets[0].BlockchainID)
t.Setenv(varName, testPk2)
},
expectedOverwritten: true,
Expand All @@ -367,7 +367,7 @@ func TestGetRelayerAccountPrivateKey_set_pk_with_global_env(t *testing.T) {
configModifier: func(c Config) Config {
// Add a second destination subnet. This PK SHOULD be overwritten
newSubnet := c.DestinationSubnets[0]
newSubnet.ChainID = testChainID2
newSubnet.BlockchainID = testBlockchainID2
newSubnet.AccountPrivateKey = testPk1
c.DestinationSubnets = append(c.DestinationSubnets, newSubnet)
return c
Expand Down
Loading

0 comments on commit ffccd45

Please sign in to comment.