Skip to content

Commit

Permalink
node: make sure that hrp is configurable (#4662)
Browse files Browse the repository at this point in the history
  • Loading branch information
dshulyak committed Jul 8, 2023
1 parent 74b167e commit db1542d
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 33 deletions.
2 changes: 1 addition & 1 deletion api/grpcserver/smesher_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestStartSmeshingPassesCorrectSmeshingOpts(t *testing.T) {
smeshingProvider := activation.NewMockSmeshingProvider(ctrl)
svc := grpcserver.NewSmesherService(postSetupProvider, smeshingProvider, time.Second, activation.DefaultPostSetupOpts())

types.DefaultTestAddressConfig()
types.SetNetworkHRP("stest")
addr, err := types.StringToAddress("stest1qqqqqqrs60l66w5uksxzmaznwq6xnhqfv56c28qlkm4a5")
require.NoError(t, err)
smeshingProvider.EXPECT().StartSmeshing(addr, activation.PostSetupOpts{
Expand Down
31 changes: 10 additions & 21 deletions common/types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,15 @@ var (
)

// Config is the configuration of the address package.
type Config struct {
NetworkHRP string `mapstructure:"network-hrp"`
}
var networkHrp = "sm"

var conf = &Config{
NetworkHRP: "sm",
func SetNetworkHRP(update string) {
networkHrp = update
log.With().Info("network hrp updated", log.String("hrp", update))
}

// DefaultAddressConfig returns the default configuration of the address package.
func DefaultAddressConfig() *Config {
return conf
}

// DefaultTestAddressConfig returns the default test configuration of the address package.
func DefaultTestAddressConfig() *Config {
conf = &Config{
NetworkHRP: "stest",
}
return conf
func NetworkHRP() string {
return networkHrp
}

// Address represents the address of a spacemesh account with AddressLength length.
Expand All @@ -71,9 +61,8 @@ func StringToAddress(src string) (Address, error) {
if len(dataConverted) != AddressLength+1 {
return addr, fmt.Errorf("expected %d bytes, got %d: %w", AddressLength, len(data), ErrWrongAddressLength)
}

if conf.NetworkHRP != hrp {
return addr, fmt.Errorf("wrong network id: expected `%s`, got `%s`: %w", conf.NetworkHRP, hrp, ErrUnsupportedNetwork)
if networkHrp != hrp {
return addr, fmt.Errorf("wrong network id: expected `%s`, got `%s`: %w", NetworkHRP(), hrp, ErrUnsupportedNetwork)
}
// check that first 4 bytes are 0.
for i := 0; i < AddressReservedSpace; i++ {
Expand Down Expand Up @@ -106,7 +95,7 @@ func (a Address) String() string {
log.Panic("error converting bech32 bits: ", err.Error())
}

result, err := bech32.Encode(conf.NetworkHRP, dataConverted)
result, err := bech32.Encode(NetworkHRP(), dataConverted)
if err != nil {
log.Panic("error encoding to bech32: ", err.Error())
}
Expand Down Expand Up @@ -146,5 +135,5 @@ func GenerateAddress(publicKey []byte) Address {

// GetHRPNetwork returns the Human-Readable-Part of bech32 addresses for a networkID.
func (a Address) GetHRPNetwork() string {
return conf.NetworkHRP
return NetworkHRP()
}
2 changes: 1 addition & 1 deletion common/types/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func init() {
types.DefaultTestAddressConfig()
types.SetNetworkHRP("stest")
}

func TestAddress_NewAddress(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/spacemeshos/go-spacemesh/beacon"
"github.com/spacemeshos/go-spacemesh/bootstrap"
"github.com/spacemeshos/go-spacemesh/checkpoint"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/fetch"
vm "github.com/spacemeshos/go-spacemesh/genvm"
hareConfig "github.com/spacemeshos/go-spacemesh/hare/config"
Expand Down Expand Up @@ -44,7 +43,6 @@ func init() {
// Config defines the top level configuration for a spacemesh node.
type Config struct {
BaseConfig `mapstructure:"main"`
Address *types.Config `mapstructure:"address"`
Genesis *GenesisConfig `mapstructure:"genesis"`
Tortoise tortoise.Config `mapstructure:"tortoise"`
P2P p2p.Config `mapstructure:"p2p"`
Expand Down Expand Up @@ -117,6 +115,8 @@ type BaseConfig struct {

DatabaseConnections int `mapstructure:"db-connections"`
DatabaseLatencyMetering bool `mapstructure:"db-latency-metering"`

NetworkHRP string `mapstructure:"network-hrp"`
}

// SmeshingConfig defines configuration for the node's smeshing (mining).
Expand All @@ -131,7 +131,6 @@ type SmeshingConfig struct {
// DefaultConfig returns the default configuration for a spacemesh node.
func DefaultConfig() Config {
return Config{
Address: types.DefaultAddressConfig(),
BaseConfig: defaultBaseConfig(),
Genesis: DefaultGenesisConfig(),
Tortoise: tortoise.DefaultConfig(),
Expand Down Expand Up @@ -159,7 +158,6 @@ func DefaultTestConfig() Config {
conf.BaseConfig = defaultTestConfig()
conf.P2P = p2p.DefaultConfig()
conf.API = grpcserver.DefaultTestConfig()
conf.Address = types.DefaultTestAddressConfig()
return conf
}

Expand All @@ -184,6 +182,7 @@ func defaultBaseConfig() BaseConfig {
OptFilterThreshold: 90,
TickSize: 100,
DatabaseConnections: 16,
NetworkHRP: "sm",
}
}

Expand All @@ -201,6 +200,7 @@ func DefaultSmeshingConfig() SmeshingConfig {
func defaultTestConfig() BaseConfig {
conf := defaultBaseConfig()
conf.MetricsPort += 10000
conf.NetworkHRP = "stest"
return conf
}

Expand Down
3 changes: 2 additions & 1 deletion config/presets/fastnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ func init() {

func fastnet() config.Config {
conf := config.DefaultConfig()
conf.Address = types.DefaultTestAddressConfig()

conf.NetworkHRP = "stest"
types.SetNetworkHRP(conf.NetworkHRP) // set to generate coinbase
conf.BaseConfig.OptFilterThreshold = 90

conf.HARE.N = 800
Expand Down
3 changes: 2 additions & 1 deletion config/presets/standalone.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func init() {

func standalone() config.Config {
conf := config.DefaultConfig()
conf.Address = types.DefaultTestAddressConfig()
conf.NetworkHRP = "standalone"
types.SetNetworkHRP(conf.NetworkHRP) // set to generate coinbase

conf.TIME.Peersync.Disable = true
conf.Standalone = true
Expand Down
4 changes: 3 additions & 1 deletion config/presets/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ func init() {

func testnet() config.Config {
conf := config.DefaultConfig()
conf.Address = types.DefaultTestAddressConfig()

conf.NetworkHRP = "stest"
types.SetNetworkHRP(conf.NetworkHRP) // set to generate coinbase

conf.HARE.N = 800
conf.HARE.ExpectedLeaders = 10
Expand Down
2 changes: 1 addition & 1 deletion genvm/cmd/vesting/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func main() {
VestingEnd: types.LayerID(uint32(*end)),
}
vaultAddress := core.ComputePrincipal(vault.TemplateAddress, vaultArgs)
types.DefaultAddressConfig().NetworkHRP = *hrp
types.SetNetworkHRP(*hrp)
fmt.Printf("vesting: %s\nvault: %s\n", vestingAddress.String(), vaultAddress.String())
fmt.Println("public keys:")
for i, key := range vestingArgs.PublicKeys {
Expand Down
2 changes: 1 addition & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ func GetCommand() *cobra.Command {

run := func(ctx context.Context) error {
types.SetLayersPerEpoch(app.Config.LayersPerEpoch)

// ensure all data folders exist
if err := os.MkdirAll(app.Config.DataDir(), 0o700); err != nil {
return fmt.Errorf("ensure folders exist: %w", err)
Expand Down Expand Up @@ -295,6 +294,7 @@ func New(opts ...Option) *App {
}
lvl := zap.NewAtomicLevelAt(zap.InfoLevel)
log.SetupGlobal(app.log.SetLevel(&lvl))
types.SetNetworkHRP(app.Config.NetworkHRP)
return app
}

Expand Down
16 changes: 16 additions & 0 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,22 @@ func TestConfig_GenesisAccounts(t *testing.T) {
})
}

func TestHRP(t *testing.T) {
c := &cobra.Command{}
cmd.AddCommands(c)
t.Cleanup(cmd.ResetConfig)

data := `{"main": {"network-hrp": "TEST"}}`
cfg := filepath.Join(t.TempDir(), "config.json")
require.NoError(t, os.WriteFile(cfg, []byte(data), 0o600))
require.NoError(t, c.ParseFlags([]string{"-c=" + cfg}))
conf, err := loadConfig(c)
require.NoError(t, err)
app := New(WithConfig(conf))
require.NotNil(t, app)
require.Equal(t, "TEST", types.NetworkHRP())
}

func TestGenesisConfig(t *testing.T) {
t.Run("config is written to a file", func(t *testing.T) {
app := New()
Expand Down
2 changes: 1 addition & 1 deletion systest/tests/nodes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

func init() {
// systest runs with `fastnet` preset. this init need to generate addresses with same hrp network prefix as fastnet.
types.DefaultTestAddressConfig()
types.SetNetworkHRP("stest")
}

func TestAddNodes(t *testing.T) {
Expand Down

0 comments on commit db1542d

Please sign in to comment.