github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/bft/config/toml.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 8 osm "github.com/gnolang/gno/tm2/pkg/os" 9 "github.com/pelletier/go-toml" 10 ) 11 12 // DefaultDirPerm is the default permissions used when creating directories. 13 const DefaultDirPerm = 0o700 14 15 // LoadConfigFile loads the TOML node configuration from the specified path 16 func LoadConfigFile(path string) (*Config, error) { 17 // Read the config file 18 content, readErr := os.ReadFile(path) 19 if readErr != nil { 20 return nil, readErr 21 } 22 23 // Parse the node config 24 var nodeConfig Config 25 26 if unmarshalErr := toml.Unmarshal(content, &nodeConfig); unmarshalErr != nil { 27 return nil, unmarshalErr 28 } 29 30 // Validate the config 31 if validateErr := nodeConfig.ValidateBasic(); validateErr != nil { 32 return nil, fmt.Errorf("unable to validate config, %w", validateErr) 33 } 34 35 return &nodeConfig, nil 36 } 37 38 /****** these are for production settings ***********/ 39 40 // WriteConfigFile renders config using the template and writes it to configFilePath. 41 func WriteConfigFile(configFilePath string, config *Config) error { 42 // Marshal the config 43 configRaw, err := toml.Marshal(config) 44 if err != nil { 45 return fmt.Errorf("unable to TOML marshal config, %w", err) 46 } 47 48 if err := osm.WriteFile(configFilePath, configRaw, 0o644); err != nil { 49 return fmt.Errorf("unable to write config file, %w", err) 50 } 51 52 return nil 53 } 54 55 /****** these are for test settings ***********/ 56 57 func ResetTestRoot(testName string) (*Config, string) { 58 chainID := "test-chain" 59 60 // create a unique, concurrency-safe test directory under os.TempDir() 61 testDir, err := os.MkdirTemp("", "") 62 if err != nil { 63 panic(err) 64 } 65 66 rootDir, err := os.MkdirTemp(testDir, fmt.Sprintf("%s-%s_", chainID, testName)) 67 if err != nil { 68 panic(err) 69 } 70 71 // ensure config and data subdirs are created 72 if err := osm.EnsureDir(filepath.Join(rootDir, defaultConfigDir), DefaultDirPerm); err != nil { 73 panic(err) 74 } 75 if err := osm.EnsureDir(filepath.Join(rootDir, defaultSecretsDir), DefaultDirPerm); err != nil { 76 panic(err) 77 } 78 if err := osm.EnsureDir(filepath.Join(rootDir, DefaultDBDir), DefaultDirPerm); err != nil { 79 panic(err) 80 } 81 82 baseConfig := DefaultBaseConfig() 83 configFilePath := filepath.Join(rootDir, defaultConfigPath) 84 // NOTE: this does not match the behaviour of the Gno.land node. 85 // However, many tests rely on the fact that they can cleanup the directory 86 // by doing RemoveAll on the rootDir; so to keep compatibility with that 87 // behaviour, we place genesis.json in the rootDir. 88 genesisFilePath := filepath.Join(rootDir, "genesis.json") 89 privKeyFilePath := filepath.Join(rootDir, baseConfig.PrivValidatorKey) 90 privStateFilePath := filepath.Join(rootDir, baseConfig.PrivValidatorState) 91 92 // Write default config file if missing. 93 if !osm.FileExists(configFilePath) { 94 WriteConfigFile(configFilePath, DefaultConfig()) 95 } 96 if !osm.FileExists(genesisFilePath) { 97 if chainID == "" { 98 chainID = "tendermint_test" 99 } 100 testGenesis := fmt.Sprintf(testGenesisFmt, chainID) 101 osm.MustWriteFile(genesisFilePath, []byte(testGenesis), 0o644) 102 } 103 // we always overwrite the priv val 104 osm.MustWriteFile(privKeyFilePath, []byte(testPrivValidatorKey), 0o644) 105 osm.MustWriteFile(privStateFilePath, []byte(testPrivValidatorState), 0o644) 106 107 config := TestConfig().SetRootDir(rootDir) 108 109 return config, genesisFilePath 110 } 111 112 var testGenesisFmt = `{ 113 "genesis_time": "2018-10-10T08:20:13.695936996Z", 114 "chain_id": "%s", 115 "validators": [ 116 { 117 "pub_key": { 118 "@type": "/tm.PubKeyEd25519", 119 "value": "cVt6w3C1DWYwwkAirnbsL49CoOe8T8ZR2BCB8MeOGRg=" 120 }, 121 "power": "10", 122 "name": "" 123 } 124 ], 125 "app_hash": "" 126 }` 127 128 var testPrivValidatorKey = `{ 129 "address": "g1uvwz22t0l2fv9az93wutmlusrjv5zdwx2n32d5", 130 "pub_key": { 131 "@type": "/tm.PubKeyEd25519", 132 "value": "cVt6w3C1DWYwwkAirnbsL49CoOe8T8ZR2BCB8MeOGRg=" 133 }, 134 "priv_key": { 135 "@type": "/tm.PrivKeyEd25519", 136 "value": "Qq4Q9QH2flPSIJShbXPIocbrQtQ4S7Kdn31uI3sKZoJxW3rDcLUNZjDCQCKuduwvj0Kg57xPxlHYEIHwx44ZGA==" 137 } 138 }` 139 140 var testPrivValidatorState = `{ 141 "height": "0", 142 "round": "0", 143 "step": 0 144 }`