Skip to content

Commit

Permalink
Merge pull request #1645 from oasisprotocol/kostko/feature/cfg-consen…
Browse files Browse the repository at this point in the history
…sus-denom

client-sdk/go: Add consensus denomination config
  • Loading branch information
kostko authored Feb 19, 2024
2 parents 2772a0a + 25750e9 commit 9fb148a
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 8 deletions.
7 changes: 7 additions & 0 deletions client-sdk/go/config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var DefaultNetworks = Networks{
Decimals: 9,
},
},
ConsensusDenomination: NativeDenominationKey,
},

// Emerald on Mainnet.
Expand All @@ -36,6 +37,7 @@ var DefaultNetworks = Networks{
Decimals: 18,
},
},
ConsensusDenomination: NativeDenominationKey,
},

// Sapphire on Mainnet.
Expand All @@ -47,6 +49,7 @@ var DefaultNetworks = Networks{
Decimals: 18,
},
},
ConsensusDenomination: NativeDenominationKey,
},
},
},
Expand All @@ -72,6 +75,7 @@ var DefaultNetworks = Networks{
Decimals: 9,
},
},
ConsensusDenomination: NativeDenominationKey,
},

// Emerald on Testnet.
Expand All @@ -83,6 +87,7 @@ var DefaultNetworks = Networks{
Decimals: 18,
},
},
ConsensusDenomination: NativeDenominationKey,
},

// Sapphire on Testnet.
Expand All @@ -94,6 +99,7 @@ var DefaultNetworks = Networks{
Decimals: 18,
},
},
ConsensusDenomination: NativeDenominationKey,
},

// Pontus-X on Testnet.
Expand All @@ -111,6 +117,7 @@ var DefaultNetworks = Networks{
Decimals: 18,
},
},
ConsensusDenomination: "TEST",
},
},
},
Expand Down
30 changes: 22 additions & 8 deletions client-sdk/go/config/paratime.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,16 @@ func (p *ParaTimes) SetDefault(name string) error {
return nil
}

// ParaTime contains the configuration parameters of a network.
// ParaTime contains the configuration parameters of a ParaTime.
type ParaTime struct {
Description string `mapstructure:"description"`
ID string `mapstructure:"id"`

// Denominations is a map of denominations supported by the ParaTime.
Denominations map[string]*DenominationInfo `mapstructure:"denominations,omitempty"`
// ConsensusDenomination is the denomination that represents the consensus layer denomination.
// If empty, it means that the ParaTime does not support consensus layer transfers.
ConsensusDenomination string `mapstructure:"consensus_denomination,omitempty"`
}

// Validate performs config validation.
Expand All @@ -134,6 +138,13 @@ func (p *ParaTime) Validate() error {
}
}

if p.ConsensusDenomination != "" {
_, exists := p.Denominations[p.ConsensusDenomination]
if !exists {
return fmt.Errorf("invalid consensus denomination '%s'", p.ConsensusDenomination)
}
}

return nil
}

Expand All @@ -153,14 +164,17 @@ func (p *ParaTime) Namespace() common.Namespace {
//
// In case the given denomination does not exist, it provides sane defaults.
func (p *ParaTime) GetDenominationInfo(d string) *DenominationInfo {
var di *DenominationInfo
if len(d) == 0 {
di = p.Denominations[NativeDenominationKey]
} else if di = p.Denominations[d]; di == nil {
di = p.Denominations[strings.ToLower(d)]
var (
di *DenominationInfo
ok bool
)
if d == "" {
d = NativeDenominationKey
}
if di, ok = p.Denominations[d]; ok {
return di
}

if di != nil {
if di, ok = p.Denominations[strings.ToLower(d)]; ok {
return di
}

Expand Down
90 changes: 90 additions & 0 deletions client-sdk/go/config/paratime_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package config

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestValidateParaTime(t *testing.T) {
require := require.New(t)

p := ParaTime{
Description: "Test ParaTime.",
ID: "000000000000000000000000000000000000000000000000f80306c9858e7279",
Denominations: map[string]*DenominationInfo{
NativeDenominationKey: {
Symbol: "FOO",
Decimals: 18,
},
"BAR": {
Symbol: "BARfoo",
Decimals: 9,
},
},
}
err := p.Validate()
require.NoError(err, "Validate should succeed with valid configuration")

p.ConsensusDenomination = NativeDenominationKey
err = p.Validate()
require.NoError(err, "Validate should succeed with valid consensus denomination")
p.ConsensusDenomination = "BAR"
err = p.Validate()
require.NoError(err, "Validate should succeed with valid consensus denomination")

invalid := p
invalid.ID = "invalid"
err = invalid.Validate()
require.Error(err, "Validate should fail with invalid ID")

invalid = p
invalid.ConsensusDenomination = "invalid"
err = invalid.Validate()
require.Error(err, "Validate should fail with invalid consensus denomination")
}

func TestDenominationInfo(t *testing.T) {
require := require.New(t)

p := ParaTime{
Description: "Test ParaTime.",
ID: "000000000000000000000000000000000000000000000000f80306c9858e7279",
Denominations: map[string]*DenominationInfo{
NativeDenominationKey: {
Symbol: "FOO",
Decimals: 18,
},
"BAR": {
Symbol: "BARfoo",
Decimals: 9,
},
"low": {
Symbol: "LOWfoo",
Decimals: 9,
},
},
}
err := p.Validate()
require.NoError(err, "Validate should succeed with valid configuration")

di := p.GetDenominationInfo("")
require.NotNil(di, "GetDenominationInfo should return a non-nil denomination info")
require.Equal(di.Symbol, "FOO")
require.EqualValues(di.Decimals, 18)

di = p.GetDenominationInfo("BAR")
require.NotNil(di, "GetDenominationInfo should return a non-nil denomination info")
require.Equal(di.Symbol, "BARfoo")
require.EqualValues(di.Decimals, 9)

di = p.GetDenominationInfo("LOW")
require.NotNil(di, "GetDenominationInfo should return a non-nil denomination info")
require.Equal(di.Symbol, "LOWfoo")
require.EqualValues(di.Decimals, 9)

di = p.GetDenominationInfo("DEFAULT")
require.NotNil(di, "GetDenominationInfo should return a non-nil denomination info")
require.Equal(di.Symbol, "DEFAULT")
require.EqualValues(di.Decimals, 9)
}

0 comments on commit 9fb148a

Please sign in to comment.