Skip to content

Commit 25f2d8d

Browse files
committed
import changes from old outdated branch to avoid conflicts
1 parent 41948cb commit 25f2d8d

File tree

7 files changed

+426
-47
lines changed

7 files changed

+426
-47
lines changed

shared/config/config.go

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@ import (
66
"io/ioutil"
77
"log"
88
"os"
9+
"path/filepath"
910

1011
cryptoPocket "github.com/pokt-network/pocket/shared/crypto"
11-
"github.com/pokt-network/pocket/shared/types/genesis"
1212
)
1313

1414
type Config struct {
15-
RootDir string `json:"root_dir"`
16-
GenesisSource *genesis.GenesisSource `json:"genesis_source"` // TECHDEBT(olshansky): we should be able to pass the struct in here.
15+
RootDir string `json:"root_dir"`
16+
Genesis string `json:"genesis"` // FIXME(olshansky): we should be able to pass the struct in here.
1717

18-
EnableTelemetry bool `json:"enable_telemetry"`
19-
PrivateKey cryptoPocket.Ed25519PrivateKey `json:"private_key"`
18+
PrivateKey cryptoPocket.Ed25519PrivateKey `json:"private_key"`
2019

21-
P2P *P2PConfig `json:"p2p"`
22-
Consensus *ConsensusConfig `json:"consensus"`
23-
// TECHDEBT(team): Consolidate `Persistence` and `PrePersistence`
20+
EnableTelemetry bool `json:"enable_telemetry"`
21+
GlobalLogLevel string `json:"global_log_level"`
22+
23+
Pre2P *Pre2PConfig `json:"pre2p"` // TECHDEBT(team): consolidate/replace this with P2P configs depending on next steps
24+
P2P *P2PConfig `json:"p2p"`
25+
Consensus *ConsensusConfig `json:"consensus"`
2426
PrePersistence *PrePersistenceConfig `json:"pre_persistence"`
2527
Persistence *PersistenceConfig `json:"persistence"`
2628
Utility *UtilityConfig `json:"utility"`
@@ -35,16 +37,33 @@ const (
3537
)
3638

3739
// TECHDEBT(team): consolidate/replace this with P2P configs depending on next steps
38-
type P2PConfig struct {
40+
type Pre2PConfig struct {
3941
ConsensusPort uint32 `json:"consensus_port"`
4042
UseRainTree bool `json:"use_raintree"`
4143
ConnectionType ConnectionType `json:"connection_type"`
44+
LogLevel string `json:"log_level"`
4245
}
4346

4447
type PrePersistenceConfig struct {
45-
Capacity int `json:"capacity"`
46-
MempoolMaxBytes int `json:"mempool_max_bytes"`
47-
MempoolMaxTxs int `json:"mempool_max_txs"`
48+
Capacity int `json:"capacity"`
49+
MempoolMaxBytes int `json:"mempool_max_bytes"`
50+
MempoolMaxTxs int `json:"mempool_max_txs"`
51+
LogLevel string `json:"log_level"`
52+
}
53+
54+
type P2PConfig struct {
55+
Protocol string `json:"protocol"`
56+
Address string `json:"address"`
57+
// TODO(derrandz): Fix the config imports appropriately
58+
// Address cryptoPocket.Address `json:"address"`
59+
ExternalIp string `json:"external_ip"`
60+
Peers []string `json:"peers"`
61+
MaxInbound uint32 `json:"max_inbound"`
62+
MaxOutbound uint32 `json:"max_outbound"`
63+
BufferSize uint `json:"connection_buffer_size"`
64+
WireHeaderLength uint `json:"max_wire_header_length"`
65+
TimeoutInMs uint `json:"timeout_in_ms"`
66+
LogLevel string `json:"log_level"`
4867
}
4968

5069
type PacemakerConfig struct {
@@ -62,20 +81,24 @@ type ConsensusConfig struct {
6281

6382
// Pacemaker
6483
Pacemaker *PacemakerConfig `json:"pacemaker"`
84+
85+
// The Log level for the consensus module
86+
LogLevel string `json:"log_level"`
6587
}
6688

6789
type PersistenceConfig struct {
68-
PostgresUrl string `json:"postgres_url"`
69-
NodeSchema string `json:"schema"`
70-
BlockStorePath string `json:"block_store_path"`
90+
DataDir string `json:"datadir"`
91+
PostgresUrl string `json:"postgres_url"`
92+
NodeSchema string `json:"schema"`
93+
LogLevel string `json:"log_level"`
7194
}
7295

7396
type UtilityConfig struct {
7497
}
7598

7699
type TelemetryConfig struct {
77-
Address string // The address the telemetry module will use to listen for metrics PULL requests (e.g. 0.0.0.0:9000 for prometheus)
78-
Endpoint string // The endpoint available to fetch recorded metrics (e.g. /metrics for prometheus)
100+
Address string // The address that the telemetry module will use to listen for metrics pulling requests (e.g: 0.0.0.0:9000 for prometheus)
101+
Endpoint string // the endpoint that will be provided to scrapers to fetch recorded metrics (e.g: /metrics for prometheus)
79102
}
80103

81104
// TODO(insert tooling issue # here): Re-evaluate how load configs should be handeled.
@@ -103,35 +126,28 @@ func LoadConfig(file string) (c *Config) {
103126
return
104127
}
105128

106-
// TODO: Exhaust all the configuration validation checks
107129
func (c *Config) ValidateAndHydrate() error {
108130
if len(c.PrivateKey) == 0 {
109131
return fmt.Errorf("private key in config file cannot be empty")
110132
}
111133

112-
if c.GenesisSource == nil {
113-
return fmt.Errorf("genesis source cannot be nil in config")
134+
if len(c.Genesis) == 0 {
135+
return fmt.Errorf("must specify a genesis file or string")
114136
}
137+
c.Genesis = rootify(c.Genesis, c.RootDir)
115138

116-
if err := c.HydrateGenesisState(); err != nil {
117-
return fmt.Errorf("error getting genesis state: %v", err)
139+
if err := c.Consensus.ValidateAndHydrate(); err != nil {
140+
log.Fatalln("Error validating or completing consensus config: ", err)
118141
}
119142

120-
if err := c.Consensus.ValidateAndHydrate(); err != nil {
121-
return fmt.Errorf("error validating or completing consensus config: %v", err)
143+
if err := c.P2P.ValidateAndHydrate(); err != nil {
144+
log.Fatalln("Error validating or completing P2P config: ", err)
122145
}
123146

124147
return nil
125148
}
126149

127-
func (c *Config) HydrateGenesisState() error {
128-
genesisState, err := genesis.GenesisStateFromGenesisSource(c.GenesisSource)
129-
if err != nil {
130-
return fmt.Errorf("error getting genesis state: %v", err)
131-
}
132-
c.GenesisSource.Source = &genesis.GenesisSource_State{
133-
State: genesisState,
134-
}
150+
func (c *P2PConfig) ValidateAndHydrate() error {
135151
return nil
136152
}
137153

@@ -154,3 +170,11 @@ func (c *ConsensusConfig) ValidateAndHydrate() error {
154170
func (c *PacemakerConfig) ValidateAndHydrate() error {
155171
return nil
156172
}
173+
174+
// Helper function to make config creation independent of root dir
175+
func rootify(path, root string) string {
176+
if filepath.IsAbs(path) {
177+
return path
178+
}
179+
return filepath.Join(root, path)
180+
}

shared/logging/loggers.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package logging
2+
3+
var (
4+
singletonLogger = CreateStdLogger(LOG_LEVEL_ALL)
5+
)
6+
7+
func GetGlobalLogger() Logger {
8+
return singletonLogger
9+
}

shared/logging/std_logger.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package logging
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"sync"
8+
)
9+
10+
type stdLogger struct {
11+
*log.Logger
12+
*sync.Mutex
13+
namespace Namespace
14+
level LogLevel
15+
}
16+
17+
func CreateStdLogger(level LogLevel) Logger {
18+
return &stdLogger{
19+
Mutex: &sync.Mutex{},
20+
Logger: log.New(os.Stdout, "[pocket]", 0),
21+
level: LOG_LEVEL_ALL,
22+
namespace: GLOBAL_NAMESPACE,
23+
}, nil
24+
}
25+
26+
func (sla *stdLogger) decorate(decor string, args []any) []any {
27+
fArgs := []any{decor}
28+
fArgs = append(fArgs, args...)
29+
return fArgs
30+
}
31+
32+
func (sla *stdLogger) logIfAtLevel(level LogLevel, args ...any) {
33+
if sla.level != LOG_LEVEL_NONE && (sla.level == level || sla.level == LOG_LEVEL_ALL) {
34+
defer sla.Unlock()
35+
sla.Lock()
36+
37+
namespacedArgs := []any{
38+
sla.namespace,
39+
}
40+
namespacedArgs = append(namespacedArgs, args...)
41+
logLine := sla.decorate(fmt.Sprintf("[%s]:", level), namespacedArgs)
42+
43+
if sla.level == LOG_LEVEL_FATAL {
44+
sla.Logger.Fatal(logLine...)
45+
} else {
46+
sla.Logger.Println(logLine...)
47+
}
48+
}
49+
}
50+
51+
// Interface for logging
52+
func (sla *stdLogger) Info(args ...any) {
53+
sla.logIfAtLevel(LOG_LEVEL_INFO, args...)
54+
}
55+
56+
func (sla *stdLogger) Error(args ...any) {
57+
sla.logIfAtLevel(LOG_LEVEL_ERROR, args...)
58+
}
59+
60+
func (sla *stdLogger) Warn(args ...any) {
61+
sla.logIfAtLevel(LOG_LEVEL_WARN, args...)
62+
}
63+
64+
func (sla *stdLogger) Debug(args ...any) {
65+
sla.logIfAtLevel(LOG_LEVEL_DEBUG, args...)
66+
}
67+
68+
func (sla *stdLogger) Fatal(args ...any) {
69+
sla.logIfAtLevel(LOG_LEVEL_FATAL, args...)
70+
}
71+
72+
func (sla *stdLogger) Log(args ...any) {
73+
sla.logIfAtLevel(LOG_LEVEL_ALL, args...)
74+
}
75+
76+
func (sla *stdLogger) SetLevel(l LogLevel) {
77+
sla.level = l
78+
}
79+
80+
func (sla *stdLogger) SetNamespace(namespace Namespace) {
81+
sla.namespace = namespace
82+
}

0 commit comments

Comments
 (0)