From 9358e7377da42fe1c5727d371df9fdf5c24df6eb Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 20 Sep 2022 09:15:01 +0100 Subject: [PATCH 01/90] refactor(Config): runtime module --- app/client/main.go | 15 +- consensus/consensus_tests/utils_test.go | 17 +- consensus/leader_election/module.go | 8 - consensus/module.go | 63 +- consensus/pacemaker.go | 8 - consensus/types/converters.go | 20 + p2p/module.go | 36 +- p2p/module_raintree_test.go | 23 +- persistence/debug.go | 6 +- persistence/module.go | 62 +- persistence/test/setup_test.go | 5 +- persistence/types/converters.go | 149 ++++ runtime/config.go | 90 +++ runtime/genesis.go | 80 +++ runtime/runtime.go | 13 + shared/bus.go | 33 +- shared/modules/bus_module.go | 5 +- shared/modules/module.go | 9 +- shared/node.go | 27 +- shared/test_artifacts/generator.go | 75 +- shared/test_artifacts/gov.go | 6 +- shared/test_artifacts/mock.go | 909 ------------------------ telemetry/module.go | 35 +- telemetry/noop_module.go | 8 - telemetry/prometheus_module.go | 8 - utility/module.go | 10 +- utility/test/module_test.go | 5 +- 27 files changed, 517 insertions(+), 1208 deletions(-) create mode 100644 consensus/types/converters.go create mode 100644 persistence/types/converters.go create mode 100644 runtime/config.go create mode 100644 runtime/genesis.go create mode 100644 runtime/runtime.go delete mode 100644 shared/test_artifacts/mock.go diff --git a/app/client/main.go b/app/client/main.go index 0fbf20a8e..bae6b6136 100644 --- a/app/client/main.go +++ b/app/client/main.go @@ -6,6 +6,7 @@ import ( "log" "os" + "github.com/pokt-network/pocket/runtime" "github.com/pokt-network/pocket/shared/debug" "github.com/pokt-network/pocket/telemetry" @@ -47,11 +48,17 @@ var consensusMod modules.ConsensusModule func main() { var err error - consensusMod, err = consensus.Create(defaultConfigPath, defaultGenesisPath, true) // TECHDEBT: extra param required for injecting private key hack for debug client + + cfg, genesis, err := runtime.Init(defaultConfigPath, defaultGenesisPath) + if err != nil { + log.Fatalf("[ERROR] Failed parse config and/or genesis: %v", err.Error()) + } + + consensusMod, err = consensus.Create(cfg.Consensus, genesis.ConsensusGenesisState, true) // TECHDEBT: extra param required for injecting private key hack for debug client if err != nil { log.Fatalf("[ERROR] Failed to create consensus module: %v", err.Error()) } - p2pMod, err = p2p.Create(defaultConfigPath, defaultGenesisPath, true) // TECHDEBT: extra param required for injecting private key hack for debug client + p2pMod, err = p2p.Create(cfg, true) // TECHDEBT: extra param required for injecting private key hack for debug client if err != nil { log.Fatalf("[ERROR] Failed to create p2p module: %v", err.Error()) } @@ -59,12 +66,12 @@ func main() { // Since this client mimics partial - networking only - functionality of a full node, some of the telemetry-related // code paths are executed. To avoid those messages interfering with the telemetry data collected, a non-nil telemetry // module that NOOPs (per the configs above) is injected. - telemetryMod, err := telemetry.Create(defaultConfigPath, defaultGenesisPath) + telemetryMod, err := telemetry.Create(cfg.Telemetry) if err != nil { log.Fatalf("[ERROR] Failed to create NOOP telemetry module: " + err.Error()) } - _ = shared.CreateBusWithOptionalModules(nil, p2pMod, nil, consensusMod, telemetryMod) + _ = shared.CreateBusWithOptionalModules(cfg.ToShared(), genesis.ToShared(), nil, p2pMod, nil, consensusMod, telemetryMod) p2pMod.Start() diff --git a/consensus/consensus_tests/utils_test.go b/consensus/consensus_tests/utils_test.go index 2ab4ff850..69e58d43f 100644 --- a/consensus/consensus_tests/utils_test.go +++ b/consensus/consensus_tests/utils_test.go @@ -14,6 +14,7 @@ import ( "testing" "time" + "github.com/pokt-network/pocket/runtime" "github.com/pokt-network/pocket/shared/debug" "github.com/pokt-network/pocket/shared/test_artifacts" @@ -62,7 +63,7 @@ type IdToNodeMapping map[typesCons.NodeId]*shared.Node /*** Node Generation Helpers ***/ -func GenerateNodeConfigs(_ *testing.T, validatorCount int) (configs []modules.Config, genesisState modules.GenesisState) { +func GenerateNodeConfigs(_ *testing.T, validatorCount int) (configs []runtime.Config, genesisState modules.GenesisState) { var keys []string genesisState, keys = test_artifacts.NewGenesisState(validatorCount, 1, 1, 1) configs = test_artifacts.NewDefaultConfigs(keys) @@ -83,7 +84,7 @@ func GenerateNodeConfigs(_ *testing.T, validatorCount int) (configs []modules.Co func CreateTestConsensusPocketNodes( t *testing.T, - configs []modules.Config, + configs []runtime.Config, genesisState modules.GenesisState, testChannel modules.EventsChannel, ) (pocketNodes IdToNodeMapping) { @@ -113,12 +114,16 @@ const ( // Creates a pocket node where all the primary modules, exception for consensus, are mocked func CreateTestConsensusPocketNode( t *testing.T, - cfg modules.Config, + cfg runtime.Config, genesisState modules.GenesisState, testChannel modules.EventsChannel, ) *shared.Node { createTestingGenesisAndConfigFiles(t, cfg, genesisState) - consensusMod, err := consensus.Create(testingConfigFilePath, testingGenesisFilePath, false) + + config, genesis, err := runtime.Init(testingConfigFilePath, testingGenesisFilePath) + require.NoError(t, err) + + consensusMod, err := consensus.Create(config.Consensus, genesis.ConsensusGenesisState, false) require.NoError(t, err) // TODO(olshansky): At the moment we are using the same base mocks for all the tests, // but note that they will need to be customized on a per test basis. @@ -127,7 +132,7 @@ func CreateTestConsensusPocketNode( utilityMock := baseUtilityMock(t, testChannel) telemetryMock := baseTelemetryMock(t, testChannel) - bus, err := shared.CreateBus(persistenceMock, p2pMock, utilityMock, consensusMod, telemetryMock) + bus, err := shared.CreateBus(config.ToShared(), genesis.ToShared(), persistenceMock, p2pMock, utilityMock, consensusMod, telemetryMock) require.NoError(t, err) pk, err := cryptoPocket.NewPrivateKey(cfg.Base.PrivateKey) @@ -141,7 +146,7 @@ func CreateTestConsensusPocketNode( return pocketNode } -func createTestingGenesisAndConfigFiles(t *testing.T, cfg modules.Config, genesisState modules.GenesisState) { +func createTestingGenesisAndConfigFiles(t *testing.T, cfg runtime.Config, genesisState modules.GenesisState) { config, err := json.Marshal(cfg.Consensus) require.NoError(t, err) diff --git a/consensus/leader_election/module.go b/consensus/leader_election/module.go index a5e24dcf3..75a6ab2f6 100644 --- a/consensus/leader_election/module.go +++ b/consensus/leader_election/module.go @@ -35,14 +35,6 @@ func (m *leaderElectionModule) Stop() error { return nil } -func (m *leaderElectionModule) InitConfig(pathToConfigJSON string) (config modules.IConfig, err error) { - return // No-op -} - -func (m *leaderElectionModule) InitGenesis(pathToGenesisJSON string) (genesis modules.IGenesis, err error) { - return // No-op -} - func (m *leaderElectionModule) GetModuleName() string { return LeaderElectionModuleName } diff --git a/consensus/module.go b/consensus/module.go index d2e3e1481..17f60376c 100644 --- a/consensus/module.go +++ b/consensus/module.go @@ -1,15 +1,12 @@ package consensus import ( - "encoding/json" "fmt" - "io/ioutil" "log" "github.com/pokt-network/pocket/consensus/leader_election" typesCons "github.com/pokt-network/pocket/consensus/types" cryptoPocket "github.com/pokt-network/pocket/shared/crypto" - "github.com/pokt-network/pocket/shared/test_artifacts" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/anypb" @@ -65,35 +62,26 @@ type ConsensusModule struct { MaxBlockBytes uint64 } -func Create(configPath, genesisPath string, useRandomPK bool) (modules.ConsensusModule, error) { - cm := new(ConsensusModule) - c, err := cm.InitConfig(configPath) - if err != nil { - return nil, err - } - g, err := cm.InitGenesis(genesisPath) - if err != nil { - return nil, err - } - cfg := c.(*typesCons.ConsensusConfig) - genesis := g.(*typesCons.ConsensusGenesisState) - leaderElectionMod, err := leader_election.Create(cfg, genesis) +func Create(cfg modules.ConsensusConfig, genesis modules.ConsensusGenesisState, useRandomPK bool) (modules.ConsensusModule, error) { + moduleCfg := cfg.(*typesCons.ConsensusConfig) + moduleGenesis := genesis.(*typesCons.ConsensusGenesisState) + leaderElectionMod, err := leader_election.Create(moduleCfg, moduleGenesis) if err != nil { return nil, err } // TODO(olshansky): Can we make this a submodule? - paceMaker, err := CreatePacemaker(cfg) + paceMaker, err := CreatePacemaker(moduleCfg) if err != nil { return nil, err } - valMap := typesCons.ValidatorListToMap(genesis.Validators) + valMap := typesCons.ValidatorListToMap(moduleGenesis.Validators) var privateKey cryptoPocket.PrivateKey if useRandomPK { privateKey, err = cryptoPocket.GeneratePrivateKey() } else { - privateKey, err = cryptoPocket.NewPrivateKey(cfg.PrivateKey) + privateKey, err = cryptoPocket.NewPrivateKey(moduleCfg.PrivateKey) } if err != nil { return nil, err @@ -105,7 +93,7 @@ func Create(configPath, genesisPath string, useRandomPK bool) (modules.Consensus bus: nil, privateKey: privateKey.(cryptoPocket.Ed25519PrivateKey), - consCfg: cfg, + consCfg: moduleCfg, Height: 0, Round: 0, @@ -129,7 +117,7 @@ func Create(configPath, genesisPath string, useRandomPK bool) (modules.Consensus logPrefix: DefaultLogPrefix, MessagePool: make(map[typesCons.HotstuffStep][]*typesCons.HotstuffMessage), - MaxBlockBytes: genesis.GetMaxBlockBytes(), + MaxBlockBytes: moduleGenesis.GetMaxBlockBytes(), } // TODO(olshansky): Look for a way to avoid doing this. @@ -138,39 +126,6 @@ func Create(configPath, genesisPath string, useRandomPK bool) (modules.Consensus return m, nil } -func (m *ConsensusModule) InitConfig(pathToConfigJSON string) (config modules.IConfig, err error) { - data, err := ioutil.ReadFile(pathToConfigJSON) - if err != nil { - return - } - // over arching configuration file - rawJSON := make(map[string]json.RawMessage) - if err = json.Unmarshal(data, &rawJSON); err != nil { - log.Fatalf("[ERROR] an error occurred unmarshalling the %s file: %v", pathToConfigJSON, err.Error()) - } - // consensus specific configuration file - config = new(typesCons.ConsensusConfig) - err = json.Unmarshal(rawJSON[m.GetModuleName()], config) - return -} - -func (m *ConsensusModule) InitGenesis(pathToGenesisJSON string) (genesis modules.IGenesis, err error) { - data, err := ioutil.ReadFile(pathToGenesisJSON) - if err != nil { - return - } - // over arching configuration file - rawJSON := make(map[string]json.RawMessage) - if err = json.Unmarshal(data, &rawJSON); err != nil { - log.Fatalf("[ERROR] an error occurred unmarshalling the %s file: %v", pathToGenesisJSON, err.Error()) - } - // consensus specific configuration file - genesis = new(typesCons.ConsensusGenesisState) - - err = json.Unmarshal(rawJSON[test_artifacts.GetGenesisFileName(m.GetModuleName())], genesis) - return -} - func (m *ConsensusModule) Start() error { m.GetBus(). GetTelemetryModule(). diff --git a/consensus/pacemaker.go b/consensus/pacemaker.go index c2655e315..9d3fe11c6 100644 --- a/consensus/pacemaker.go +++ b/consensus/pacemaker.go @@ -50,14 +50,6 @@ type paceMaker struct { paceMakerDebug } -func (p *paceMaker) InitConfig(pathToConfigJSON string) (config modules.IConfig, err error) { - return // No-op -} - -func (p *paceMaker) InitGenesis(pathToGenesisJSON string) (genesis modules.IGenesis, err error) { - return // No-op -} - func CreatePacemaker(cfg *typesCons.ConsensusConfig) (m *paceMaker, err error) { return &paceMaker{ bus: nil, diff --git a/consensus/types/converters.go b/consensus/types/converters.go new file mode 100644 index 000000000..b5c950a56 --- /dev/null +++ b/consensus/types/converters.go @@ -0,0 +1,20 @@ +package types + +import "github.com/pokt-network/pocket/shared/modules" + +func actorToValidator(actor modules.Actor) *Validator { + return &Validator{ + Address: actor.GetAddress(), + PublicKey: actor.GetPublicKey(), + StakedAmount: actor.GetStakedAmount(), + GenericParam: actor.GetGenericParam(), + } +} + +func ToConsensusValidators(actors []modules.Actor) []*Validator { + r := make([]*Validator, 0) + for _, a := range actors { + r = append(r, actorToValidator(a)) + } + return r +} diff --git a/p2p/module.go b/p2p/module.go index eeac684ce..bfd0b7aa3 100644 --- a/p2p/module.go +++ b/p2p/module.go @@ -5,8 +5,6 @@ package p2p // to be a "real" replacement for now. import ( - "encoding/json" - "io/ioutil" "log" "github.com/pokt-network/pocket/shared/debug" @@ -42,14 +40,10 @@ func (m *p2pModule) GetAddress() (cryptoPocket.Address, error) { return m.address, nil } -func Create(configPath, genesisPath string, useRandomPK bool) (m modules.P2PModule, err error) { +func Create(cfg modules.P2PConfig, useRandomPK bool) (m modules.P2PModule, err error) { log.Println("Creating network module") - c, err := new(p2pModule).InitConfig(configPath) - if err != nil { - return nil, err - } - cfg := c.(*typesP2P.P2PConfig) - l, err := CreateListener(cfg) + moduleCfg := cfg.(*typesP2P.P2PConfig) + l, err := CreateListener(moduleCfg) if err != nil { return nil, err } @@ -57,13 +51,13 @@ func Create(configPath, genesisPath string, useRandomPK bool) (m modules.P2PModu if useRandomPK { privateKey, err = cryptoPocket.GeneratePrivateKey() } else { - privateKey, err = cryptoPocket.NewPrivateKey(cfg.PrivateKey) + privateKey, err = cryptoPocket.NewPrivateKey(moduleCfg.PrivateKey) } if err != nil { return nil, err } m = &p2pModule{ - p2pConfig: cfg, + p2pConfig: moduleCfg, listener: l, address: privateKey.Address(), @@ -71,26 +65,6 @@ func Create(configPath, genesisPath string, useRandomPK bool) (m modules.P2PModu return m, nil } -func (m *p2pModule) InitConfig(pathToConfigJSON string) (config modules.IConfig, err error) { - data, err := ioutil.ReadFile(pathToConfigJSON) - if err != nil { - return - } - // over arching configuration file - rawJSON := make(map[string]json.RawMessage) - if err = json.Unmarshal(data, &rawJSON); err != nil { - log.Fatalf("[ERROR] an error occurred unmarshalling the %s file: %v", pathToConfigJSON, err.Error()) - } - // p2p specific configuration file - config = new(typesP2P.P2PConfig) - err = json.Unmarshal(rawJSON[m.GetModuleName()], config) - return -} - -func (m *p2pModule) InitGenesis(pathToGenesisJSON string) (genesis modules.IGenesis, err error) { - return // No-op -} - func (m *p2pModule) SetBus(bus modules.Bus) { // INVESTIGATE: Can the code flow be modified to set the bus here? // m.network.SetBus(m.GetBus()) diff --git a/p2p/module_raintree_test.go b/p2p/module_raintree_test.go index e7ed4be8e..35b06fd11 100644 --- a/p2p/module_raintree_test.go +++ b/p2p/module_raintree_test.go @@ -14,12 +14,14 @@ import ( "testing" "time" + "github.com/pokt-network/pocket/runtime" "github.com/pokt-network/pocket/shared/debug" "github.com/pokt-network/pocket/shared/test_artifacts" "github.com/golang/mock/gomock" typesP2P "github.com/pokt-network/pocket/p2p/types" mocksP2P "github.com/pokt-network/pocket/p2p/types/mocks" + typesPers "github.com/pokt-network/pocket/persistence/types" cryptoPocket "github.com/pokt-network/pocket/shared/crypto" "github.com/pokt-network/pocket/shared/modules" modulesMock "github.com/pokt-network/pocket/shared/modules/mocks" @@ -281,7 +283,8 @@ func generateKeys(_ *testing.T, numValidators int) []cryptoPocket.PrivateKey { // A mock of the application specific to know if a message was sent to be handled by the application // INVESTIGATE(olshansky): Double check that how the expected calls are counted is accurate per the -// expectation with RainTree by comparing with Telemetry after updating specs. +// +// expectation with RainTree by comparing with Telemetry after updating specs. func prepareBusMock(t *testing.T, wg *sync.WaitGroup, consensusMock *modulesMock.MockConsensusModule, telemetryMock *modulesMock.MockTelemetryModule) *modulesMock.MockBus { ctrl := gomock.NewController(t) busMock := modulesMock.NewMockBus(ctrl) @@ -349,7 +352,8 @@ func prepareEventMetricsAgentMock(t *testing.T) *modulesMock.MockEventMetricsAge // is a race condition here, but it is okay because our goal is to achieve max coverage with an upper limit // on the number of expected messages propagated. // INVESTIGATE(olshansky): Double check that how the expected calls are counted is accurate per the -// expectation with RainTree by comparing with Telemetry after updating specs. +// +// expectation with RainTree by comparing with Telemetry after updating specs. func prepareConnMock(t *testing.T, expectedNumNetworkReads, expectedNumNetworkWrites uint16) typesP2P.Transport { testChannel := make(chan []byte, testChannelSize) ctrl := gomock.NewController(t) @@ -374,7 +378,14 @@ func prepareP2PModules(t *testing.T, configs []modules.Config) (p2pModules map[s p2pModules = make(map[string]*p2pModule, len(configs)) for i, config := range configs { createTestingGenesisAndConfigFiles(t, config, modules.GenesisState{}, i) - p2pMod, err := Create(testingConfigFilePath+strconv.Itoa(i)+jsonPosfix, testingGenesisFilePath+jsonPosfix, false) + + configFilePath := testingConfigFilePath + strconv.Itoa(i) + jsonPosfix + genesisFilePath := testingGenesisFilePath + jsonPosfix + + cfg, _, err := runtime.Init(configFilePath, genesisFilePath) + require.NoError(t, err) + + p2pMod, err := Create(cfg.P2P, false) require.NoError(t, err) p2pModules[validatorId(t, i+1)] = p2pMod.(*p2pModule) } @@ -433,7 +444,7 @@ func createGenesisState(t *testing.T, valKeys []cryptoPocket.PrivateKey) modules validators := make([]modules.Actor, len(valKeys)) for i, valKey := range valKeys { addr := valKey.Address().String() - val := &test_artifacts.MockActor{ + val := &typesPers.Actor{ Address: addr, PublicKey: valKey.PublicKey().String(), GenericParam: validatorId(t, i+1), @@ -445,8 +456,8 @@ func createGenesisState(t *testing.T, valKeys []cryptoPocket.PrivateKey) modules validators[i] = val } return modules.GenesisState{ - PersistenceGenesisState: &test_artifacts.MockPersistenceGenesisState{ - Validators: validators, + PersistenceGenesisState: &typesPers.PersistenceGenesisState{ + Validators: typesPers.ToPersistanceActors(validators), }, } } diff --git a/persistence/debug.go b/persistence/debug.go index 3fdc583a4..5ffe37c7c 100644 --- a/persistence/debug.go +++ b/persistence/debug.go @@ -4,7 +4,7 @@ import ( "log" typesCons "github.com/pokt-network/pocket/consensus/types" - "github.com/pokt-network/pocket/persistence/types" + "github.com/pokt-network/pocket/runtime" "github.com/pokt-network/pocket/shared/codec" "github.com/pokt-network/pocket/shared/debug" ) @@ -15,11 +15,11 @@ func (m *PersistenceModule) HandleDebugMessage(debugMessage *debug.DebugMessage) m.showLatestBlockInStore(debugMessage) case debug.DebugMessageAction_DEBUG_CLEAR_STATE: m.clearState(debugMessage) - g, err := m.InitGenesis(m.genesisPath) + g, err := runtime.ParseGenesisJSON(m.genesisPath) if err != nil { return err } - m.populateGenesisState(g.(*types.PersistenceGenesisState)) + m.populateGenesisState(g.PersistenceGenesisState) default: log.Printf("Debug message not handled by persistence module: %s \n", debugMessage.Message) } diff --git a/persistence/module.go b/persistence/module.go index a7e434735..c10760d62 100644 --- a/persistence/module.go +++ b/persistence/module.go @@ -2,9 +2,7 @@ package persistence import ( "context" - "encoding/json" "fmt" - "io/ioutil" "log" "github.com/pokt-network/pocket/persistence/types" @@ -12,7 +10,6 @@ import ( "github.com/jackc/pgx/v4" "github.com/pokt-network/pocket/persistence/kvstore" "github.com/pokt-network/pocket/shared/modules" - "github.com/pokt-network/pocket/shared/test_artifacts" ) var _ modules.PersistenceModule = &PersistenceModule{} @@ -36,19 +33,10 @@ const ( PersistenceModuleName = "persistence" ) -func Create(configPath, genesisPath string) (modules.PersistenceModule, error) { - m := new(PersistenceModule) - c, err := m.InitConfig(configPath) - if err != nil { - return nil, err - } - cfg := c.(*types.PersistenceConfig) - g, err := m.InitGenesis(genesisPath) - if err != nil { - return nil, err - } - genesis := g.(*types.PersistenceGenesisState) - conn, err := connectToDatabase(cfg.GetPostgresUrl(), cfg.GetNodeSchema()) +func Create(cfg modules.PersistenceConfig, genesis modules.PersistenceGenesisState) (modules.PersistenceModule, error) { + moduleCfg := cfg.(*types.PersistenceConfig) + moduleGenesis := genesis.(*types.PersistenceGenesisState) + conn, err := connectToDatabase(moduleCfg.GetPostgresUrl(), moduleCfg.GetNodeSchema()) if err != nil { return nil, err } @@ -57,16 +45,16 @@ func Create(configPath, genesisPath string) (modules.PersistenceModule, error) { } conn.Close(context.TODO()) - blockStore, err := initializeBlockStore(cfg.GetBlockStorePath()) + blockStore, err := initializeBlockStore(moduleCfg.GetBlockStorePath()) if err != nil { return nil, err } persistenceMod := &PersistenceModule{ bus: nil, - postgresURL: cfg.GetPostgresUrl(), - nodeSchema: cfg.GetNodeSchema(), - genesisPath: genesisPath, + postgresURL: moduleCfg.GetPostgresUrl(), + nodeSchema: moduleCfg.GetNodeSchema(), + genesisPath: "genesisPath", blockStore: blockStore, writeContext: nil, } @@ -79,7 +67,7 @@ func Create(configPath, genesisPath string) (modules.PersistenceModule, error) { // this forces the genesis state to be reloaded on every node startup until state sync is // implemented. // NOTE: `populateGenesisState` does not return an error but logs a fatal error if there's a problem - persistenceMod.populateGenesisState(genesis) + persistenceMod.populateGenesisState(moduleGenesis) } else { log.Println("Loading state from previous state...") } @@ -87,38 +75,6 @@ func Create(configPath, genesisPath string) (modules.PersistenceModule, error) { return persistenceMod, nil } -func (m *PersistenceModule) InitConfig(pathToConfigJSON string) (config modules.IConfig, err error) { - data, err := ioutil.ReadFile(pathToConfigJSON) - if err != nil { - return - } - // over arching configuration file - rawJSON := make(map[string]json.RawMessage) - if err = json.Unmarshal(data, &rawJSON); err != nil { - log.Fatalf("[ERROR] an error occurred unmarshalling the %s file: %v", pathToConfigJSON, err.Error()) - } - // persistence specific configuration file - config = new(types.PersistenceConfig) - err = json.Unmarshal(rawJSON[m.GetModuleName()], config) - return -} - -func (m *PersistenceModule) InitGenesis(pathToGenesisJSON string) (genesis modules.IGenesis, err error) { - data, err := ioutil.ReadFile(pathToGenesisJSON) - if err != nil { - return - } - // over arching configuration file - rawJSON := make(map[string]json.RawMessage) - if err = json.Unmarshal(data, &rawJSON); err != nil { - log.Fatalf("[ERROR] an error occurred unmarshalling the %s file: %v", pathToGenesisJSON, err.Error()) - } - // persistence specific configuration file - genesis = new(types.PersistenceGenesisState) - err = json.Unmarshal(rawJSON[test_artifacts.GetGenesisFileName(m.GetModuleName())], genesis) - return -} - func (m *PersistenceModule) Start() error { log.Println("Starting persistence module...") return nil diff --git a/persistence/test/setup_test.go b/persistence/test/setup_test.go index e3162a4d9..fea5f57cb 100644 --- a/persistence/test/setup_test.go +++ b/persistence/test/setup_test.go @@ -107,7 +107,7 @@ func newTestPersistenceModule(databaseUrl string) modules.PersistenceModule { } genesisState, _ := test_artifacts.NewGenesisState(5, 1, 1, 1) createTestingGenesisAndConfigFiles(cfg, genesisState) - persistenceMod, err := persistence.Create(testingConfigFilePath, testingGenesisFilePath) + persistenceMod, err := persistence.Create(cfg.Persistence, genesisState.PersistenceGenesisState) if err != nil { log.Fatalf("Error creating persistence module: %s", err) } @@ -284,7 +284,8 @@ func fuzzSingleProtocolActor( } // TODO(olshansky): Make these functions & variables more functional to avoid having "unexpected" -// side effects and making it clearer to the reader. +// +// side effects and making it clearer to the reader. const ( testingGenesisFilePath = "genesis.json" testingConfigFilePath = "config.json" diff --git a/persistence/types/converters.go b/persistence/types/converters.go new file mode 100644 index 000000000..2bea61c95 --- /dev/null +++ b/persistence/types/converters.go @@ -0,0 +1,149 @@ +package types + +import "github.com/pokt-network/pocket/shared/modules" + +func toPersistanceActor(actor modules.Actor) *Actor { + return &Actor{ + Address: actor.GetAddress(), + PublicKey: actor.GetPublicKey(), + StakedAmount: actor.GetStakedAmount(), + GenericParam: actor.GetGenericParam(), + } +} + +func ToPersistanceActors(actors []modules.Actor) []*Actor { + r := make([]*Actor, 0) + for _, a := range actors { + r = append(r, toPersistanceActor(a)) + } + return r +} + +func toPersistanceAccount(account modules.Account) *Account { + return &Account{ + Address: account.GetAddress(), + Amount: account.GetAmount(), + } +} + +func ToPersistanceAccounts(accounts []modules.Account) []*Account { + r := make([]*Account, 0) + for _, a := range accounts { + r = append(r, toPersistanceAccount(a)) + } + return r +} + +func ToPersistanceParams(params modules.Params) *Params { + return &Params{ + BlocksPerSession: params.GetBlocksPerSession(), + AppMinimumStake: params.GetAppMinimumStake(), + AppMaxChains: params.GetAppMaxChains(), + AppBaselineStakeRate: params.GetAppBaselineStakeRate(), + AppStakingAdjustment: params.GetAppStakingAdjustment(), + AppUnstakingBlocks: params.GetAppUnstakingBlocks(), + AppMinimumPauseBlocks: params.GetAppMinimumPauseBlocks(), + AppMaxPauseBlocks: params.GetAppMaxPauseBlocks(), + ServiceNodeMinimumStake: params.GetServiceNodeMinimumStake(), + ServiceNodeMaxChains: params.GetServiceNodeMaxChains(), + ServiceNodeUnstakingBlocks: params.GetServiceNodeUnstakingBlocks(), + ServiceNodeMinimumPauseBlocks: params.GetServiceNodeMinimumPauseBlocks(), + ServiceNodeMaxPauseBlocks: params.GetServiceNodeMaxPauseBlocks(), + ServiceNodesPerSession: params.GetServiceNodesPerSession(), + FishermanMinimumStake: params.GetFishermanMinimumStake(), + FishermanMaxChains: params.GetFishermanMaxChains(), + FishermanUnstakingBlocks: params.GetFishermanUnstakingBlocks(), + FishermanMinimumPauseBlocks: params.GetFishermanMinimumPauseBlocks(), + FishermanMaxPauseBlocks: params.GetFishermanMaxPauseBlocks(), + ValidatorMinimumStake: params.GetValidatorMinimumStake(), + ValidatorUnstakingBlocks: params.GetValidatorUnstakingBlocks(), + ValidatorMinimumPauseBlocks: params.GetValidatorMinimumPauseBlocks(), + ValidatorMaxPauseBlocks: params.GetValidatorMaxPauseBlocks(), + ValidatorMaximumMissedBlocks: params.GetValidatorMaximumMissedBlocks(), + ValidatorMaxEvidenceAgeInBlocks: params.GetValidatorMaxEvidenceAgeInBlocks(), + ProposerPercentageOfFees: params.GetProposerPercentageOfFees(), + MissedBlocksBurnPercentage: params.GetMissedBlocksBurnPercentage(), + DoubleSignBurnPercentage: params.GetDoubleSignBurnPercentage(), + MessageDoubleSignFee: params.GetMessageDoubleSignFee(), + MessageSendFee: params.GetMessageSendFee(), + MessageStakeFishermanFee: params.GetMessageStakeFishermanFee(), + MessageEditStakeFishermanFee: params.GetMessageEditStakeFishermanFee(), + MessageUnstakeFishermanFee: params.GetMessageUnstakeFishermanFee(), + MessagePauseFishermanFee: params.GetMessagePauseFishermanFee(), + MessageUnpauseFishermanFee: params.GetMessageUnpauseFishermanFee(), + MessageFishermanPauseServiceNodeFee: params.GetMessageFishermanPauseServiceNodeFee(), + MessageTestScoreFee: params.GetMessageTestScoreFee(), + MessageProveTestScoreFee: params.GetMessageProveTestScoreFee(), + MessageStakeAppFee: params.GetMessageStakeAppFee(), + MessageEditStakeAppFee: params.GetMessageEditStakeAppFee(), + MessageUnstakeAppFee: params.GetMessageUnstakeAppFee(), + MessagePauseAppFee: params.GetMessagePauseAppFee(), + MessageUnpauseAppFee: params.GetMessageUnpauseAppFee(), + MessageStakeValidatorFee: params.GetMessageStakeValidatorFee(), + MessageEditStakeValidatorFee: params.GetMessageEditStakeValidatorFee(), + MessageUnstakeValidatorFee: params.GetMessageUnstakeValidatorFee(), + MessagePauseValidatorFee: params.GetMessagePauseValidatorFee(), + MessageUnpauseValidatorFee: params.GetMessageUnpauseValidatorFee(), + MessageStakeServiceNodeFee: params.GetMessageStakeServiceNodeFee(), + MessageEditStakeServiceNodeFee: params.GetMessageEditStakeServiceNodeFee(), + MessageUnstakeServiceNodeFee: params.GetMessageUnstakeServiceNodeFee(), + MessagePauseServiceNodeFee: params.GetMessagePauseServiceNodeFee(), + MessageUnpauseServiceNodeFee: params.GetMessageUnpauseServiceNodeFee(), + MessageChangeParameterFee: params.GetMessageChangeParameterFee(), + AclOwner: params.GetAclOwner(), + BlocksPerSessionOwner: params.GetBlocksPerSessionOwner(), + AppMinimumStakeOwner: params.GetAppMinimumStakeOwner(), + AppMaxChainsOwner: params.GetAppMaxChainsOwner(), + AppBaselineStakeRateOwner: params.GetAppBaselineStakeRateOwner(), + AppStakingAdjustmentOwner: params.GetAppStakingAdjustmentOwner(), + AppUnstakingBlocksOwner: params.GetAppUnstakingBlocksOwner(), + AppMinimumPauseBlocksOwner: params.GetAppMinimumPauseBlocksOwner(), + AppMaxPausedBlocksOwner: params.GetAppMaxPausedBlocksOwner(), + ServiceNodeMinimumStakeOwner: params.GetServiceNodeMinimumStakeOwner(), + ServiceNodeMaxChainsOwner: params.GetServiceNodeMaxChainsOwner(), + ServiceNodeUnstakingBlocksOwner: params.GetServiceNodeUnstakingBlocksOwner(), + ServiceNodeMinimumPauseBlocksOwner: params.GetServiceNodeMinimumPauseBlocksOwner(), + ServiceNodeMaxPausedBlocksOwner: params.GetServiceNodeMaxPausedBlocksOwner(), + ServiceNodesPerSessionOwner: params.GetServiceNodesPerSessionOwner(), + FishermanMinimumStakeOwner: params.GetFishermanMinimumStakeOwner(), + FishermanMaxChainsOwner: params.GetFishermanMaxChainsOwner(), + FishermanUnstakingBlocksOwner: params.GetFishermanUnstakingBlocksOwner(), + FishermanMinimumPauseBlocksOwner: params.GetFishermanMinimumPauseBlocksOwner(), + FishermanMaxPausedBlocksOwner: params.GetFishermanMaxPausedBlocksOwner(), + ValidatorMinimumStakeOwner: params.GetValidatorMinimumStakeOwner(), + ValidatorUnstakingBlocksOwner: params.GetValidatorUnstakingBlocksOwner(), + ValidatorMinimumPauseBlocksOwner: params.GetValidatorMinimumPauseBlocksOwner(), + ValidatorMaxPausedBlocksOwner: params.GetValidatorMaxPausedBlocksOwner(), + ValidatorMaximumMissedBlocksOwner: params.GetValidatorMaximumMissedBlocksOwner(), + ValidatorMaxEvidenceAgeInBlocksOwner: params.GetValidatorMaxEvidenceAgeInBlocksOwner(), + ProposerPercentageOfFeesOwner: params.GetProposerPercentageOfFeesOwner(), + MissedBlocksBurnPercentageOwner: params.GetMissedBlocksBurnPercentageOwner(), + DoubleSignBurnPercentageOwner: params.GetDoubleSignBurnPercentageOwner(), + MessageDoubleSignFeeOwner: params.GetMessageDoubleSignFeeOwner(), + MessageSendFeeOwner: params.GetMessageSendFeeOwner(), + MessageStakeFishermanFeeOwner: params.GetMessageStakeFishermanFeeOwner(), + MessageEditStakeFishermanFeeOwner: params.GetMessageEditStakeFishermanFeeOwner(), + MessageUnstakeFishermanFeeOwner: params.GetMessageUnstakeFishermanFeeOwner(), + MessagePauseFishermanFeeOwner: params.GetMessagePauseFishermanFeeOwner(), + MessageUnpauseFishermanFeeOwner: params.GetMessageUnpauseFishermanFeeOwner(), + MessageFishermanPauseServiceNodeFeeOwner: params.GetMessageFishermanPauseServiceNodeFeeOwner(), + MessageTestScoreFeeOwner: params.GetMessageTestScoreFeeOwner(), + MessageProveTestScoreFeeOwner: params.GetMessageProveTestScoreFeeOwner(), + MessageStakeAppFeeOwner: params.GetMessageStakeAppFeeOwner(), + MessageEditStakeAppFeeOwner: params.GetMessageEditStakeAppFeeOwner(), + MessageUnstakeAppFeeOwner: params.GetMessageUnstakeAppFeeOwner(), + MessagePauseAppFeeOwner: params.GetMessagePauseAppFeeOwner(), + MessageUnpauseAppFeeOwner: params.GetMessageUnpauseAppFeeOwner(), + MessageStakeValidatorFeeOwner: params.GetMessageStakeValidatorFeeOwner(), + MessageEditStakeValidatorFeeOwner: params.GetMessageEditStakeValidatorFeeOwner(), + MessageUnstakeValidatorFeeOwner: params.GetMessageUnstakeValidatorFeeOwner(), + MessagePauseValidatorFeeOwner: params.GetMessagePauseValidatorFeeOwner(), + MessageUnpauseValidatorFeeOwner: params.GetMessageUnpauseValidatorFeeOwner(), + MessageStakeServiceNodeFeeOwner: params.GetMessageStakeServiceNodeFeeOwner(), + MessageEditStakeServiceNodeFeeOwner: params.GetMessageEditStakeServiceNodeFeeOwner(), + MessageUnstakeServiceNodeFeeOwner: params.GetMessageUnstakeServiceNodeFeeOwner(), + MessagePauseServiceNodeFeeOwner: params.GetMessagePauseServiceNodeFeeOwner(), + MessageUnpauseServiceNodeFeeOwner: params.GetMessageUnpauseServiceNodeFeeOwner(), + MessageChangeParameterFeeOwner: params.GetMessageChangeParameterFeeOwner(), + } +} diff --git a/runtime/config.go b/runtime/config.go new file mode 100644 index 000000000..1ba3bbabc --- /dev/null +++ b/runtime/config.go @@ -0,0 +1,90 @@ +package runtime + +import ( + "encoding/json" + "os" + + typesCons "github.com/pokt-network/pocket/consensus/types" + typesP2P "github.com/pokt-network/pocket/p2p/types" + typesPers "github.com/pokt-network/pocket/persistence/types" + "github.com/pokt-network/pocket/shared/modules" + typesTelemetry "github.com/pokt-network/pocket/telemetry" + typesUtil "github.com/pokt-network/pocket/utility/types" +) + +var _ modules.ConsensusConfig = &Config{} +var _ modules.P2PConfig = &Config{} +var _ modules.PersistenceConfig = &Config{} + +type Config struct { + Base *BaseConfig `json:"base"` + Consensus *typesCons.ConsensusConfig `json:"consensus"` + Utility *typesUtil.UtilityConfig `json:"utility"` + Persistence *typesPers.PersistenceConfig `json:"persistence"` + P2P *typesP2P.P2PConfig `json:"p2p"` + Telemetry *typesTelemetry.TelemetryConfig `json:"telemetry"` + + configPath string + genesisPath string +} + +func (c *Config) ToShared() modules.Config { + return modules.Config{ + Base: (*modules.BaseConfig)(c.Base), + Consensus: c.Consensus, + Utility: c.Utility, + Persistence: c.Persistence, + P2P: c.P2P, + Telemetry: c.Telemetry, + } +} + +type BaseConfig struct { + RootDirectory string `json:"root_directory"` + PrivateKey string `json:"private_key"` // TODO (pocket/issues/150) better architecture for key management (keybase, keyfiles, etc.) +} + +func ParseConfigJSON(configPath string) (config *Config, err error) { + data, err := os.ReadFile(configPath) + if err != nil { + return + } + + // general configuration file + config = new(Config) + err = json.Unmarshal(data, &config) + return +} + +// modules.ConsensusConfig + +func (c *Config) GetMaxMempoolBytes() uint64 { + return c.Consensus.MaxMempoolBytes +} +func (c *Config) GetPaceMakerConfig() modules.PacemakerConfig { + return c.Consensus.PacemakerConfig +} + +// modules.P2PConfig + +func (c *Config) GetConsensusPort() uint32 { + return c.P2P.ConsensusPort +} +func (c *Config) GetUseRainTree() bool { + return c.P2P.UseRainTree +} +func (c *Config) IsEmptyConnType() bool { // TODO (team) make enum + return c.P2P.IsEmptyConnectionType +} + +// modules.PersistenceConfig + +func (c *Config) GetPostgresUrl() string { + return c.Persistence.PostgresUrl +} +func (c *Config) GetNodeSchema() string { + return c.Persistence.NodeSchema +} +func (c *Config) GetBlockStorePath() string { + return c.Persistence.BlockStorePath +} diff --git a/runtime/genesis.go b/runtime/genesis.go new file mode 100644 index 000000000..3475c76b8 --- /dev/null +++ b/runtime/genesis.go @@ -0,0 +1,80 @@ +package runtime + +import ( + "encoding/json" + "os" + + typesCons "github.com/pokt-network/pocket/consensus/types" + typesPers "github.com/pokt-network/pocket/persistence/types" + "github.com/pokt-network/pocket/shared/modules" + "google.golang.org/protobuf/types/known/timestamppb" +) + +var _ modules.ConsensusGenesisState = &Genesis{} +var _ modules.PersistenceGenesisState = &Genesis{} + +type Genesis struct { + ConsensusGenesisState *typesCons.ConsensusGenesisState `json:"consensus_genesis_state"` + PersistenceGenesisState *typesPers.PersistenceGenesisState `json:"persistence_genesis_state"` +} + +func (g *Genesis) ToShared() modules.GenesisState { + return modules.GenesisState{ + PersistenceGenesisState: g, + ConsensusGenesisState: g, + } +} + +func ParseGenesisJSON(genesisPath string) (genesis *Genesis, err error) { + data, err := os.ReadFile(genesisPath) + if err != nil { + return + } + + // general genesis file + genesis = new(Genesis) + err = json.Unmarshal(data, &genesis) + return +} + +// modules.ConsensusGenesisState + +func (g *Genesis) GetGenesisTime() *timestamppb.Timestamp { + return g.ConsensusGenesisState.GenesisTime +} +func (g *Genesis) GetChainId() string { + return g.ConsensusGenesisState.ChainId +} +func (g *Genesis) GetMaxBlockBytes() uint64 { + return g.ConsensusGenesisState.MaxBlockBytes +} + +// modules.PersistenceGenesisState + +func (g *Genesis) GetAccs() []modules.Account { + return g.PersistenceGenesisState.GetAccs() +} + +func (g *Genesis) GetAccPools() []modules.Account { + return g.PersistenceGenesisState.GetAccPools() +} + +func (g *Genesis) GetApps() []modules.Actor { + return g.PersistenceGenesisState.GetApps() +} + +func (g *Genesis) GetVals() []modules.Actor { + return g.PersistenceGenesisState.GetVals() +} + +func (g *Genesis) GetFish() []modules.Actor { + return g.PersistenceGenesisState.GetFish() +} + +func (g *Genesis) GetNodes() []modules.Actor { + return g.PersistenceGenesisState.GetNodes() +} + +func (g *Genesis) GetParameters() modules.Params { + return g.PersistenceGenesisState.GetParameters() +} diff --git a/runtime/runtime.go b/runtime/runtime.go new file mode 100644 index 000000000..f67a2cfe3 --- /dev/null +++ b/runtime/runtime.go @@ -0,0 +1,13 @@ +package runtime + +func Init(configPath, genesisPath string) (config *Config, genesis *Genesis, err error) { + config, err = ParseConfigJSON(configPath) + if err != nil { + return + } + config.configPath = configPath + config.genesisPath = genesisPath + + genesis, err = ParseGenesisJSON(genesisPath) + return +} diff --git a/shared/bus.go b/shared/bus.go index aead9647c..8867ad7fc 100644 --- a/shared/bus.go +++ b/shared/bus.go @@ -1,9 +1,10 @@ package shared import ( - "github.com/pokt-network/pocket/shared/debug" "log" + "github.com/pokt-network/pocket/shared/debug" + "github.com/pokt-network/pocket/shared/modules" ) @@ -21,6 +22,9 @@ type bus struct { utility modules.UtilityModule consensus modules.ConsensusModule telemetry modules.TelemetryModule + + config modules.Config + genesis modules.GenesisState } const ( @@ -28,6 +32,8 @@ const ( ) func CreateBus( + cfg modules.Config, + genesis modules.GenesisState, persistence modules.PersistenceModule, p2p modules.P2PModule, utility modules.UtilityModule, @@ -42,6 +48,9 @@ func CreateBus( utility: utility, consensus: consensus, telemetry: telemetry, + + config: cfg, + genesis: genesis, } modules := map[string]modules.Module{ @@ -69,13 +78,14 @@ func CreateBus( // // Example of usage: `app/client/main.go` // -// We want to use the pre2p module in isolation to communicate with nodes in the network. -// The pre2p module expects to retrieve a telemetry module through the bus to perform instrumentation, thus we need to inject a bus that can retrieve a telemetry module. -// However, we don't need telemetry for the dev client. -// Using `CreateBusWithOptionalModules`, we can create a bus with only pre2p and a NOOP telemetry module -// so that we can the pre2p module without any issues. -// +// We want to use the pre2p module in isolation to communicate with nodes in the network. +// The pre2p module expects to retrieve a telemetry module through the bus to perform instrumentation, thus we need to inject a bus that can retrieve a telemetry module. +// However, we don't need telemetry for the dev client. +// Using `CreateBusWithOptionalModules`, we can create a bus with only pre2p and a NOOP telemetry module +// so that we can the pre2p module without any issues. func CreateBusWithOptionalModules( + cfg modules.Config, + genesis modules.GenesisState, persistence modules.PersistenceModule, p2p modules.P2PModule, utility modules.UtilityModule, @@ -89,6 +99,8 @@ func CreateBusWithOptionalModules( utility: utility, consensus: consensus, telemetry: telemetry, + config: cfg, + genesis: genesis, } maybeSetModuleBus := func(mod modules.Module) { @@ -138,3 +150,10 @@ func (m bus) GetConsensusModule() modules.ConsensusModule { func (m bus) GetTelemetryModule() modules.TelemetryModule { return m.telemetry } + +func (m bus) GetConfig() modules.Config { + return m.config +} +func (m bus) GetGenesis() modules.GenesisState { + return m.genesis +} diff --git a/shared/modules/bus_module.go b/shared/modules/bus_module.go index 2c895ba26..acb1e7845 100644 --- a/shared/modules/bus_module.go +++ b/shared/modules/bus_module.go @@ -1,7 +1,6 @@ package modules import ( - "encoding/json" "github.com/pokt-network/pocket/shared/debug" ) @@ -24,6 +23,6 @@ type Bus interface { GetTelemetryModule() TelemetryModule // Configuration - GetConfig() map[string]json.RawMessage - GetGenesis() map[string]json.RawMessage + GetConfig() Config + GetGenesis() GenesisState } diff --git a/shared/modules/module.go b/shared/modules/module.go index 1255d5cba..f25507003 100644 --- a/shared/modules/module.go +++ b/shared/modules/module.go @@ -4,7 +4,8 @@ package modules // TODO(drewsky): Add `Create` function; pocket/issues/163 // TODO(drewsky): Do not embed this inside of modules but force it via an implicit cast at compile time type Module interface { - InitializableModule + GetModuleName() string + IntegratableModule InterruptableModule } @@ -18,9 +19,3 @@ type InterruptableModule interface { Start() error Stop() error } - -type InitializableModule interface { - GetModuleName() string - InitConfig(pathToConfigJSON string) (IConfig, error) - InitGenesis(pathToGenesisJSON string) (IGenesis, error) -} diff --git a/shared/node.go b/shared/node.go index 7d65b8dc2..74663917c 100644 --- a/shared/node.go +++ b/shared/node.go @@ -3,6 +3,7 @@ package shared import ( "log" + "github.com/pokt-network/pocket/runtime" "github.com/pokt-network/pocket/shared/debug" "github.com/pokt-network/pocket/telemetry" @@ -28,32 +29,38 @@ type Node struct { } func Create(configPath, genesisPath string) (n *Node, err error) { - persistenceMod, err := persistence.Create(configPath, genesisPath) + + cfg, genesis, err := runtime.Init(configPath, genesisPath) + if err != nil { + return nil, err + } + + persistenceMod, err := persistence.Create(cfg.Persistence, genesis.PersistenceGenesisState) if err != nil { return nil, err } - p2pMod, err := p2p.Create(configPath, genesisPath, false) + p2pMod, err := p2p.Create(cfg.P2P, false) if err != nil { return nil, err } - utilityMod, err := utility.Create(configPath, genesisPath) + utilityMod, err := utility.Create(cfg.Utility) if err != nil { return nil, err } - consensusMod, err := consensus.Create(configPath, genesisPath, false) + consensusMod, err := consensus.Create(cfg.Consensus, genesis.ConsensusGenesisState, false) if err != nil { return nil, err } - telemetryMod, err := telemetry.Create(configPath, genesisPath) + telemetryMod, err := telemetry.Create(cfg.Telemetry) if err != nil { return nil, err } - bus, err := CreateBus(persistenceMod, p2pMod, utilityMod, consensusMod, telemetryMod) + bus, err := CreateBus(cfg.ToShared(), genesis.ToShared(), persistenceMod, p2pMod, utilityMod, consensusMod, telemetryMod) if err != nil { return nil, err } @@ -164,11 +171,3 @@ func (node *Node) handleDebugEvent(anyMessage *anypb.Any) error { func (node *Node) GetModuleName() string { return MainModuleName } - -func (node *Node) InitConfig(pathToConfigJSON string) (modules.IConfig, error) { - return nil, nil -} - -func (node *Node) InitGenesis(pathToGenesisJSON string) (modules.IGenesis, error) { - return nil, nil -} diff --git a/shared/test_artifacts/generator.go b/shared/test_artifacts/generator.go index 912e9fe2b..1e5894a8c 100644 --- a/shared/test_artifacts/generator.go +++ b/shared/test_artifacts/generator.go @@ -2,13 +2,20 @@ package test_artifacts import ( "fmt" + "math/big" + "strconv" + typesPersistence "github.com/pokt-network/pocket/persistence/types" + "github.com/pokt-network/pocket/runtime" "github.com/pokt-network/pocket/shared/modules" "github.com/pokt-network/pocket/utility/types" - "math/big" - "strconv" + typesCons "github.com/pokt-network/pocket/consensus/types" + typesP2P "github.com/pokt-network/pocket/p2p/types" + typesPers "github.com/pokt-network/pocket/persistence/types" "github.com/pokt-network/pocket/shared/crypto" + typesTelemetry "github.com/pokt-network/pocket/telemetry" + typesUtil "github.com/pokt-network/pocket/utility/types" "google.golang.org/protobuf/types/known/timestamppb" ) @@ -34,64 +41,64 @@ var ( // TODO (Team) this is meant to be a **temporary** replacement for the recently deprecated // 'genesis config' option. We need to implement a real suite soon! func NewGenesisState(numValidators, numServiceNodes, numApplications, numFisherman int) (genesisState modules.GenesisState, validatorPrivateKeys []string) { - apps, appsPrivateKeys := NewActors(MockActorType_App, numApplications) - vals, validatorPrivateKeys := NewActors(MockActorType_Val, numValidators) - serviceNodes, snPrivateKeys := NewActors(MockActorType_Node, numServiceNodes) - fish, fishPrivateKeys := NewActors(MockActorType_Fish, numFisherman) + apps, appsPrivateKeys := NewActors(types.UtilActorType_App, numApplications) + vals, validatorPrivateKeys := NewActors(types.UtilActorType_Val, numValidators) + serviceNodes, snPrivateKeys := NewActors(types.UtilActorType_Node, numServiceNodes) + fish, fishPrivateKeys := NewActors(types.UtilActorType_Fish, numFisherman) return modules.GenesisState{ - ConsensusGenesisState: &MockConsensusGenesisState{ + ConsensusGenesisState: &typesCons.ConsensusGenesisState{ GenesisTime: timestamppb.Now(), ChainId: DefaultChainID, MaxBlockBytes: DefaultMaxBlockBytes, - Validators: vals, + Validators: typesCons.ToConsensusValidators(vals), }, - PersistenceGenesisState: &MockPersistenceGenesisState{ - Pools: NewPools(), - Accounts: NewAccounts(numValidators+numServiceNodes+numApplications+numFisherman, append(append(append(validatorPrivateKeys, snPrivateKeys...), fishPrivateKeys...), appsPrivateKeys...)...), // TODO(olshansky): clean this up - Applications: apps, - Validators: vals, - ServiceNodes: serviceNodes, - Fishermen: fish, - Params: DefaultParams(), + PersistenceGenesisState: &typesPers.PersistenceGenesisState{ + Pools: typesPers.ToPersistanceAccounts(NewPools()), + Accounts: typesPers.ToPersistanceAccounts(NewAccounts(numValidators+numServiceNodes+numApplications+numFisherman, append(append(append(validatorPrivateKeys, snPrivateKeys...), fishPrivateKeys...), appsPrivateKeys...)...)), // TODO(olshansky): clean this up + Applications: typesPers.ToPersistanceActors(apps), + Validators: typesPers.ToPersistanceActors(vals), + ServiceNodes: typesPers.ToPersistanceActors(serviceNodes), + Fishermen: typesPers.ToPersistanceActors(fish), + Params: typesPers.ToPersistanceParams(DefaultParams()), }, }, validatorPrivateKeys } -func NewDefaultConfigs(privateKeys []string) (configs []modules.Config) { +func NewDefaultConfigs(privateKeys []string) (configs []runtime.Config) { for i, pk := range privateKeys { configs = append(configs, NewDefaultConfig(i, pk)) } return } -func NewDefaultConfig(i int, pk string) modules.Config { - return modules.Config{ - Base: &modules.BaseConfig{ +func NewDefaultConfig(i int, pk string) runtime.Config { + return runtime.Config{ + Base: &runtime.BaseConfig{ RootDirectory: "/go/src/github.com/pocket-network", PrivateKey: pk, }, - Consensus: &MockConsensusConfig{ + Consensus: &typesCons.ConsensusConfig{ MaxMempoolBytes: 500000000, - PacemakerConfig: &MockPacemakerConfig{ + PacemakerConfig: &typesCons.PacemakerConfig{ TimeoutMsec: 5000, Manual: true, DebugTimeBetweenStepsMsec: 1000, }, PrivateKey: pk, }, - Utility: &MockUtilityConfig{}, - Persistence: &typesPersistence.PersistenceConfig{ + Utility: &typesUtil.UtilityConfig{}, + Persistence: &typesPers.PersistenceConfig{ PostgresUrl: "postgres://postgres:postgres@pocket-db:5432/postgres", NodeSchema: "node" + strconv.Itoa(i+1), BlockStorePath: "/var/blockstore", }, - P2P: &MockP2PConfig{ + P2P: &typesP2P.P2PConfig{ ConsensusPort: 8080, UseRainTree: true, IsEmptyConnectionType: false, PrivateKey: pk, }, - Telemetry: &MockTelemetryConfig{ + Telemetry: &typesTelemetry.TelemetryConfig{ Enabled: true, Address: "0.0.0.0:9000", Endpoint: "/metrics", @@ -102,13 +109,13 @@ func NewDefaultConfig(i int, pk string) modules.Config { func NewPools() (pools []modules.Account) { // TODO (Team) in the real testing suite, we need to populate the pool amounts dependent on the actors for _, name := range typesPersistence.Pool_Names_name { if name == typesPersistence.Pool_Names_FeeCollector.String() { - pools = append(pools, &MockAcc{ + pools = append(pools, &typesPers.Account{ Address: name, Amount: "0", }) continue } - pools = append(pools, &MockAcc{ + pools = append(pools, &typesPers.Account{ Address: name, Amount: DefaultAccountAmountString, }) @@ -123,7 +130,7 @@ func NewAccounts(n int, privateKeys ...string) (accounts []modules.Account) { pk, _ := crypto.NewPrivateKey(privateKeys[i]) addr = pk.Address().String() } - accounts = append(accounts, &MockAcc{ + accounts = append(accounts, &typesPers.Account{ Address: addr, Amount: DefaultAccountAmountString, }) @@ -131,10 +138,10 @@ func NewAccounts(n int, privateKeys ...string) (accounts []modules.Account) { return } -func NewActors(actorType MockActorType, n int) (actors []modules.Actor, privateKeys []string) { +func NewActors(actorType typesUtil.UtilActorType, n int) (actors []modules.Actor, privateKeys []string) { for i := 0; i < n; i++ { genericParam := fmt.Sprintf("node%d.consensus:8080", i+1) - if int32(actorType) == int32(MockActorType_App) { + if int32(actorType) == int32(types.UtilActorType_App) { genericParam = DefaultMaxRelaysString } actor, pk := NewDefaultActor(int32(actorType), genericParam) @@ -149,10 +156,10 @@ func NewDefaultActor(actorType int32, genericParam string) (actor modules.Actor, chains := DefaultChains if actorType == int32(typesPersistence.ActorType_Val) { chains = nil - } else if actorType == int32(MockActorType_App) { + } else if actorType == int32(types.UtilActorType_App) { genericParam = DefaultMaxRelaysString } - return &MockActor{ + return &typesPers.Actor{ Address: addr, PublicKey: pubKey, Chains: chains, @@ -161,7 +168,7 @@ func NewDefaultActor(actorType int32, genericParam string) (actor modules.Actor, PausedHeight: DefaultPauseHeight, UnstakingHeight: DefaultUnstakingHeight, Output: addr, - ActorType: MockActorType(actorType), + ActorType: typesPers.ActorType(actorType), }, privKey } diff --git a/shared/test_artifacts/gov.go b/shared/test_artifacts/gov.go index 84deeb1c0..febf92d25 100644 --- a/shared/test_artifacts/gov.go +++ b/shared/test_artifacts/gov.go @@ -1,9 +1,11 @@ package test_artifacts import ( + "math/big" + + typesPers "github.com/pokt-network/pocket/persistence/types" "github.com/pokt-network/pocket/shared/modules" "github.com/pokt-network/pocket/utility/types" - "math/big" "github.com/pokt-network/pocket/shared/crypto" ) @@ -15,7 +17,7 @@ var ( ) func DefaultParams() modules.Params { - return &MockParams{ + return &typesPers.Params{ BlocksPerSession: 4, AppMinimumStake: types.BigIntToString(big.NewInt(15000000000)), AppMaxChains: 15, diff --git a/shared/test_artifacts/mock.go b/shared/test_artifacts/mock.go deleted file mode 100644 index 99c61665d..000000000 --- a/shared/test_artifacts/mock.go +++ /dev/null @@ -1,909 +0,0 @@ -package test_artifacts - -import ( - "fmt" - "github.com/pokt-network/pocket/shared/modules" - "google.golang.org/protobuf/types/known/timestamppb" -) - -// This file contains *seemingly* necessary implementations of the shared interfaces in order to -// do things like: create a genesis file and perform testing where cross module implementations are needed -// (see call to action in generator.go to try to remove the cross module testing code) - -// TODO (Team) convert these to proper mocks -var _ modules.Actor = &MockActor{} -var _ modules.Account = &MockAcc{} - -type MockAcc struct { - Address string `json:"address"` - Amount string `json:"amount"` -} - -func (m *MockAcc) GetAddress() string { - return m.Address -} - -func (m *MockAcc) GetAmount() string { - return m.Amount -} - -type MockActor struct { - Address string `json:"address"` - PublicKey string `json:"public_key"` - Chains []string `json:"chains"` - GenericParam string `json:"generic_param"` - StakedAmount string `json:"staked_amount"` - PausedHeight int64 `json:"paused_height"` - UnstakingHeight int64 `json:"unstaking_height"` - Output string `json:"output"` - ActorType MockActorType `json:"actor_type"` -} - -const ( - MockActorType_App MockActorType = 0 - MockActorType_Node MockActorType = 1 - MockActorType_Fish MockActorType = 2 - MockActorType_Val MockActorType = 3 -) - -func (m *MockActor) GetAddress() string { - return m.Address -} - -func (m *MockActor) GetPublicKey() string { - return m.PublicKey -} - -func (m *MockActor) GetChains() []string { - return m.Chains -} - -func (m *MockActor) GetGenericParam() string { - return m.GenericParam -} - -func (m *MockActor) GetStakedAmount() string { - return m.StakedAmount -} - -func (m *MockActor) GetPausedHeight() int64 { - return m.PausedHeight -} - -func (m *MockActor) GetUnstakingHeight() int64 { - return m.UnstakingHeight -} - -func (m *MockActor) GetOutput() string { - return m.Output -} - -func (m *MockActor) GetActorTyp() modules.ActorType { - return m.ActorType -} - -type MockActorType int32 - -func (m MockActorType) String() string { - return fmt.Sprintf("%d", m) -} - -var _ modules.PersistenceGenesisState = &MockPersistenceGenesisState{} -var _ modules.ConsensusGenesisState = &MockConsensusGenesisState{} - -type MockConsensusGenesisState struct { - GenesisTime *timestamppb.Timestamp `json:"genesis_time"` - ChainId string `json:"chain_id"` - MaxBlockBytes uint64 `json:"max_block_bytes"` - Validators []modules.Actor `json:"validators"` -} - -func (m *MockConsensusGenesisState) GetGenesisTime() *timestamppb.Timestamp { - return m.GenesisTime -} - -func (m *MockConsensusGenesisState) GetChainId() string { - return m.ChainId -} - -func (m *MockConsensusGenesisState) GetMaxBlockBytes() uint64 { - return m.MaxBlockBytes -} - -type MockPersistenceGenesisState struct { - Accounts []modules.Account `json:"accounts"` - Pools []modules.Account `json:"pools"` - Validators []modules.Actor `json:"validators"` - Applications []modules.Actor `json:"applications"` - ServiceNodes []modules.Actor `json:"service_nodes"` - Fishermen []modules.Actor `json:"fishermen"` - Params modules.Params `json:"params"` -} - -func (m MockPersistenceGenesisState) GetAccs() []modules.Account { - return m.Accounts -} - -func (m MockPersistenceGenesisState) GetAccPools() []modules.Account { - return m.Pools -} - -func (m MockPersistenceGenesisState) GetApps() []modules.Actor { - return m.Applications -} - -func (m MockPersistenceGenesisState) GetVals() []modules.Actor { - return m.Validators -} - -func (m MockPersistenceGenesisState) GetFish() []modules.Actor { - return m.Fishermen -} - -func (m MockPersistenceGenesisState) GetNodes() []modules.Actor { - return m.ServiceNodes -} - -func (m MockPersistenceGenesisState) GetParameters() modules.Params { - return m.Params -} - -var _ modules.ConsensusConfig = &MockConsensusConfig{} -var _ modules.PacemakerConfig = &MockPacemakerConfig{} -var _ modules.PersistenceConfig = &MockPersistenceConfig{} - -type MockPersistenceConfig struct { - PostgresUrl string `json:"postgres_url"` - NodeSchema string `json:"node_schema"` - BlockStorePath string `json:"block_store_path"` -} - -func (m *MockPersistenceConfig) GetPostgresUrl() string { - return m.PostgresUrl -} - -func (m *MockPersistenceConfig) GetNodeSchema() string { - return m.NodeSchema -} - -func (m *MockPersistenceConfig) GetBlockStorePath() string { - return m.BlockStorePath -} - -type MockConsensusConfig struct { - MaxMempoolBytes uint64 `json:"max_mempool_bytes"` - PacemakerConfig *MockPacemakerConfig `json:"pacemaker_config"` - PrivateKey string `json:"private_key"` -} - -func (m *MockConsensusConfig) GetMaxMempoolBytes() uint64 { - return m.MaxMempoolBytes -} - -func (m *MockConsensusConfig) GetPaceMakerConfig() modules.PacemakerConfig { - return m.PacemakerConfig -} - -type MockPacemakerConfig struct { - TimeoutMsec uint64 `json:"timeout_msec"` - Manual bool `json:"manual"` - DebugTimeBetweenStepsMsec uint64 `json:"debug_time_between_steps_msec"` -} - -func (m *MockPacemakerConfig) SetTimeoutMsec(u uint64) { - m.TimeoutMsec = u -} - -func (m *MockPacemakerConfig) GetTimeoutMsec() uint64 { - return m.TimeoutMsec -} - -func (m *MockPacemakerConfig) GetManual() bool { - return m.Manual -} - -func (m *MockPacemakerConfig) GetDebugTimeBetweenStepsMsec() uint64 { - return m.DebugTimeBetweenStepsMsec -} - -var _ modules.P2PConfig = &MockP2PConfig{} - -type MockP2PConfig struct { - ConsensusPort uint32 `json:"consensus_port"` - UseRainTree bool `json:"use_rain_tree"` - IsEmptyConnectionType bool `json:"is_empty_connection_type"` - PrivateKey string `json:"private_key"` -} - -func (m *MockP2PConfig) GetConsensusPort() uint32 { - return m.ConsensusPort -} - -func (m *MockP2PConfig) GetUseRainTree() bool { - return m.UseRainTree -} - -func (m *MockP2PConfig) IsEmptyConnType() bool { - return m.IsEmptyConnectionType -} - -var _ modules.TelemetryConfig = &MockTelemetryConfig{} - -type MockTelemetryConfig struct { - Enabled bool `json:"enabled"` - Address string `json:"address"` - Endpoint string `json:"endpoint"` -} - -func (m *MockTelemetryConfig) GetEnabled() bool { - return m.Enabled -} - -func (m *MockTelemetryConfig) GetAddress() string { - return m.Address -} - -func (m *MockTelemetryConfig) GetEndpoint() string { - return m.Endpoint -} - -type MockUtilityConfig struct{} - -var _ modules.Params = &MockParams{} - -type MockParams struct { - //@gotags: pokt:"val_type=BIGINT" - BlocksPerSession int32 `protobuf:"varint,1,opt,name=blocks_per_session,json=blocksPerSession,proto3" json:"blocks_per_session,omitempty"` - //@gotags: pokt:"val_type=STRING" - AppMinimumStake string `protobuf:"bytes,2,opt,name=app_minimum_stake,json=appMinimumStake,proto3" json:"app_minimum_stake,omitempty"` - //@gotags: pokt:"val_type=SMALLINT" - AppMaxChains int32 `protobuf:"varint,3,opt,name=app_max_chains,json=appMaxChains,proto3" json:"app_max_chains,omitempty"` - //@gotags: pokt:"val_type=BIGINT" - AppBaselineStakeRate int32 `protobuf:"varint,4,opt,name=app_baseline_stake_rate,json=appBaselineStakeRate,proto3" json:"app_baseline_stake_rate,omitempty"` - //@gotags: pokt:"val_type=BIGINT" - AppStakingAdjustment int32 `protobuf:"varint,5,opt,name=app_staking_adjustment,json=appStakingAdjustment,proto3" json:"app_staking_adjustment,omitempty"` - //@gotags: pokt:"val_type=BIGINT" - AppUnstakingBlocks int32 `protobuf:"varint,6,opt,name=app_unstaking_blocks,json=appUnstakingBlocks,proto3" json:"app_unstaking_blocks,omitempty"` - //@gotags: pokt:"val_type=SMALLINT" - AppMinimumPauseBlocks int32 `protobuf:"varint,7,opt,name=app_minimum_pause_blocks,json=appMinimumPauseBlocks,proto3" json:"app_minimum_pause_blocks,omitempty"` - //@gotags: pokt:"val_type=BIGINT" - AppMaxPauseBlocks int32 `protobuf:"varint,8,opt,name=app_max_pause_blocks,json=appMaxPauseBlocks,proto3" json:"app_max_pause_blocks,omitempty"` - //@gotags: pokt:"val_type=STRING" - ServiceNodeMinimumStake string `protobuf:"bytes,9,opt,name=service_node_minimum_stake,json=serviceNodeMinimumStake,proto3" json:"service_node_minimum_stake,omitempty"` - //@gotags: pokt:"val_type=SMALLINT" - ServiceNodeMaxChains int32 `protobuf:"varint,10,opt,name=service_node_max_chains,json=serviceNodeMaxChains,proto3" json:"service_node_max_chains,omitempty"` - //@gotags: pokt:"val_type=BIGINT" - ServiceNodeUnstakingBlocks int32 `protobuf:"varint,11,opt,name=service_node_unstaking_blocks,json=serviceNodeUnstakingBlocks,proto3" json:"service_node_unstaking_blocks,omitempty"` - //@gotags: pokt:"val_type=SMALLINT" - ServiceNodeMinimumPauseBlocks int32 `protobuf:"varint,12,opt,name=service_node_minimum_pause_blocks,json=serviceNodeMinimumPauseBlocks,proto3" json:"service_node_minimum_pause_blocks,omitempty"` - //@gotags: pokt:"val_type=BIGINT" - ServiceNodeMaxPauseBlocks int32 `protobuf:"varint,13,opt,name=service_node_max_pause_blocks,json=serviceNodeMaxPauseBlocks,proto3" json:"service_node_max_pause_blocks,omitempty"` - //@gotags: pokt:"val_type=SMALLINT" - ServiceNodesPerSession int32 `protobuf:"varint,14,opt,name=service_nodes_per_session,json=serviceNodesPerSession,proto3" json:"service_nodes_per_session,omitempty"` - //@gotags: pokt:"val_type=STRING" - FishermanMinimumStake string `protobuf:"bytes,15,opt,name=fisherman_minimum_stake,json=fishermanMinimumStake,proto3" json:"fisherman_minimum_stake,omitempty"` - //@gotags: pokt:"val_type=SMALLINT" - FishermanMaxChains int32 `protobuf:"varint,16,opt,name=fisherman_max_chains,json=fishermanMaxChains,proto3" json:"fisherman_max_chains,omitempty"` - //@gotags: pokt:"val_type=BIGINT" - FishermanUnstakingBlocks int32 `protobuf:"varint,17,opt,name=fisherman_unstaking_blocks,json=fishermanUnstakingBlocks,proto3" json:"fisherman_unstaking_blocks,omitempty"` - //@gotags: pokt:"val_type=SMALLINT" - FishermanMinimumPauseBlocks int32 `protobuf:"varint,18,opt,name=fisherman_minimum_pause_blocks,json=fishermanMinimumPauseBlocks,proto3" json:"fisherman_minimum_pause_blocks,omitempty"` - //@gotags: pokt:"val_type=SMALLINT" - FishermanMaxPauseBlocks int32 `protobuf:"varint,19,opt,name=fisherman_max_pause_blocks,json=fishermanMaxPauseBlocks,proto3" json:"fisherman_max_pause_blocks,omitempty"` - //@gotags: pokt:"val_type=STRING" - ValidatorMinimumStake string `protobuf:"bytes,20,opt,name=validator_minimum_stake,json=validatorMinimumStake,proto3" json:"validator_minimum_stake,omitempty"` - //@gotags: pokt:"val_type=BIGINT" - ValidatorUnstakingBlocks int32 `protobuf:"varint,21,opt,name=validator_unstaking_blocks,json=validatorUnstakingBlocks,proto3" json:"validator_unstaking_blocks,omitempty"` - //@gotags: pokt:"val_type=SMALLINT" - ValidatorMinimumPauseBlocks int32 `protobuf:"varint,22,opt,name=validator_minimum_pause_blocks,json=validatorMinimumPauseBlocks,proto3" json:"validator_minimum_pause_blocks,omitempty"` - //@gotags: pokt:"val_type=SMALLINT" - ValidatorMaxPauseBlocks int32 `protobuf:"varint,23,opt,name=validator_max_pause_blocks,json=validatorMaxPauseBlocks,proto3" json:"validator_max_pause_blocks,omitempty"` - //@gotags: pokt:"val_type=SMALLINT" - ValidatorMaximumMissedBlocks int32 `protobuf:"varint,24,opt,name=validator_maximum_missed_blocks,json=validatorMaximumMissedBlocks,proto3" json:"validator_maximum_missed_blocks,omitempty"` - //@gotags: pokt:"val_type=SMALLINT" - ValidatorMaxEvidenceAgeInBlocks int32 `protobuf:"varint,25,opt,name=validator_max_evidence_age_in_blocks,json=validatorMaxEvidenceAgeInBlocks,proto3" json:"validator_max_evidence_age_in_blocks,omitempty"` - //@gotags: pokt:"val_type=SMALLINT" - ProposerPercentageOfFees int32 `protobuf:"varint,26,opt,name=proposer_percentage_of_fees,json=proposerPercentageOfFees,proto3" json:"proposer_percentage_of_fees,omitempty"` - //@gotags: pokt:"val_type=SMALLINT" - MissedBlocksBurnPercentage int32 `protobuf:"varint,27,opt,name=missed_blocks_burn_percentage,json=missedBlocksBurnPercentage,proto3" json:"missed_blocks_burn_percentage,omitempty"` - //@gotags: pokt:"val_type=SMALLINT" - DoubleSignBurnPercentage int32 `protobuf:"varint,28,opt,name=double_sign_burn_percentage,json=doubleSignBurnPercentage,proto3" json:"double_sign_burn_percentage,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageDoubleSignFee string `protobuf:"bytes,29,opt,name=message_double_sign_fee,json=messageDoubleSignFee,proto3" json:"message_double_sign_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageSendFee string `protobuf:"bytes,30,opt,name=message_send_fee,json=messageSendFee,proto3" json:"message_send_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageStakeFishermanFee string `protobuf:"bytes,31,opt,name=message_stake_fisherman_fee,json=messageStakeFishermanFee,proto3" json:"message_stake_fisherman_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageEditStakeFishermanFee string `protobuf:"bytes,32,opt,name=message_edit_stake_fisherman_fee,json=messageEditStakeFishermanFee,proto3" json:"message_edit_stake_fisherman_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageUnstakeFishermanFee string `protobuf:"bytes,33,opt,name=message_unstake_fisherman_fee,json=messageUnstakeFishermanFee,proto3" json:"message_unstake_fisherman_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessagePauseFishermanFee string `protobuf:"bytes,34,opt,name=message_pause_fisherman_fee,json=messagePauseFishermanFee,proto3" json:"message_pause_fisherman_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageUnpauseFishermanFee string `protobuf:"bytes,35,opt,name=message_unpause_fisherman_fee,json=messageUnpauseFishermanFee,proto3" json:"message_unpause_fisherman_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageFishermanPauseServiceNodeFee string `protobuf:"bytes,36,opt,name=message_fisherman_pause_service_node_fee,json=messageFishermanPauseServiceNodeFee,proto3" json:"message_fisherman_pause_service_node_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageTestScoreFee string `protobuf:"bytes,37,opt,name=message_test_score_fee,json=messageTestScoreFee,proto3" json:"message_test_score_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageProveTestScoreFee string `protobuf:"bytes,38,opt,name=message_prove_test_score_fee,json=messageProveTestScoreFee,proto3" json:"message_prove_test_score_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageStakeAppFee string `protobuf:"bytes,39,opt,name=message_stake_app_fee,json=messageStakeAppFee,proto3" json:"message_stake_app_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageEditStakeAppFee string `protobuf:"bytes,40,opt,name=message_edit_stake_app_fee,json=messageEditStakeAppFee,proto3" json:"message_edit_stake_app_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageUnstakeAppFee string `protobuf:"bytes,41,opt,name=message_unstake_app_fee,json=messageUnstakeAppFee,proto3" json:"message_unstake_app_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessagePauseAppFee string `protobuf:"bytes,42,opt,name=message_pause_app_fee,json=messagePauseAppFee,proto3" json:"message_pause_app_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageUnpauseAppFee string `protobuf:"bytes,43,opt,name=message_unpause_app_fee,json=messageUnpauseAppFee,proto3" json:"message_unpause_app_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageStakeValidatorFee string `protobuf:"bytes,44,opt,name=message_stake_validator_fee,json=messageStakeValidatorFee,proto3" json:"message_stake_validator_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageEditStakeValidatorFee string `protobuf:"bytes,45,opt,name=message_edit_stake_validator_fee,json=messageEditStakeValidatorFee,proto3" json:"message_edit_stake_validator_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageUnstakeValidatorFee string `protobuf:"bytes,46,opt,name=message_unstake_validator_fee,json=messageUnstakeValidatorFee,proto3" json:"message_unstake_validator_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessagePauseValidatorFee string `protobuf:"bytes,47,opt,name=message_pause_validator_fee,json=messagePauseValidatorFee,proto3" json:"message_pause_validator_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageUnpauseValidatorFee string `protobuf:"bytes,48,opt,name=message_unpause_validator_fee,json=messageUnpauseValidatorFee,proto3" json:"message_unpause_validator_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageStakeServiceNodeFee string `protobuf:"bytes,49,opt,name=message_stake_service_node_fee,json=messageStakeServiceNodeFee,proto3" json:"message_stake_service_node_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageEditStakeServiceNodeFee string `protobuf:"bytes,50,opt,name=message_edit_stake_service_node_fee,json=messageEditStakeServiceNodeFee,proto3" json:"message_edit_stake_service_node_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageUnstakeServiceNodeFee string `protobuf:"bytes,51,opt,name=message_unstake_service_node_fee,json=messageUnstakeServiceNodeFee,proto3" json:"message_unstake_service_node_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessagePauseServiceNodeFee string `protobuf:"bytes,52,opt,name=message_pause_service_node_fee,json=messagePauseServiceNodeFee,proto3" json:"message_pause_service_node_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageUnpauseServiceNodeFee string `protobuf:"bytes,53,opt,name=message_unpause_service_node_fee,json=messageUnpauseServiceNodeFee,proto3" json:"message_unpause_service_node_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageChangeParameterFee string `protobuf:"bytes,54,opt,name=message_change_parameter_fee,json=messageChangeParameterFee,proto3" json:"message_change_parameter_fee,omitempty"` - //@gotags: pokt:"val_type=STRING" - AclOwner string `protobuf:"bytes,55,opt,name=acl_owner,json=aclOwner,proto3" json:"acl_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - BlocksPerSessionOwner string `protobuf:"bytes,56,opt,name=blocks_per_session_owner,json=blocksPerSessionOwner,proto3" json:"blocks_per_session_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - AppMinimumStakeOwner string `protobuf:"bytes,57,opt,name=app_minimum_stake_owner,json=appMinimumStakeOwner,proto3" json:"app_minimum_stake_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - AppMaxChainsOwner string `protobuf:"bytes,58,opt,name=app_max_chains_owner,json=appMaxChainsOwner,proto3" json:"app_max_chains_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - AppBaselineStakeRateOwner string `protobuf:"bytes,59,opt,name=app_baseline_stake_rate_owner,json=appBaselineStakeRateOwner,proto3" json:"app_baseline_stake_rate_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - AppStakingAdjustmentOwner string `protobuf:"bytes,60,opt,name=app_staking_adjustment_owner,json=appStakingAdjustmentOwner,proto3" json:"app_staking_adjustment_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - AppUnstakingBlocksOwner string `protobuf:"bytes,61,opt,name=app_unstaking_blocks_owner,json=appUnstakingBlocksOwner,proto3" json:"app_unstaking_blocks_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - AppMinimumPauseBlocksOwner string `protobuf:"bytes,62,opt,name=app_minimum_pause_blocks_owner,json=appMinimumPauseBlocksOwner,proto3" json:"app_minimum_pause_blocks_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - AppMaxPausedBlocksOwner string `protobuf:"bytes,63,opt,name=app_max_paused_blocks_owner,json=appMaxPausedBlocksOwner,proto3" json:"app_max_paused_blocks_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - ServiceNodeMinimumStakeOwner string `protobuf:"bytes,64,opt,name=service_node_minimum_stake_owner,json=serviceNodeMinimumStakeOwner,proto3" json:"service_node_minimum_stake_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - ServiceNodeMaxChainsOwner string `protobuf:"bytes,65,opt,name=service_node_max_chains_owner,json=serviceNodeMaxChainsOwner,proto3" json:"service_node_max_chains_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - ServiceNodeUnstakingBlocksOwner string `protobuf:"bytes,66,opt,name=service_node_unstaking_blocks_owner,json=serviceNodeUnstakingBlocksOwner,proto3" json:"service_node_unstaking_blocks_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - ServiceNodeMinimumPauseBlocksOwner string `protobuf:"bytes,67,opt,name=service_node_minimum_pause_blocks_owner,json=serviceNodeMinimumPauseBlocksOwner,proto3" json:"service_node_minimum_pause_blocks_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - ServiceNodeMaxPausedBlocksOwner string `protobuf:"bytes,68,opt,name=service_node_max_paused_blocks_owner,json=serviceNodeMaxPausedBlocksOwner,proto3" json:"service_node_max_paused_blocks_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - ServiceNodesPerSessionOwner string `protobuf:"bytes,69,opt,name=service_nodes_per_session_owner,json=serviceNodesPerSessionOwner,proto3" json:"service_nodes_per_session_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - FishermanMinimumStakeOwner string `protobuf:"bytes,70,opt,name=fisherman_minimum_stake_owner,json=fishermanMinimumStakeOwner,proto3" json:"fisherman_minimum_stake_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - FishermanMaxChainsOwner string `protobuf:"bytes,71,opt,name=fisherman_max_chains_owner,json=fishermanMaxChainsOwner,proto3" json:"fisherman_max_chains_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - FishermanUnstakingBlocksOwner string `protobuf:"bytes,72,opt,name=fisherman_unstaking_blocks_owner,json=fishermanUnstakingBlocksOwner,proto3" json:"fisherman_unstaking_blocks_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - FishermanMinimumPauseBlocksOwner string `protobuf:"bytes,73,opt,name=fisherman_minimum_pause_blocks_owner,json=fishermanMinimumPauseBlocksOwner,proto3" json:"fisherman_minimum_pause_blocks_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - FishermanMaxPausedBlocksOwner string `protobuf:"bytes,74,opt,name=fisherman_max_paused_blocks_owner,json=fishermanMaxPausedBlocksOwner,proto3" json:"fisherman_max_paused_blocks_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - ValidatorMinimumStakeOwner string `protobuf:"bytes,75,opt,name=validator_minimum_stake_owner,json=validatorMinimumStakeOwner,proto3" json:"validator_minimum_stake_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - ValidatorUnstakingBlocksOwner string `protobuf:"bytes,76,opt,name=validator_unstaking_blocks_owner,json=validatorUnstakingBlocksOwner,proto3" json:"validator_unstaking_blocks_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - ValidatorMinimumPauseBlocksOwner string `protobuf:"bytes,77,opt,name=validator_minimum_pause_blocks_owner,json=validatorMinimumPauseBlocksOwner,proto3" json:"validator_minimum_pause_blocks_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - ValidatorMaxPausedBlocksOwner string `protobuf:"bytes,78,opt,name=validator_max_paused_blocks_owner,json=validatorMaxPausedBlocksOwner,proto3" json:"validator_max_paused_blocks_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - ValidatorMaximumMissedBlocksOwner string `protobuf:"bytes,79,opt,name=validator_maximum_missed_blocks_owner,json=validatorMaximumMissedBlocksOwner,proto3" json:"validator_maximum_missed_blocks_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - ValidatorMaxEvidenceAgeInBlocksOwner string `protobuf:"bytes,80,opt,name=validator_max_evidence_age_in_blocks_owner,json=validatorMaxEvidenceAgeInBlocksOwner,proto3" json:"validator_max_evidence_age_in_blocks_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - ProposerPercentageOfFeesOwner string `protobuf:"bytes,81,opt,name=proposer_percentage_of_fees_owner,json=proposerPercentageOfFeesOwner,proto3" json:"proposer_percentage_of_fees_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MissedBlocksBurnPercentageOwner string `protobuf:"bytes,82,opt,name=missed_blocks_burn_percentage_owner,json=missedBlocksBurnPercentageOwner,proto3" json:"missed_blocks_burn_percentage_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - DoubleSignBurnPercentageOwner string `protobuf:"bytes,83,opt,name=double_sign_burn_percentage_owner,json=doubleSignBurnPercentageOwner,proto3" json:"double_sign_burn_percentage_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageDoubleSignFeeOwner string `protobuf:"bytes,84,opt,name=message_double_sign_fee_owner,json=messageDoubleSignFeeOwner,proto3" json:"message_double_sign_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageSendFeeOwner string `protobuf:"bytes,85,opt,name=message_send_fee_owner,json=messageSendFeeOwner,proto3" json:"message_send_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageStakeFishermanFeeOwner string `protobuf:"bytes,86,opt,name=message_stake_fisherman_fee_owner,json=messageStakeFishermanFeeOwner,proto3" json:"message_stake_fisherman_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageEditStakeFishermanFeeOwner string `protobuf:"bytes,87,opt,name=message_edit_stake_fisherman_fee_owner,json=messageEditStakeFishermanFeeOwner,proto3" json:"message_edit_stake_fisherman_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageUnstakeFishermanFeeOwner string `protobuf:"bytes,88,opt,name=message_unstake_fisherman_fee_owner,json=messageUnstakeFishermanFeeOwner,proto3" json:"message_unstake_fisherman_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessagePauseFishermanFeeOwner string `protobuf:"bytes,89,opt,name=message_pause_fisherman_fee_owner,json=messagePauseFishermanFeeOwner,proto3" json:"message_pause_fisherman_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageUnpauseFishermanFeeOwner string `protobuf:"bytes,90,opt,name=message_unpause_fisherman_fee_owner,json=messageUnpauseFishermanFeeOwner,proto3" json:"message_unpause_fisherman_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageFishermanPauseServiceNodeFeeOwner string `protobuf:"bytes,91,opt,name=message_fisherman_pause_service_node_fee_owner,json=messageFishermanPauseServiceNodeFeeOwner,proto3" json:"message_fisherman_pause_service_node_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageTestScoreFeeOwner string `protobuf:"bytes,92,opt,name=message_test_score_fee_owner,json=messageTestScoreFeeOwner,proto3" json:"message_test_score_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageProveTestScoreFeeOwner string `protobuf:"bytes,93,opt,name=message_prove_test_score_fee_owner,json=messageProveTestScoreFeeOwner,proto3" json:"message_prove_test_score_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageStakeAppFeeOwner string `protobuf:"bytes,94,opt,name=message_stake_app_fee_owner,json=messageStakeAppFeeOwner,proto3" json:"message_stake_app_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageEditStakeAppFeeOwner string `protobuf:"bytes,95,opt,name=message_edit_stake_app_fee_owner,json=messageEditStakeAppFeeOwner,proto3" json:"message_edit_stake_app_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageUnstakeAppFeeOwner string `protobuf:"bytes,96,opt,name=message_unstake_app_fee_owner,json=messageUnstakeAppFeeOwner,proto3" json:"message_unstake_app_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessagePauseAppFeeOwner string `protobuf:"bytes,97,opt,name=message_pause_app_fee_owner,json=messagePauseAppFeeOwner,proto3" json:"message_pause_app_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageUnpauseAppFeeOwner string `protobuf:"bytes,98,opt,name=message_unpause_app_fee_owner,json=messageUnpauseAppFeeOwner,proto3" json:"message_unpause_app_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageStakeValidatorFeeOwner string `protobuf:"bytes,99,opt,name=message_stake_validator_fee_owner,json=messageStakeValidatorFeeOwner,proto3" json:"message_stake_validator_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageEditStakeValidatorFeeOwner string `protobuf:"bytes,100,opt,name=message_edit_stake_validator_fee_owner,json=messageEditStakeValidatorFeeOwner,proto3" json:"message_edit_stake_validator_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageUnstakeValidatorFeeOwner string `protobuf:"bytes,101,opt,name=message_unstake_validator_fee_owner,json=messageUnstakeValidatorFeeOwner,proto3" json:"message_unstake_validator_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessagePauseValidatorFeeOwner string `protobuf:"bytes,102,opt,name=message_pause_validator_fee_owner,json=messagePauseValidatorFeeOwner,proto3" json:"message_pause_validator_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageUnpauseValidatorFeeOwner string `protobuf:"bytes,103,opt,name=message_unpause_validator_fee_owner,json=messageUnpauseValidatorFeeOwner,proto3" json:"message_unpause_validator_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageStakeServiceNodeFeeOwner string `protobuf:"bytes,104,opt,name=message_stake_service_node_fee_owner,json=messageStakeServiceNodeFeeOwner,proto3" json:"message_stake_service_node_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageEditStakeServiceNodeFeeOwner string `protobuf:"bytes,105,opt,name=message_edit_stake_service_node_fee_owner,json=messageEditStakeServiceNodeFeeOwner,proto3" json:"message_edit_stake_service_node_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageUnstakeServiceNodeFeeOwner string `protobuf:"bytes,106,opt,name=message_unstake_service_node_fee_owner,json=messageUnstakeServiceNodeFeeOwner,proto3" json:"message_unstake_service_node_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessagePauseServiceNodeFeeOwner string `protobuf:"bytes,107,opt,name=message_pause_service_node_fee_owner,json=messagePauseServiceNodeFeeOwner,proto3" json:"message_pause_service_node_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageUnpauseServiceNodeFeeOwner string `protobuf:"bytes,108,opt,name=message_unpause_service_node_fee_owner,json=messageUnpauseServiceNodeFeeOwner,proto3" json:"message_unpause_service_node_fee_owner,omitempty"` - //@gotags: pokt:"val_type=STRING" - MessageChangeParameterFeeOwner string `protobuf:"bytes,109,opt,name=message_change_parameter_fee_owner,json=messageChangeParameterFeeOwner,proto3" json:"message_change_parameter_fee_owner,omitempty"` -} - -func (m *MockParams) GetBlocksPerSession() int32 { - return m.BlocksPerSession -} - -func (m *MockParams) GetAppMinimumStake() string { - return m.AppMinimumStake -} - -func (m *MockParams) GetAppMaxChains() int32 { - return m.AppMaxChains -} - -func (m *MockParams) GetAppBaselineStakeRate() int32 { - return m.AppBaselineStakeRate -} - -func (m *MockParams) GetAppStakingAdjustment() int32 { - return m.AppStakingAdjustment -} - -func (m *MockParams) GetAppUnstakingBlocks() int32 { - return m.AppUnstakingBlocks -} - -func (m *MockParams) GetAppMinimumPauseBlocks() int32 { - return m.AppMinimumPauseBlocks -} - -func (m *MockParams) GetAppMaxPauseBlocks() int32 { - return m.AppMaxPauseBlocks -} - -func (m *MockParams) GetServiceNodeMinimumStake() string { - return m.ServiceNodeMinimumStake -} - -func (m *MockParams) GetServiceNodeMaxChains() int32 { - return m.ServiceNodeMaxChains -} - -func (m *MockParams) GetServiceNodeUnstakingBlocks() int32 { - return m.ServiceNodeUnstakingBlocks -} - -func (m *MockParams) GetServiceNodeMinimumPauseBlocks() int32 { - return m.ServiceNodeMinimumPauseBlocks -} - -func (m *MockParams) GetServiceNodeMaxPauseBlocks() int32 { - return m.ServiceNodeMaxPauseBlocks -} - -func (m *MockParams) GetServiceNodesPerSession() int32 { - return m.ServiceNodesPerSession -} - -func (m *MockParams) GetFishermanMinimumStake() string { - return m.FishermanMinimumStake -} - -func (m *MockParams) GetFishermanMaxChains() int32 { - return m.FishermanMaxChains -} - -func (m *MockParams) GetFishermanUnstakingBlocks() int32 { - return m.FishermanUnstakingBlocks -} - -func (m *MockParams) GetFishermanMinimumPauseBlocks() int32 { - return m.FishermanMinimumPauseBlocks -} - -func (m *MockParams) GetFishermanMaxPauseBlocks() int32 { - return m.FishermanMaxPauseBlocks -} - -func (m *MockParams) GetValidatorMinimumStake() string { - return m.ValidatorMinimumStake -} - -func (m *MockParams) GetValidatorUnstakingBlocks() int32 { - return m.ValidatorUnstakingBlocks -} - -func (m *MockParams) GetValidatorMinimumPauseBlocks() int32 { - return m.ValidatorMinimumPauseBlocks -} - -func (m *MockParams) GetValidatorMaxPauseBlocks() int32 { - return m.ValidatorMaxPauseBlocks -} - -func (m *MockParams) GetValidatorMaximumMissedBlocks() int32 { - return m.ValidatorMaximumMissedBlocks -} - -func (m *MockParams) GetValidatorMaxEvidenceAgeInBlocks() int32 { - return m.ValidatorMaxEvidenceAgeInBlocks -} - -func (m *MockParams) GetProposerPercentageOfFees() int32 { - return m.ProposerPercentageOfFees -} - -func (m *MockParams) GetMissedBlocksBurnPercentage() int32 { - return m.MissedBlocksBurnPercentage -} - -func (m *MockParams) GetDoubleSignBurnPercentage() int32 { - return m.DoubleSignBurnPercentage -} - -func (m *MockParams) GetMessageDoubleSignFee() string { - return m.MessageDoubleSignFee -} - -func (m *MockParams) GetMessageSendFee() string { - return m.MessageSendFee -} - -func (m *MockParams) GetMessageStakeFishermanFee() string { - return m.MessageStakeFishermanFee -} - -func (m *MockParams) GetMessageEditStakeFishermanFee() string { - return m.MessageEditStakeFishermanFee -} - -func (m *MockParams) GetMessageUnstakeFishermanFee() string { - return m.MessageUnstakeFishermanFee -} - -func (m *MockParams) GetMessagePauseFishermanFee() string { - return m.MessagePauseFishermanFee -} - -func (m *MockParams) GetMessageUnpauseFishermanFee() string { - return m.MessageUnpauseFishermanFee -} - -func (m *MockParams) GetMessageFishermanPauseServiceNodeFee() string { - return m.MessageFishermanPauseServiceNodeFee -} - -func (m *MockParams) GetMessageTestScoreFee() string { - return m.MessageTestScoreFee -} - -func (m *MockParams) GetMessageProveTestScoreFee() string { - return m.MessageProveTestScoreFee -} - -func (m *MockParams) GetMessageStakeAppFee() string { - return m.MessageStakeAppFee -} - -func (m *MockParams) GetMessageEditStakeAppFee() string { - return m.MessageEditStakeAppFee -} - -func (m *MockParams) GetMessageUnstakeAppFee() string { - return m.MessageUnstakeAppFee -} - -func (m *MockParams) GetMessagePauseAppFee() string { - return m.MessagePauseAppFee -} - -func (m *MockParams) GetMessageUnpauseAppFee() string { - return m.MessageUnpauseAppFee -} - -func (m *MockParams) GetMessageStakeValidatorFee() string { - return m.MessageStakeValidatorFee -} - -func (m *MockParams) GetMessageEditStakeValidatorFee() string { - return m.MessageEditStakeValidatorFee -} - -func (m *MockParams) GetMessageUnstakeValidatorFee() string { - return m.MessageUnstakeValidatorFee -} - -func (m *MockParams) GetMessagePauseValidatorFee() string { - return m.MessagePauseValidatorFee -} - -func (m *MockParams) GetMessageUnpauseValidatorFee() string { - return m.MessageUnpauseValidatorFee -} - -func (m *MockParams) GetMessageStakeServiceNodeFee() string { - return m.MessageStakeServiceNodeFee -} - -func (m *MockParams) GetMessageEditStakeServiceNodeFee() string { - return m.MessageEditStakeServiceNodeFee -} - -func (m *MockParams) GetMessageUnstakeServiceNodeFee() string { - return m.MessageUnstakeServiceNodeFee -} - -func (m *MockParams) GetMessagePauseServiceNodeFee() string { - return m.MessagePauseServiceNodeFee -} - -func (m *MockParams) GetMessageUnpauseServiceNodeFee() string { - return m.MessageUnpauseServiceNodeFee -} - -func (m *MockParams) GetMessageChangeParameterFee() string { - return m.MessageChangeParameterFee -} - -func (m *MockParams) GetAclOwner() string { - return m.AclOwner -} - -func (m *MockParams) GetBlocksPerSessionOwner() string { - return m.BlocksPerSessionOwner -} - -func (m *MockParams) GetAppMinimumStakeOwner() string { - return m.AppMinimumStakeOwner -} - -func (m *MockParams) GetAppMaxChainsOwner() string { - return m.AppMaxChainsOwner -} - -func (m *MockParams) GetAppBaselineStakeRateOwner() string { - return m.AppBaselineStakeRateOwner -} - -func (m *MockParams) GetAppStakingAdjustmentOwner() string { - return m.AppStakingAdjustmentOwner -} - -func (m *MockParams) GetAppUnstakingBlocksOwner() string { - return m.AppUnstakingBlocksOwner -} - -func (m *MockParams) GetAppMinimumPauseBlocksOwner() string { - return m.AppMinimumPauseBlocksOwner -} - -func (m *MockParams) GetAppMaxPausedBlocksOwner() string { - return m.AppMaxPausedBlocksOwner -} - -func (m *MockParams) GetServiceNodeMinimumStakeOwner() string { - return m.ServiceNodeMinimumStakeOwner -} - -func (m *MockParams) GetServiceNodeMaxChainsOwner() string { - return m.ServiceNodeMaxChainsOwner -} - -func (m *MockParams) GetServiceNodeUnstakingBlocksOwner() string { - return m.ServiceNodeUnstakingBlocksOwner -} - -func (m *MockParams) GetServiceNodeMinimumPauseBlocksOwner() string { - return m.ServiceNodeMinimumPauseBlocksOwner -} - -func (m *MockParams) GetServiceNodeMaxPausedBlocksOwner() string { - return m.ServiceNodeMaxPausedBlocksOwner -} - -func (m *MockParams) GetServiceNodesPerSessionOwner() string { - return m.ServiceNodesPerSessionOwner -} - -func (m *MockParams) GetFishermanMinimumStakeOwner() string { - return m.FishermanMinimumStakeOwner -} - -func (m *MockParams) GetFishermanMaxChainsOwner() string { - return m.FishermanMaxChainsOwner -} - -func (m *MockParams) GetFishermanUnstakingBlocksOwner() string { - return m.FishermanUnstakingBlocksOwner -} - -func (m *MockParams) GetFishermanMinimumPauseBlocksOwner() string { - return m.FishermanMinimumPauseBlocksOwner -} - -func (m *MockParams) GetFishermanMaxPausedBlocksOwner() string { - return m.FishermanMaxPausedBlocksOwner -} - -func (m *MockParams) GetValidatorMinimumStakeOwner() string { - return m.ValidatorMinimumStakeOwner -} - -func (m *MockParams) GetValidatorUnstakingBlocksOwner() string { - return m.ValidatorUnstakingBlocksOwner -} - -func (m *MockParams) GetValidatorMinimumPauseBlocksOwner() string { - return m.ValidatorMinimumPauseBlocksOwner -} - -func (m *MockParams) GetValidatorMaxPausedBlocksOwner() string { - return m.ValidatorMaxPausedBlocksOwner -} - -func (m *MockParams) GetValidatorMaximumMissedBlocksOwner() string { - return m.ValidatorMaximumMissedBlocksOwner -} - -func (m *MockParams) GetValidatorMaxEvidenceAgeInBlocksOwner() string { - return m.ValidatorMaxEvidenceAgeInBlocksOwner -} - -func (m *MockParams) GetProposerPercentageOfFeesOwner() string { - return m.ProposerPercentageOfFeesOwner -} - -func (m *MockParams) GetMissedBlocksBurnPercentageOwner() string { - return m.MissedBlocksBurnPercentageOwner -} - -func (m *MockParams) GetDoubleSignBurnPercentageOwner() string { - return m.DoubleSignBurnPercentageOwner -} - -func (m *MockParams) GetMessageDoubleSignFeeOwner() string { - return m.MessageDoubleSignFeeOwner -} - -func (m *MockParams) GetMessageSendFeeOwner() string { - return m.MessageSendFeeOwner -} - -func (m *MockParams) GetMessageStakeFishermanFeeOwner() string { - return m.MessageStakeFishermanFeeOwner -} - -func (m *MockParams) GetMessageEditStakeFishermanFeeOwner() string { - return m.MessageEditStakeFishermanFeeOwner -} - -func (m *MockParams) GetMessageUnstakeFishermanFeeOwner() string { - return m.MessageUnstakeFishermanFeeOwner -} - -func (m *MockParams) GetMessagePauseFishermanFeeOwner() string { - return m.MessagePauseFishermanFeeOwner -} - -func (m *MockParams) GetMessageUnpauseFishermanFeeOwner() string { - return m.MessageUnpauseFishermanFeeOwner -} - -func (m *MockParams) GetMessageFishermanPauseServiceNodeFeeOwner() string { - return m.MessageFishermanPauseServiceNodeFeeOwner -} - -func (m *MockParams) GetMessageTestScoreFeeOwner() string { - return m.MessageTestScoreFeeOwner -} - -func (m *MockParams) GetMessageProveTestScoreFeeOwner() string { - return m.MessageProveTestScoreFeeOwner -} - -func (m *MockParams) GetMessageStakeAppFeeOwner() string { - return m.MessageStakeAppFeeOwner -} - -func (m *MockParams) GetMessageEditStakeAppFeeOwner() string { - return m.MessageEditStakeAppFeeOwner -} - -func (m *MockParams) GetMessageUnstakeAppFeeOwner() string { - return m.MessageUnstakeAppFeeOwner -} - -func (m *MockParams) GetMessagePauseAppFeeOwner() string { - return m.MessagePauseAppFeeOwner -} - -func (m *MockParams) GetMessageUnpauseAppFeeOwner() string { - return m.MessageUnpauseAppFeeOwner -} - -func (m *MockParams) GetMessageStakeValidatorFeeOwner() string { - return m.MessageStakeValidatorFeeOwner -} - -func (m *MockParams) GetMessageEditStakeValidatorFeeOwner() string { - return m.MessageEditStakeValidatorFeeOwner -} - -func (m *MockParams) GetMessageUnstakeValidatorFeeOwner() string { - return m.MessageUnstakeValidatorFeeOwner -} - -func (m *MockParams) GetMessagePauseValidatorFeeOwner() string { - return m.MessagePauseValidatorFeeOwner -} - -func (m *MockParams) GetMessageUnpauseValidatorFeeOwner() string { - return m.MessageUnpauseValidatorFeeOwner -} - -func (m *MockParams) GetMessageStakeServiceNodeFeeOwner() string { - return m.MessageStakeServiceNodeFeeOwner -} - -func (m *MockParams) GetMessageEditStakeServiceNodeFeeOwner() string { - return m.MessageEditStakeServiceNodeFeeOwner -} - -func (m *MockParams) GetMessageUnstakeServiceNodeFeeOwner() string { - return m.MessageUnstakeServiceNodeFeeOwner -} - -func (m *MockParams) GetMessagePauseServiceNodeFeeOwner() string { - return m.MessagePauseServiceNodeFeeOwner -} - -func (m *MockParams) GetMessageUnpauseServiceNodeFeeOwner() string { - return m.MessageUnpauseServiceNodeFeeOwner -} - -func (m *MockParams) GetMessageChangeParameterFeeOwner() string { - return m.MessageChangeParameterFeeOwner -} diff --git a/telemetry/module.go b/telemetry/module.go index 2c70d7052..46d15f93c 100644 --- a/telemetry/module.go +++ b/telemetry/module.go @@ -1,10 +1,6 @@ package telemetry import ( - "encoding/json" - "io/ioutil" - "log" - "github.com/pokt-network/pocket/shared/modules" ) @@ -16,38 +12,17 @@ const ( ) // TODO(pocket/issues/99): Add a switch statement and configuration variable when support for other telemetry modules is added. -func Create(configPath, genesisPath string) (modules.TelemetryModule, error) { - tm := new(telemetryModule) - c, err := tm.InitConfig(configPath) - if err != nil { - return nil, err - } - cfg := c.(*TelemetryConfig) - if cfg.GetEnabled() { - return CreatePrometheusTelemetryModule(cfg) +func Create(cfg modules.TelemetryConfig) (modules.TelemetryModule, error) { + moduleCfg := cfg.(*TelemetryConfig) + if moduleCfg.GetEnabled() { + return CreatePrometheusTelemetryModule(moduleCfg) } else { - return CreateNoopTelemetryModule(cfg) + return CreateNoopTelemetryModule(moduleCfg) } } type telemetryModule struct{} -func (t *telemetryModule) InitConfig(pathToConfigJSON string) (config modules.IConfig, err error) { - data, err := ioutil.ReadFile(pathToConfigJSON) - if err != nil { - return - } - // over arching configuration file - rawJSON := make(map[string]json.RawMessage) - if err = json.Unmarshal(data, &rawJSON); err != nil { - log.Fatalf("[ERROR] an error occurred unmarshalling the %s file: %v", pathToConfigJSON, err.Error()) - } - // telemetry specific configuration file - config = new(TelemetryConfig) - err = json.Unmarshal(rawJSON[t.GetModuleName()], config) - return -} - func (t *telemetryModule) GetModuleName() string { return TelemetryModuleName } func (t *telemetryModule) InitGenesis(_ string) (genesis modules.IGenesis, err error) { return } func (t *telemetryModule) SetBus(bus modules.Bus) {} diff --git a/telemetry/noop_module.go b/telemetry/noop_module.go index a1cbeb9d1..1d23ac003 100644 --- a/telemetry/noop_module.go +++ b/telemetry/noop_module.go @@ -39,14 +39,6 @@ func (m *NoopTelemetryModule) Stop() error { return nil } -func (m *NoopTelemetryModule) InitConfig(pathToConfigJSON string) (config modules.IConfig, err error) { - return // No-op -} - -func (m *NoopTelemetryModule) InitGenesis(pathToGenesisJSON string) (genesis modules.IGenesis, err error) { - return // No-op -} - func (m *NoopTelemetryModule) GetModuleName() string { return NoOpModuleName } diff --git a/telemetry/prometheus_module.go b/telemetry/prometheus_module.go index 1c4ff2f6c..ab6b46740 100644 --- a/telemetry/prometheus_module.go +++ b/telemetry/prometheus_module.go @@ -59,14 +59,6 @@ func (m *PrometheusTelemetryModule) Stop() error { return nil } -func (m *PrometheusTelemetryModule) InitConfig(pathToConfigJSON string) (config modules.IConfig, err error) { - return // No-op -} - -func (m *PrometheusTelemetryModule) InitGenesis(pathToGenesisJSON string) (genesis modules.IGenesis, err error) { - return // No-op -} - func (m *PrometheusTelemetryModule) SetBus(bus modules.Bus) { m.bus = bus } diff --git a/utility/module.go b/utility/module.go index 9cfc65a96..e780b0816 100644 --- a/utility/module.go +++ b/utility/module.go @@ -20,21 +20,13 @@ const ( UtilityModuleName = "utility" ) -func Create(configPath, genesisPath string) (modules.UtilityModule, error) { +func Create(cfg modules.UtilityConfig) (modules.UtilityModule, error) { return &UtilityModule{ // TODO: Add `maxTransactionBytes` and `maxTransactions` to cfg.Utility Mempool: types.NewMempool(1000, 1000), }, nil } -func (u *UtilityModule) InitConfig(pathToConfigJSON string) (config modules.IConfig, err error) { - return // No-op -} - -func (u *UtilityModule) InitGenesis(pathToGenesisJSON string) (genesis modules.IGenesis, err error) { - return // No-op -} - func (u *UtilityModule) Start() error { return nil } diff --git a/utility/test/module_test.go b/utility/test/module_test.go index e8785baf1..ba7ae7014 100644 --- a/utility/test/module_test.go +++ b/utility/test/module_test.go @@ -8,6 +8,7 @@ import ( "os" "testing" + typesPers "github.com/pokt-network/pocket/persistence/types" "github.com/pokt-network/pocket/shared/test_artifacts" utilTypes "github.com/pokt-network/pocket/utility/types" @@ -63,7 +64,7 @@ func NewTestingUtilityContext(t *testing.T, height int64) utility.UtilityContext // TODO(andrew): Take in `t` and fail the test if there's an error func newTestPersistenceModule(databaseUrl string) modules.PersistenceModule { cfg := modules.Config{ - Persistence: &test_artifacts.MockPersistenceConfig{ + Persistence: &typesPers.PersistenceConfig{ PostgresUrl: databaseUrl, NodeSchema: testSchema, BlockStorePath: "", @@ -72,7 +73,7 @@ func newTestPersistenceModule(databaseUrl string) modules.PersistenceModule { // TODO(andrew): Move the number of actors into local constants genesisState, _ := test_artifacts.NewGenesisState(5, 1, 1, 1) createTestingGenesisAndConfigFiles(cfg, genesisState) - persistenceMod, err := persistence.Create(testingConfigFilePath, testingGenesisFilePath) // TODO (Drewsky) this is the last remaining cross module import and needs a fix... + persistenceMod, err := persistence.Create(cfg.Persistence, genesisState.PersistenceGenesisState) // TODO (Drewsky) this is the last remaining cross module import and needs a fix... if err != nil { log.Fatalf("Error creating persistence module: %s", err) } From b05c71cec1f4b7cae1fab4b34c45ea378cffc952 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 20 Sep 2022 09:34:57 +0100 Subject: [PATCH 02/90] style(Client): consistency --- app/client/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/client/main.go b/app/client/main.go index bae6b6136..d4c4322b7 100644 --- a/app/client/main.go +++ b/app/client/main.go @@ -58,7 +58,7 @@ func main() { if err != nil { log.Fatalf("[ERROR] Failed to create consensus module: %v", err.Error()) } - p2pMod, err = p2p.Create(cfg, true) // TECHDEBT: extra param required for injecting private key hack for debug client + p2pMod, err = p2p.Create(cfg.P2P, true) // TECHDEBT: extra param required for injecting private key hack for debug client if err != nil { log.Fatalf("[ERROR] Failed to create p2p module: %v", err.Error()) } From 0163d713b499dbc4aa0df6f8b92ddcb7f9ad6db9 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 20 Sep 2022 09:53:41 +0100 Subject: [PATCH 03/90] fix(Config): make configPath and genesisPath available in Base --- persistence/debug.go | 2 +- persistence/module.go | 1 - runtime/config.go | 5 ++--- runtime/runtime.go | 4 ++-- shared/modules/types.go | 2 ++ 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/persistence/debug.go b/persistence/debug.go index 5ffe37c7c..87900b08f 100644 --- a/persistence/debug.go +++ b/persistence/debug.go @@ -15,7 +15,7 @@ func (m *PersistenceModule) HandleDebugMessage(debugMessage *debug.DebugMessage) m.showLatestBlockInStore(debugMessage) case debug.DebugMessageAction_DEBUG_CLEAR_STATE: m.clearState(debugMessage) - g, err := runtime.ParseGenesisJSON(m.genesisPath) + g, err := runtime.ParseGenesisJSON(m.GetBus().GetConfig().Base.GenesisPath) if err != nil { return err } diff --git a/persistence/module.go b/persistence/module.go index c10760d62..c4967edce 100644 --- a/persistence/module.go +++ b/persistence/module.go @@ -54,7 +54,6 @@ func Create(cfg modules.PersistenceConfig, genesis modules.PersistenceGenesisSta bus: nil, postgresURL: moduleCfg.GetPostgresUrl(), nodeSchema: moduleCfg.GetNodeSchema(), - genesisPath: "genesisPath", blockStore: blockStore, writeContext: nil, } diff --git a/runtime/config.go b/runtime/config.go index 1ba3bbabc..be4de55a2 100644 --- a/runtime/config.go +++ b/runtime/config.go @@ -23,9 +23,6 @@ type Config struct { Persistence *typesPers.PersistenceConfig `json:"persistence"` P2P *typesP2P.P2PConfig `json:"p2p"` Telemetry *typesTelemetry.TelemetryConfig `json:"telemetry"` - - configPath string - genesisPath string } func (c *Config) ToShared() modules.Config { @@ -42,6 +39,8 @@ func (c *Config) ToShared() modules.Config { type BaseConfig struct { RootDirectory string `json:"root_directory"` PrivateKey string `json:"private_key"` // TODO (pocket/issues/150) better architecture for key management (keybase, keyfiles, etc.) + ConfigPath string `json:"config_path"` + GenesisPath string `json:"genesis_path"` } func ParseConfigJSON(configPath string) (config *Config, err error) { diff --git a/runtime/runtime.go b/runtime/runtime.go index f67a2cfe3..20fd43b78 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -5,9 +5,9 @@ func Init(configPath, genesisPath string) (config *Config, genesis *Genesis, err if err != nil { return } - config.configPath = configPath - config.genesisPath = genesisPath + config.Base.ConfigPath = configPath + config.Base.GenesisPath = genesisPath genesis, err = ParseGenesisJSON(genesisPath) return } diff --git a/shared/modules/types.go b/shared/modules/types.go index 3c179f3a5..2b9b90270 100644 --- a/shared/modules/types.go +++ b/shared/modules/types.go @@ -15,6 +15,8 @@ type GenesisState struct { type BaseConfig struct { RootDirectory string `json:"root_directory"` PrivateKey string `json:"private_key"` // TODO (pocket/issues/150) better architecture for key management (keybase, keyfiles, etc.) + ConfigPath string `json:"config_path"` + GenesisPath string `json:"genesis_path"` } type Config struct { From 5283cd5a1b005f42f41e6e3bbab0beb121921e25 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 20 Sep 2022 10:59:55 +0100 Subject: [PATCH 04/90] style(Persistence): Persistance -> Persistence --- p2p/module_raintree_test.go | 2 +- persistence/types/converters.go | 14 +++++++------- shared/bus.go | 1 + shared/test_artifacts/generator.go | 14 +++++++------- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/p2p/module_raintree_test.go b/p2p/module_raintree_test.go index 35b06fd11..11a06bb83 100644 --- a/p2p/module_raintree_test.go +++ b/p2p/module_raintree_test.go @@ -457,7 +457,7 @@ func createGenesisState(t *testing.T, valKeys []cryptoPocket.PrivateKey) modules } return modules.GenesisState{ PersistenceGenesisState: &typesPers.PersistenceGenesisState{ - Validators: typesPers.ToPersistanceActors(validators), + Validators: typesPers.ToPersistenceActors(validators), }, } } diff --git a/persistence/types/converters.go b/persistence/types/converters.go index 2bea61c95..e118e66cb 100644 --- a/persistence/types/converters.go +++ b/persistence/types/converters.go @@ -2,7 +2,7 @@ package types import "github.com/pokt-network/pocket/shared/modules" -func toPersistanceActor(actor modules.Actor) *Actor { +func toPersistenceActor(actor modules.Actor) *Actor { return &Actor{ Address: actor.GetAddress(), PublicKey: actor.GetPublicKey(), @@ -11,30 +11,30 @@ func toPersistanceActor(actor modules.Actor) *Actor { } } -func ToPersistanceActors(actors []modules.Actor) []*Actor { +func ToPersistenceActors(actors []modules.Actor) []*Actor { r := make([]*Actor, 0) for _, a := range actors { - r = append(r, toPersistanceActor(a)) + r = append(r, toPersistenceActor(a)) } return r } -func toPersistanceAccount(account modules.Account) *Account { +func toPersistenceAccount(account modules.Account) *Account { return &Account{ Address: account.GetAddress(), Amount: account.GetAmount(), } } -func ToPersistanceAccounts(accounts []modules.Account) []*Account { +func ToPersistenceAccounts(accounts []modules.Account) []*Account { r := make([]*Account, 0) for _, a := range accounts { - r = append(r, toPersistanceAccount(a)) + r = append(r, toPersistenceAccount(a)) } return r } -func ToPersistanceParams(params modules.Params) *Params { +func ToPersistenceParams(params modules.Params) *Params { return &Params{ BlocksPerSession: params.GetBlocksPerSession(), AppMinimumStake: params.GetAppMinimumStake(), diff --git a/shared/bus.go b/shared/bus.go index 8867ad7fc..713d5e3a8 100644 --- a/shared/bus.go +++ b/shared/bus.go @@ -154,6 +154,7 @@ func (m bus) GetTelemetryModule() modules.TelemetryModule { func (m bus) GetConfig() modules.Config { return m.config } + func (m bus) GetGenesis() modules.GenesisState { return m.genesis } diff --git a/shared/test_artifacts/generator.go b/shared/test_artifacts/generator.go index 1e5894a8c..9ea3ed250 100644 --- a/shared/test_artifacts/generator.go +++ b/shared/test_artifacts/generator.go @@ -53,13 +53,13 @@ func NewGenesisState(numValidators, numServiceNodes, numApplications, numFisherm Validators: typesCons.ToConsensusValidators(vals), }, PersistenceGenesisState: &typesPers.PersistenceGenesisState{ - Pools: typesPers.ToPersistanceAccounts(NewPools()), - Accounts: typesPers.ToPersistanceAccounts(NewAccounts(numValidators+numServiceNodes+numApplications+numFisherman, append(append(append(validatorPrivateKeys, snPrivateKeys...), fishPrivateKeys...), appsPrivateKeys...)...)), // TODO(olshansky): clean this up - Applications: typesPers.ToPersistanceActors(apps), - Validators: typesPers.ToPersistanceActors(vals), - ServiceNodes: typesPers.ToPersistanceActors(serviceNodes), - Fishermen: typesPers.ToPersistanceActors(fish), - Params: typesPers.ToPersistanceParams(DefaultParams()), + Pools: typesPers.ToPersistenceAccounts(NewPools()), + Accounts: typesPers.ToPersistenceAccounts(NewAccounts(numValidators+numServiceNodes+numApplications+numFisherman, append(append(append(validatorPrivateKeys, snPrivateKeys...), fishPrivateKeys...), appsPrivateKeys...)...)), // TODO(olshansky): clean this up + Applications: typesPers.ToPersistenceActors(apps), + Validators: typesPers.ToPersistenceActors(vals), + ServiceNodes: typesPers.ToPersistenceActors(serviceNodes), + Fishermen: typesPers.ToPersistenceActors(fish), + Params: typesPers.ToPersistenceParams(DefaultParams()), }, }, validatorPrivateKeys } From 2aa7c294d59765a209423af0cc7bdccec7951ed9 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 20 Sep 2022 11:42:34 +0100 Subject: [PATCH 05/90] fix(Config): Base init --- runtime/runtime.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/runtime/runtime.go b/runtime/runtime.go index 20fd43b78..99b4f177b 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -6,8 +6,12 @@ func Init(configPath, genesisPath string) (config *Config, genesis *Genesis, err return } + if config.Base == nil { + config.Base = &BaseConfig{} + } config.Base.ConfigPath = configPath config.Base.GenesisPath = genesisPath + genesis, err = ParseGenesisJSON(genesisPath) return } From c0afedf1a054a44822393e2bf811e7a3c27d544a Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 20 Sep 2022 13:29:14 +0100 Subject: [PATCH 06/90] feat(Config): viper config parsing + environment variables --- go.mod | 11 +++++++++++ go.sum | 12 ++++++++++++ runtime/runtime.go | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index a884d4814..e59edf170 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,8 @@ require ( require ( github.com/dgraph-io/badger/v3 v3.2103.2 github.com/jackc/pgconn v1.11.0 + github.com/mitchellh/mapstructure v1.1.2 + github.com/spf13/viper v1.3.2 ) require ( @@ -67,7 +69,9 @@ require ( filippo.io/edwards25519 v1.0.0-rc.1 // indirect github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/fsnotify/fsnotify v1.4.7 // indirect github.com/google/go-cmp v0.5.6 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect github.com/jackc/chunkreader/v2 v2.0.1 // indirect github.com/jackc/pgio v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect @@ -75,9 +79,16 @@ require ( github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect github.com/jackc/pgtype v1.10.0 // indirect github.com/kr/pretty v0.3.0 // indirect + github.com/magiconair/properties v1.8.0 // indirect + github.com/pelletier/go-toml v1.2.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/spf13/afero v1.1.2 // indirect + github.com/spf13/cast v1.3.0 // indirect + github.com/spf13/jwalterweatherman v1.0.0 // indirect + github.com/spf13/pflag v1.0.3 // indirect golang.org/x/sys v0.0.0-20220405210540-1e041c57c461 // indirect golang.org/x/text v0.3.7 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) diff --git a/go.sum b/go.sum index 6de6b21be..ea7100e37 100644 --- a/go.sum +++ b/go.sum @@ -38,6 +38,7 @@ github.com/46bit/ristretto v0.1.0-with-arm-fix h1:jcjBpelxSNRlrGUo5jZnJNESgCsiJz github.com/46bit/ristretto v0.1.0-with-arm-fix/go.mod h1:fux0lOrBhrVCJd3lcTHsIJhq1T2rokOu6v9Vcb3Q9ug= github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8= github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= +github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= @@ -115,6 +116,7 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7 github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k= +github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/go-fonts/dejavu v0.1.0/go.mod h1:4Wt4I4OU2Nq9asgDCteaAaWZOV24E+0/Pwo0gppep4g= github.com/go-fonts/latin-modern v0.2.0/go.mod h1:rQVLdDMK+mK1xscDwsqM5J8U2jrRa3T0ecnM9pNujks= @@ -205,6 +207,7 @@ github.com/gotestyourself/gotestyourself v2.2.0+incompatible h1:AQwinXlbQR2HvPjQ github.com/gotestyourself/gotestyourself v2.2.0+incompatible/go.mod h1:zZKM6oeNM8k+FRljX1mnzVYeS8wiGgQyvST1/GafPbY= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= @@ -288,6 +291,7 @@ github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8= github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA= github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg= @@ -299,6 +303,7 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/moby/sys/mountinfo v0.5.0/go.mod h1:3bMD3Rg+zkqx8MRYPi7Pyb0Ie97QEBmdxbhnCLlSvSU= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -319,6 +324,7 @@ github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417/go.m github.com/opencontainers/selinux v1.10.0/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI= github.com/ory/dockertest v3.3.5+incompatible h1:iLLK6SQwIhcbrG783Dghaaa3WPzGc+4Emza6EbVUUGA= github.com/ory/dockertest v3.3.5+incompatible/go.mod h1:1vX4m9wsvi00u5bseYwXaSnhNrne+V0E6LAcBILJdPs= +github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/phpdave11/gofpdf v1.4.2/go.mod h1:zpO6xFn9yxo3YLyMvW8HcKWVdbNqgIfOOp2dXMnm1mY= github.com/phpdave11/gofpdi v1.0.12/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI= @@ -375,11 +381,16 @@ github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= +github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= +github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= +github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/spf13/viper v1.3.2 h1:VUFqw5KcqRf7i70GOzW7N+Q7+gxVBkSSqiXB12+JQ4M= github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -759,6 +770,7 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= diff --git a/runtime/runtime.go b/runtime/runtime.go index 99b4f177b..e0c091452 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -1,7 +1,39 @@ package runtime +import ( + "path" + "path/filepath" + "strings" + + "github.com/mitchellh/mapstructure" + "github.com/spf13/viper" +) + func Init(configPath, genesisPath string) (config *Config, genesis *Genesis, err error) { - config, err = ParseConfigJSON(configPath) + dir, file := path.Split(configPath) + filename := strings.TrimSuffix(file, filepath.Ext(file)) + + viper.AddConfigPath(".") + viper.AddConfigPath(dir) + viper.SetConfigName(filename) + viper.SetConfigName("config") + viper.SetConfigType("json") + + // The lines below allow for environment variables configuration (12 factor app) + // Eg: POCKET_CONSENSUS_PRIVATE_KEY=somekey would override `consensus.private_key` in config + viper.SetEnvPrefix("POCKET") + viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + viper.AutomaticEnv() + + err = viper.ReadInConfig() + if err != nil { + return + } + err = viper.Unmarshal(&config, func(dc *mapstructure.DecoderConfig) { + // This is to leverage the `json` struct tags without having to add `mapstructure` ones. + // Until we have complex use cases, this should work just fine. + dc.TagName = "json" + }) if err != nil { return } From 5e0bade152e9875b6316f0456093dbe266148fd6 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 20 Sep 2022 14:02:23 +0100 Subject: [PATCH 07/90] fix(Config): filename handling --- runtime/runtime.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/runtime.go b/runtime/runtime.go index e0c091452..d8ad2af21 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -16,7 +16,7 @@ func Init(configPath, genesisPath string) (config *Config, genesis *Genesis, err viper.AddConfigPath(".") viper.AddConfigPath(dir) viper.SetConfigName(filename) - viper.SetConfigName("config") + //viper.SetConfigName("config") viper.SetConfigType("json") // The lines below allow for environment variables configuration (12 factor app) From 5505933249e5c2d45349e4973f96803c2286507c Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 20 Sep 2022 15:47:47 +0100 Subject: [PATCH 08/90] refactor(Config): runtime module and InitializableModule interface --- app/client/main.go | 12 +++--- consensus/consensus_tests/utils_test.go | 9 +++-- consensus/module.go | 17 +++++++-- p2p/module_raintree_test.go | 4 +- runtime/config.go | 14 +++++++ runtime/genesis.go | 4 +- runtime/runtime.go | 50 ++++++++++++++++++++++--- shared/modules/module.go | 7 +++- shared/modules/runtime_module.go | 6 +++ shared/node.go | 12 +++--- 10 files changed, 103 insertions(+), 32 deletions(-) create mode 100644 shared/modules/runtime_module.go diff --git a/app/client/main.go b/app/client/main.go index d4c4322b7..e79a53809 100644 --- a/app/client/main.go +++ b/app/client/main.go @@ -49,12 +49,12 @@ var consensusMod modules.ConsensusModule func main() { var err error - cfg, genesis, err := runtime.Init(defaultConfigPath, defaultGenesisPath) - if err != nil { - log.Fatalf("[ERROR] Failed parse config and/or genesis: %v", err.Error()) - } + runtime := runtime.NewBuilder(defaultConfigPath, defaultGenesisPath) + cfg := runtime.GetConfig() + genesis := runtime.GetGenesis() - consensusMod, err = consensus.Create(cfg.Consensus, genesis.ConsensusGenesisState, true) // TECHDEBT: extra param required for injecting private key hack for debug client + cons, err := consensus.Create(runtime, false) // TECHDEBT: extra param required for injecting private key hack for debug client + consensusMod := cons.(modules.ConsensusModule) if err != nil { log.Fatalf("[ERROR] Failed to create consensus module: %v", err.Error()) } @@ -71,7 +71,7 @@ func main() { log.Fatalf("[ERROR] Failed to create NOOP telemetry module: " + err.Error()) } - _ = shared.CreateBusWithOptionalModules(cfg.ToShared(), genesis.ToShared(), nil, p2pMod, nil, consensusMod, telemetryMod) + _ = shared.CreateBusWithOptionalModules(cfg, genesis, nil, p2pMod, nil, consensusMod, telemetryMod) p2pMod.Start() diff --git a/consensus/consensus_tests/utils_test.go b/consensus/consensus_tests/utils_test.go index 69e58d43f..429af8e62 100644 --- a/consensus/consensus_tests/utils_test.go +++ b/consensus/consensus_tests/utils_test.go @@ -120,10 +120,11 @@ func CreateTestConsensusPocketNode( ) *shared.Node { createTestingGenesisAndConfigFiles(t, cfg, genesisState) - config, genesis, err := runtime.Init(testingConfigFilePath, testingGenesisFilePath) - require.NoError(t, err) + runtime := runtime.NewBuilder(testingConfigFilePath, testingGenesisFilePath) + config := runtime.GetConfig() + genesis := runtime.GetGenesis() - consensusMod, err := consensus.Create(config.Consensus, genesis.ConsensusGenesisState, false) + consensusMod, err := consensus.Create(runtime, false) require.NoError(t, err) // TODO(olshansky): At the moment we are using the same base mocks for all the tests, // but note that they will need to be customized on a per test basis. @@ -132,7 +133,7 @@ func CreateTestConsensusPocketNode( utilityMock := baseUtilityMock(t, testChannel) telemetryMock := baseTelemetryMock(t, testChannel) - bus, err := shared.CreateBus(config.ToShared(), genesis.ToShared(), persistenceMock, p2pMock, utilityMock, consensusMod, telemetryMock) + bus, err := shared.CreateBus(config, genesis, persistenceMock, p2pMock, utilityMock, consensusMod.(modules.ConsensusModule), telemetryMock) require.NoError(t, err) pk, err := cryptoPocket.NewPrivateKey(cfg.Base.PrivateKey) diff --git a/consensus/module.go b/consensus/module.go index 17f60376c..dd847caca 100644 --- a/consensus/module.go +++ b/consensus/module.go @@ -23,6 +23,8 @@ var _ modules.ConsensusGenesisState = &typesCons.ConsensusGenesisState{} var _ modules.PacemakerConfig = &typesCons.PacemakerConfig{} var _ modules.ConsensusConfig = &typesCons.ConsensusConfig{} var _ modules.ConsensusModule = &ConsensusModule{} +var _ modules.Module = &ConsensusModule{} +var _ modules.InitializableModule = &ConsensusModule{} // TODO(olshansky): Any reason to make all of these attributes local only (i.e. not exposed outside the struct)? // TODO(olshansky): Look for a way to not externalize the `ConsensusModule` struct @@ -62,9 +64,18 @@ type ConsensusModule struct { MaxBlockBytes uint64 } -func Create(cfg modules.ConsensusConfig, genesis modules.ConsensusGenesisState, useRandomPK bool) (modules.ConsensusModule, error) { - moduleCfg := cfg.(*typesCons.ConsensusConfig) - moduleGenesis := genesis.(*typesCons.ConsensusGenesisState) +func Create(builder modules.Runtime, useRandomPK bool) (modules.Module, error) { + var m ConsensusModule + return m.Create(builder, useRandomPK) +} + +func (*ConsensusModule) Create(builder modules.Runtime, useRandomPK bool) (modules.Module, error) { + cfg := builder.GetConfig() + genesis := builder.GetGenesis() + + moduleCfg := cfg.Consensus.(*typesCons.ConsensusConfig) + moduleGenesis := genesis.ConsensusGenesisState.(*typesCons.ConsensusGenesisState) + leaderElectionMod, err := leader_election.Create(moduleCfg, moduleGenesis) if err != nil { return nil, err diff --git a/p2p/module_raintree_test.go b/p2p/module_raintree_test.go index 11a06bb83..dfd964a83 100644 --- a/p2p/module_raintree_test.go +++ b/p2p/module_raintree_test.go @@ -382,8 +382,8 @@ func prepareP2PModules(t *testing.T, configs []modules.Config) (p2pModules map[s configFilePath := testingConfigFilePath + strconv.Itoa(i) + jsonPosfix genesisFilePath := testingGenesisFilePath + jsonPosfix - cfg, _, err := runtime.Init(configFilePath, genesisFilePath) - require.NoError(t, err) + runtime := runtime.NewBuilder(configFilePath, genesisFilePath) + cfg := runtime.GetConfig() p2pMod, err := Create(cfg.P2P, false) require.NoError(t, err) diff --git a/runtime/config.go b/runtime/config.go index be4de55a2..e2eee91bf 100644 --- a/runtime/config.go +++ b/runtime/config.go @@ -15,6 +15,8 @@ import ( var _ modules.ConsensusConfig = &Config{} var _ modules.P2PConfig = &Config{} var _ modules.PersistenceConfig = &Config{} +var _ modules.TelemetryConfig = &Config{} +var _ modules.UtilityConfig = &Config{} type Config struct { Base *BaseConfig `json:"base"` @@ -87,3 +89,15 @@ func (c *Config) GetNodeSchema() string { func (c *Config) GetBlockStorePath() string { return c.Persistence.BlockStorePath } + +// modules.TelemetryConfig + +func (c *Config) GetEnabled() bool { + return c.Telemetry.Enabled +} +func (c *Config) GetAddress() string { + return c.Telemetry.Address +} +func (c *Config) GetEndpoint() string { + return c.Telemetry.Endpoint +} diff --git a/runtime/genesis.go b/runtime/genesis.go index 3475c76b8..98947e27a 100644 --- a/runtime/genesis.go +++ b/runtime/genesis.go @@ -20,8 +20,8 @@ type Genesis struct { func (g *Genesis) ToShared() modules.GenesisState { return modules.GenesisState{ - PersistenceGenesisState: g, - ConsensusGenesisState: g, + PersistenceGenesisState: g.PersistenceGenesisState, + ConsensusGenesisState: g.ConsensusGenesisState, } } diff --git a/runtime/runtime.go b/runtime/runtime.go index d8ad2af21..98391a0dc 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -1,22 +1,53 @@ package runtime import ( + "log" "path" "path/filepath" "strings" "github.com/mitchellh/mapstructure" + "github.com/pokt-network/pocket/shared/modules" "github.com/spf13/viper" ) -func Init(configPath, genesisPath string) (config *Config, genesis *Genesis, err error) { - dir, file := path.Split(configPath) +var _ modules.Runtime = &builder{} + +type builder struct { + configPath string + genesisPath string + + config *Config + genesis *Genesis +} + +func NewBuilder(configPath, genesisPath string, options ...func(*builder)) *builder { + b := &builder{ + configPath: configPath, + genesisPath: genesisPath, + } + + cfg, genesis, err := b.init() + if err != nil { + log.Fatalf("[ERROR] Failed to initialize runtime builder: %v", err) + } + b.config = cfg + b.genesis = genesis + + for _, o := range options { + o(b) + } + + return b +} + +func (b *builder) init() (config *Config, genesis *Genesis, err error) { + dir, file := path.Split(b.configPath) filename := strings.TrimSuffix(file, filepath.Ext(file)) viper.AddConfigPath(".") viper.AddConfigPath(dir) viper.SetConfigName(filename) - //viper.SetConfigName("config") viper.SetConfigType("json") // The lines below allow for environment variables configuration (12 factor app) @@ -41,9 +72,16 @@ func Init(configPath, genesisPath string) (config *Config, genesis *Genesis, err if config.Base == nil { config.Base = &BaseConfig{} } - config.Base.ConfigPath = configPath - config.Base.GenesisPath = genesisPath + config.Base.ConfigPath = b.configPath + config.Base.GenesisPath = b.genesisPath - genesis, err = ParseGenesisJSON(genesisPath) + genesis, err = ParseGenesisJSON(b.genesisPath) return } + +func (b *builder) GetConfig() modules.Config { + return b.config.ToShared() +} +func (b *builder) GetGenesis() modules.GenesisState { + return b.genesis.ToShared() +} diff --git a/shared/modules/module.go b/shared/modules/module.go index f25507003..4d88625e5 100644 --- a/shared/modules/module.go +++ b/shared/modules/module.go @@ -4,8 +4,6 @@ package modules // TODO(drewsky): Add `Create` function; pocket/issues/163 // TODO(drewsky): Do not embed this inside of modules but force it via an implicit cast at compile time type Module interface { - GetModuleName() string - IntegratableModule InterruptableModule } @@ -19,3 +17,8 @@ type InterruptableModule interface { Start() error Stop() error } + +type InitializableModule interface { + GetModuleName() string + Create(builder Runtime, useRandomPK bool) (Module, error) +} diff --git a/shared/modules/runtime_module.go b/shared/modules/runtime_module.go new file mode 100644 index 000000000..64001f814 --- /dev/null +++ b/shared/modules/runtime_module.go @@ -0,0 +1,6 @@ +package modules + +type Runtime interface { + GetConfig() Config + GetGenesis() GenesisState +} diff --git a/shared/node.go b/shared/node.go index 74663917c..f657a0161 100644 --- a/shared/node.go +++ b/shared/node.go @@ -29,11 +29,9 @@ type Node struct { } func Create(configPath, genesisPath string) (n *Node, err error) { - - cfg, genesis, err := runtime.Init(configPath, genesisPath) - if err != nil { - return nil, err - } + runtime := runtime.NewBuilder(configPath, genesisPath) + cfg := runtime.GetConfig() + genesis := runtime.GetGenesis() persistenceMod, err := persistence.Create(cfg.Persistence, genesis.PersistenceGenesisState) if err != nil { @@ -50,7 +48,7 @@ func Create(configPath, genesisPath string) (n *Node, err error) { return nil, err } - consensusMod, err := consensus.Create(cfg.Consensus, genesis.ConsensusGenesisState, false) + consensusMod, err := consensus.Create(runtime, false) if err != nil { return nil, err } @@ -60,7 +58,7 @@ func Create(configPath, genesisPath string) (n *Node, err error) { return nil, err } - bus, err := CreateBus(cfg.ToShared(), genesis.ToShared(), persistenceMod, p2pMod, utilityMod, consensusMod, telemetryMod) + bus, err := CreateBus(cfg, genesis, persistenceMod, p2pMod, utilityMod, consensusMod.(modules.ConsensusModule), telemetryMod) if err != nil { return nil, err } From cfc88c259dc475df13890f6b5abc68e1c44518c1 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 20 Sep 2022 18:03:51 +0100 Subject: [PATCH 09/90] feat(Config): useRandomPK in runtime --- app/client/main.go | 4 ++-- consensus/consensus_tests/utils_test.go | 2 +- consensus/module.go | 8 ++++---- runtime/runtime.go | 25 ++++++++++++++++++------- shared/modules/module.go | 2 +- shared/modules/runtime_module.go | 1 + shared/node.go | 2 +- 7 files changed, 28 insertions(+), 16 deletions(-) diff --git a/app/client/main.go b/app/client/main.go index e79a53809..19221678c 100644 --- a/app/client/main.go +++ b/app/client/main.go @@ -49,11 +49,11 @@ var consensusMod modules.ConsensusModule func main() { var err error - runtime := runtime.NewBuilder(defaultConfigPath, defaultGenesisPath) + runtime := runtime.NewBuilder(defaultConfigPath, defaultGenesisPath, runtime.WithRandomPK()) cfg := runtime.GetConfig() genesis := runtime.GetGenesis() - cons, err := consensus.Create(runtime, false) // TECHDEBT: extra param required for injecting private key hack for debug client + cons, err := consensus.Create(runtime) // TECHDEBT: extra param required for injecting private key hack for debug client consensusMod := cons.(modules.ConsensusModule) if err != nil { log.Fatalf("[ERROR] Failed to create consensus module: %v", err.Error()) diff --git a/consensus/consensus_tests/utils_test.go b/consensus/consensus_tests/utils_test.go index 429af8e62..55c018433 100644 --- a/consensus/consensus_tests/utils_test.go +++ b/consensus/consensus_tests/utils_test.go @@ -124,7 +124,7 @@ func CreateTestConsensusPocketNode( config := runtime.GetConfig() genesis := runtime.GetGenesis() - consensusMod, err := consensus.Create(runtime, false) + consensusMod, err := consensus.Create(runtime) require.NoError(t, err) // TODO(olshansky): At the moment we are using the same base mocks for all the tests, // but note that they will need to be customized on a per test basis. diff --git a/consensus/module.go b/consensus/module.go index dd847caca..5e99dc2f5 100644 --- a/consensus/module.go +++ b/consensus/module.go @@ -64,12 +64,12 @@ type ConsensusModule struct { MaxBlockBytes uint64 } -func Create(builder modules.Runtime, useRandomPK bool) (modules.Module, error) { +func Create(builder modules.Runtime) (modules.Module, error) { var m ConsensusModule - return m.Create(builder, useRandomPK) + return m.Create(builder) } -func (*ConsensusModule) Create(builder modules.Runtime, useRandomPK bool) (modules.Module, error) { +func (*ConsensusModule) Create(builder modules.Runtime) (modules.Module, error) { cfg := builder.GetConfig() genesis := builder.GetGenesis() @@ -89,7 +89,7 @@ func (*ConsensusModule) Create(builder modules.Runtime, useRandomPK bool) (modul valMap := typesCons.ValidatorListToMap(moduleGenesis.Validators) var privateKey cryptoPocket.PrivateKey - if useRandomPK { + if builder.ShouldUseRandomPK() { privateKey, err = cryptoPocket.GeneratePrivateKey() } else { privateKey, err = cryptoPocket.NewPrivateKey(moduleCfg.PrivateKey) diff --git a/runtime/runtime.go b/runtime/runtime.go index 98391a0dc..253503555 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -11,18 +11,20 @@ import ( "github.com/spf13/viper" ) -var _ modules.Runtime = &builder{} +var _ modules.Runtime = &Builder{} -type builder struct { +type Builder struct { configPath string genesisPath string config *Config genesis *Genesis + + useRandomPK bool } -func NewBuilder(configPath, genesisPath string, options ...func(*builder)) *builder { - b := &builder{ +func NewBuilder(configPath, genesisPath string, options ...func(*Builder)) *Builder { + b := &Builder{ configPath: configPath, genesisPath: genesisPath, } @@ -41,7 +43,7 @@ func NewBuilder(configPath, genesisPath string, options ...func(*builder)) *buil return b } -func (b *builder) init() (config *Config, genesis *Genesis, err error) { +func (b *Builder) init() (config *Config, genesis *Genesis, err error) { dir, file := path.Split(b.configPath) filename := strings.TrimSuffix(file, filepath.Ext(file)) @@ -79,9 +81,18 @@ func (b *builder) init() (config *Config, genesis *Genesis, err error) { return } -func (b *builder) GetConfig() modules.Config { +func (b *Builder) GetConfig() modules.Config { return b.config.ToShared() } -func (b *builder) GetGenesis() modules.GenesisState { + +func (b *Builder) GetGenesis() modules.GenesisState { return b.genesis.ToShared() } + +func (b *Builder) ShouldUseRandomPK() bool { + return b.useRandomPK +} + +func WithRandomPK() func(*Builder) { + return func(b *Builder) { b.useRandomPK = true } +} diff --git a/shared/modules/module.go b/shared/modules/module.go index 4d88625e5..81a247b1f 100644 --- a/shared/modules/module.go +++ b/shared/modules/module.go @@ -20,5 +20,5 @@ type InterruptableModule interface { type InitializableModule interface { GetModuleName() string - Create(builder Runtime, useRandomPK bool) (Module, error) + Create(runtime Runtime) (Module, error) } diff --git a/shared/modules/runtime_module.go b/shared/modules/runtime_module.go index 64001f814..33889f72f 100644 --- a/shared/modules/runtime_module.go +++ b/shared/modules/runtime_module.go @@ -3,4 +3,5 @@ package modules type Runtime interface { GetConfig() Config GetGenesis() GenesisState + ShouldUseRandomPK() bool } diff --git a/shared/node.go b/shared/node.go index f657a0161..151f94bbf 100644 --- a/shared/node.go +++ b/shared/node.go @@ -48,7 +48,7 @@ func Create(configPath, genesisPath string) (n *Node, err error) { return nil, err } - consensusMod, err := consensus.Create(runtime, false) + consensusMod, err := consensus.Create(runtime) if err != nil { return nil, err } From 39f4a9d2d24d935db0cb5f513d3144cbf730ce9c Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 20 Sep 2022 18:10:43 +0100 Subject: [PATCH 10/90] refactor(Config): renamed Builder / updated signatures --- app/client/main.go | 2 +- consensus/consensus_tests/utils_test.go | 2 +- p2p/module_raintree_test.go | 2 +- runtime/runtime.go | 20 ++++++++++---------- shared/node.go | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/app/client/main.go b/app/client/main.go index 19221678c..7bc8ce138 100644 --- a/app/client/main.go +++ b/app/client/main.go @@ -49,7 +49,7 @@ var consensusMod modules.ConsensusModule func main() { var err error - runtime := runtime.NewBuilder(defaultConfigPath, defaultGenesisPath, runtime.WithRandomPK()) + runtime := runtime.New(defaultConfigPath, defaultGenesisPath, runtime.WithRandomPK()) cfg := runtime.GetConfig() genesis := runtime.GetGenesis() diff --git a/consensus/consensus_tests/utils_test.go b/consensus/consensus_tests/utils_test.go index 55c018433..5621a0d42 100644 --- a/consensus/consensus_tests/utils_test.go +++ b/consensus/consensus_tests/utils_test.go @@ -120,7 +120,7 @@ func CreateTestConsensusPocketNode( ) *shared.Node { createTestingGenesisAndConfigFiles(t, cfg, genesisState) - runtime := runtime.NewBuilder(testingConfigFilePath, testingGenesisFilePath) + runtime := runtime.New(testingConfigFilePath, testingGenesisFilePath) config := runtime.GetConfig() genesis := runtime.GetGenesis() diff --git a/p2p/module_raintree_test.go b/p2p/module_raintree_test.go index dfd964a83..3083f6c38 100644 --- a/p2p/module_raintree_test.go +++ b/p2p/module_raintree_test.go @@ -382,7 +382,7 @@ func prepareP2PModules(t *testing.T, configs []modules.Config) (p2pModules map[s configFilePath := testingConfigFilePath + strconv.Itoa(i) + jsonPosfix genesisFilePath := testingGenesisFilePath + jsonPosfix - runtime := runtime.NewBuilder(configFilePath, genesisFilePath) + runtime := runtime.New(configFilePath, genesisFilePath) cfg := runtime.GetConfig() p2pMod, err := Create(cfg.P2P, false) diff --git a/runtime/runtime.go b/runtime/runtime.go index 253503555..1e39be217 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -11,9 +11,9 @@ import ( "github.com/spf13/viper" ) -var _ modules.Runtime = &Builder{} +var _ modules.Runtime = &RuntimeConfig{} -type Builder struct { +type RuntimeConfig struct { configPath string genesisPath string @@ -23,8 +23,8 @@ type Builder struct { useRandomPK bool } -func NewBuilder(configPath, genesisPath string, options ...func(*Builder)) *Builder { - b := &Builder{ +func New(configPath, genesisPath string, options ...func(*RuntimeConfig)) *RuntimeConfig { + b := &RuntimeConfig{ configPath: configPath, genesisPath: genesisPath, } @@ -43,7 +43,7 @@ func NewBuilder(configPath, genesisPath string, options ...func(*Builder)) *Buil return b } -func (b *Builder) init() (config *Config, genesis *Genesis, err error) { +func (b *RuntimeConfig) init() (config *Config, genesis *Genesis, err error) { dir, file := path.Split(b.configPath) filename := strings.TrimSuffix(file, filepath.Ext(file)) @@ -81,18 +81,18 @@ func (b *Builder) init() (config *Config, genesis *Genesis, err error) { return } -func (b *Builder) GetConfig() modules.Config { +func (b *RuntimeConfig) GetConfig() modules.Config { return b.config.ToShared() } -func (b *Builder) GetGenesis() modules.GenesisState { +func (b *RuntimeConfig) GetGenesis() modules.GenesisState { return b.genesis.ToShared() } -func (b *Builder) ShouldUseRandomPK() bool { +func (b *RuntimeConfig) ShouldUseRandomPK() bool { return b.useRandomPK } -func WithRandomPK() func(*Builder) { - return func(b *Builder) { b.useRandomPK = true } +func WithRandomPK() func(*RuntimeConfig) { + return func(b *RuntimeConfig) { b.useRandomPK = true } } diff --git a/shared/node.go b/shared/node.go index 151f94bbf..22c326acd 100644 --- a/shared/node.go +++ b/shared/node.go @@ -29,7 +29,7 @@ type Node struct { } func Create(configPath, genesisPath string) (n *Node, err error) { - runtime := runtime.NewBuilder(configPath, genesisPath) + runtime := runtime.New(configPath, genesisPath) cfg := runtime.GetConfig() genesis := runtime.GetGenesis() From 1160ab94f81b0fedc900e25267fc19532794dc1a Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 20 Sep 2022 18:44:51 +0100 Subject: [PATCH 11/90] style(Config): builder -> runtime --- consensus/module.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/consensus/module.go b/consensus/module.go index 5e99dc2f5..41e7062a3 100644 --- a/consensus/module.go +++ b/consensus/module.go @@ -64,14 +64,14 @@ type ConsensusModule struct { MaxBlockBytes uint64 } -func Create(builder modules.Runtime) (modules.Module, error) { +func Create(runtime modules.Runtime) (modules.Module, error) { var m ConsensusModule - return m.Create(builder) + return m.Create(runtime) } -func (*ConsensusModule) Create(builder modules.Runtime) (modules.Module, error) { - cfg := builder.GetConfig() - genesis := builder.GetGenesis() +func (*ConsensusModule) Create(runtime modules.Runtime) (modules.Module, error) { + cfg := runtime.GetConfig() + genesis := runtime.GetGenesis() moduleCfg := cfg.Consensus.(*typesCons.ConsensusConfig) moduleGenesis := genesis.ConsensusGenesisState.(*typesCons.ConsensusGenesisState) @@ -89,7 +89,7 @@ func (*ConsensusModule) Create(builder modules.Runtime) (modules.Module, error) valMap := typesCons.ValidatorListToMap(moduleGenesis.Validators) var privateKey cryptoPocket.PrivateKey - if builder.ShouldUseRandomPK() { + if runtime.ShouldUseRandomPK() { privateKey, err = cryptoPocket.GeneratePrivateKey() } else { privateKey, err = cryptoPocket.NewPrivateKey(moduleCfg.PrivateKey) From 9283f64dc7f1f36facf6700728b6e5aeb0c492f4 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 20 Sep 2022 21:05:21 +0100 Subject: [PATCH 12/90] feat(Config): implement InitlializableModule in all modules --- app/client/main.go | 6 ++++-- p2p/module.go | 16 +++++++++++++--- p2p/module_raintree_test.go | 3 +-- persistence/module.go | 18 +++++++++++++++--- persistence/test/setup_test.go | 8 ++++++-- shared/node.go | 18 ++++++++++++------ telemetry/module.go | 13 +++++++++++-- utility/module.go | 9 ++++++++- utility/test/module_test.go | 7 +++++-- 9 files changed, 75 insertions(+), 23 deletions(-) diff --git a/app/client/main.go b/app/client/main.go index 7bc8ce138..7abf36b89 100644 --- a/app/client/main.go +++ b/app/client/main.go @@ -58,18 +58,20 @@ func main() { if err != nil { log.Fatalf("[ERROR] Failed to create consensus module: %v", err.Error()) } - p2pMod, err = p2p.Create(cfg.P2P, true) // TECHDEBT: extra param required for injecting private key hack for debug client + p2pm, err := p2p.Create(runtime) // TECHDEBT: extra param required for injecting private key hack for debug client if err != nil { log.Fatalf("[ERROR] Failed to create p2p module: %v", err.Error()) } + p2pMod := p2pm.(modules.P2PModule) // This telemetry module instance is a NOOP because the 'enable_telemetry' flag in the `cfg` above is set to false. // Since this client mimics partial - networking only - functionality of a full node, some of the telemetry-related // code paths are executed. To avoid those messages interfering with the telemetry data collected, a non-nil telemetry // module that NOOPs (per the configs above) is injected. - telemetryMod, err := telemetry.Create(cfg.Telemetry) + telemetryM, err := telemetry.Create(runtime) if err != nil { log.Fatalf("[ERROR] Failed to create NOOP telemetry module: " + err.Error()) } + telemetryMod := telemetryM.(modules.TelemetryModule) _ = shared.CreateBusWithOptionalModules(cfg, genesis, nil, p2pMod, nil, consensusMod, telemetryMod) diff --git a/p2p/module.go b/p2p/module.go index bfd0b7aa3..8d7c4f2ff 100644 --- a/p2p/module.go +++ b/p2p/module.go @@ -20,6 +20,8 @@ import ( ) var _ modules.P2PModule = &p2pModule{} +var _ modules.Module = &p2pModule{} +var _ modules.InitializableModule = &p2pModule{} const ( P2PModuleName = "p2p" @@ -40,15 +42,23 @@ func (m *p2pModule) GetAddress() (cryptoPocket.Address, error) { return m.address, nil } -func Create(cfg modules.P2PConfig, useRandomPK bool) (m modules.P2PModule, err error) { +func Create(runtime modules.Runtime) (modules.Module, error) { + var m p2pModule + return m.Create(runtime) +} + +func (*p2pModule) Create(runtime modules.Runtime) (m modules.Module, err error) { log.Println("Creating network module") - moduleCfg := cfg.(*typesP2P.P2PConfig) + + cfg := runtime.GetConfig() + moduleCfg := cfg.P2P.(*typesP2P.P2PConfig) + l, err := CreateListener(moduleCfg) if err != nil { return nil, err } var privateKey cryptoPocket.PrivateKey - if useRandomPK { + if runtime.ShouldUseRandomPK() { privateKey, err = cryptoPocket.GeneratePrivateKey() } else { privateKey, err = cryptoPocket.NewPrivateKey(moduleCfg.PrivateKey) diff --git a/p2p/module_raintree_test.go b/p2p/module_raintree_test.go index 3083f6c38..4a8de2a19 100644 --- a/p2p/module_raintree_test.go +++ b/p2p/module_raintree_test.go @@ -383,9 +383,8 @@ func prepareP2PModules(t *testing.T, configs []modules.Config) (p2pModules map[s genesisFilePath := testingGenesisFilePath + jsonPosfix runtime := runtime.New(configFilePath, genesisFilePath) - cfg := runtime.GetConfig() - p2pMod, err := Create(cfg.P2P, false) + p2pMod, err := Create(runtime) require.NoError(t, err) p2pModules[validatorId(t, i+1)] = p2pMod.(*p2pModule) } diff --git a/persistence/module.go b/persistence/module.go index c4967edce..13a10565a 100644 --- a/persistence/module.go +++ b/persistence/module.go @@ -16,6 +16,9 @@ var _ modules.PersistenceModule = &PersistenceModule{} var _ modules.PersistenceRWContext = &PostgresContext{} var _ modules.PersistenceGenesisState = &types.PersistenceGenesisState{} var _ modules.PersistenceConfig = &types.PersistenceConfig{} +var _ modules.PersistenceModule = &PersistenceModule{} +var _ modules.Module = &PersistenceModule{} +var _ modules.InitializableModule = &PersistenceModule{} type PersistenceModule struct { bus modules.Bus @@ -33,9 +36,18 @@ const ( PersistenceModuleName = "persistence" ) -func Create(cfg modules.PersistenceConfig, genesis modules.PersistenceGenesisState) (modules.PersistenceModule, error) { - moduleCfg := cfg.(*types.PersistenceConfig) - moduleGenesis := genesis.(*types.PersistenceGenesisState) +func Create(runtime modules.Runtime) (modules.Module, error) { + var m PersistenceModule + return m.Create(runtime) +} + +func (*PersistenceModule) Create(runtime modules.Runtime) (modules.Module, error) { + cfg := runtime.GetConfig() + genesis := runtime.GetGenesis() + + moduleCfg := cfg.Persistence.(*types.PersistenceConfig) + moduleGenesis := genesis.PersistenceGenesisState.(*types.PersistenceGenesisState) + conn, err := connectToDatabase(moduleCfg.GetPostgresUrl(), moduleCfg.GetNodeSchema()) if err != nil { return nil, err diff --git a/persistence/test/setup_test.go b/persistence/test/setup_test.go index fea5f57cb..4f32a01ad 100644 --- a/persistence/test/setup_test.go +++ b/persistence/test/setup_test.go @@ -14,6 +14,7 @@ import ( "time" "github.com/pokt-network/pocket/persistence/types" + "github.com/pokt-network/pocket/runtime" "github.com/pokt-network/pocket/shared/test_artifacts" "github.com/pokt-network/pocket/persistence" @@ -107,11 +108,14 @@ func newTestPersistenceModule(databaseUrl string) modules.PersistenceModule { } genesisState, _ := test_artifacts.NewGenesisState(5, 1, 1, 1) createTestingGenesisAndConfigFiles(cfg, genesisState) - persistenceMod, err := persistence.Create(cfg.Persistence, genesisState.PersistenceGenesisState) + + runtime := runtime.New(testingConfigFilePath, testingGenesisFilePath) + + persistenceMod, err := persistence.Create(runtime) if err != nil { log.Fatalf("Error creating persistence module: %s", err) } - return persistenceMod + return persistenceMod.(modules.PersistenceModule) } // IMPROVE(team): Extend this to more complex and variable test cases challenging & randomizing the state of persistence. diff --git a/shared/node.go b/shared/node.go index 22c326acd..11ac13c9b 100644 --- a/shared/node.go +++ b/shared/node.go @@ -33,17 +33,17 @@ func Create(configPath, genesisPath string) (n *Node, err error) { cfg := runtime.GetConfig() genesis := runtime.GetGenesis() - persistenceMod, err := persistence.Create(cfg.Persistence, genesis.PersistenceGenesisState) + persistenceMod, err := persistence.Create(runtime) if err != nil { return nil, err } - p2pMod, err := p2p.Create(cfg.P2P, false) + p2pMod, err := p2p.Create(runtime) if err != nil { return nil, err } - utilityMod, err := utility.Create(cfg.Utility) + utilityMod, err := utility.Create(runtime) if err != nil { return nil, err } @@ -53,16 +53,22 @@ func Create(configPath, genesisPath string) (n *Node, err error) { return nil, err } - telemetryMod, err := telemetry.Create(cfg.Telemetry) + telemetryMod, err := telemetry.Create(runtime) if err != nil { return nil, err } - bus, err := CreateBus(cfg, genesis, persistenceMod, p2pMod, utilityMod, consensusMod.(modules.ConsensusModule), telemetryMod) + bus, err := CreateBus(cfg, genesis, + persistenceMod.(modules.PersistenceModule), + p2pMod.(modules.P2PModule), + utilityMod.(modules.UtilityModule), + consensusMod.(modules.ConsensusModule), + telemetryMod.(modules.TelemetryModule), + ) if err != nil { return nil, err } - addr, err := p2pMod.GetAddress() + addr, err := p2pMod.(modules.P2PModule).GetAddress() if err != nil { return nil, err } diff --git a/telemetry/module.go b/telemetry/module.go index 46d15f93c..2517a534a 100644 --- a/telemetry/module.go +++ b/telemetry/module.go @@ -6,14 +6,23 @@ import ( var _ modules.Module = &telemetryModule{} var _ modules.TelemetryConfig = &TelemetryConfig{} +var _ modules.InitializableModule = &telemetryModule{} const ( TelemetryModuleName = "telemetry" ) +func Create(runtime modules.Runtime) (modules.Module, error) { + var m telemetryModule + return m.Create(runtime) +} + // TODO(pocket/issues/99): Add a switch statement and configuration variable when support for other telemetry modules is added. -func Create(cfg modules.TelemetryConfig) (modules.TelemetryModule, error) { - moduleCfg := cfg.(*TelemetryConfig) +func (*telemetryModule) Create(runtime modules.Runtime) (modules.Module, error) { + cfg := runtime.GetConfig() + + moduleCfg := cfg.Telemetry.(*TelemetryConfig) + if moduleCfg.GetEnabled() { return CreatePrometheusTelemetryModule(moduleCfg) } else { diff --git a/utility/module.go b/utility/module.go index e780b0816..12e0f7225 100644 --- a/utility/module.go +++ b/utility/module.go @@ -10,6 +10,8 @@ import ( var _ modules.UtilityModule = &UtilityModule{} var _ modules.UtilityConfig = &types.UtilityConfig{} +var _ modules.Module = &UtilityModule{} +var _ modules.InitializableModule = &UtilityModule{} type UtilityModule struct { bus modules.Bus @@ -20,7 +22,12 @@ const ( UtilityModuleName = "utility" ) -func Create(cfg modules.UtilityConfig) (modules.UtilityModule, error) { +func Create(runtime modules.Runtime) (modules.Module, error) { + var m UtilityModule + return m.Create(runtime) +} + +func (*UtilityModule) Create(runtime modules.Runtime) (modules.Module, error) { return &UtilityModule{ // TODO: Add `maxTransactionBytes` and `maxTransactions` to cfg.Utility Mempool: types.NewMempool(1000, 1000), diff --git a/utility/test/module_test.go b/utility/test/module_test.go index ba7ae7014..091c36d60 100644 --- a/utility/test/module_test.go +++ b/utility/test/module_test.go @@ -9,6 +9,7 @@ import ( "testing" typesPers "github.com/pokt-network/pocket/persistence/types" + "github.com/pokt-network/pocket/runtime" "github.com/pokt-network/pocket/shared/test_artifacts" utilTypes "github.com/pokt-network/pocket/utility/types" @@ -73,12 +74,14 @@ func newTestPersistenceModule(databaseUrl string) modules.PersistenceModule { // TODO(andrew): Move the number of actors into local constants genesisState, _ := test_artifacts.NewGenesisState(5, 1, 1, 1) createTestingGenesisAndConfigFiles(cfg, genesisState) - persistenceMod, err := persistence.Create(cfg.Persistence, genesisState.PersistenceGenesisState) // TODO (Drewsky) this is the last remaining cross module import and needs a fix... + runtime := runtime.New(testingConfigFilePath, testingGenesisFilePath) + + persistenceMod, err := persistence.Create(runtime) // TODO (Drewsky) this is the last remaining cross module import and needs a fix... if err != nil { log.Fatalf("Error creating persistence module: %s", err) } persistenceMod.Start() // TODO: Check for error - return persistenceMod + return persistenceMod.(modules.PersistenceModule) } const ( From 8bfaafe8b1c6e5441f57ecb4a7f22b33e6c46b5b Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 20 Sep 2022 21:06:08 +0100 Subject: [PATCH 13/90] fix(Consensus): remove pacemakerConfig from interface --- runtime/config.go | 9 ++++++--- shared/modules/types.go | 1 - 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/runtime/config.go b/runtime/config.go index e2eee91bf..089fb6551 100644 --- a/runtime/config.go +++ b/runtime/config.go @@ -62,18 +62,17 @@ func ParseConfigJSON(configPath string) (config *Config, err error) { func (c *Config) GetMaxMempoolBytes() uint64 { return c.Consensus.MaxMempoolBytes } -func (c *Config) GetPaceMakerConfig() modules.PacemakerConfig { - return c.Consensus.PacemakerConfig -} // modules.P2PConfig func (c *Config) GetConsensusPort() uint32 { return c.P2P.ConsensusPort } + func (c *Config) GetUseRainTree() bool { return c.P2P.UseRainTree } + func (c *Config) IsEmptyConnType() bool { // TODO (team) make enum return c.P2P.IsEmptyConnectionType } @@ -83,9 +82,11 @@ func (c *Config) IsEmptyConnType() bool { // TODO (team) make enum func (c *Config) GetPostgresUrl() string { return c.Persistence.PostgresUrl } + func (c *Config) GetNodeSchema() string { return c.Persistence.NodeSchema } + func (c *Config) GetBlockStorePath() string { return c.Persistence.BlockStorePath } @@ -95,9 +96,11 @@ func (c *Config) GetBlockStorePath() string { func (c *Config) GetEnabled() bool { return c.Telemetry.Enabled } + func (c *Config) GetAddress() string { return c.Telemetry.Address } + func (c *Config) GetEndpoint() string { return c.Telemetry.Endpoint } diff --git a/shared/modules/types.go b/shared/modules/types.go index 2b9b90270..4cc9a77ba 100644 --- a/shared/modules/types.go +++ b/shared/modules/types.go @@ -30,7 +30,6 @@ type Config struct { type ConsensusConfig interface { GetMaxMempoolBytes() uint64 - GetPaceMakerConfig() PacemakerConfig } type PacemakerConfig interface { From 37f12f9247061bf69460a7bc066232cdb8775310 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 20 Sep 2022 21:07:18 +0100 Subject: [PATCH 14/90] feat(Config): params/flags injection (stab @private key for CLI) --- runtime/runtime.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/runtime/runtime.go b/runtime/runtime.go index 1e39be217..597af1415 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/mitchellh/mapstructure" + "github.com/pokt-network/pocket/consensus/types" "github.com/pokt-network/pocket/shared/modules" "github.com/spf13/viper" ) @@ -96,3 +97,12 @@ func (b *RuntimeConfig) ShouldUseRandomPK() bool { func WithRandomPK() func(*RuntimeConfig) { return func(b *RuntimeConfig) { b.useRandomPK = true } } + +func WithPK(pk string) func(*RuntimeConfig) { + return func(b *RuntimeConfig) { + if b.config.Consensus == nil { + b.config.Consensus = &types.ConsensusConfig{} + } + b.config.Consensus.PrivateKey = pk + } +} From 2fec88ba5c73fc52211b6f0abb39a3ed32eba39f Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Wed, 21 Sep 2022 13:29:37 +0100 Subject: [PATCH 15/90] style(Client): cleanup --- app/client/main.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/client/main.go b/app/client/main.go index 7abf36b89..a0b61f530 100644 --- a/app/client/main.go +++ b/app/client/main.go @@ -53,16 +53,16 @@ func main() { cfg := runtime.GetConfig() genesis := runtime.GetGenesis() - cons, err := consensus.Create(runtime) // TECHDEBT: extra param required for injecting private key hack for debug client - consensusMod := cons.(modules.ConsensusModule) + consM, err := consensus.Create(runtime) + consensusMod := consM.(modules.ConsensusModule) if err != nil { log.Fatalf("[ERROR] Failed to create consensus module: %v", err.Error()) } - p2pm, err := p2p.Create(runtime) // TECHDEBT: extra param required for injecting private key hack for debug client + p2pM, err := p2p.Create(runtime) if err != nil { log.Fatalf("[ERROR] Failed to create p2p module: %v", err.Error()) } - p2pMod := p2pm.(modules.P2PModule) + p2pMod := p2pM.(modules.P2PModule) // This telemetry module instance is a NOOP because the 'enable_telemetry' flag in the `cfg` above is set to false. // Since this client mimics partial - networking only - functionality of a full node, some of the telemetry-related // code paths are executed. To avoid those messages interfering with the telemetry data collected, a non-nil telemetry From 25979e351e0da2f5b43ead67245afa97e773c9be Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Wed, 21 Sep 2022 13:55:54 +0100 Subject: [PATCH 16/90] =?UTF-8?q?feat(Shared):=20ConfigurableModule=20and?= =?UTF-8?q?=20GenesisDependentModule=20=F0=9F=91=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- consensus/module.go | 30 +++++++++++++++++++++++++++--- shared/modules/module.go | 8 ++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/consensus/module.go b/consensus/module.go index 41e7062a3..a52e0fe41 100644 --- a/consensus/module.go +++ b/consensus/module.go @@ -25,6 +25,8 @@ var _ modules.ConsensusConfig = &typesCons.ConsensusConfig{} var _ modules.ConsensusModule = &ConsensusModule{} var _ modules.Module = &ConsensusModule{} var _ modules.InitializableModule = &ConsensusModule{} +var _ modules.ConfigurableModule = &ConsensusModule{} +var _ modules.GenesisDependentModule = &ConsensusModule{} // TODO(olshansky): Any reason to make all of these attributes local only (i.e. not exposed outside the struct)? // TODO(olshansky): Look for a way to not externalize the `ConsensusModule` struct @@ -70,10 +72,18 @@ func Create(runtime modules.Runtime) (modules.Module, error) { } func (*ConsensusModule) Create(runtime modules.Runtime) (modules.Module, error) { - cfg := runtime.GetConfig() - genesis := runtime.GetGenesis() + var m *ConsensusModule + cfg := runtime.GetConfig() + if err := m.ValidateConfig(cfg); err != nil { + log.Fatalf("config validation failed: %v", err) + } moduleCfg := cfg.Consensus.(*typesCons.ConsensusConfig) + + genesis := runtime.GetGenesis() + if err := m.ValidateGenesis(genesis); err != nil { + log.Fatalf("genesis validation failed: %v", err) + } moduleGenesis := genesis.ConsensusGenesisState.(*typesCons.ConsensusGenesisState) leaderElectionMod, err := leader_election.Create(moduleCfg, moduleGenesis) @@ -100,7 +110,7 @@ func (*ConsensusModule) Create(runtime modules.Runtime) (modules.Module, error) address := privateKey.Address().String() valIdMap, idValMap := typesCons.GetValAddrToIdMap(valMap) - m := &ConsensusModule{ + m = &ConsensusModule{ bus: nil, privateKey: privateKey.(cryptoPocket.Ed25519PrivateKey), @@ -182,6 +192,20 @@ func (m *ConsensusModule) SetBus(pocketBus modules.Bus) { m.leaderElectionMod.SetBus(pocketBus) } +func (*ConsensusModule) ValidateConfig(cfg modules.Config) error { + if _, ok := cfg.Consensus.(*typesCons.ConsensusConfig); !ok { + return fmt.Errorf("cannot cast to ConsensusConfig") + } + return nil +} + +func (*ConsensusModule) ValidateGenesis(genesis modules.GenesisState) error { + if _, ok := genesis.ConsensusGenesisState.(*typesCons.ConsensusGenesisState); !ok { + return fmt.Errorf("cannot cast to ConsensusGenesisState") + } + return nil +} + func (m *ConsensusModule) loadPersistedState() error { persistenceContext, err := m.GetBus().GetPersistenceModule().NewReadContext(-1) // Unknown height if err != nil { diff --git a/shared/modules/module.go b/shared/modules/module.go index 81a247b1f..a49afd61d 100644 --- a/shared/modules/module.go +++ b/shared/modules/module.go @@ -22,3 +22,11 @@ type InitializableModule interface { GetModuleName() string Create(runtime Runtime) (Module, error) } + +type ConfigurableModule interface { + ValidateConfig(Config) error +} + +type GenesisDependentModule interface { + ValidateGenesis(GenesisState) error +} From ba15b7c01f2c9e2df1c01480c7ef168be94bcfb3 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Wed, 21 Sep 2022 14:01:33 +0100 Subject: [PATCH 17/90] chore(go.mod): tidy --- go.mod | 1 + 1 file changed, 1 insertion(+) diff --git a/go.mod b/go.mod index e59edf170..01321ca9c 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,7 @@ require ( require ( github.com/dgraph-io/badger/v3 v3.2103.2 github.com/jackc/pgconn v1.11.0 + github.com/jordanorelli/lexnum v0.0.0-20141216151731-460eeb125754 github.com/mitchellh/mapstructure v1.1.2 github.com/spf13/viper v1.3.2 ) From 7c5b94399f48b7587336b621274edc41c20c406a Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Wed, 21 Sep 2022 14:14:15 +0100 Subject: [PATCH 18/90] =?UTF-8?q?feat(Shared):=20KeyholderModule=20?= =?UTF-8?q?=F0=9F=91=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- consensus/module.go | 16 ++++++++++------ shared/modules/module.go | 6 ++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/consensus/module.go b/consensus/module.go index a52e0fe41..017cf8b5f 100644 --- a/consensus/module.go +++ b/consensus/module.go @@ -27,6 +27,7 @@ var _ modules.Module = &ConsensusModule{} var _ modules.InitializableModule = &ConsensusModule{} var _ modules.ConfigurableModule = &ConsensusModule{} var _ modules.GenesisDependentModule = &ConsensusModule{} +var _ modules.KeyholderModule = &ConsensusModule{} // TODO(olshansky): Any reason to make all of these attributes local only (i.e. not exposed outside the struct)? // TODO(olshansky): Look for a way to not externalize the `ConsensusModule` struct @@ -98,12 +99,8 @@ func (*ConsensusModule) Create(runtime modules.Runtime) (modules.Module, error) } valMap := typesCons.ValidatorListToMap(moduleGenesis.Validators) - var privateKey cryptoPocket.PrivateKey - if runtime.ShouldUseRandomPK() { - privateKey, err = cryptoPocket.GeneratePrivateKey() - } else { - privateKey, err = cryptoPocket.NewPrivateKey(moduleCfg.PrivateKey) - } + + privateKey, err := m.GetPrivateKey(runtime) if err != nil { return nil, err } @@ -206,6 +203,13 @@ func (*ConsensusModule) ValidateGenesis(genesis modules.GenesisState) error { return nil } +func (*ConsensusModule) GetPrivateKey(runtime modules.Runtime) (cryptoPocket.PrivateKey, error) { + if runtime.ShouldUseRandomPK() { + return cryptoPocket.GeneratePrivateKey() + } + return cryptoPocket.NewPrivateKey(runtime.GetConfig().Consensus.(*typesCons.ConsensusConfig).PrivateKey) +} + func (m *ConsensusModule) loadPersistedState() error { persistenceContext, err := m.GetBus().GetPersistenceModule().NewReadContext(-1) // Unknown height if err != nil { diff --git a/shared/modules/module.go b/shared/modules/module.go index a49afd61d..1a17cbaf2 100644 --- a/shared/modules/module.go +++ b/shared/modules/module.go @@ -1,5 +1,7 @@ package modules +import "github.com/pokt-network/pocket/shared/crypto" + // TODO(olshansky): Show an example of `TypicalUsage` // TODO(drewsky): Add `Create` function; pocket/issues/163 // TODO(drewsky): Do not embed this inside of modules but force it via an implicit cast at compile time @@ -30,3 +32,7 @@ type ConfigurableModule interface { type GenesisDependentModule interface { ValidateGenesis(GenesisState) error } + +type KeyholderModule interface { + GetPrivateKey(Runtime) (crypto.PrivateKey, error) +} From 49980dc8a92dbe8a7f80302f9ce2e130001ed8ac Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Wed, 21 Sep 2022 14:58:46 +0100 Subject: [PATCH 19/90] fix(Shared): Enforcing for all modules --- consensus/leader_election/module.go | 9 +++++-- consensus/module.go | 32 +++++++++++++---------- consensus/pacemaker.go | 40 ++++++++++++++++++++++++----- p2p/module.go | 2 -- persistence/module.go | 2 -- shared/modules/module.go | 1 + shared/node.go | 7 ++++- telemetry/module.go | 11 ++++---- telemetry/noop_module.go | 7 ++++- telemetry/prometheus_module.go | 33 +++++++++++++++++++----- utility/module.go | 1 - 11 files changed, 105 insertions(+), 40 deletions(-) diff --git a/consensus/leader_election/module.go b/consensus/leader_election/module.go index 75a6ab2f6..80aaf55b0 100644 --- a/consensus/leader_election/module.go +++ b/consensus/leader_election/module.go @@ -16,13 +16,18 @@ type LeaderElectionModule interface { ElectNextLeader(*typesCons.HotstuffMessage) (typesCons.NodeId, error) } -var _ leaderElectionModule = leaderElectionModule{} +var _ LeaderElectionModule = &leaderElectionModule{} type leaderElectionModule struct { bus modules.Bus } -func Create(_ *typesCons.ConsensusConfig, _ *typesCons.ConsensusGenesisState) (LeaderElectionModule, error) { +func Create(runtime modules.Runtime) (modules.Module, error) { + var m leaderElectionModule + return m.Create(runtime) +} + +func (*leaderElectionModule) Create(runtime modules.Runtime) (modules.Module, error) { return &leaderElectionModule{}, nil } diff --git a/consensus/module.go b/consensus/module.go index 017cf8b5f..6d66e8803 100644 --- a/consensus/module.go +++ b/consensus/module.go @@ -19,15 +19,17 @@ const ( ConsensusModuleName = "consensus" ) -var _ modules.ConsensusGenesisState = &typesCons.ConsensusGenesisState{} -var _ modules.PacemakerConfig = &typesCons.PacemakerConfig{} -var _ modules.ConsensusConfig = &typesCons.ConsensusConfig{} -var _ modules.ConsensusModule = &ConsensusModule{} -var _ modules.Module = &ConsensusModule{} -var _ modules.InitializableModule = &ConsensusModule{} -var _ modules.ConfigurableModule = &ConsensusModule{} -var _ modules.GenesisDependentModule = &ConsensusModule{} -var _ modules.KeyholderModule = &ConsensusModule{} +var ( + _ modules.ConsensusGenesisState = &typesCons.ConsensusGenesisState{} + + _ modules.PacemakerConfig = &typesCons.PacemakerConfig{} + _ modules.ConsensusConfig = &typesCons.ConsensusConfig{} + + _ modules.ConsensusModule = &ConsensusModule{} + _ modules.ConfigurableModule = &ConsensusModule{} + _ modules.GenesisDependentModule = &ConsensusModule{} + _ modules.KeyholderModule = &ConsensusModule{} +) // TODO(olshansky): Any reason to make all of these attributes local only (i.e. not exposed outside the struct)? // TODO(olshansky): Look for a way to not externalize the `ConsensusModule` struct @@ -87,13 +89,13 @@ func (*ConsensusModule) Create(runtime modules.Runtime) (modules.Module, error) } moduleGenesis := genesis.ConsensusGenesisState.(*typesCons.ConsensusGenesisState) - leaderElectionMod, err := leader_election.Create(moduleCfg, moduleGenesis) + leaderElectionMod, err := leader_election.Create(runtime) if err != nil { return nil, err } // TODO(olshansky): Can we make this a submodule? - paceMaker, err := CreatePacemaker(moduleCfg) + paceMaker, err := CreatePacemaker(runtime) if err != nil { return nil, err } @@ -107,6 +109,8 @@ func (*ConsensusModule) Create(runtime modules.Runtime) (modules.Module, error) address := privateKey.Address().String() valIdMap, idValMap := typesCons.GetValAddrToIdMap(valMap) + pacemakerMod := paceMaker.(Pacemaker) + m = &ConsensusModule{ bus: nil, @@ -130,8 +134,8 @@ func (*ConsensusModule) Create(runtime modules.Runtime) (modules.Module, error) validatorMap: valMap, utilityContext: nil, - paceMaker: paceMaker, - leaderElectionMod: leaderElectionMod, + paceMaker: pacemakerMod, + leaderElectionMod: leaderElectionMod.(leader_election.LeaderElectionModule), logPrefix: DefaultLogPrefix, MessagePool: make(map[typesCons.HotstuffStep][]*typesCons.HotstuffMessage), @@ -139,7 +143,7 @@ func (*ConsensusModule) Create(runtime modules.Runtime) (modules.Module, error) } // TODO(olshansky): Look for a way to avoid doing this. - paceMaker.SetConsensusModule(m) + pacemakerMod.SetConsensusModule(m) return m, nil } diff --git a/consensus/pacemaker.go b/consensus/pacemaker.go index 9d3fe11c6..9812403e8 100644 --- a/consensus/pacemaker.go +++ b/consensus/pacemaker.go @@ -2,6 +2,7 @@ package consensus import ( "context" + "fmt" "log" "time" @@ -30,8 +31,11 @@ type Pacemaker interface { InterruptRound() } -var _ modules.Module = &paceMaker{} -var _ PacemakerDebug = &paceMaker{} +var ( + _ modules.Module = &paceMaker{} + _ modules.ConfigurableModule = &paceMaker{} + _ PacemakerDebug = &paceMaker{} +) type paceMaker struct { bus modules.Bus @@ -50,18 +54,30 @@ type paceMaker struct { paceMakerDebug } -func CreatePacemaker(cfg *typesCons.ConsensusConfig) (m *paceMaker, err error) { +func CreatePacemaker(runtime modules.Runtime) (modules.Module, error) { + var m paceMaker + return m.Create(runtime) +} + +func (m *paceMaker) Create(runtime modules.Runtime) (modules.Module, error) { + cfg := runtime.GetConfig() + if err := m.ValidateConfig(cfg); err != nil { + log.Fatalf("config validation failed: %v", err) + } + + pacemakerConfig := cfg.Consensus.(*typesCons.ConsensusConfig).PacemakerConfig + return &paceMaker{ bus: nil, consensusMod: nil, - pacemakerConfigs: cfg.GetPaceMakerConfig(), + pacemakerConfigs: pacemakerConfig, stepCancelFunc: nil, // Only set on restarts paceMakerDebug: paceMakerDebug{ - manualMode: cfg.GetPaceMakerConfig().GetManual(), - debugTimeBetweenStepsMsec: cfg.GetPaceMakerConfig().GetDebugTimeBetweenStepsMsec(), + manualMode: pacemakerConfig.GetManual(), + debugTimeBetweenStepsMsec: pacemakerConfig.GetDebugTimeBetweenStepsMsec(), quorumCertificate: nil, }, }, nil @@ -90,6 +106,18 @@ func (m *paceMaker) GetBus() modules.Bus { return m.bus } +func (*paceMaker) ValidateConfig(cfg modules.Config) error { + consCfg, ok := cfg.Consensus.(*typesCons.ConsensusConfig) + if !ok { + return fmt.Errorf("cannot cast to TelemetryConfig") + } + + if consCfg.PacemakerConfig == nil { + return fmt.Errorf("PacemakerConfig is nil") + } + return nil +} + func (m *paceMaker) SetConsensusModule(c *ConsensusModule) { m.consensusMod = c } diff --git a/p2p/module.go b/p2p/module.go index 8d7c4f2ff..15c49a485 100644 --- a/p2p/module.go +++ b/p2p/module.go @@ -20,8 +20,6 @@ import ( ) var _ modules.P2PModule = &p2pModule{} -var _ modules.Module = &p2pModule{} -var _ modules.InitializableModule = &p2pModule{} const ( P2PModuleName = "p2p" diff --git a/persistence/module.go b/persistence/module.go index 13a10565a..3d0d122aa 100644 --- a/persistence/module.go +++ b/persistence/module.go @@ -17,8 +17,6 @@ var _ modules.PersistenceRWContext = &PostgresContext{} var _ modules.PersistenceGenesisState = &types.PersistenceGenesisState{} var _ modules.PersistenceConfig = &types.PersistenceConfig{} var _ modules.PersistenceModule = &PersistenceModule{} -var _ modules.Module = &PersistenceModule{} -var _ modules.InitializableModule = &PersistenceModule{} type PersistenceModule struct { bus modules.Bus diff --git a/shared/modules/module.go b/shared/modules/module.go index 1a17cbaf2..40124f489 100644 --- a/shared/modules/module.go +++ b/shared/modules/module.go @@ -6,6 +6,7 @@ import "github.com/pokt-network/pocket/shared/crypto" // TODO(drewsky): Add `Create` function; pocket/issues/163 // TODO(drewsky): Do not embed this inside of modules but force it via an implicit cast at compile time type Module interface { + InitializableModule IntegratableModule InterruptableModule } diff --git a/shared/node.go b/shared/node.go index 11ac13c9b..e342fc118 100644 --- a/shared/node.go +++ b/shared/node.go @@ -28,8 +28,13 @@ type Node struct { Address cryptoPocket.Address } -func Create(configPath, genesisPath string) (n *Node, err error) { +func Create(configPath, genesisPath string) (modules.Module, error) { + var m Node runtime := runtime.New(configPath, genesisPath) + return m.Create(runtime) +} + +func (m *Node) Create(runtime modules.Runtime) (modules.Module, error) { cfg := runtime.GetConfig() genesis := runtime.GetGenesis() diff --git a/telemetry/module.go b/telemetry/module.go index 2517a534a..6c04ec28e 100644 --- a/telemetry/module.go +++ b/telemetry/module.go @@ -4,9 +4,10 @@ import ( "github.com/pokt-network/pocket/shared/modules" ) -var _ modules.Module = &telemetryModule{} -var _ modules.TelemetryConfig = &TelemetryConfig{} -var _ modules.InitializableModule = &telemetryModule{} +var ( + _ modules.Module = &telemetryModule{} + _ modules.TelemetryConfig = &TelemetryConfig{} +) const ( TelemetryModuleName = "telemetry" @@ -24,9 +25,9 @@ func (*telemetryModule) Create(runtime modules.Runtime) (modules.Module, error) moduleCfg := cfg.Telemetry.(*TelemetryConfig) if moduleCfg.GetEnabled() { - return CreatePrometheusTelemetryModule(moduleCfg) + return CreatePrometheusTelemetryModule(runtime) } else { - return CreateNoopTelemetryModule(moduleCfg) + return CreateNoopTelemetryModule(runtime) } } diff --git a/telemetry/noop_module.go b/telemetry/noop_module.go index 1d23ac003..b4fbe9121 100644 --- a/telemetry/noop_module.go +++ b/telemetry/noop_module.go @@ -25,7 +25,12 @@ func NOOP() { log.Printf("\n[telemetry=noop]\n") } -func CreateNoopTelemetryModule(_ *TelemetryConfig) (*NoopTelemetryModule, error) { +func CreateNoopTelemetryModule(runtime modules.Runtime) (modules.Module, error) { + var m NoopTelemetryModule + return m.Create(runtime) +} + +func (m *NoopTelemetryModule) Create(runtime modules.Runtime) (modules.Module, error) { return &NoopTelemetryModule{}, nil } diff --git a/telemetry/prometheus_module.go b/telemetry/prometheus_module.go index ab6b46740..4ab4a1059 100644 --- a/telemetry/prometheus_module.go +++ b/telemetry/prometheus_module.go @@ -1,6 +1,7 @@ package telemetry import ( + "fmt" "log" "net/http" @@ -11,9 +12,11 @@ import ( ) var ( - _ modules.TelemetryModule = &PrometheusTelemetryModule{} - _ modules.EventMetricsAgent = &PrometheusTelemetryModule{} - _ modules.TimeSeriesAgent = &PrometheusTelemetryModule{} + _ modules.Module = &PrometheusTelemetryModule{} + _ modules.ConfigurableModule = &PrometheusTelemetryModule{} + _ modules.TelemetryModule = &PrometheusTelemetryModule{} + _ modules.EventMetricsAgent = &PrometheusTelemetryModule{} + _ modules.TimeSeriesAgent = &PrometheusTelemetryModule{} ) // DISCUSS(team): Should the warning logs in this module be handled differently? @@ -33,14 +36,25 @@ const ( PrometheusModuleName = "prometheus" ) -func CreatePrometheusTelemetryModule(cfg *TelemetryConfig) (*PrometheusTelemetryModule, error) { +func CreatePrometheusTelemetryModule(runtime modules.Runtime) (modules.Module, error) { + var m PrometheusTelemetryModule + return m.Create(runtime) +} + +func (m *PrometheusTelemetryModule) Create(runtime modules.Runtime) (modules.Module, error) { + cfg := runtime.GetConfig() + if err := m.ValidateConfig(cfg); err != nil { + log.Fatalf("config validation failed: %v", err) + } + moduleCfg := cfg.Telemetry.(*TelemetryConfig) + return &PrometheusTelemetryModule{ counters: map[string]prometheus.Counter{}, gauges: map[string]prometheus.Gauge{}, gaugeVectors: map[string]prometheus.GaugeVec{}, - address: cfg.GetAddress(), - endpoint: cfg.GetEndpoint(), + address: moduleCfg.GetAddress(), + endpoint: moduleCfg.GetEndpoint(), }, nil } @@ -74,6 +88,13 @@ func (m *PrometheusTelemetryModule) GetBus() modules.Bus { return m.bus } +func (*PrometheusTelemetryModule) ValidateConfig(cfg modules.Config) error { + if _, ok := cfg.Telemetry.(*TelemetryConfig); !ok { + return fmt.Errorf("cannot cast to TelemetryConfig") + } + return nil +} + // EventMetricsAgent interface implementation func (m *PrometheusTelemetryModule) GetEventMetricsAgent() modules.EventMetricsAgent { return modules.EventMetricsAgent(m) diff --git a/utility/module.go b/utility/module.go index 12e0f7225..cd26813f1 100644 --- a/utility/module.go +++ b/utility/module.go @@ -11,7 +11,6 @@ import ( var _ modules.UtilityModule = &UtilityModule{} var _ modules.UtilityConfig = &types.UtilityConfig{} var _ modules.Module = &UtilityModule{} -var _ modules.InitializableModule = &UtilityModule{} type UtilityModule struct { bus modules.Bus From 69af7f16829f0be9f49c83b6ea547cc09937b5fd Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Wed, 21 Sep 2022 15:28:55 +0100 Subject: [PATCH 20/90] refactor(Runtime): renamed b to rc --- runtime/runtime.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/runtime/runtime.go b/runtime/runtime.go index 597af1415..ea3830ecd 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -25,27 +25,27 @@ type RuntimeConfig struct { } func New(configPath, genesisPath string, options ...func(*RuntimeConfig)) *RuntimeConfig { - b := &RuntimeConfig{ + rc := &RuntimeConfig{ configPath: configPath, genesisPath: genesisPath, } - cfg, genesis, err := b.init() + cfg, genesis, err := rc.init() if err != nil { log.Fatalf("[ERROR] Failed to initialize runtime builder: %v", err) } - b.config = cfg - b.genesis = genesis + rc.config = cfg + rc.genesis = genesis for _, o := range options { - o(b) + o(rc) } - return b + return rc } -func (b *RuntimeConfig) init() (config *Config, genesis *Genesis, err error) { - dir, file := path.Split(b.configPath) +func (rc *RuntimeConfig) init() (config *Config, genesis *Genesis, err error) { + dir, file := path.Split(rc.configPath) filename := strings.TrimSuffix(file, filepath.Ext(file)) viper.AddConfigPath(".") @@ -59,26 +59,26 @@ func (b *RuntimeConfig) init() (config *Config, genesis *Genesis, err error) { viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.AutomaticEnv() - err = viper.ReadInConfig() - if err != nil { + if err = viper.ReadInConfig(); err != nil { return } - err = viper.Unmarshal(&config, func(dc *mapstructure.DecoderConfig) { + + decoderConfig := func(dc *mapstructure.DecoderConfig) { // This is to leverage the `json` struct tags without having to add `mapstructure` ones. // Until we have complex use cases, this should work just fine. dc.TagName = "json" - }) - if err != nil { + } + if err = viper.Unmarshal(&config, decoderConfig); err != nil { return } if config.Base == nil { config.Base = &BaseConfig{} } - config.Base.ConfigPath = b.configPath - config.Base.GenesisPath = b.genesisPath + config.Base.ConfigPath = rc.configPath + config.Base.GenesisPath = rc.genesisPath - genesis, err = ParseGenesisJSON(b.genesisPath) + genesis, err = ParseGenesisJSON(rc.genesisPath) return } From 56be6750b57fa284eb86f92e6b58445faa2738fc Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Wed, 21 Sep 2022 15:43:32 +0100 Subject: [PATCH 21/90] style(Shared): renamed moduleCfg to moduleNameCfg --- consensus/module.go | 4 ++-- p2p/module.go | 8 ++++---- persistence/module.go | 10 +++++----- telemetry/module.go | 4 ++-- telemetry/prometheus_module.go | 6 +++--- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/consensus/module.go b/consensus/module.go index 6d66e8803..0d338a139 100644 --- a/consensus/module.go +++ b/consensus/module.go @@ -81,7 +81,7 @@ func (*ConsensusModule) Create(runtime modules.Runtime) (modules.Module, error) if err := m.ValidateConfig(cfg); err != nil { log.Fatalf("config validation failed: %v", err) } - moduleCfg := cfg.Consensus.(*typesCons.ConsensusConfig) + consensusCfg := cfg.Consensus.(*typesCons.ConsensusConfig) genesis := runtime.GetGenesis() if err := m.ValidateGenesis(genesis); err != nil { @@ -115,7 +115,7 @@ func (*ConsensusModule) Create(runtime modules.Runtime) (modules.Module, error) bus: nil, privateKey: privateKey.(cryptoPocket.Ed25519PrivateKey), - consCfg: moduleCfg, + consCfg: consensusCfg, Height: 0, Round: 0, diff --git a/p2p/module.go b/p2p/module.go index 15c49a485..37af2a217 100644 --- a/p2p/module.go +++ b/p2p/module.go @@ -49,9 +49,9 @@ func (*p2pModule) Create(runtime modules.Runtime) (m modules.Module, err error) log.Println("Creating network module") cfg := runtime.GetConfig() - moduleCfg := cfg.P2P.(*typesP2P.P2PConfig) + p2pCfg := cfg.P2P.(*typesP2P.P2PConfig) - l, err := CreateListener(moduleCfg) + l, err := CreateListener(p2pCfg) if err != nil { return nil, err } @@ -59,13 +59,13 @@ func (*p2pModule) Create(runtime modules.Runtime) (m modules.Module, err error) if runtime.ShouldUseRandomPK() { privateKey, err = cryptoPocket.GeneratePrivateKey() } else { - privateKey, err = cryptoPocket.NewPrivateKey(moduleCfg.PrivateKey) + privateKey, err = cryptoPocket.NewPrivateKey(p2pCfg.PrivateKey) } if err != nil { return nil, err } m = &p2pModule{ - p2pConfig: moduleCfg, + p2pConfig: p2pCfg, listener: l, address: privateKey.Address(), diff --git a/persistence/module.go b/persistence/module.go index 3d0d122aa..88b131300 100644 --- a/persistence/module.go +++ b/persistence/module.go @@ -43,10 +43,10 @@ func (*PersistenceModule) Create(runtime modules.Runtime) (modules.Module, error cfg := runtime.GetConfig() genesis := runtime.GetGenesis() - moduleCfg := cfg.Persistence.(*types.PersistenceConfig) + persistenceCfg := cfg.Persistence.(*types.PersistenceConfig) moduleGenesis := genesis.PersistenceGenesisState.(*types.PersistenceGenesisState) - conn, err := connectToDatabase(moduleCfg.GetPostgresUrl(), moduleCfg.GetNodeSchema()) + conn, err := connectToDatabase(persistenceCfg.GetPostgresUrl(), persistenceCfg.GetNodeSchema()) if err != nil { return nil, err } @@ -55,15 +55,15 @@ func (*PersistenceModule) Create(runtime modules.Runtime) (modules.Module, error } conn.Close(context.TODO()) - blockStore, err := initializeBlockStore(moduleCfg.GetBlockStorePath()) + blockStore, err := initializeBlockStore(persistenceCfg.GetBlockStorePath()) if err != nil { return nil, err } persistenceMod := &PersistenceModule{ bus: nil, - postgresURL: moduleCfg.GetPostgresUrl(), - nodeSchema: moduleCfg.GetNodeSchema(), + postgresURL: persistenceCfg.GetPostgresUrl(), + nodeSchema: persistenceCfg.GetNodeSchema(), blockStore: blockStore, writeContext: nil, } diff --git a/telemetry/module.go b/telemetry/module.go index 6c04ec28e..188a71a15 100644 --- a/telemetry/module.go +++ b/telemetry/module.go @@ -22,9 +22,9 @@ func Create(runtime modules.Runtime) (modules.Module, error) { func (*telemetryModule) Create(runtime modules.Runtime) (modules.Module, error) { cfg := runtime.GetConfig() - moduleCfg := cfg.Telemetry.(*TelemetryConfig) + telemetryCfg := cfg.Telemetry.(*TelemetryConfig) - if moduleCfg.GetEnabled() { + if telemetryCfg.GetEnabled() { return CreatePrometheusTelemetryModule(runtime) } else { return CreateNoopTelemetryModule(runtime) diff --git a/telemetry/prometheus_module.go b/telemetry/prometheus_module.go index 4ab4a1059..21b7347bc 100644 --- a/telemetry/prometheus_module.go +++ b/telemetry/prometheus_module.go @@ -46,15 +46,15 @@ func (m *PrometheusTelemetryModule) Create(runtime modules.Runtime) (modules.Mod if err := m.ValidateConfig(cfg); err != nil { log.Fatalf("config validation failed: %v", err) } - moduleCfg := cfg.Telemetry.(*TelemetryConfig) + telemetryCfg := cfg.Telemetry.(*TelemetryConfig) return &PrometheusTelemetryModule{ counters: map[string]prometheus.Counter{}, gauges: map[string]prometheus.Gauge{}, gaugeVectors: map[string]prometheus.GaugeVec{}, - address: moduleCfg.GetAddress(), - endpoint: moduleCfg.GetEndpoint(), + address: telemetryCfg.GetAddress(), + endpoint: telemetryCfg.GetEndpoint(), }, nil } From cb863cf604cfcdb29d6d8a8b9074e5bcbb1b8722 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Mon, 26 Sep 2022 17:35:09 +0100 Subject: [PATCH 22/90] fix(SHARED): build error for importing unnecessary package --- shared/modules/bus_module.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/shared/modules/bus_module.go b/shared/modules/bus_module.go index 278a8a3d0..3bd0a433d 100644 --- a/shared/modules/bus_module.go +++ b/shared/modules/bus_module.go @@ -3,8 +3,6 @@ package modules //go:generate mockgen -source=$GOFILE -destination=./mocks/bus_module_mock.go -aux_files=github.com/pokt-network/pocket/shared/modules=module.go import ( - "encoding/json" - "github.com/pokt-network/pocket/shared/debug" ) From eb25f351dbd75e92b1850bf884fb382c0c4800f4 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Mon, 26 Sep 2022 17:37:29 +0100 Subject: [PATCH 23/90] Update shared/modules/runtime_module.go Co-authored-by: Daniel Olshansky --- shared/modules/runtime_module.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/modules/runtime_module.go b/shared/modules/runtime_module.go index 33889f72f..eaa4b781f 100644 --- a/shared/modules/runtime_module.go +++ b/shared/modules/runtime_module.go @@ -3,5 +3,5 @@ package modules type Runtime interface { GetConfig() Config GetGenesis() GenesisState - ShouldUseRandomPK() bool + ShouldUseRandomPK() bool // INVESTIGATE: look into how we can remove this from the runtime interface } From c3ac3cc55bd9d59f79c1918cb5a88caa24e6dfa9 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Mon, 26 Sep 2022 17:41:59 +0100 Subject: [PATCH 24/90] refactor(runtime): renamed RuntimeConfig -> runtimeConfig --- runtime/runtime.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/runtime/runtime.go b/runtime/runtime.go index ea3830ecd..0a3edd638 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -12,9 +12,9 @@ import ( "github.com/spf13/viper" ) -var _ modules.Runtime = &RuntimeConfig{} +var _ modules.Runtime = &runtimeConfig{} -type RuntimeConfig struct { +type runtimeConfig struct { configPath string genesisPath string @@ -24,8 +24,8 @@ type RuntimeConfig struct { useRandomPK bool } -func New(configPath, genesisPath string, options ...func(*RuntimeConfig)) *RuntimeConfig { - rc := &RuntimeConfig{ +func New(configPath, genesisPath string, options ...func(*runtimeConfig)) *runtimeConfig { + rc := &runtimeConfig{ configPath: configPath, genesisPath: genesisPath, } @@ -44,7 +44,7 @@ func New(configPath, genesisPath string, options ...func(*RuntimeConfig)) *Runti return rc } -func (rc *RuntimeConfig) init() (config *Config, genesis *Genesis, err error) { +func (rc *runtimeConfig) init() (config *Config, genesis *Genesis, err error) { dir, file := path.Split(rc.configPath) filename := strings.TrimSuffix(file, filepath.Ext(file)) @@ -82,24 +82,24 @@ func (rc *RuntimeConfig) init() (config *Config, genesis *Genesis, err error) { return } -func (b *RuntimeConfig) GetConfig() modules.Config { +func (b *runtimeConfig) GetConfig() modules.Config { return b.config.ToShared() } -func (b *RuntimeConfig) GetGenesis() modules.GenesisState { +func (b *runtimeConfig) GetGenesis() modules.GenesisState { return b.genesis.ToShared() } -func (b *RuntimeConfig) ShouldUseRandomPK() bool { +func (b *runtimeConfig) ShouldUseRandomPK() bool { return b.useRandomPK } -func WithRandomPK() func(*RuntimeConfig) { - return func(b *RuntimeConfig) { b.useRandomPK = true } +func WithRandomPK() func(*runtimeConfig) { + return func(b *runtimeConfig) { b.useRandomPK = true } } -func WithPK(pk string) func(*RuntimeConfig) { - return func(b *RuntimeConfig) { +func WithPK(pk string) func(*runtimeConfig) { + return func(b *runtimeConfig) { if b.config.Consensus == nil { b.config.Consensus = &types.ConsensusConfig{} } From 66b4806adb2aebea3fbbedcfa7b067921656b9f4 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Mon, 26 Sep 2022 17:54:08 +0100 Subject: [PATCH 25/90] fix(runtime): improved WithRandomPK() --- consensus/module.go | 3 --- p2p/module.go | 7 +------ runtime/runtime.go | 15 ++++++++------- shared/modules/runtime_module.go | 1 - 4 files changed, 9 insertions(+), 17 deletions(-) diff --git a/consensus/module.go b/consensus/module.go index 0d338a139..a052e9aab 100644 --- a/consensus/module.go +++ b/consensus/module.go @@ -208,9 +208,6 @@ func (*ConsensusModule) ValidateGenesis(genesis modules.GenesisState) error { } func (*ConsensusModule) GetPrivateKey(runtime modules.Runtime) (cryptoPocket.PrivateKey, error) { - if runtime.ShouldUseRandomPK() { - return cryptoPocket.GeneratePrivateKey() - } return cryptoPocket.NewPrivateKey(runtime.GetConfig().Consensus.(*typesCons.ConsensusConfig).PrivateKey) } diff --git a/p2p/module.go b/p2p/module.go index 37af2a217..560d63d29 100644 --- a/p2p/module.go +++ b/p2p/module.go @@ -55,12 +55,7 @@ func (*p2pModule) Create(runtime modules.Runtime) (m modules.Module, err error) if err != nil { return nil, err } - var privateKey cryptoPocket.PrivateKey - if runtime.ShouldUseRandomPK() { - privateKey, err = cryptoPocket.GeneratePrivateKey() - } else { - privateKey, err = cryptoPocket.NewPrivateKey(p2pCfg.PrivateKey) - } + privateKey, err := cryptoPocket.NewPrivateKey(p2pCfg.PrivateKey) if err != nil { return nil, err } diff --git a/runtime/runtime.go b/runtime/runtime.go index 0a3edd638..9f1c0a274 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -8,6 +8,7 @@ import ( "github.com/mitchellh/mapstructure" "github.com/pokt-network/pocket/consensus/types" + cryptoPocket "github.com/pokt-network/pocket/shared/crypto" "github.com/pokt-network/pocket/shared/modules" "github.com/spf13/viper" ) @@ -20,8 +21,6 @@ type runtimeConfig struct { config *Config genesis *Genesis - - useRandomPK bool } func New(configPath, genesisPath string, options ...func(*runtimeConfig)) *runtimeConfig { @@ -90,12 +89,13 @@ func (b *runtimeConfig) GetGenesis() modules.GenesisState { return b.genesis.ToShared() } -func (b *runtimeConfig) ShouldUseRandomPK() bool { - return b.useRandomPK -} - func WithRandomPK() func(*runtimeConfig) { - return func(b *runtimeConfig) { b.useRandomPK = true } + privateKey, err := cryptoPocket.GeneratePrivateKey() + if err != nil { + log.Fatalf("unable to generate private key") + } + + return WithPK(privateKey.String()) } func WithPK(pk string) func(*runtimeConfig) { @@ -104,5 +104,6 @@ func WithPK(pk string) func(*runtimeConfig) { b.config.Consensus = &types.ConsensusConfig{} } b.config.Consensus.PrivateKey = pk + b.config.P2P.PrivateKey = pk } } diff --git a/shared/modules/runtime_module.go b/shared/modules/runtime_module.go index 33889f72f..64001f814 100644 --- a/shared/modules/runtime_module.go +++ b/shared/modules/runtime_module.go @@ -3,5 +3,4 @@ package modules type Runtime interface { GetConfig() Config GetGenesis() GenesisState - ShouldUseRandomPK() bool } From 8318c20874df07b46b530442a5fb8afd5ef43699 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Mon, 26 Sep 2022 17:57:21 +0100 Subject: [PATCH 26/90] docs(runtime): // RuntimeConfig option helpers comment --- runtime/runtime.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/runtime/runtime.go b/runtime/runtime.go index 9f1c0a274..61dd4ca10 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -89,6 +89,8 @@ func (b *runtimeConfig) GetGenesis() modules.GenesisState { return b.genesis.ToShared() } +// RuntimeConfig option helpers + func WithRandomPK() func(*runtimeConfig) { privateKey, err := cryptoPocket.GeneratePrivateKey() if err != nil { From d663287b23063f9d9d0c701d54087942a0874bb8 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Mon, 26 Sep 2022 18:56:27 +0100 Subject: [PATCH 27/90] refactor(Shared): nodeModule, P2PAddressableModule --- consensus/consensus_tests/pacemaker_test.go | 2 +- consensus/consensus_tests/utils_test.go | 17 ++++++++--------- shared/modules/module.go | 9 ++++++++- shared/modules/node_module.go | 8 ++++++++ shared/node.go | 18 +++++++++++++----- 5 files changed, 38 insertions(+), 16 deletions(-) create mode 100644 shared/modules/node_module.go diff --git a/consensus/consensus_tests/pacemaker_test.go b/consensus/consensus_tests/pacemaker_test.go index 25553aa01..271e228b3 100644 --- a/consensus/consensus_tests/pacemaker_test.go +++ b/consensus/consensus_tests/pacemaker_test.go @@ -133,7 +133,7 @@ func TestPacemakerCatchupSameStepDifferentRounds(t *testing.T) { Hash: hex.EncodeToString(appHash), NumTxs: 0, LastBlockHash: "", - ProposerAddress: leader.Address.Bytes(), + ProposerAddress: leader.GetP2PAddress().Bytes(), QuorumCertificate: nil, } block := &typesCons.Block{ diff --git a/consensus/consensus_tests/utils_test.go b/consensus/consensus_tests/utils_test.go index 5621a0d42..4bdce94b8 100644 --- a/consensus/consensus_tests/utils_test.go +++ b/consensus/consensus_tests/utils_test.go @@ -59,7 +59,7 @@ func init() { } } -type IdToNodeMapping map[typesCons.NodeId]*shared.Node +type IdToNodeMapping map[typesCons.NodeId]modules.NodeModule /*** Node Generation Helpers ***/ @@ -139,9 +139,8 @@ func CreateTestConsensusPocketNode( pk, err := cryptoPocket.NewPrivateKey(cfg.Base.PrivateKey) require.NoError(t, err) - pocketNode := &shared.Node{ - Address: pk.Address(), - } + pocketNode := shared.NewWithAddress(pk.Address()) + pocketNode.SetBus(bus) return pocketNode @@ -182,21 +181,21 @@ func StartAllTestPocketNodes(t *testing.T, pocketNodes IdToNodeMapping) { // TODO(discuss): Should we use reflections inside the testing module as being done here or explicitly // define the interfaces used for debug/development. The latter will probably scale more but will // require more effort and pollute the source code with debugging information. -func GetConsensusNodeState(node *shared.Node) typesCons.ConsensusNodeState { +func GetConsensusNodeState(node modules.NodeModule) typesCons.ConsensusNodeState { return reflect.ValueOf(node.GetBus().GetConsensusModule()).MethodByName("GetNodeState").Call([]reflect.Value{})[0].Interface().(typesCons.ConsensusNodeState) } -func GetConsensusModImplementation(node *shared.Node) reflect.Value { +func GetConsensusModImplementation(node modules.NodeModule) reflect.Value { return reflect.ValueOf(node.GetBus().GetConsensusModule()).Elem() } /*** Debug/Development Message Helpers ***/ -func TriggerNextView(t *testing.T, node *shared.Node) { +func TriggerNextView(t *testing.T, node modules.NodeModule) { triggerDebugMessage(t, node, debug.DebugMessageAction_DEBUG_CONSENSUS_TRIGGER_NEXT_VIEW) } -func triggerDebugMessage(t *testing.T, node *shared.Node, action debug.DebugMessageAction) { +func triggerDebugMessage(t *testing.T, node modules.NodeModule, action debug.DebugMessageAction) { debugMessage := &debug.DebugMessage{ Action: debug.DebugMessageAction_DEBUG_CONSENSUS_TRIGGER_NEXT_VIEW, Message: nil, @@ -217,7 +216,7 @@ func P2PBroadcast(_ *testing.T, nodes IdToNodeMapping, any *anypb.Any) { } } -func P2PSend(_ *testing.T, node *shared.Node, any *anypb.Any) { +func P2PSend(_ *testing.T, node modules.NodeModule, any *anypb.Any) { e := &debug.PocketEvent{Topic: debug.PocketTopic_CONSENSUS_MESSAGE_TOPIC, Data: any} node.GetBus().PublishEventToBus(e) } diff --git a/shared/modules/module.go b/shared/modules/module.go index 40124f489..9882357a3 100644 --- a/shared/modules/module.go +++ b/shared/modules/module.go @@ -1,6 +1,9 @@ package modules -import "github.com/pokt-network/pocket/shared/crypto" +import ( + "github.com/pokt-network/pocket/shared/crypto" + cryptoPocket "github.com/pokt-network/pocket/shared/crypto" +) // TODO(olshansky): Show an example of `TypicalUsage` // TODO(drewsky): Add `Create` function; pocket/issues/163 @@ -37,3 +40,7 @@ type GenesisDependentModule interface { type KeyholderModule interface { GetPrivateKey(Runtime) (crypto.PrivateKey, error) } + +type P2PAddressableModule interface { + GetP2PAddress() cryptoPocket.Address +} diff --git a/shared/modules/node_module.go b/shared/modules/node_module.go new file mode 100644 index 000000000..16366da66 --- /dev/null +++ b/shared/modules/node_module.go @@ -0,0 +1,8 @@ +package modules + +//go:generate mockgen -source=$GOFILE -destination=./mocks/node_module_mock.go -aux_files=github.com/pokt-network/pocket/shared/modules=module.go + +type NodeModule interface { + Module + P2PAddressableModule +} diff --git a/shared/node.go b/shared/node.go index e342fc118..d3acd5409 100644 --- a/shared/node.go +++ b/shared/node.go @@ -17,15 +17,19 @@ import ( "google.golang.org/protobuf/types/known/anypb" ) -var _ modules.Module = &Node{} +var _ modules.NodeModule = &Node{} const ( MainModuleName = "main" ) type Node struct { - bus modules.Bus - Address cryptoPocket.Address + bus modules.Bus + p2pAddress cryptoPocket.Address +} + +func NewWithAddress(address cryptoPocket.Address) *Node { + return &Node{p2pAddress: address} } func Create(configPath, genesisPath string) (modules.Module, error) { @@ -78,8 +82,8 @@ func (m *Node) Create(runtime modules.Runtime) (modules.Module, error) { return nil, err } return &Node{ - bus: bus, - Address: addr, + bus: bus, + p2pAddress: addr, }, nil } @@ -180,3 +184,7 @@ func (node *Node) handleDebugEvent(anyMessage *anypb.Any) error { func (node *Node) GetModuleName() string { return MainModuleName } + +func (node *Node) GetP2PAddress() cryptoPocket.Address { + return node.p2pAddress +} From 9e5602b0fed0c066875072df67c9767d1af515fd Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Mon, 26 Sep 2022 19:00:42 +0100 Subject: [PATCH 28/90] refactor(Consensus): interface embedding --- consensus/module.go | 11 +++-------- consensus/pacemaker.go | 1 + shared/modules/consensus_module.go | 3 +++ 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/consensus/module.go b/consensus/module.go index a052e9aab..245902a2a 100644 --- a/consensus/module.go +++ b/consensus/module.go @@ -20,15 +20,10 @@ const ( ) var ( - _ modules.ConsensusGenesisState = &typesCons.ConsensusGenesisState{} - - _ modules.PacemakerConfig = &typesCons.PacemakerConfig{} - _ modules.ConsensusConfig = &typesCons.ConsensusConfig{} + _ modules.ConsensusModule = &ConsensusModule{} - _ modules.ConsensusModule = &ConsensusModule{} - _ modules.ConfigurableModule = &ConsensusModule{} - _ modules.GenesisDependentModule = &ConsensusModule{} - _ modules.KeyholderModule = &ConsensusModule{} + _ modules.ConsensusConfig = &typesCons.ConsensusConfig{} + _ modules.ConsensusGenesisState = &typesCons.ConsensusGenesisState{} ) // TODO(olshansky): Any reason to make all of these attributes local only (i.e. not exposed outside the struct)? diff --git a/consensus/pacemaker.go b/consensus/pacemaker.go index 9812403e8..7aa6ae3d0 100644 --- a/consensus/pacemaker.go +++ b/consensus/pacemaker.go @@ -35,6 +35,7 @@ var ( _ modules.Module = &paceMaker{} _ modules.ConfigurableModule = &paceMaker{} _ PacemakerDebug = &paceMaker{} + _ modules.PacemakerConfig = &typesCons.PacemakerConfig{} ) type paceMaker struct { diff --git a/shared/modules/consensus_module.go b/shared/modules/consensus_module.go index cd05f73ec..d02aa901d 100644 --- a/shared/modules/consensus_module.go +++ b/shared/modules/consensus_module.go @@ -11,6 +11,9 @@ type ValidatorMap map[string]Actor // TODO (Drewsky) deprecate Validator map or type ConsensusModule interface { Module + ConfigurableModule + GenesisDependentModule + KeyholderModule // Consensus Engine HandleMessage(*anypb.Any) error From fb873b6fead51aef65b563791fc41b2c1ace8af5 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Mon, 26 Sep 2022 19:03:31 +0100 Subject: [PATCH 29/90] refactor(Consensus): consistency in naming --- consensus/module.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/consensus/module.go b/consensus/module.go index 245902a2a..64a576298 100644 --- a/consensus/module.go +++ b/consensus/module.go @@ -82,7 +82,7 @@ func (*ConsensusModule) Create(runtime modules.Runtime) (modules.Module, error) if err := m.ValidateGenesis(genesis); err != nil { log.Fatalf("genesis validation failed: %v", err) } - moduleGenesis := genesis.ConsensusGenesisState.(*typesCons.ConsensusGenesisState) + consensusGenesis := genesis.ConsensusGenesisState.(*typesCons.ConsensusGenesisState) leaderElectionMod, err := leader_election.Create(runtime) if err != nil { @@ -95,7 +95,7 @@ func (*ConsensusModule) Create(runtime modules.Runtime) (modules.Module, error) return nil, err } - valMap := typesCons.ValidatorListToMap(moduleGenesis.Validators) + valMap := typesCons.ValidatorListToMap(consensusGenesis.Validators) privateKey, err := m.GetPrivateKey(runtime) if err != nil { @@ -134,7 +134,7 @@ func (*ConsensusModule) Create(runtime modules.Runtime) (modules.Module, error) logPrefix: DefaultLogPrefix, MessagePool: make(map[typesCons.HotstuffStep][]*typesCons.HotstuffMessage), - MaxBlockBytes: moduleGenesis.GetMaxBlockBytes(), + MaxBlockBytes: consensusGenesis.GetMaxBlockBytes(), } // TODO(olshansky): Look for a way to avoid doing this. From 8d26fd389701e23295921b6de5a709722011329b Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Mon, 26 Sep 2022 19:03:44 +0100 Subject: [PATCH 30/90] refactor(Persistence): consistence in naming --- persistence/module.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/persistence/module.go b/persistence/module.go index 88b131300..c3624fcf6 100644 --- a/persistence/module.go +++ b/persistence/module.go @@ -44,7 +44,7 @@ func (*PersistenceModule) Create(runtime modules.Runtime) (modules.Module, error genesis := runtime.GetGenesis() persistenceCfg := cfg.Persistence.(*types.PersistenceConfig) - moduleGenesis := genesis.PersistenceGenesisState.(*types.PersistenceGenesisState) + persistenceGenesis := genesis.PersistenceGenesisState.(*types.PersistenceGenesisState) conn, err := connectToDatabase(persistenceCfg.GetPostgresUrl(), persistenceCfg.GetNodeSchema()) if err != nil { @@ -76,7 +76,7 @@ func (*PersistenceModule) Create(runtime modules.Runtime) (modules.Module, error // this forces the genesis state to be reloaded on every node startup until state sync is // implemented. // NOTE: `populateGenesisState` does not return an error but logs a fatal error if there's a problem - persistenceMod.populateGenesisState(moduleGenesis) + persistenceMod.populateGenesisState(persistenceGenesis) } else { log.Println("Loading state from previous state...") } From dd78b2ead26dc5ec6373e412a75e5ee1837a132e Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Mon, 26 Sep 2022 19:55:33 +0100 Subject: [PATCH 31/90] feat(P2P): P2P module now implements ConfigurableModule --- p2p/module.go | 16 ++++++++++++++-- runtime/config.go | 4 ---- shared/modules/p2p_module.go | 2 ++ shared/modules/types.go | 1 - 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/p2p/module.go b/p2p/module.go index 560d63d29..5fabbc6b1 100644 --- a/p2p/module.go +++ b/p2p/module.go @@ -5,6 +5,7 @@ package p2p // to be a "real" replacement for now. import ( + "fmt" "log" "github.com/pokt-network/pocket/shared/debug" @@ -45,10 +46,14 @@ func Create(runtime modules.Runtime) (modules.Module, error) { return m.Create(runtime) } -func (*p2pModule) Create(runtime modules.Runtime) (m modules.Module, err error) { +func (*p2pModule) Create(runtime modules.Runtime) (modules.Module, error) { log.Println("Creating network module") + var m *p2pModule cfg := runtime.GetConfig() + if err := m.ValidateConfig(cfg); err != nil { + log.Fatalf("config validation failed: %v", err) + } p2pCfg := cfg.P2P.(*typesP2P.P2PConfig) l, err := CreateListener(p2pCfg) @@ -102,7 +107,7 @@ func (m *p2pModule) Start() error { return err } - if m.p2pConfig.GetUseRainTree() { + if m.p2pConfig.(*typesP2P.P2PConfig).UseRainTree { m.network = raintree.NewRainTreeNetwork(m.address, addrBook) } else { m.network = stdnetwork.NewNetwork(addrBook) @@ -162,6 +167,13 @@ func (m *p2pModule) Send(addr cryptoPocket.Address, msg *anypb.Any, topic debug. return m.network.NetworkSend(data, addr) } +func (*p2pModule) ValidateConfig(cfg modules.Config) error { + if _, ok := cfg.P2P.(*typesP2P.P2PConfig); !ok { + return fmt.Errorf("cannot cast to P2PConfig") + } + return nil +} + func (m *p2pModule) handleNetworkMessage(networkMsgData []byte) { appMsgData, err := m.network.HandleNetworkData(networkMsgData) if err != nil { diff --git a/runtime/config.go b/runtime/config.go index 089fb6551..277fb3505 100644 --- a/runtime/config.go +++ b/runtime/config.go @@ -69,10 +69,6 @@ func (c *Config) GetConsensusPort() uint32 { return c.P2P.ConsensusPort } -func (c *Config) GetUseRainTree() bool { - return c.P2P.UseRainTree -} - func (c *Config) IsEmptyConnType() bool { // TODO (team) make enum return c.P2P.IsEmptyConnectionType } diff --git a/shared/modules/p2p_module.go b/shared/modules/p2p_module.go index a927ca693..6c6eef045 100644 --- a/shared/modules/p2p_module.go +++ b/shared/modules/p2p_module.go @@ -10,6 +10,8 @@ import ( type P2PModule interface { Module + ConfigurableModule + Broadcast(msg *anypb.Any, topic debug.PocketTopic) error // TECHDEBT: get rid of topic Send(addr cryptoPocket.Address, msg *anypb.Any, topic debug.PocketTopic) error // TECHDEBT: get rid of topic GetAddress() (cryptoPocket.Address, error) diff --git a/shared/modules/types.go b/shared/modules/types.go index 4cc9a77ba..ee23d9b4d 100644 --- a/shared/modules/types.go +++ b/shared/modules/types.go @@ -47,7 +47,6 @@ type PersistenceConfig interface { type P2PConfig interface { GetConsensusPort() uint32 - GetUseRainTree() bool IsEmptyConnType() bool // TODO (team) make enum } From c6c95dd570d9b9cdb0cf0008af3f8d248d16da5f Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Mon, 26 Sep 2022 20:46:28 +0100 Subject: [PATCH 32/90] fix(Persistence): improved DEBUG_CLEAR_STATE --- persistence/debug.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/persistence/debug.go b/persistence/debug.go index 87900b08f..fec1edf4f 100644 --- a/persistence/debug.go +++ b/persistence/debug.go @@ -4,7 +4,7 @@ import ( "log" typesCons "github.com/pokt-network/pocket/consensus/types" - "github.com/pokt-network/pocket/runtime" + "github.com/pokt-network/pocket/persistence/types" "github.com/pokt-network/pocket/shared/codec" "github.com/pokt-network/pocket/shared/debug" ) @@ -15,11 +15,8 @@ func (m *PersistenceModule) HandleDebugMessage(debugMessage *debug.DebugMessage) m.showLatestBlockInStore(debugMessage) case debug.DebugMessageAction_DEBUG_CLEAR_STATE: m.clearState(debugMessage) - g, err := runtime.ParseGenesisJSON(m.GetBus().GetConfig().Base.GenesisPath) - if err != nil { - return err - } - m.populateGenesisState(g.PersistenceGenesisState) + g := m.GetBus().GetGenesis().PersistenceGenesisState.(*types.PersistenceGenesisState) + m.populateGenesisState(g) default: log.Printf("Debug message not handled by persistence module: %s \n", debugMessage.Message) } From f5ea7e47c9d7c1b2e84b08cb40a0ca70c61299b5 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 27 Sep 2022 08:40:32 +0100 Subject: [PATCH 33/90] feat(Shared): modules implement ConfigurableModule --- persistence/module.go | 46 ++++++++++++++++++++++------ shared/modules/persistence_module.go | 3 ++ shared/modules/telemetry_module.go | 1 + shared/modules/utility_module.go | 1 + telemetry/noop_module.go | 8 +++++ utility/module.go | 15 +++++++++ 6 files changed, 64 insertions(+), 10 deletions(-) diff --git a/persistence/module.go b/persistence/module.go index ebf6d4792..a65fe4ec2 100644 --- a/persistence/module.go +++ b/persistence/module.go @@ -12,11 +12,14 @@ import ( "github.com/pokt-network/pocket/shared/modules" ) -var _ modules.PersistenceModule = &PersistenceModule{} -var _ modules.PersistenceRWContext = &PostgresContext{} -var _ modules.PersistenceGenesisState = &types.PersistenceGenesisState{} -var _ modules.PersistenceConfig = &types.PersistenceConfig{} -var _ modules.PersistenceModule = &PersistenceModule{} +var ( + _ modules.PersistenceModule = &PersistenceModule{} + _ modules.PersistenceModule = &PersistenceModule{} + + _ modules.PersistenceRWContext = &PostgresContext{} + _ modules.PersistenceGenesisState = &types.PersistenceGenesisState{} + _ modules.PersistenceConfig = &types.PersistenceConfig{} +) type PersistenceModule struct { bus modules.Bus @@ -40,10 +43,19 @@ func Create(runtime modules.Runtime) (modules.Module, error) { } func (*PersistenceModule) Create(runtime modules.Runtime) (modules.Module, error) { + var m *PersistenceModule + cfg := runtime.GetConfig() - genesis := runtime.GetGenesis() + if err := m.ValidateConfig(cfg); err != nil { + log.Fatalf("config validation failed: %v", err) + } persistenceCfg := cfg.Persistence.(*types.PersistenceConfig) + + genesis := runtime.GetGenesis() + if err := m.ValidateGenesis(genesis); err != nil { + log.Fatalf("genesis validation failed: %v", err) + } persistenceGenesis := genesis.PersistenceGenesisState.(*types.PersistenceGenesisState) conn, err := connectToDatabase(persistenceCfg.GetPostgresUrl(), persistenceCfg.GetNodeSchema()) @@ -60,7 +72,7 @@ func (*PersistenceModule) Create(runtime modules.Runtime) (modules.Module, error return nil, err } - persistenceMod := &PersistenceModule{ + m = &PersistenceModule{ bus: nil, postgresURL: persistenceCfg.GetPostgresUrl(), nodeSchema: persistenceCfg.GetNodeSchema(), @@ -69,19 +81,19 @@ func (*PersistenceModule) Create(runtime modules.Runtime) (modules.Module, error } // Determine if we should hydrate the genesis db or use the current state of the DB attached - if shouldHydrateGenesis, err := persistenceMod.shouldHydrateGenesisDb(); err != nil { + if shouldHydrateGenesis, err := m.shouldHydrateGenesisDb(); err != nil { return nil, err } else if shouldHydrateGenesis { // TECHDEBT: reconsider if this is the best place to call `populateGenesisState`. Note that // this forces the genesis state to be reloaded on every node startup until state sync is // implemented. // NOTE: `populateGenesisState` does not return an error but logs a fatal error if there's a problem - persistenceMod.populateGenesisState(persistenceGenesis) + m.populateGenesisState(persistenceGenesis) } else { log.Println("Loading state from previous state...") } - return persistenceMod, nil + return m, nil } func (m *PersistenceModule) Start() error { @@ -109,6 +121,20 @@ func (m *PersistenceModule) GetBus() modules.Bus { return m.bus } +func (*PersistenceModule) ValidateConfig(cfg modules.Config) error { + if _, ok := cfg.Persistence.(*types.PersistenceConfig); !ok { + return fmt.Errorf("cannot cast to PersistenceConfig") + } + return nil +} + +func (*PersistenceModule) ValidateGenesis(genesis modules.GenesisState) error { + if _, ok := genesis.PersistenceGenesisState.(*types.PersistenceGenesisState); !ok { + return fmt.Errorf("cannot cast to PersistenceGenesisState") + } + return nil +} + func (m *PersistenceModule) NewRWContext(height int64) (modules.PersistenceRWContext, error) { if m.writeContext != nil && !m.writeContext.conn.IsClosed() { return nil, fmt.Errorf("write context already exists") diff --git a/shared/modules/persistence_module.go b/shared/modules/persistence_module.go index edc0e30cc..13477bf48 100644 --- a/shared/modules/persistence_module.go +++ b/shared/modules/persistence_module.go @@ -9,6 +9,9 @@ import ( type PersistenceModule interface { Module + ConfigurableModule + GenesisDependentModule + NewRWContext(height int64) (PersistenceRWContext, error) NewReadContext(height int64) (PersistenceReadContext, error) diff --git a/shared/modules/telemetry_module.go b/shared/modules/telemetry_module.go index 21e83efa3..fbe398ada 100644 --- a/shared/modules/telemetry_module.go +++ b/shared/modules/telemetry_module.go @@ -6,6 +6,7 @@ import "github.com/prometheus/client_golang/prometheus" type TelemetryModule interface { Module + ConfigurableModule GetTimeSeriesAgent() TimeSeriesAgent GetEventMetricsAgent() EventMetricsAgent diff --git a/shared/modules/utility_module.go b/shared/modules/utility_module.go index 67617399a..c7e9389df 100644 --- a/shared/modules/utility_module.go +++ b/shared/modules/utility_module.go @@ -4,6 +4,7 @@ package modules type UtilityModule interface { Module + ConfigurableModule NewContext(height int64) (UtilityContext, error) } diff --git a/telemetry/noop_module.go b/telemetry/noop_module.go index b4fbe9121..18a92a5fe 100644 --- a/telemetry/noop_module.go +++ b/telemetry/noop_module.go @@ -1,6 +1,7 @@ package telemetry import ( + "fmt" "log" "github.com/pokt-network/pocket/shared/modules" @@ -59,6 +60,13 @@ func (m *NoopTelemetryModule) GetBus() modules.Bus { return m.bus } +func (*NoopTelemetryModule) ValidateConfig(cfg modules.Config) error { + if _, ok := cfg.Telemetry.(*TelemetryConfig); !ok { + return fmt.Errorf("cannot cast to TelemetryConfig") + } + return nil +} + func (m *NoopTelemetryModule) GetEventMetricsAgent() modules.EventMetricsAgent { return modules.EventMetricsAgent(m) } diff --git a/utility/module.go b/utility/module.go index cd26813f1..24941f094 100644 --- a/utility/module.go +++ b/utility/module.go @@ -1,6 +1,7 @@ package utility import ( + "fmt" "log" "github.com/pokt-network/pocket/utility/types" @@ -27,6 +28,13 @@ func Create(runtime modules.Runtime) (modules.Module, error) { } func (*UtilityModule) Create(runtime modules.Runtime) (modules.Module, error) { + var m *UtilityModule + + cfg := runtime.GetConfig() + if err := m.ValidateConfig(cfg); err != nil { + log.Fatalf("config validation failed: %v", err) + } + return &UtilityModule{ // TODO: Add `maxTransactionBytes` and `maxTransactions` to cfg.Utility Mempool: types.NewMempool(1000, 1000), @@ -55,3 +63,10 @@ func (u *UtilityModule) GetBus() modules.Bus { } return u.bus } + +func (*UtilityModule) ValidateConfig(cfg modules.Config) error { + if _, ok := cfg.Utility.(*types.UtilityConfig); !ok { + return fmt.Errorf("cannot cast to UtilityConfig") + } + return nil +} From c60206fc430c0b2a8c93ec473d9626aaebadef20 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 27 Sep 2022 09:52:57 +0100 Subject: [PATCH 34/90] refactor(Shared): consistency: adding config *structType in modules --- consensus/hotstuff_leader.go | 2 +- consensus/module.go | 5 +++-- p2p/module.go | 4 ++-- persistence/module.go | 15 ++++++--------- telemetry/prometheus_module.go | 16 ++++++---------- utility/module.go | 6 +++++- 6 files changed, 23 insertions(+), 25 deletions(-) diff --git a/consensus/hotstuff_leader.go b/consensus/hotstuff_leader.go index d691ebc0c..fb5d127d9 100644 --- a/consensus/hotstuff_leader.go +++ b/consensus/hotstuff_leader.go @@ -292,7 +292,7 @@ func (m *ConsensusModule) aggregateMessage(msg *typesCons.HotstuffMessage) { // TODO(olshansky): Add proper tests for this when we figure out where the mempool should live. // NOTE: This is just a placeholder at the moment. It doesn't actually work because SizeOf returns // the size of the map pointer, and does not recursively determine the size of all the underlying elements. - if m.consCfg.GetMaxMempoolBytes() < uint64(unsafe.Sizeof(m.MessagePool)) { + if m.config.GetMaxMempoolBytes() < uint64(unsafe.Sizeof(m.MessagePool)) { m.nodeLogError(typesCons.DisregardHotstuffMessage, typesCons.ErrConsensusMempoolFull) return } diff --git a/consensus/module.go b/consensus/module.go index 64a576298..42e989bf5 100644 --- a/consensus/module.go +++ b/consensus/module.go @@ -5,6 +5,7 @@ import ( "log" "github.com/pokt-network/pocket/consensus/leader_election" + "github.com/pokt-network/pocket/consensus/types" typesCons "github.com/pokt-network/pocket/consensus/types" cryptoPocket "github.com/pokt-network/pocket/shared/crypto" "google.golang.org/protobuf/proto" @@ -31,7 +32,7 @@ var ( type ConsensusModule struct { bus modules.Bus privateKey cryptoPocket.Ed25519PrivateKey - consCfg modules.ConsensusConfig + config *types.ConsensusConfig // Hotstuff Height uint64 @@ -110,7 +111,7 @@ func (*ConsensusModule) Create(runtime modules.Runtime) (modules.Module, error) bus: nil, privateKey: privateKey.(cryptoPocket.Ed25519PrivateKey), - consCfg: consensusCfg, + config: consensusCfg, Height: 0, Round: 0, diff --git a/p2p/module.go b/p2p/module.go index 5fabbc6b1..b92fb2c2a 100644 --- a/p2p/module.go +++ b/p2p/module.go @@ -28,7 +28,7 @@ const ( type p2pModule struct { bus modules.Bus - p2pConfig modules.P2PConfig // TODO (olshansky): to remove this since it'll be available via the bus + p2pConfig *typesP2P.P2PConfig // TODO (olshansky): to remove this since it'll be available via the bus listener typesP2P.Transport address cryptoPocket.Address @@ -107,7 +107,7 @@ func (m *p2pModule) Start() error { return err } - if m.p2pConfig.(*typesP2P.P2PConfig).UseRainTree { + if m.p2pConfig.UseRainTree { m.network = raintree.NewRainTreeNetwork(m.address, addrBook) } else { m.network = stdnetwork.NewNetwork(addrBook) diff --git a/persistence/module.go b/persistence/module.go index a65fe4ec2..06ef2627e 100644 --- a/persistence/module.go +++ b/persistence/module.go @@ -22,12 +22,10 @@ var ( ) type PersistenceModule struct { - bus modules.Bus + bus modules.Bus + config *types.PersistenceConfig - postgresURL string - nodeSchema string - genesisPath string - blockStore kvstore.KVStore // INVESTIGATE: We may need to create a custom `BlockStore` package in the future + blockStore kvstore.KVStore // INVESTIGATE: We may need to create a custom `BlockStore` package in the future // TECHDEBT: Need to implement context pooling (for writes), timeouts (for read & writes), etc... writeContext *PostgresContext // only one write context is allowed at a time @@ -74,8 +72,7 @@ func (*PersistenceModule) Create(runtime modules.Runtime) (modules.Module, error m = &PersistenceModule{ bus: nil, - postgresURL: persistenceCfg.GetPostgresUrl(), - nodeSchema: persistenceCfg.GetNodeSchema(), + config: persistenceCfg, blockStore: blockStore, writeContext: nil, } @@ -139,7 +136,7 @@ func (m *PersistenceModule) NewRWContext(height int64) (modules.PersistenceRWCon if m.writeContext != nil && !m.writeContext.conn.IsClosed() { return nil, fmt.Errorf("write context already exists") } - conn, err := connectToDatabase(m.postgresURL, m.nodeSchema) + conn, err := connectToDatabase(m.config.PostgresUrl, m.config.NodeSchema) if err != nil { return nil, err } @@ -164,7 +161,7 @@ func (m *PersistenceModule) NewRWContext(height int64) (modules.PersistenceRWCon } func (m *PersistenceModule) NewReadContext(height int64) (modules.PersistenceReadContext, error) { - conn, err := connectToDatabase(m.postgresURL, m.nodeSchema) + conn, err := connectToDatabase(m.config.PostgresUrl, m.config.NodeSchema) if err != nil { return nil, err } diff --git a/telemetry/prometheus_module.go b/telemetry/prometheus_module.go index 21b7347bc..33bc08577 100644 --- a/telemetry/prometheus_module.go +++ b/telemetry/prometheus_module.go @@ -22,10 +22,8 @@ var ( // DISCUSS(team): Should the warning logs in this module be handled differently? type PrometheusTelemetryModule struct { - bus modules.Bus - - address string - endpoint string + bus modules.Bus + config *TelemetryConfig counters map[string]prometheus.Counter gauges map[string]prometheus.Gauge @@ -49,20 +47,18 @@ func (m *PrometheusTelemetryModule) Create(runtime modules.Runtime) (modules.Mod telemetryCfg := cfg.Telemetry.(*TelemetryConfig) return &PrometheusTelemetryModule{ + config: telemetryCfg, counters: map[string]prometheus.Counter{}, gauges: map[string]prometheus.Gauge{}, gaugeVectors: map[string]prometheus.GaugeVec{}, - - address: telemetryCfg.GetAddress(), - endpoint: telemetryCfg.GetEndpoint(), }, nil } func (m *PrometheusTelemetryModule) Start() error { - log.Printf("\nPrometheus metrics exporter: Starting at %s%s...\n", m.address, m.endpoint) + log.Printf("\nPrometheus metrics exporter: Starting at %s%s...\n", m.config.Address, m.config.Endpoint) - http.Handle(m.endpoint, promhttp.Handler()) - go http.ListenAndServe(m.address, nil) + http.Handle(m.config.Endpoint, promhttp.Handler()) + go http.ListenAndServe(m.config.Address, nil) log.Println("Prometheus metrics exporter started: OK") diff --git a/utility/module.go b/utility/module.go index 24941f094..ea19a16e2 100644 --- a/utility/module.go +++ b/utility/module.go @@ -14,7 +14,9 @@ var _ modules.UtilityConfig = &types.UtilityConfig{} var _ modules.Module = &UtilityModule{} type UtilityModule struct { - bus modules.Bus + bus modules.Bus + config *types.UtilityConfig + Mempool types.Mempool } @@ -34,8 +36,10 @@ func (*UtilityModule) Create(runtime modules.Runtime) (modules.Module, error) { if err := m.ValidateConfig(cfg); err != nil { log.Fatalf("config validation failed: %v", err) } + utilityCfg := cfg.Utility.(*types.UtilityConfig) return &UtilityModule{ + config: utilityCfg, // TODO: Add `maxTransactionBytes` and `maxTransactions` to cfg.Utility Mempool: types.NewMempool(1000, 1000), }, nil From b86c4bb82ba57f28a73e77e54d2e5dd7dc3a4343 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 27 Sep 2022 10:57:41 +0100 Subject: [PATCH 35/90] Update consensus/module.go Co-authored-by: Daniel Olshansky --- consensus/module.go | 1 - 1 file changed, 1 deletion(-) diff --git a/consensus/module.go b/consensus/module.go index 42e989bf5..edd965ba3 100644 --- a/consensus/module.go +++ b/consensus/module.go @@ -22,7 +22,6 @@ const ( var ( _ modules.ConsensusModule = &ConsensusModule{} - _ modules.ConsensusConfig = &typesCons.ConsensusConfig{} _ modules.ConsensusGenesisState = &typesCons.ConsensusGenesisState{} ) From 3f939300f7013b457dd504dad74b5c4c08ea9c82 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 27 Sep 2022 12:33:51 +0100 Subject: [PATCH 36/90] fix(Runtime): fix WithPK for when called with a nil P2PConfig --- runtime/runtime.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/runtime/runtime.go b/runtime/runtime.go index 61dd4ca10..4cec6c090 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -8,6 +8,7 @@ import ( "github.com/mitchellh/mapstructure" "github.com/pokt-network/pocket/consensus/types" + typesP2P "github.com/pokt-network/pocket/p2p/types" cryptoPocket "github.com/pokt-network/pocket/shared/crypto" "github.com/pokt-network/pocket/shared/modules" "github.com/spf13/viper" @@ -106,6 +107,10 @@ func WithPK(pk string) func(*runtimeConfig) { b.config.Consensus = &types.ConsensusConfig{} } b.config.Consensus.PrivateKey = pk + + if b.config.P2P == nil { + b.config.P2P = &typesP2P.P2PConfig{} + } b.config.P2P.PrivateKey = pk } } From 382eacf2360089bfb6b48d26a870b1b30e479385 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Wed, 28 Sep 2022 16:07:19 +0100 Subject: [PATCH 37/90] fix(Shared): bus creation via runtime --- app/client/main.go | 7 +++---- consensus/consensus_tests/utils_test.go | 4 +--- shared/bus.go | 14 ++++++-------- shared/node.go | 5 +---- 4 files changed, 11 insertions(+), 19 deletions(-) diff --git a/app/client/main.go b/app/client/main.go index a0b61f530..1310d76fc 100644 --- a/app/client/main.go +++ b/app/client/main.go @@ -50,14 +50,13 @@ func main() { var err error runtime := runtime.New(defaultConfigPath, defaultGenesisPath, runtime.WithRandomPK()) - cfg := runtime.GetConfig() - genesis := runtime.GetGenesis() consM, err := consensus.Create(runtime) - consensusMod := consM.(modules.ConsensusModule) if err != nil { log.Fatalf("[ERROR] Failed to create consensus module: %v", err.Error()) } + consensusMod := consM.(modules.ConsensusModule) + p2pM, err := p2p.Create(runtime) if err != nil { log.Fatalf("[ERROR] Failed to create p2p module: %v", err.Error()) @@ -73,7 +72,7 @@ func main() { } telemetryMod := telemetryM.(modules.TelemetryModule) - _ = shared.CreateBusWithOptionalModules(cfg, genesis, nil, p2pMod, nil, consensusMod, telemetryMod) + _ = shared.CreateBusWithOptionalModules(runtime, nil, p2pMod, nil, consensusMod, telemetryMod) p2pMod.Start() diff --git a/consensus/consensus_tests/utils_test.go b/consensus/consensus_tests/utils_test.go index 4bdce94b8..3ffb459af 100644 --- a/consensus/consensus_tests/utils_test.go +++ b/consensus/consensus_tests/utils_test.go @@ -121,8 +121,6 @@ func CreateTestConsensusPocketNode( createTestingGenesisAndConfigFiles(t, cfg, genesisState) runtime := runtime.New(testingConfigFilePath, testingGenesisFilePath) - config := runtime.GetConfig() - genesis := runtime.GetGenesis() consensusMod, err := consensus.Create(runtime) require.NoError(t, err) @@ -133,7 +131,7 @@ func CreateTestConsensusPocketNode( utilityMock := baseUtilityMock(t, testChannel) telemetryMock := baseTelemetryMock(t, testChannel) - bus, err := shared.CreateBus(config, genesis, persistenceMock, p2pMock, utilityMock, consensusMod.(modules.ConsensusModule), telemetryMock) + bus, err := shared.CreateBus(runtime, persistenceMock, p2pMock, utilityMock, consensusMod.(modules.ConsensusModule), telemetryMock) require.NoError(t, err) pk, err := cryptoPocket.NewPrivateKey(cfg.Base.PrivateKey) diff --git a/shared/bus.go b/shared/bus.go index 713d5e3a8..5bf555afb 100644 --- a/shared/bus.go +++ b/shared/bus.go @@ -32,8 +32,7 @@ const ( ) func CreateBus( - cfg modules.Config, - genesis modules.GenesisState, + runtime modules.Runtime, persistence modules.PersistenceModule, p2p modules.P2PModule, utility modules.UtilityModule, @@ -49,8 +48,8 @@ func CreateBus( consensus: consensus, telemetry: telemetry, - config: cfg, - genesis: genesis, + config: runtime.GetConfig(), + genesis: runtime.GetGenesis(), } modules := map[string]modules.Module{ @@ -84,8 +83,7 @@ func CreateBus( // Using `CreateBusWithOptionalModules`, we can create a bus with only pre2p and a NOOP telemetry module // so that we can the pre2p module without any issues. func CreateBusWithOptionalModules( - cfg modules.Config, - genesis modules.GenesisState, + runtime modules.Runtime, persistence modules.PersistenceModule, p2p modules.P2PModule, utility modules.UtilityModule, @@ -99,8 +97,8 @@ func CreateBusWithOptionalModules( utility: utility, consensus: consensus, telemetry: telemetry, - config: cfg, - genesis: genesis, + config: runtime.GetConfig(), + genesis: runtime.GetGenesis(), } maybeSetModuleBus := func(mod modules.Module) { diff --git a/shared/node.go b/shared/node.go index d3acd5409..b9a5a4da7 100644 --- a/shared/node.go +++ b/shared/node.go @@ -39,9 +39,6 @@ func Create(configPath, genesisPath string) (modules.Module, error) { } func (m *Node) Create(runtime modules.Runtime) (modules.Module, error) { - cfg := runtime.GetConfig() - genesis := runtime.GetGenesis() - persistenceMod, err := persistence.Create(runtime) if err != nil { return nil, err @@ -67,7 +64,7 @@ func (m *Node) Create(runtime modules.Runtime) (modules.Module, error) { return nil, err } - bus, err := CreateBus(cfg, genesis, + bus, err := CreateBus(runtime, persistenceMod.(modules.PersistenceModule), p2pMod.(modules.P2PModule), utilityMod.(modules.UtilityModule), From 45a549175981bba177c7f2c2d52421e899c537e6 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Wed, 28 Sep 2022 16:09:19 +0100 Subject: [PATCH 38/90] refactor(Shared): NewWithAddress -> NewNodeWithAddress --- consensus/consensus_tests/utils_test.go | 2 +- shared/node.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/consensus/consensus_tests/utils_test.go b/consensus/consensus_tests/utils_test.go index 3ffb459af..19ec50664 100644 --- a/consensus/consensus_tests/utils_test.go +++ b/consensus/consensus_tests/utils_test.go @@ -137,7 +137,7 @@ func CreateTestConsensusPocketNode( pk, err := cryptoPocket.NewPrivateKey(cfg.Base.PrivateKey) require.NoError(t, err) - pocketNode := shared.NewWithAddress(pk.Address()) + pocketNode := shared.NewNodeWithAddress(pk.Address()) pocketNode.SetBus(bus) diff --git a/shared/node.go b/shared/node.go index b9a5a4da7..8ca61cccc 100644 --- a/shared/node.go +++ b/shared/node.go @@ -28,7 +28,7 @@ type Node struct { p2pAddress cryptoPocket.Address } -func NewWithAddress(address cryptoPocket.Address) *Node { +func NewNodeWithAddress(address cryptoPocket.Address) *Node { return &Node{p2pAddress: address} } From 843f5f7d675360ce22fc558d12ed29e4c3b37fcf Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Wed, 28 Sep 2022 16:24:58 +0100 Subject: [PATCH 39/90] refactor(Shared): improved Create, making all modules unexported --- consensus/block.go | 13 +++--- consensus/consensus_tests/utils_test.go | 2 +- consensus/debugging.go | 15 +++---- consensus/helpers.go | 29 +++++++------- consensus/hotstuff_leader.go | 22 +++++----- consensus/hotstuff_replica.go | 20 +++++----- consensus/leader_election/module.go | 3 +- consensus/messages.go | 4 +- consensus/module.go | 53 ++++++++++++------------- consensus/pacemaker.go | 6 +-- p2p/module.go | 3 +- persistence/debug.go | 6 +-- persistence/genesis.go | 6 ++- persistence/module.go | 39 +++++++++--------- persistence/test/setup_test.go | 2 +- telemetry/module.go | 3 +- utility/context.go | 3 +- utility/module.go | 27 ++++++------- utility/test/module_test.go | 2 +- 19 files changed, 129 insertions(+), 129 deletions(-) diff --git a/consensus/block.go b/consensus/block.go index ae531bcb8..5e55d2f45 100644 --- a/consensus/block.go +++ b/consensus/block.go @@ -2,14 +2,15 @@ package consensus import ( "encoding/hex" - "github.com/pokt-network/pocket/shared/codec" "unsafe" + "github.com/pokt-network/pocket/shared/codec" + typesCons "github.com/pokt-network/pocket/consensus/types" ) // TODO(olshansky): Sync with Andrew on the type of validation we need here. -func (m *ConsensusModule) validateBlock(block *typesCons.Block) error { +func (m *consensusModule) validateBlock(block *typesCons.Block) error { if block == nil { return typesCons.ErrNilBlock } @@ -17,7 +18,7 @@ func (m *ConsensusModule) validateBlock(block *typesCons.Block) error { } // This is a helper function intended to be called by a leader/validator during a view change -func (m *ConsensusModule) prepareBlockAsLeader() (*typesCons.Block, error) { +func (m *consensusModule) prepareBlockAsLeader() (*typesCons.Block, error) { if m.isReplica() { return nil, typesCons.ErrReplicaPrepareBlock } @@ -54,7 +55,7 @@ func (m *ConsensusModule) prepareBlockAsLeader() (*typesCons.Block, error) { } // This is a helper function intended to be called by a replica/voter during a view change -func (m *ConsensusModule) applyBlockAsReplica(block *typesCons.Block) error { +func (m *consensusModule) applyBlockAsReplica(block *typesCons.Block) error { if m.isLeader() { return typesCons.ErrLeaderApplyBLock } @@ -82,7 +83,7 @@ func (m *ConsensusModule) applyBlockAsReplica(block *typesCons.Block) error { } // Creates a new Utility context and clears/nullifies any previous contexts if they exist -func (m *ConsensusModule) refreshUtilityContext() error { +func (m *consensusModule) refreshUtilityContext() error { // This is a catch-all to release the previous utility context if it wasn't cleaned up // in the proper lifecycle (e.g. catch up, error, network partition, etc...). Ideally, this // should not be called. @@ -101,7 +102,7 @@ func (m *ConsensusModule) refreshUtilityContext() error { return nil } -func (m *ConsensusModule) commitBlock(block *typesCons.Block) error { +func (m *consensusModule) commitBlock(block *typesCons.Block) error { m.nodeLog(typesCons.CommittingBlock(m.Height, len(block.Transactions))) // Store the block in the KV store diff --git a/consensus/consensus_tests/utils_test.go b/consensus/consensus_tests/utils_test.go index 19ec50664..f067a1606 100644 --- a/consensus/consensus_tests/utils_test.go +++ b/consensus/consensus_tests/utils_test.go @@ -153,7 +153,7 @@ func createTestingGenesisAndConfigFiles(t *testing.T, cfg runtime.Config, genesi genesisFile := make(map[string]json.RawMessage) configFile := make(map[string]json.RawMessage) - consensusModName := new(consensus.ConsensusModule).GetModuleName() + consensusModName := consensus.ConsensusModuleName genesisFile[test_artifacts.GetGenesisFileName(consensusModName)] = genesis configFile[consensusModName] = config diff --git a/consensus/debugging.go b/consensus/debugging.go index 5e111095e..41fedaf53 100644 --- a/consensus/debugging.go +++ b/consensus/debugging.go @@ -1,14 +1,15 @@ package consensus import ( - "github.com/pokt-network/pocket/shared/debug" "log" "time" + "github.com/pokt-network/pocket/shared/debug" + typesCons "github.com/pokt-network/pocket/consensus/types" ) -func (m *ConsensusModule) HandleDebugMessage(debugMessage *debug.DebugMessage) error { +func (m *consensusModule) HandleDebugMessage(debugMessage *debug.DebugMessage) error { switch debugMessage.Action { case debug.DebugMessageAction_DEBUG_CONSENSUS_RESET_TO_GENESIS: m.resetToGenesis(debugMessage) @@ -24,7 +25,7 @@ func (m *ConsensusModule) HandleDebugMessage(debugMessage *debug.DebugMessage) e return nil } -func (m *ConsensusModule) GetNodeState() typesCons.ConsensusNodeState { +func (m *consensusModule) GetNodeState() typesCons.ConsensusNodeState { leaderId := typesCons.NodeId(0) if m.LeaderId != nil { leaderId = *m.LeaderId @@ -39,7 +40,7 @@ func (m *ConsensusModule) GetNodeState() typesCons.ConsensusNodeState { } } -func (m *ConsensusModule) resetToGenesis(_ *debug.DebugMessage) { +func (m *consensusModule) resetToGenesis(_ *debug.DebugMessage) { m.nodeLog(typesCons.DebugResetToGenesis) m.Height = 0 @@ -59,12 +60,12 @@ func (m *ConsensusModule) resetToGenesis(_ *debug.DebugMessage) { m.GetBus().GetPersistenceModule().Start() // reload genesis state } -func (m *ConsensusModule) printNodeState(_ *debug.DebugMessage) { +func (m *consensusModule) printNodeState(_ *debug.DebugMessage) { state := m.GetNodeState() m.nodeLog(typesCons.DebugNodeState(state)) } -func (m *ConsensusModule) triggerNextView(_ *debug.DebugMessage) { +func (m *consensusModule) triggerNextView(_ *debug.DebugMessage) { m.nodeLog(typesCons.DebugTriggerNextView) if m.Height == 0 || (m.Step == Decide && m.paceMaker.IsManualMode()) { @@ -78,7 +79,7 @@ func (m *ConsensusModule) triggerNextView(_ *debug.DebugMessage) { } } -func (m *ConsensusModule) togglePacemakerManualMode(_ *debug.DebugMessage) { +func (m *consensusModule) togglePacemakerManualMode(_ *debug.DebugMessage) { newMode := !m.paceMaker.IsManualMode() if newMode { m.nodeLog(typesCons.DebugTogglePacemakerManualMode("MANUAL")) diff --git a/consensus/helpers.go b/consensus/helpers.go index 24a84612c..7305b8397 100644 --- a/consensus/helpers.go +++ b/consensus/helpers.go @@ -2,9 +2,10 @@ package consensus import ( "encoding/base64" - "github.com/pokt-network/pocket/shared/debug" "log" + "github.com/pokt-network/pocket/shared/debug" + "google.golang.org/protobuf/proto" typesCons "github.com/pokt-network/pocket/consensus/types" @@ -38,7 +39,7 @@ var ( // ** Hotstuff Helpers ** // -func (m *ConsensusModule) getQuorumCertificate(height uint64, step typesCons.HotstuffStep, round uint64) (*typesCons.QuorumCertificate, error) { +func (m *consensusModule) getQuorumCertificate(height uint64, step typesCons.HotstuffStep, round uint64) (*typesCons.QuorumCertificate, error) { var pss []*typesCons.PartialSignature for _, msg := range m.MessagePool[step] { // TODO(olshansky): Add tests for this @@ -78,7 +79,7 @@ func (m *ConsensusModule) getQuorumCertificate(height uint64, step typesCons.Hot }, nil } -func (m *ConsensusModule) findHighQC(step typesCons.HotstuffStep) (qc *typesCons.QuorumCertificate) { +func (m *consensusModule) findHighQC(step typesCons.HotstuffStep) (qc *typesCons.QuorumCertificate) { for _, m := range m.MessagePool[step] { if m.GetQuorumCertificate() == nil { continue @@ -112,11 +113,11 @@ func isSignatureValid(m *typesCons.HotstuffMessage, pubKeyString string, signatu return pubKey.Verify(bytesToVerify, signature) } -func (m *ConsensusModule) didReceiveEnoughMessageForStep(step typesCons.HotstuffStep) error { +func (m *consensusModule) didReceiveEnoughMessageForStep(step typesCons.HotstuffStep) error { return m.isOptimisticThresholdMet(len(m.MessagePool[step])) } -func (m *ConsensusModule) isOptimisticThresholdMet(n int) error { +func (m *consensusModule) isOptimisticThresholdMet(n int) error { numValidators := len(m.validatorMap) if !(float64(n) > ByzantineThreshold*float64(numValidators)) { return typesCons.ErrByzantineThresholdCheck(n, ByzantineThreshold*float64(numValidators)) @@ -134,7 +135,7 @@ func protoHash(m proto.Message) string { /*** P2P Helpers ***/ -func (m *ConsensusModule) sendToNode(msg *typesCons.HotstuffMessage) { +func (m *consensusModule) sendToNode(msg *typesCons.HotstuffMessage) { // TODO(olshansky): This can happen due to a race condition with the pacemaker. if m.LeaderId == nil { m.nodeLogError(typesCons.ErrNilLeaderId.Error(), nil) @@ -154,7 +155,7 @@ func (m *ConsensusModule) sendToNode(msg *typesCons.HotstuffMessage) { } } -func (m *ConsensusModule) broadcastToNodes(msg *typesCons.HotstuffMessage) { +func (m *consensusModule) broadcastToNodes(msg *typesCons.HotstuffMessage) { m.nodeLog(typesCons.BroadcastingMessage(msg)) anyConsensusMessage, err := anypb.New(msg) if err != nil { @@ -170,7 +171,7 @@ func (m *ConsensusModule) broadcastToNodes(msg *typesCons.HotstuffMessage) { /*** Persistence Helpers ***/ -func (m *ConsensusModule) clearMessagesPool() { +func (m *consensusModule) clearMessagesPool() { for _, step := range HotstuffSteps { m.MessagePool[step] = make([]*typesCons.HotstuffMessage, 0) } @@ -178,20 +179,20 @@ func (m *ConsensusModule) clearMessagesPool() { /*** Leader Election Helpers ***/ -func (m *ConsensusModule) isLeader() bool { +func (m *consensusModule) isLeader() bool { return m.LeaderId != nil && *m.LeaderId == m.NodeId } -func (m *ConsensusModule) isReplica() bool { +func (m *consensusModule) isReplica() bool { return !m.isLeader() } -func (m *ConsensusModule) clearLeader() { +func (m *consensusModule) clearLeader() { m.logPrefix = DefaultLogPrefix m.LeaderId = nil } -func (m *ConsensusModule) electNextLeader(message *typesCons.HotstuffMessage) { +func (m *consensusModule) electNextLeader(message *typesCons.HotstuffMessage) { leaderId, err := m.leaderElectionMod.ElectNextLeader(message) if err != nil || leaderId == 0 { m.nodeLogError(typesCons.ErrLeaderElection(message).Error(), err) @@ -212,10 +213,10 @@ func (m *ConsensusModule) electNextLeader(message *typesCons.HotstuffMessage) { /*** General Infrastructure Helpers ***/ -func (m *ConsensusModule) nodeLog(s string) { +func (m *consensusModule) nodeLog(s string) { log.Printf("[%s][%d] %s\n", m.logPrefix, m.NodeId, s) } -func (m *ConsensusModule) nodeLogError(s string, err error) { +func (m *consensusModule) nodeLogError(s string, err error) { log.Printf("[ERROR][%s][%d] %s: %v\n", m.logPrefix, m.NodeId, s, err) } diff --git a/consensus/hotstuff_leader.go b/consensus/hotstuff_leader.go index fb5d127d9..7da0c9b86 100644 --- a/consensus/hotstuff_leader.go +++ b/consensus/hotstuff_leader.go @@ -9,7 +9,7 @@ import ( var ( LeaderMessageHandler HotstuffMessageHandler = &HotstuffLeaderMessageHandler{} - leaderHandlers = map[typesCons.HotstuffStep]func(*ConsensusModule, *typesCons.HotstuffMessage){ + leaderHandlers = map[typesCons.HotstuffStep]func(*consensusModule, *typesCons.HotstuffMessage){ NewRound: LeaderMessageHandler.HandleNewRoundMessage, Prepare: LeaderMessageHandler.HandlePrepareMessage, PreCommit: LeaderMessageHandler.HandlePrecommitMessage, @@ -22,7 +22,7 @@ type HotstuffLeaderMessageHandler struct{} /*** Prepare Step ***/ -func (handler *HotstuffLeaderMessageHandler) HandleNewRoundMessage(m *ConsensusModule, msg *typesCons.HotstuffMessage) { +func (handler *HotstuffLeaderMessageHandler) HandleNewRoundMessage(m *consensusModule, msg *typesCons.HotstuffMessage) { handler.emitTelemetryEvent(m, msg) if err := handler.anteHandle(m, msg); err != nil { @@ -78,7 +78,7 @@ func (handler *HotstuffLeaderMessageHandler) HandleNewRoundMessage(m *ConsensusM /*** PreCommit Step ***/ -func (handler *HotstuffLeaderMessageHandler) HandlePrepareMessage(m *ConsensusModule, msg *typesCons.HotstuffMessage) { +func (handler *HotstuffLeaderMessageHandler) HandlePrepareMessage(m *consensusModule, msg *typesCons.HotstuffMessage) { handler.emitTelemetryEvent(m, msg) if err := handler.anteHandle(m, msg); err != nil { @@ -122,7 +122,7 @@ func (handler *HotstuffLeaderMessageHandler) HandlePrepareMessage(m *ConsensusMo /*** Commit Step ***/ -func (handler *HotstuffLeaderMessageHandler) HandlePrecommitMessage(m *ConsensusModule, msg *typesCons.HotstuffMessage) { +func (handler *HotstuffLeaderMessageHandler) HandlePrecommitMessage(m *consensusModule, msg *typesCons.HotstuffMessage) { handler.emitTelemetryEvent(m, msg) if err := handler.anteHandle(m, msg); err != nil { @@ -166,7 +166,7 @@ func (handler *HotstuffLeaderMessageHandler) HandlePrecommitMessage(m *Consensus /*** Decide Step ***/ -func (handler *HotstuffLeaderMessageHandler) HandleCommitMessage(m *ConsensusModule, msg *typesCons.HotstuffMessage) { +func (handler *HotstuffLeaderMessageHandler) HandleCommitMessage(m *consensusModule, msg *typesCons.HotstuffMessage) { handler.emitTelemetryEvent(m, msg) if err := handler.anteHandle(m, msg); err != nil { @@ -215,7 +215,7 @@ func (handler *HotstuffLeaderMessageHandler) HandleCommitMessage(m *ConsensusMod ) } -func (handler *HotstuffLeaderMessageHandler) HandleDecideMessage(m *ConsensusModule, msg *typesCons.HotstuffMessage) { +func (handler *HotstuffLeaderMessageHandler) HandleDecideMessage(m *consensusModule, msg *typesCons.HotstuffMessage) { handler.emitTelemetryEvent(m, msg) if err := handler.anteHandle(m, msg); err != nil { @@ -225,7 +225,7 @@ func (handler *HotstuffLeaderMessageHandler) HandleDecideMessage(m *ConsensusMod } // anteHandle is the general handler called for every before every specific HotstuffLeaderMessageHandler handler -func (handler *HotstuffLeaderMessageHandler) anteHandle(m *ConsensusModule, msg *typesCons.HotstuffMessage) error { +func (handler *HotstuffLeaderMessageHandler) anteHandle(m *consensusModule, msg *typesCons.HotstuffMessage) error { if err := handler.validateBasic(m, msg); err != nil { return err } @@ -233,7 +233,7 @@ func (handler *HotstuffLeaderMessageHandler) anteHandle(m *ConsensusModule, msg return nil } -func (handler *HotstuffLeaderMessageHandler) emitTelemetryEvent(m *ConsensusModule, msg *typesCons.HotstuffMessage) { +func (handler *HotstuffLeaderMessageHandler) emitTelemetryEvent(m *consensusModule, msg *typesCons.HotstuffMessage) { m.GetBus(). GetTelemetryModule(). GetEventMetricsAgent(). @@ -247,7 +247,7 @@ func (handler *HotstuffLeaderMessageHandler) emitTelemetryEvent(m *ConsensusModu } // ValidateBasic general validation checks that apply to every HotstuffLeaderMessage -func (handler *HotstuffLeaderMessageHandler) validateBasic(m *ConsensusModule, msg *typesCons.HotstuffMessage) error { +func (handler *HotstuffLeaderMessageHandler) validateBasic(m *consensusModule, msg *typesCons.HotstuffMessage) error { // Discard messages with invalid partial signatures before storing it in the leader's consensus mempool if err := m.validatePartialSignature(msg); err != nil { return err @@ -255,7 +255,7 @@ func (handler *HotstuffLeaderMessageHandler) validateBasic(m *ConsensusModule, m return nil } -func (m *ConsensusModule) validatePartialSignature(msg *typesCons.HotstuffMessage) error { +func (m *consensusModule) validatePartialSignature(msg *typesCons.HotstuffMessage) error { if msg.Step == NewRound { m.nodeLog(typesCons.ErrUnnecessaryPartialSigForNewRound.Error()) return nil @@ -288,7 +288,7 @@ func (m *ConsensusModule) validatePartialSignature(msg *typesCons.HotstuffMessag address, m.ValAddrToIdMap[address], msg, pubKey) } -func (m *ConsensusModule) aggregateMessage(msg *typesCons.HotstuffMessage) { +func (m *consensusModule) aggregateMessage(msg *typesCons.HotstuffMessage) { // TODO(olshansky): Add proper tests for this when we figure out where the mempool should live. // NOTE: This is just a placeholder at the moment. It doesn't actually work because SizeOf returns // the size of the map pointer, and does not recursively determine the size of all the underlying elements. diff --git a/consensus/hotstuff_replica.go b/consensus/hotstuff_replica.go index 25041c766..84ba1cb60 100644 --- a/consensus/hotstuff_replica.go +++ b/consensus/hotstuff_replica.go @@ -12,7 +12,7 @@ type HotstuffReplicaMessageHandler struct{} var ( ReplicaMessageHandler HotstuffMessageHandler = &HotstuffReplicaMessageHandler{} - replicaHandlers = map[typesCons.HotstuffStep]func(*ConsensusModule, *typesCons.HotstuffMessage){ + replicaHandlers = map[typesCons.HotstuffStep]func(*consensusModule, *typesCons.HotstuffMessage){ NewRound: ReplicaMessageHandler.HandleNewRoundMessage, Prepare: ReplicaMessageHandler.HandlePrepareMessage, PreCommit: ReplicaMessageHandler.HandlePrecommitMessage, @@ -23,7 +23,7 @@ var ( /*** NewRound Step ***/ -func (handler *HotstuffReplicaMessageHandler) HandleNewRoundMessage(m *ConsensusModule, msg *typesCons.HotstuffMessage) { +func (handler *HotstuffReplicaMessageHandler) HandleNewRoundMessage(m *consensusModule, msg *typesCons.HotstuffMessage) { handler.emitTelemetryEvent(m, msg) if err := handler.anteHandle(m, msg); err != nil { @@ -37,7 +37,7 @@ func (handler *HotstuffReplicaMessageHandler) HandleNewRoundMessage(m *Consensus /*** Prepare Step ***/ -func (handler *HotstuffReplicaMessageHandler) HandlePrepareMessage(m *ConsensusModule, msg *typesCons.HotstuffMessage) { +func (handler *HotstuffReplicaMessageHandler) HandlePrepareMessage(m *consensusModule, msg *typesCons.HotstuffMessage) { handler.emitTelemetryEvent(m, msg) if err := handler.anteHandle(m, msg); err != nil { @@ -70,7 +70,7 @@ func (handler *HotstuffReplicaMessageHandler) HandlePrepareMessage(m *ConsensusM /*** PreCommit Step ***/ -func (handler *HotstuffReplicaMessageHandler) HandlePrecommitMessage(m *ConsensusModule, msg *typesCons.HotstuffMessage) { +func (handler *HotstuffReplicaMessageHandler) HandlePrecommitMessage(m *consensusModule, msg *typesCons.HotstuffMessage) { handler.emitTelemetryEvent(m, msg) if err := handler.anteHandle(m, msg); err != nil { @@ -98,7 +98,7 @@ func (handler *HotstuffReplicaMessageHandler) HandlePrecommitMessage(m *Consensu /*** Commit Step ***/ -func (handler *HotstuffReplicaMessageHandler) HandleCommitMessage(m *ConsensusModule, msg *typesCons.HotstuffMessage) { +func (handler *HotstuffReplicaMessageHandler) HandleCommitMessage(m *consensusModule, msg *typesCons.HotstuffMessage) { handler.emitTelemetryEvent(m, msg) if err := handler.anteHandle(m, msg); err != nil { @@ -126,7 +126,7 @@ func (handler *HotstuffReplicaMessageHandler) HandleCommitMessage(m *ConsensusMo /*** Decide Step ***/ -func (handler *HotstuffReplicaMessageHandler) HandleDecideMessage(m *ConsensusModule, msg *typesCons.HotstuffMessage) { +func (handler *HotstuffReplicaMessageHandler) HandleDecideMessage(m *consensusModule, msg *typesCons.HotstuffMessage) { handler.emitTelemetryEvent(m, msg) if err := handler.anteHandle(m, msg); err != nil { @@ -150,12 +150,12 @@ func (handler *HotstuffReplicaMessageHandler) HandleDecideMessage(m *ConsensusMo } // anteHandle is the handler called on every replica message before specific handler -func (handler *HotstuffReplicaMessageHandler) anteHandle(m *ConsensusModule, msg *typesCons.HotstuffMessage) error { +func (handler *HotstuffReplicaMessageHandler) anteHandle(m *consensusModule, msg *typesCons.HotstuffMessage) error { log.Println("TODO: Hotstuff replica ante handle not implemented yet") return nil } -func (handler *HotstuffReplicaMessageHandler) emitTelemetryEvent(m *ConsensusModule, msg *typesCons.HotstuffMessage) { +func (handler *HotstuffReplicaMessageHandler) emitTelemetryEvent(m *consensusModule, msg *typesCons.HotstuffMessage) { m.GetBus(). GetTelemetryModule(). GetEventMetricsAgent(). @@ -168,7 +168,7 @@ func (handler *HotstuffReplicaMessageHandler) emitTelemetryEvent(m *ConsensusMod ) } -func (m *ConsensusModule) validateProposal(msg *typesCons.HotstuffMessage) error { +func (m *consensusModule) validateProposal(msg *typesCons.HotstuffMessage) error { if !(msg.Type == Propose && msg.Step == Prepare) { return typesCons.ErrProposalNotValidInPrepare } @@ -209,7 +209,7 @@ func (m *ConsensusModule) validateProposal(msg *typesCons.HotstuffMessage) error return typesCons.ErrUnhandledProposalCase } -func (m *ConsensusModule) validateQuorumCertificate(qc *typesCons.QuorumCertificate) error { +func (m *consensusModule) validateQuorumCertificate(qc *typesCons.QuorumCertificate) error { if qc == nil { return typesCons.ErrNilQC } diff --git a/consensus/leader_election/module.go b/consensus/leader_election/module.go index 80aaf55b0..0faf0f45d 100644 --- a/consensus/leader_election/module.go +++ b/consensus/leader_election/module.go @@ -23,8 +23,7 @@ type leaderElectionModule struct { } func Create(runtime modules.Runtime) (modules.Module, error) { - var m leaderElectionModule - return m.Create(runtime) + return new(leaderElectionModule).Create(runtime) } func (*leaderElectionModule) Create(runtime modules.Runtime) (modules.Module, error) { diff --git a/consensus/messages.go b/consensus/messages.go index 4fd09cea5..bd7bf244c 100644 --- a/consensus/messages.go +++ b/consensus/messages.go @@ -9,7 +9,7 @@ import ( ) func CreateProposeMessage( - m *ConsensusModule, + m *consensusModule, step typesCons.HotstuffStep, // step can be taken from `m` but is specified explicitly via interface to avoid ambiguity qc *typesCons.QuorumCertificate, ) (*typesCons.HotstuffMessage, error) { @@ -42,7 +42,7 @@ func CreateProposeMessage( } func CreateVoteMessage( - m *ConsensusModule, + m *consensusModule, step typesCons.HotstuffStep, // step can be taken from `m` but is specified explicitly via interface to avoid ambiguity block *typesCons.Block, ) (*typesCons.HotstuffMessage, error) { diff --git a/consensus/module.go b/consensus/module.go index edd965ba3..3acbfea58 100644 --- a/consensus/module.go +++ b/consensus/module.go @@ -21,14 +21,14 @@ const ( ) var ( - _ modules.ConsensusModule = &ConsensusModule{} + _ modules.ConsensusModule = &consensusModule{} _ modules.ConsensusConfig = &typesCons.ConsensusConfig{} _ modules.ConsensusGenesisState = &typesCons.ConsensusGenesisState{} ) // TODO(olshansky): Any reason to make all of these attributes local only (i.e. not exposed outside the struct)? -// TODO(olshansky): Look for a way to not externalize the `ConsensusModule` struct -type ConsensusModule struct { +// TODO(olshansky): Look for a way to not externalize the `consensusModule` struct +type consensusModule struct { bus modules.Bus privateKey cryptoPocket.Ed25519PrivateKey config *types.ConsensusConfig @@ -65,12 +65,11 @@ type ConsensusModule struct { } func Create(runtime modules.Runtime) (modules.Module, error) { - var m ConsensusModule - return m.Create(runtime) + return new(consensusModule).Create(runtime) } -func (*ConsensusModule) Create(runtime modules.Runtime) (modules.Module, error) { - var m *ConsensusModule +func (*consensusModule) Create(runtime modules.Runtime) (modules.Module, error) { + var m *consensusModule cfg := runtime.GetConfig() if err := m.ValidateConfig(cfg); err != nil { @@ -106,7 +105,7 @@ func (*ConsensusModule) Create(runtime modules.Runtime) (modules.Module, error) pacemakerMod := paceMaker.(Pacemaker) - m = &ConsensusModule{ + m = &consensusModule{ bus: nil, privateKey: privateKey.(cryptoPocket.Ed25519PrivateKey), @@ -143,7 +142,7 @@ func (*ConsensusModule) Create(runtime modules.Runtime) (modules.Module, error) return m, nil } -func (m *ConsensusModule) Start() error { +func (m *consensusModule) Start() error { m.GetBus(). GetTelemetryModule(). GetTimeSeriesAgent(). @@ -167,46 +166,46 @@ func (m *ConsensusModule) Start() error { return nil } -func (m *ConsensusModule) Stop() error { +func (m *consensusModule) Stop() error { return nil } -func (m *ConsensusModule) GetModuleName() string { +func (m *consensusModule) GetModuleName() string { return ConsensusModuleName } -func (m *ConsensusModule) GetBus() modules.Bus { +func (m *consensusModule) GetBus() modules.Bus { if m.bus == nil { log.Fatalf("PocketBus is not initialized") } return m.bus } -func (m *ConsensusModule) SetBus(pocketBus modules.Bus) { +func (m *consensusModule) SetBus(pocketBus modules.Bus) { m.bus = pocketBus m.paceMaker.SetBus(pocketBus) m.leaderElectionMod.SetBus(pocketBus) } -func (*ConsensusModule) ValidateConfig(cfg modules.Config) error { +func (*consensusModule) ValidateConfig(cfg modules.Config) error { if _, ok := cfg.Consensus.(*typesCons.ConsensusConfig); !ok { return fmt.Errorf("cannot cast to ConsensusConfig") } return nil } -func (*ConsensusModule) ValidateGenesis(genesis modules.GenesisState) error { +func (*consensusModule) ValidateGenesis(genesis modules.GenesisState) error { if _, ok := genesis.ConsensusGenesisState.(*typesCons.ConsensusGenesisState); !ok { return fmt.Errorf("cannot cast to ConsensusGenesisState") } return nil } -func (*ConsensusModule) GetPrivateKey(runtime modules.Runtime) (cryptoPocket.PrivateKey, error) { +func (*consensusModule) GetPrivateKey(runtime modules.Runtime) (cryptoPocket.PrivateKey, error) { return cryptoPocket.NewPrivateKey(runtime.GetConfig().Consensus.(*typesCons.ConsensusConfig).PrivateKey) } -func (m *ConsensusModule) loadPersistedState() error { +func (m *consensusModule) loadPersistedState() error { persistenceContext, err := m.GetBus().GetPersistenceModule().NewReadContext(-1) // Unknown height if err != nil { return nil @@ -252,14 +251,14 @@ handler which has both pros and cons: // TODO(olshansky): Should we just make these singletons or embed them directly in the ConsensusModule? type HotstuffMessageHandler interface { - HandleNewRoundMessage(*ConsensusModule, *typesCons.HotstuffMessage) - HandlePrepareMessage(*ConsensusModule, *typesCons.HotstuffMessage) - HandlePrecommitMessage(*ConsensusModule, *typesCons.HotstuffMessage) - HandleCommitMessage(*ConsensusModule, *typesCons.HotstuffMessage) - HandleDecideMessage(*ConsensusModule, *typesCons.HotstuffMessage) + HandleNewRoundMessage(*consensusModule, *typesCons.HotstuffMessage) + HandlePrepareMessage(*consensusModule, *typesCons.HotstuffMessage) + HandlePrecommitMessage(*consensusModule, *typesCons.HotstuffMessage) + HandleCommitMessage(*consensusModule, *typesCons.HotstuffMessage) + HandleDecideMessage(*consensusModule, *typesCons.HotstuffMessage) } -func (m *ConsensusModule) HandleMessage(message *anypb.Any) error { +func (m *consensusModule) HandleMessage(message *anypb.Any) error { switch message.MessageName() { case HotstuffMessage: var hotstuffMessage typesCons.HotstuffMessage @@ -277,7 +276,7 @@ func (m *ConsensusModule) HandleMessage(message *anypb.Any) error { return nil } -func (m *ConsensusModule) handleHotstuffMessage(msg *typesCons.HotstuffMessage) { +func (m *consensusModule) handleHotstuffMessage(msg *typesCons.HotstuffMessage) { m.nodeLog(typesCons.DebugHandlingHotstuffMessage(msg)) // Liveness & safety checks @@ -305,14 +304,14 @@ func (m *ConsensusModule) handleHotstuffMessage(msg *typesCons.HotstuffMessage) leaderHandlers[msg.Step](m, msg) } -func (m *ConsensusModule) AppHash() string { +func (m *consensusModule) AppHash() string { return m.appHash } -func (m *ConsensusModule) CurrentHeight() uint64 { +func (m *consensusModule) CurrentHeight() uint64 { return m.Height } -func (m *ConsensusModule) ValidatorMap() modules.ValidatorMap { +func (m *consensusModule) ValidatorMap() modules.ValidatorMap { return typesCons.ValidatorMapToModulesValidatorMap(m.validatorMap) } diff --git a/consensus/pacemaker.go b/consensus/pacemaker.go index 7aa6ae3d0..3deba9525 100644 --- a/consensus/pacemaker.go +++ b/consensus/pacemaker.go @@ -23,7 +23,7 @@ type Pacemaker interface { // TODO(olshansky): Rather than exposing the underlying `ConsensusModule` struct, // we could create a `ConsensusModuleDebug` interface that'll expose setters/getters // for the height/round/step/etc, and interface with the module that way. - SetConsensusModule(module *ConsensusModule) + SetConsensusModule(module *consensusModule) ValidateMessage(message *typesCons.HotstuffMessage) error RestartTimer() @@ -45,7 +45,7 @@ type paceMaker struct { // due to it's dependency on the underlying implementation of `ConsensusModule`. Think // through a way to decouple these. This could be fixed with reflection but that's not // a great idea in production code. - consensusMod *ConsensusModule + consensusMod *consensusModule pacemakerConfigs modules.PacemakerConfig @@ -119,7 +119,7 @@ func (*paceMaker) ValidateConfig(cfg modules.Config) error { return nil } -func (m *paceMaker) SetConsensusModule(c *ConsensusModule) { +func (m *paceMaker) SetConsensusModule(c *consensusModule) { m.consensusMod = c } diff --git a/p2p/module.go b/p2p/module.go index b92fb2c2a..946562dc4 100644 --- a/p2p/module.go +++ b/p2p/module.go @@ -42,8 +42,7 @@ func (m *p2pModule) GetAddress() (cryptoPocket.Address, error) { } func Create(runtime modules.Runtime) (modules.Module, error) { - var m p2pModule - return m.Create(runtime) + return new(p2pModule).Create(runtime) } func (*p2pModule) Create(runtime modules.Runtime) (modules.Module, error) { diff --git a/persistence/debug.go b/persistence/debug.go index fec1edf4f..19e1d6d21 100644 --- a/persistence/debug.go +++ b/persistence/debug.go @@ -9,7 +9,7 @@ import ( "github.com/pokt-network/pocket/shared/debug" ) -func (m *PersistenceModule) HandleDebugMessage(debugMessage *debug.DebugMessage) error { +func (m *persistenceModule) HandleDebugMessage(debugMessage *debug.DebugMessage) error { switch debugMessage.Action { case debug.DebugMessageAction_DEBUG_SHOW_LATEST_BLOCK_IN_STORE: m.showLatestBlockInStore(debugMessage) @@ -24,7 +24,7 @@ func (m *PersistenceModule) HandleDebugMessage(debugMessage *debug.DebugMessage) } // TODO(olshansky): Create a shared interface `Block` to avoid the use of typesCons here. -func (m *PersistenceModule) showLatestBlockInStore(_ *debug.DebugMessage) { +func (m *persistenceModule) showLatestBlockInStore(_ *debug.DebugMessage) { // TODO: Add an iterator to the `kvstore` and use that instead height := m.GetBus().GetConsensusModule().CurrentHeight() - 1 // -1 because we want the latest committed height blockBytes, err := m.GetBlockStore().Get(heightToBytes(int64(height))) @@ -39,7 +39,7 @@ func (m *PersistenceModule) showLatestBlockInStore(_ *debug.DebugMessage) { log.Printf("Block at height %d with %d transactions: %+v \n", height, len(block.Transactions), block) } -func (m *PersistenceModule) clearState(_ *debug.DebugMessage) { +func (m *persistenceModule) clearState(_ *debug.DebugMessage) { context, err := m.NewRWContext(-1) defer context.Commit() if err != nil { diff --git a/persistence/genesis.go b/persistence/genesis.go index 1c7e82f14..e6d4de463 100644 --- a/persistence/genesis.go +++ b/persistence/genesis.go @@ -12,7 +12,7 @@ import ( // TODO(andrew): generalize with the `actors interface` // WARNING: This function crashes the process if there is an error populating the genesis state. -func (m *PersistenceModule) populateGenesisState(state *types.PersistenceGenesisState) { +func (m *persistenceModule) populateGenesisState(state *types.PersistenceGenesisState) { log.Println("Populating genesis state...") // REFACTOR: This business logic should probably live in `types/genesis.go` @@ -155,7 +155,9 @@ func (m *PersistenceModule) populateGenesisState(state *types.PersistenceGenesis } // TODO(pocket/issues/149): All of the functions below following a structure similar to `GetAll` -// can easily be refactored and condensed into a single function using a generic type or a common +// +// can easily be refactored and condensed into a single function using a generic type or a common +// // interface. func (p PostgresContext) GetAllAccounts(height int64) (accs []modules.Account, err error) { ctx, tx, err := p.GetCtxAndTx() diff --git a/persistence/module.go b/persistence/module.go index 06ef2627e..1cd07e911 100644 --- a/persistence/module.go +++ b/persistence/module.go @@ -13,15 +13,15 @@ import ( ) var ( - _ modules.PersistenceModule = &PersistenceModule{} - _ modules.PersistenceModule = &PersistenceModule{} + _ modules.PersistenceModule = &persistenceModule{} + _ modules.PersistenceModule = &persistenceModule{} _ modules.PersistenceRWContext = &PostgresContext{} _ modules.PersistenceGenesisState = &types.PersistenceGenesisState{} _ modules.PersistenceConfig = &types.PersistenceConfig{} ) -type PersistenceModule struct { +type persistenceModule struct { bus modules.Bus config *types.PersistenceConfig @@ -36,12 +36,11 @@ const ( ) func Create(runtime modules.Runtime) (modules.Module, error) { - var m PersistenceModule - return m.Create(runtime) + return new(persistenceModule).Create(runtime) } -func (*PersistenceModule) Create(runtime modules.Runtime) (modules.Module, error) { - var m *PersistenceModule +func (*persistenceModule) Create(runtime modules.Runtime) (modules.Module, error) { + var m *persistenceModule cfg := runtime.GetConfig() @@ -70,7 +69,7 @@ func (*PersistenceModule) Create(runtime modules.Runtime) (modules.Module, error return nil, err } - m = &PersistenceModule{ + m = &persistenceModule{ bus: nil, config: persistenceCfg, blockStore: blockStore, @@ -93,46 +92,46 @@ func (*PersistenceModule) Create(runtime modules.Runtime) (modules.Module, error return m, nil } -func (m *PersistenceModule) Start() error { +func (m *persistenceModule) Start() error { log.Println("Starting persistence module...") return nil } -func (m *PersistenceModule) Stop() error { +func (m *persistenceModule) Stop() error { m.blockStore.Stop() return nil } -func (m *PersistenceModule) GetModuleName() string { +func (m *persistenceModule) GetModuleName() string { return PersistenceModuleName } -func (m *PersistenceModule) SetBus(bus modules.Bus) { +func (m *persistenceModule) SetBus(bus modules.Bus) { m.bus = bus } -func (m *PersistenceModule) GetBus() modules.Bus { +func (m *persistenceModule) GetBus() modules.Bus { if m.bus == nil { log.Fatalf("PocketBus is not initialized") } return m.bus } -func (*PersistenceModule) ValidateConfig(cfg modules.Config) error { +func (*persistenceModule) ValidateConfig(cfg modules.Config) error { if _, ok := cfg.Persistence.(*types.PersistenceConfig); !ok { return fmt.Errorf("cannot cast to PersistenceConfig") } return nil } -func (*PersistenceModule) ValidateGenesis(genesis modules.GenesisState) error { +func (*persistenceModule) ValidateGenesis(genesis modules.GenesisState) error { if _, ok := genesis.PersistenceGenesisState.(*types.PersistenceGenesisState); !ok { return fmt.Errorf("cannot cast to PersistenceGenesisState") } return nil } -func (m *PersistenceModule) NewRWContext(height int64) (modules.PersistenceRWContext, error) { +func (m *persistenceModule) NewRWContext(height int64) (modules.PersistenceRWContext, error) { if m.writeContext != nil && !m.writeContext.conn.IsClosed() { return nil, fmt.Errorf("write context already exists") } @@ -160,7 +159,7 @@ func (m *PersistenceModule) NewRWContext(height int64) (modules.PersistenceRWCon } -func (m *PersistenceModule) NewReadContext(height int64) (modules.PersistenceReadContext, error) { +func (m *persistenceModule) NewReadContext(height int64) (modules.PersistenceReadContext, error) { conn, err := connectToDatabase(m.config.PostgresUrl, m.config.NodeSchema) if err != nil { return nil, err @@ -182,7 +181,7 @@ func (m *PersistenceModule) NewReadContext(height int64) (modules.PersistenceRea }, nil } -func (m *PersistenceModule) ResetContext() error { +func (m *persistenceModule) ResetContext() error { if m.writeContext != nil { if !m.writeContext.GetTx().Conn().IsClosed() { if err := m.writeContext.Release(); err != nil { @@ -194,7 +193,7 @@ func (m *PersistenceModule) ResetContext() error { return nil } -func (m *PersistenceModule) GetBlockStore() kvstore.KVStore { +func (m *persistenceModule) GetBlockStore() kvstore.KVStore { return m.blockStore } @@ -207,7 +206,7 @@ func initializeBlockStore(blockStorePath string) (kvstore.KVStore, error) { // TODO(drewsky): Simplify and externalize the logic for whether genesis should be populated and // move the if logic out of this file. -func (m *PersistenceModule) shouldHydrateGenesisDb() (bool, error) { +func (m *persistenceModule) shouldHydrateGenesisDb() (bool, error) { checkContext, err := m.NewReadContext(-1) if err != nil { return false, err diff --git a/persistence/test/setup_test.go b/persistence/test/setup_test.go index b082d0c2c..af06b804a 100644 --- a/persistence/test/setup_test.go +++ b/persistence/test/setup_test.go @@ -310,7 +310,7 @@ func createTestingGenesisAndConfigFiles(cfg modules.Config, genesisState modules } genesisFile := make(map[string]json.RawMessage) configFile := make(map[string]json.RawMessage) - persistenceModuleName := new(persistence.PersistenceModule).GetModuleName() + persistenceModuleName := persistence.PersistenceModuleName genesisFile[test_artifacts.GetGenesisFileName(persistenceModuleName)] = genesis configFile[persistenceModuleName] = config genesisFileBz, err := json.MarshalIndent(genesisFile, "", " ") diff --git a/telemetry/module.go b/telemetry/module.go index 188a71a15..18c97fa7f 100644 --- a/telemetry/module.go +++ b/telemetry/module.go @@ -14,8 +14,7 @@ const ( ) func Create(runtime modules.Runtime) (modules.Module, error) { - var m telemetryModule - return m.Create(runtime) + return new(telemetryModule).Create(runtime) } // TODO(pocket/issues/99): Add a switch statement and configuration variable when support for other telemetry modules is added. diff --git a/utility/context.go b/utility/context.go index 460b7cfa1..a60e81901 100644 --- a/utility/context.go +++ b/utility/context.go @@ -2,6 +2,7 @@ package utility import ( "encoding/hex" + "github.com/pokt-network/pocket/shared/codec" "github.com/pokt-network/pocket/shared/modules" typesUtil "github.com/pokt-network/pocket/utility/types" @@ -19,7 +20,7 @@ type Context struct { SavePoints [][]byte } -func (u *UtilityModule) NewContext(height int64) (modules.UtilityContext, error) { +func (u *utilityModule) NewContext(height int64) (modules.UtilityContext, error) { ctx, err := u.GetBus().GetPersistenceModule().NewRWContext(height) if err != nil { return nil, typesUtil.ErrNewPersistenceContext(err) diff --git a/utility/module.go b/utility/module.go index ea19a16e2..936b68a90 100644 --- a/utility/module.go +++ b/utility/module.go @@ -9,11 +9,11 @@ import ( "github.com/pokt-network/pocket/shared/modules" ) -var _ modules.UtilityModule = &UtilityModule{} +var _ modules.UtilityModule = &utilityModule{} var _ modules.UtilityConfig = &types.UtilityConfig{} -var _ modules.Module = &UtilityModule{} +var _ modules.Module = &utilityModule{} -type UtilityModule struct { +type utilityModule struct { bus modules.Bus config *types.UtilityConfig @@ -25,12 +25,11 @@ const ( ) func Create(runtime modules.Runtime) (modules.Module, error) { - var m UtilityModule - return m.Create(runtime) + return new(utilityModule).Create(runtime) } -func (*UtilityModule) Create(runtime modules.Runtime) (modules.Module, error) { - var m *UtilityModule +func (*utilityModule) Create(runtime modules.Runtime) (modules.Module, error) { + var m *utilityModule cfg := runtime.GetConfig() if err := m.ValidateConfig(cfg); err != nil { @@ -38,37 +37,37 @@ func (*UtilityModule) Create(runtime modules.Runtime) (modules.Module, error) { } utilityCfg := cfg.Utility.(*types.UtilityConfig) - return &UtilityModule{ + return &utilityModule{ config: utilityCfg, // TODO: Add `maxTransactionBytes` and `maxTransactions` to cfg.Utility Mempool: types.NewMempool(1000, 1000), }, nil } -func (u *UtilityModule) Start() error { +func (u *utilityModule) Start() error { return nil } -func (u *UtilityModule) Stop() error { +func (u *utilityModule) Stop() error { return nil } -func (u *UtilityModule) GetModuleName() string { +func (u *utilityModule) GetModuleName() string { return UtilityModuleName } -func (u *UtilityModule) SetBus(bus modules.Bus) { +func (u *utilityModule) SetBus(bus modules.Bus) { u.bus = bus } -func (u *UtilityModule) GetBus() modules.Bus { +func (u *utilityModule) GetBus() modules.Bus { if u.bus == nil { log.Fatalf("Bus is not initialized") } return u.bus } -func (*UtilityModule) ValidateConfig(cfg modules.Config) error { +func (*utilityModule) ValidateConfig(cfg modules.Config) error { if _, ok := cfg.Utility.(*types.UtilityConfig); !ok { return fmt.Errorf("cannot cast to UtilityConfig") } diff --git a/utility/test/module_test.go b/utility/test/module_test.go index 091c36d60..86b589c43 100644 --- a/utility/test/module_test.go +++ b/utility/test/module_test.go @@ -100,7 +100,7 @@ func createTestingGenesisAndConfigFiles(cfg modules.Config, genesisState modules } genesisFile := make(map[string]json.RawMessage) configFile := make(map[string]json.RawMessage) - persistenceModuleName := new(persistence.PersistenceModule).GetModuleName() + persistenceModuleName := persistence.PersistenceModuleName genesisFile[test_artifacts.GetGenesisFileName(persistenceModuleName)] = genesis configFile[persistenceModuleName] = config genesisFileBz, err := json.MarshalIndent(genesisFile, "", " ") From 2fa9eb8b247d3d2798426ad308ae0b3691db4fa6 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Wed, 28 Sep 2022 16:38:11 +0100 Subject: [PATCH 40/90] refactor(Consensus): consistency in naming paceMaker --- consensus/module.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/consensus/module.go b/consensus/module.go index 3acbfea58..3437b7dbb 100644 --- a/consensus/module.go +++ b/consensus/module.go @@ -89,7 +89,7 @@ func (*consensusModule) Create(runtime modules.Runtime) (modules.Module, error) } // TODO(olshansky): Can we make this a submodule? - paceMaker, err := CreatePacemaker(runtime) + paceMakerMod, err := CreatePacemaker(runtime) if err != nil { return nil, err } @@ -103,7 +103,7 @@ func (*consensusModule) Create(runtime modules.Runtime) (modules.Module, error) address := privateKey.Address().String() valIdMap, idValMap := typesCons.GetValAddrToIdMap(valMap) - pacemakerMod := paceMaker.(Pacemaker) + paceMaker := paceMakerMod.(Pacemaker) m = &consensusModule{ bus: nil, @@ -128,7 +128,7 @@ func (*consensusModule) Create(runtime modules.Runtime) (modules.Module, error) validatorMap: valMap, utilityContext: nil, - paceMaker: pacemakerMod, + paceMaker: paceMaker, leaderElectionMod: leaderElectionMod.(leader_election.LeaderElectionModule), logPrefix: DefaultLogPrefix, @@ -137,7 +137,7 @@ func (*consensusModule) Create(runtime modules.Runtime) (modules.Module, error) } // TODO(olshansky): Look for a way to avoid doing this. - pacemakerMod.SetConsensusModule(m) + paceMaker.SetConsensusModule(m) return m, nil } From b244e17e2c59f6d5d15bdd2541694b77ff5560ea Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Wed, 28 Sep 2022 17:20:36 +0100 Subject: [PATCH 41/90] refactor(Shared): runtime -> runtimeCfg --- app/client/main.go | 10 +++++----- consensus/consensus_tests/utils_test.go | 6 +++--- p2p/module_raintree_test.go | 4 ++-- persistence/test/setup_test.go | 4 ++-- shared/node.go | 4 +--- utility/test/module_test.go | 4 ++-- 6 files changed, 15 insertions(+), 17 deletions(-) diff --git a/app/client/main.go b/app/client/main.go index 1310d76fc..bbd6bec5b 100644 --- a/app/client/main.go +++ b/app/client/main.go @@ -49,15 +49,15 @@ var consensusMod modules.ConsensusModule func main() { var err error - runtime := runtime.New(defaultConfigPath, defaultGenesisPath, runtime.WithRandomPK()) + runtimeCfg := runtime.New(defaultConfigPath, defaultGenesisPath, runtime.WithRandomPK()) - consM, err := consensus.Create(runtime) + consM, err := consensus.Create(runtimeCfg) if err != nil { log.Fatalf("[ERROR] Failed to create consensus module: %v", err.Error()) } consensusMod := consM.(modules.ConsensusModule) - p2pM, err := p2p.Create(runtime) + p2pM, err := p2p.Create(runtimeCfg) if err != nil { log.Fatalf("[ERROR] Failed to create p2p module: %v", err.Error()) } @@ -66,13 +66,13 @@ func main() { // Since this client mimics partial - networking only - functionality of a full node, some of the telemetry-related // code paths are executed. To avoid those messages interfering with the telemetry data collected, a non-nil telemetry // module that NOOPs (per the configs above) is injected. - telemetryM, err := telemetry.Create(runtime) + telemetryM, err := telemetry.Create(runtimeCfg) if err != nil { log.Fatalf("[ERROR] Failed to create NOOP telemetry module: " + err.Error()) } telemetryMod := telemetryM.(modules.TelemetryModule) - _ = shared.CreateBusWithOptionalModules(runtime, nil, p2pMod, nil, consensusMod, telemetryMod) + _ = shared.CreateBusWithOptionalModules(runtimeCfg, nil, p2pMod, nil, consensusMod, telemetryMod) p2pMod.Start() diff --git a/consensus/consensus_tests/utils_test.go b/consensus/consensus_tests/utils_test.go index f067a1606..33e376884 100644 --- a/consensus/consensus_tests/utils_test.go +++ b/consensus/consensus_tests/utils_test.go @@ -120,9 +120,9 @@ func CreateTestConsensusPocketNode( ) *shared.Node { createTestingGenesisAndConfigFiles(t, cfg, genesisState) - runtime := runtime.New(testingConfigFilePath, testingGenesisFilePath) + runtimeCfg := runtime.New(testingConfigFilePath, testingGenesisFilePath) - consensusMod, err := consensus.Create(runtime) + consensusMod, err := consensus.Create(runtimeCfg) require.NoError(t, err) // TODO(olshansky): At the moment we are using the same base mocks for all the tests, // but note that they will need to be customized on a per test basis. @@ -131,7 +131,7 @@ func CreateTestConsensusPocketNode( utilityMock := baseUtilityMock(t, testChannel) telemetryMock := baseTelemetryMock(t, testChannel) - bus, err := shared.CreateBus(runtime, persistenceMock, p2pMock, utilityMock, consensusMod.(modules.ConsensusModule), telemetryMock) + bus, err := shared.CreateBus(runtimeCfg, persistenceMock, p2pMock, utilityMock, consensusMod.(modules.ConsensusModule), telemetryMock) require.NoError(t, err) pk, err := cryptoPocket.NewPrivateKey(cfg.Base.PrivateKey) diff --git a/p2p/module_raintree_test.go b/p2p/module_raintree_test.go index dcf317eb6..ac09b0034 100644 --- a/p2p/module_raintree_test.go +++ b/p2p/module_raintree_test.go @@ -383,9 +383,9 @@ func prepareP2PModules(t *testing.T, configs []modules.Config) (p2pModules map[s configFilePath := testingConfigFilePath + strconv.Itoa(i) + jsonPosfix genesisFilePath := testingGenesisFilePath + jsonPosfix - runtime := runtime.New(configFilePath, genesisFilePath) + runtimeCfg := runtime.New(configFilePath, genesisFilePath) - p2pMod, err := Create(runtime) + p2pMod, err := Create(runtimeCfg) require.NoError(t, err) p2pModules[validatorId(t, i+1)] = p2pMod.(*p2pModule) } diff --git a/persistence/test/setup_test.go b/persistence/test/setup_test.go index af06b804a..09921b3de 100644 --- a/persistence/test/setup_test.go +++ b/persistence/test/setup_test.go @@ -112,9 +112,9 @@ func newTestPersistenceModule(databaseUrl string) modules.PersistenceModule { genesisState, _ := test_artifacts.NewGenesisState(5, 1, 1, 1) createTestingGenesisAndConfigFiles(cfg, genesisState) - runtime := runtime.New(testingConfigFilePath, testingGenesisFilePath) + runtimeCfg := runtime.New(testingConfigFilePath, testingGenesisFilePath) - persistenceMod, err := persistence.Create(runtime) + persistenceMod, err := persistence.Create(runtimeCfg) if err != nil { log.Fatalf("Error creating persistence module: %s", err) } diff --git a/shared/node.go b/shared/node.go index 8ca61cccc..e3fc68066 100644 --- a/shared/node.go +++ b/shared/node.go @@ -33,9 +33,7 @@ func NewNodeWithAddress(address cryptoPocket.Address) *Node { } func Create(configPath, genesisPath string) (modules.Module, error) { - var m Node - runtime := runtime.New(configPath, genesisPath) - return m.Create(runtime) + return new(Node).Create(runtime.New(configPath, genesisPath)) } func (m *Node) Create(runtime modules.Runtime) (modules.Module, error) { diff --git a/utility/test/module_test.go b/utility/test/module_test.go index 86b589c43..5d33507b5 100644 --- a/utility/test/module_test.go +++ b/utility/test/module_test.go @@ -74,9 +74,9 @@ func newTestPersistenceModule(databaseUrl string) modules.PersistenceModule { // TODO(andrew): Move the number of actors into local constants genesisState, _ := test_artifacts.NewGenesisState(5, 1, 1, 1) createTestingGenesisAndConfigFiles(cfg, genesisState) - runtime := runtime.New(testingConfigFilePath, testingGenesisFilePath) + runtimeCfg := runtime.New(testingConfigFilePath, testingGenesisFilePath) - persistenceMod, err := persistence.Create(runtime) // TODO (Drewsky) this is the last remaining cross module import and needs a fix... + persistenceMod, err := persistence.Create(runtimeCfg) // TODO (Drewsky) this is the last remaining cross module import and needs a fix... if err != nil { log.Fatalf("Error creating persistence module: %s", err) } From 1924e0e7e3c6988d13cb0b4a2a4edc5c07c64f2b Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 29 Sep 2022 14:42:30 +0100 Subject: [PATCH 42/90] refactor(Shared): code review feedback --- app/client/main.go | 14 +- consensus/consensus_tests/hotstuff_test.go | 4 +- consensus/consensus_tests/pacemaker_test.go | 19 ++- consensus/consensus_tests/utils_test.go | 95 ++++++------- consensus/leader_election/module.go | 4 +- consensus/module.go | 28 ++-- consensus/pacemaker.go | 12 +- consensus/types/consensus_genesis.go | 5 + p2p/module.go | 12 +- p2p/module_raintree_test.go | 131 ++++++++---------- p2p/raintree/addrbook_utils_test.go | 3 +- persistence/debug.go | 2 +- persistence/module.go | 18 +-- persistence/test/setup_test.go | 98 ++++++------- persistence/types/persistence_genesis.go | 2 + runtime/base_config.go | 18 +++ runtime/config.go | 115 +++++++-------- runtime/genesis.go | 72 +++------- runtime/{runtime.go => manager.go} | 82 +++++++---- shared/bus.go | 4 +- .../types => shared/converters}/converters.go | 27 ++-- shared/modules/module.go | 4 +- shared/modules/runtime_module.go | 4 +- shared/modules/types.go | 43 ++---- shared/node.go | 4 +- shared/test_artifacts/generator.go | 68 ++++----- telemetry/module.go | 18 +-- telemetry/noop_module.go | 6 +- telemetry/prometheus_module.go | 8 +- utility/module.go | 8 +- utility/test/module_test.go | 82 ++++------- 31 files changed, 482 insertions(+), 528 deletions(-) create mode 100644 consensus/types/consensus_genesis.go create mode 100644 runtime/base_config.go rename runtime/{runtime.go => manager.go} (53%) rename {persistence/types => shared/converters}/converters.go (93%) diff --git a/app/client/main.go b/app/client/main.go index bbd6bec5b..7be62aa11 100644 --- a/app/client/main.go +++ b/app/client/main.go @@ -49,30 +49,30 @@ var consensusMod modules.ConsensusModule func main() { var err error - runtimeCfg := runtime.New(defaultConfigPath, defaultGenesisPath, runtime.WithRandomPK()) + runtimeMgr := runtime.NewManagerFromFiles(defaultConfigPath, defaultGenesisPath, runtime.WithRandomPK()) - consM, err := consensus.Create(runtimeCfg) + consM, err := consensus.Create(runtimeMgr) if err != nil { log.Fatalf("[ERROR] Failed to create consensus module: %v", err.Error()) } - consensusMod := consM.(modules.ConsensusModule) + consensusMod = consM.(modules.ConsensusModule) - p2pM, err := p2p.Create(runtimeCfg) + p2pM, err := p2p.Create(runtimeMgr) if err != nil { log.Fatalf("[ERROR] Failed to create p2p module: %v", err.Error()) } - p2pMod := p2pM.(modules.P2PModule) + p2pMod = p2pM.(modules.P2PModule) // This telemetry module instance is a NOOP because the 'enable_telemetry' flag in the `cfg` above is set to false. // Since this client mimics partial - networking only - functionality of a full node, some of the telemetry-related // code paths are executed. To avoid those messages interfering with the telemetry data collected, a non-nil telemetry // module that NOOPs (per the configs above) is injected. - telemetryM, err := telemetry.Create(runtimeCfg) + telemetryM, err := telemetry.Create(runtimeMgr) if err != nil { log.Fatalf("[ERROR] Failed to create NOOP telemetry module: " + err.Error()) } telemetryMod := telemetryM.(modules.TelemetryModule) - _ = shared.CreateBusWithOptionalModules(runtimeCfg, nil, p2pMod, nil, consensusMod, telemetryMod) + _ = shared.CreateBusWithOptionalModules(runtimeMgr, nil, p2pMod, nil, consensusMod, telemetryMod) p2pMod.Start() diff --git a/consensus/consensus_tests/hotstuff_test.go b/consensus/consensus_tests/hotstuff_test.go index d131564a1..f9a598e3f 100644 --- a/consensus/consensus_tests/hotstuff_test.go +++ b/consensus/consensus_tests/hotstuff_test.go @@ -13,11 +13,11 @@ import ( func TestHotstuff4Nodes1BlockHappyPath(t *testing.T) { // Test configs numNodes := 4 - configs, genesisStates := GenerateNodeConfigs(t, numNodes) + runtimeMgrs := GenerateNodeRuntimeMgrs(t, numNodes) // Create & start test pocket nodes testChannel := make(modules.EventsChannel, 100) - pocketNodes := CreateTestConsensusPocketNodes(t, configs, genesisStates, testChannel) + pocketNodes := CreateTestConsensusPocketNodes(t, runtimeMgrs, testChannel) StartAllTestPocketNodes(t, pocketNodes) // Debug message to start consensus by triggering first view change diff --git a/consensus/consensus_tests/pacemaker_test.go b/consensus/consensus_tests/pacemaker_test.go index 271e228b3..13082d938 100644 --- a/consensus/consensus_tests/pacemaker_test.go +++ b/consensus/consensus_tests/pacemaker_test.go @@ -29,14 +29,16 @@ func TestTinyPacemakerTimeouts(t *testing.T) { numNodes := 4 paceMakerTimeoutMsec := uint64(50) // Set a very small pacemaker timeout paceMakerTimeout := 50 * time.Millisecond - configs, genesisStates := GenerateNodeConfigs(t, numNodes) - for _, config := range configs { - config.Consensus.GetPaceMakerConfig().SetTimeoutMsec(paceMakerTimeoutMsec) + runtimeMgrs := GenerateNodeRuntimeMgrs(t, numNodes) + for _, runtimeConfig := range runtimeMgrs { + if consCfg, ok := runtimeConfig.GetConfig().GetConsensusConfig().(*typesCons.ConsensusConfig); ok { + consCfg.GetPaceMakerConfig().SetTimeoutMsec(paceMakerTimeoutMsec) + } } // Create & start test pocket nodes testChannel := make(modules.EventsChannel, 100) - pocketNodes := CreateTestConsensusPocketNodes(t, configs, genesisStates, testChannel) + pocketNodes := CreateTestConsensusPocketNodes(t, runtimeMgrs, testChannel) StartAllTestPocketNodes(t, pocketNodes) // Debug message to start consensus by triggering next view. @@ -111,11 +113,11 @@ func TestTinyPacemakerTimeouts(t *testing.T) { func TestPacemakerCatchupSameStepDifferentRounds(t *testing.T) { numNodes := 4 - configs, genesisStates := GenerateNodeConfigs(t, numNodes) + runtimeConfigs := GenerateNodeRuntimeMgrs(t, numNodes) // Create & start test pocket nodes testChannel := make(modules.EventsChannel, 100) - pocketNodes := CreateTestConsensusPocketNodes(t, configs, genesisStates, testChannel) + pocketNodes := CreateTestConsensusPocketNodes(t, runtimeConfigs, testChannel) StartAllTestPocketNodes(t, pocketNodes) // Starting point @@ -127,13 +129,16 @@ func TestPacemakerCatchupSameStepDifferentRounds(t *testing.T) { leader := pocketNodes[leaderId] leaderRound := uint64(6) + consensusPK, err := leader.GetBus().GetConsensusModule().GetPrivateKey() + require.NoError(t, err) + // Placeholder block blockHeader := &typesCons.BlockHeader{ Height: int64(testHeight), Hash: hex.EncodeToString(appHash), NumTxs: 0, LastBlockHash: "", - ProposerAddress: leader.GetP2PAddress().Bytes(), + ProposerAddress: consensusPK.Address(), QuorumCertificate: nil, } block := &typesCons.Block{ diff --git a/consensus/consensus_tests/utils_test.go b/consensus/consensus_tests/utils_test.go index 33e376884..2a27425d6 100644 --- a/consensus/consensus_tests/utils_test.go +++ b/consensus/consensus_tests/utils_test.go @@ -3,10 +3,8 @@ package consensus_tests import ( "context" "encoding/hex" - "encoding/json" "flag" "fmt" - "io/ioutil" "log" "os" "reflect" @@ -63,43 +61,67 @@ type IdToNodeMapping map[typesCons.NodeId]modules.NodeModule /*** Node Generation Helpers ***/ -func GenerateNodeConfigs(_ *testing.T, validatorCount int) (configs []runtime.Config, genesisState modules.GenesisState) { +func GenerateNodeRuntimeMgrs(_ *testing.T, validatorCount int) []runtime.Manager { + runtimeMgrs := make([]runtime.Manager, 0) var keys []string - genesisState, keys = test_artifacts.NewGenesisState(validatorCount, 1, 1, 1) - configs = test_artifacts.NewDefaultConfigs(keys) - for i, config := range configs { - config.Consensus = &typesCons.ConsensusConfig{ - PrivateKey: config.Base.PrivateKey, + genesisState, keys := test_artifacts.NewGenesisState(validatorCount, 1, 1, 1) + configs := test_artifacts.NewDefaultConfigs(keys) + for _, config := range configs { + runtime.WithConsensusConfig(&typesCons.ConsensusConfig{ + PrivateKey: config.GetBaseConfig().GetPrivateKey(), MaxMempoolBytes: 500000000, PacemakerConfig: &typesCons.PacemakerConfig{ TimeoutMsec: 5000, Manual: false, DebugTimeBetweenStepsMsec: 0, }, - } - configs[i] = config + })(config) + runtimeMgrs = append(runtimeMgrs, *runtime.NewManager(config, genesisState)) } - return + return runtimeMgrs } func CreateTestConsensusPocketNodes( t *testing.T, - configs []runtime.Config, - genesisState modules.GenesisState, + runtimeMgrs []runtime.Manager, testChannel modules.EventsChannel, ) (pocketNodes IdToNodeMapping) { - pocketNodes = make(IdToNodeMapping, len(configs)) + pocketNodes = make(IdToNodeMapping, len(runtimeMgrs)) // TODO(design): The order here is important in order for NodeId to be set correctly below. // This logic will need to change once proper leader election is implemented. - sort.Slice(configs, func(i, j int) bool { - pk, err := cryptoPocket.NewPrivateKey(configs[i].Base.PrivateKey) + sort.Slice(runtimeMgrs, func(i, j int) bool { + pk, err := cryptoPocket.NewPrivateKey(runtimeMgrs[i].GetConfig().GetBaseConfig().GetPrivateKey()) require.NoError(t, err) - pk2, err := cryptoPocket.NewPrivateKey(configs[j].Base.PrivateKey) + pk2, err := cryptoPocket.NewPrivateKey(runtimeMgrs[j].GetConfig().GetBaseConfig().GetPrivateKey()) require.NoError(t, err) return pk.Address().String() < pk2.Address().String() }) - for i, cfg := range configs { - pocketNode := CreateTestConsensusPocketNode(t, cfg, genesisState, testChannel) + for i, runtimeConfig := range runtimeMgrs { + pocketNode := CreateTestConsensusPocketNode(t, &runtimeConfig, testChannel) + // TODO(olshansky): Figure this part out. + pocketNodes[typesCons.NodeId(i+1)] = pocketNode + } + return +} + +func CreateTestConsensusPocketNodesNew( + t *testing.T, + runtimeMgrs []runtime.Manager, + testChannel modules.EventsChannel, +) (pocketNodes IdToNodeMapping) { + pocketNodes = make(IdToNodeMapping, len(runtimeMgrs)) + // TODO(design): The order here is important in order for NodeId to be set correctly below. + // This logic will need to change once proper leader election is implemented. + sort.Slice(runtimeMgrs, func(i, j int) bool { + + pk, err := cryptoPocket.NewPrivateKey(runtimeMgrs[i].GetConfig().GetBaseConfig().GetPrivateKey()) + require.NoError(t, err) + pk2, err := cryptoPocket.NewPrivateKey(runtimeMgrs[j].GetConfig().GetBaseConfig().GetPrivateKey()) + require.NoError(t, err) + return pk.Address().String() < pk2.Address().String() + }) + for i, runtimeMgr := range runtimeMgrs { + pocketNode := CreateTestConsensusPocketNode(t, &runtimeMgr, testChannel) // TODO(olshansky): Figure this part out. pocketNodes[typesCons.NodeId(i+1)] = pocketNode } @@ -114,15 +136,12 @@ const ( // Creates a pocket node where all the primary modules, exception for consensus, are mocked func CreateTestConsensusPocketNode( t *testing.T, - cfg runtime.Config, - genesisState modules.GenesisState, + runtimeMgr *runtime.Manager, testChannel modules.EventsChannel, ) *shared.Node { - createTestingGenesisAndConfigFiles(t, cfg, genesisState) - - runtimeCfg := runtime.New(testingConfigFilePath, testingGenesisFilePath) + //createTestingGenesisAndConfigFiles(t, cfg, genesisState) - consensusMod, err := consensus.Create(runtimeCfg) + consensusMod, err := consensus.Create(runtimeMgr) require.NoError(t, err) // TODO(olshansky): At the moment we are using the same base mocks for all the tests, // but note that they will need to be customized on a per test basis. @@ -131,10 +150,10 @@ func CreateTestConsensusPocketNode( utilityMock := baseUtilityMock(t, testChannel) telemetryMock := baseTelemetryMock(t, testChannel) - bus, err := shared.CreateBus(runtimeCfg, persistenceMock, p2pMock, utilityMock, consensusMod.(modules.ConsensusModule), telemetryMock) + bus, err := shared.CreateBus(runtimeMgr, persistenceMock, p2pMock, utilityMock, consensusMod.(modules.ConsensusModule), telemetryMock) require.NoError(t, err) - pk, err := cryptoPocket.NewPrivateKey(cfg.Base.PrivateKey) + pk, err := cryptoPocket.NewPrivateKey(runtimeMgr.GetConfig().GetBaseConfig().GetPrivateKey()) require.NoError(t, err) pocketNode := shared.NewNodeWithAddress(pk.Address()) @@ -144,28 +163,6 @@ func CreateTestConsensusPocketNode( return pocketNode } -func createTestingGenesisAndConfigFiles(t *testing.T, cfg runtime.Config, genesisState modules.GenesisState) { - config, err := json.Marshal(cfg.Consensus) - require.NoError(t, err) - - genesis, err := json.Marshal(genesisState.ConsensusGenesisState) - require.NoError(t, err) - - genesisFile := make(map[string]json.RawMessage) - configFile := make(map[string]json.RawMessage) - consensusModName := consensus.ConsensusModuleName - genesisFile[test_artifacts.GetGenesisFileName(consensusModName)] = genesis - configFile[consensusModName] = config - - genesisFileBz, err := json.MarshalIndent(genesisFile, "", " ") - require.NoError(t, err) - - consensusFileBz, err := json.MarshalIndent(configFile, "", " ") - require.NoError(t, err) - require.NoError(t, ioutil.WriteFile(testingGenesisFilePath, genesisFileBz, 0777)) - require.NoError(t, ioutil.WriteFile(testingConfigFilePath, consensusFileBz, 0777)) -} - func StartAllTestPocketNodes(t *testing.T, pocketNodes IdToNodeMapping) { for _, pocketNode := range pocketNodes { go pocketNode.Start() diff --git a/consensus/leader_election/module.go b/consensus/leader_election/module.go index 0faf0f45d..8341a162c 100644 --- a/consensus/leader_election/module.go +++ b/consensus/leader_election/module.go @@ -22,11 +22,11 @@ type leaderElectionModule struct { bus modules.Bus } -func Create(runtime modules.Runtime) (modules.Module, error) { +func Create(runtime modules.RuntimeMgr) (modules.Module, error) { return new(leaderElectionModule).Create(runtime) } -func (*leaderElectionModule) Create(runtime modules.Runtime) (modules.Module, error) { +func (*leaderElectionModule) Create(runtime modules.RuntimeMgr) (modules.Module, error) { return &leaderElectionModule{}, nil } diff --git a/consensus/module.go b/consensus/module.go index 3437b7dbb..0f0789143 100644 --- a/consensus/module.go +++ b/consensus/module.go @@ -64,39 +64,39 @@ type consensusModule struct { MaxBlockBytes uint64 } -func Create(runtime modules.Runtime) (modules.Module, error) { - return new(consensusModule).Create(runtime) +func Create(runtimeMgr modules.RuntimeMgr) (modules.Module, error) { + return new(consensusModule).Create(runtimeMgr) } -func (*consensusModule) Create(runtime modules.Runtime) (modules.Module, error) { +func (*consensusModule) Create(runtimeMgr modules.RuntimeMgr) (modules.Module, error) { var m *consensusModule - cfg := runtime.GetConfig() + cfg := runtimeMgr.GetConfig() if err := m.ValidateConfig(cfg); err != nil { log.Fatalf("config validation failed: %v", err) } - consensusCfg := cfg.Consensus.(*typesCons.ConsensusConfig) + consensusCfg := cfg.GetConsensusConfig().(*typesCons.ConsensusConfig) - genesis := runtime.GetGenesis() + genesis := runtimeMgr.GetGenesis() if err := m.ValidateGenesis(genesis); err != nil { log.Fatalf("genesis validation failed: %v", err) } - consensusGenesis := genesis.ConsensusGenesisState.(*typesCons.ConsensusGenesisState) + consensusGenesis := genesis.GetConsensusGenesisState().(*typesCons.ConsensusGenesisState) - leaderElectionMod, err := leader_election.Create(runtime) + leaderElectionMod, err := leader_election.Create(runtimeMgr) if err != nil { return nil, err } // TODO(olshansky): Can we make this a submodule? - paceMakerMod, err := CreatePacemaker(runtime) + paceMakerMod, err := CreatePacemaker(runtimeMgr) if err != nil { return nil, err } valMap := typesCons.ValidatorListToMap(consensusGenesis.Validators) - privateKey, err := m.GetPrivateKey(runtime) + privateKey, err := m.GetPrivateKey(runtimeMgr) if err != nil { return nil, err } @@ -188,21 +188,21 @@ func (m *consensusModule) SetBus(pocketBus modules.Bus) { } func (*consensusModule) ValidateConfig(cfg modules.Config) error { - if _, ok := cfg.Consensus.(*typesCons.ConsensusConfig); !ok { + if _, ok := cfg.GetConsensusConfig().(*typesCons.ConsensusConfig); !ok { return fmt.Errorf("cannot cast to ConsensusConfig") } return nil } func (*consensusModule) ValidateGenesis(genesis modules.GenesisState) error { - if _, ok := genesis.ConsensusGenesisState.(*typesCons.ConsensusGenesisState); !ok { + if _, ok := genesis.GetConsensusGenesisState().(*typesCons.ConsensusGenesisState); !ok { return fmt.Errorf("cannot cast to ConsensusGenesisState") } return nil } -func (*consensusModule) GetPrivateKey(runtime modules.Runtime) (cryptoPocket.PrivateKey, error) { - return cryptoPocket.NewPrivateKey(runtime.GetConfig().Consensus.(*typesCons.ConsensusConfig).PrivateKey) +func (*consensusModule) GetPrivateKey(runtime modules.RuntimeMgr) (cryptoPocket.PrivateKey, error) { + return cryptoPocket.NewPrivateKey(runtime.GetConfig().GetConsensusConfig().(*typesCons.ConsensusConfig).PrivateKey) } func (m *consensusModule) loadPersistedState() error { diff --git a/consensus/pacemaker.go b/consensus/pacemaker.go index 3deba9525..4be30611f 100644 --- a/consensus/pacemaker.go +++ b/consensus/pacemaker.go @@ -55,18 +55,18 @@ type paceMaker struct { paceMakerDebug } -func CreatePacemaker(runtime modules.Runtime) (modules.Module, error) { +func CreatePacemaker(runtimeMgr modules.RuntimeMgr) (modules.Module, error) { var m paceMaker - return m.Create(runtime) + return m.Create(runtimeMgr) } -func (m *paceMaker) Create(runtime modules.Runtime) (modules.Module, error) { - cfg := runtime.GetConfig() +func (m *paceMaker) Create(runtimeMgr modules.RuntimeMgr) (modules.Module, error) { + cfg := runtimeMgr.GetConfig() if err := m.ValidateConfig(cfg); err != nil { log.Fatalf("config validation failed: %v", err) } - pacemakerConfig := cfg.Consensus.(*typesCons.ConsensusConfig).PacemakerConfig + pacemakerConfig := cfg.GetConsensusConfig().(*typesCons.ConsensusConfig).PacemakerConfig return &paceMaker{ bus: nil, @@ -108,7 +108,7 @@ func (m *paceMaker) GetBus() modules.Bus { } func (*paceMaker) ValidateConfig(cfg modules.Config) error { - consCfg, ok := cfg.Consensus.(*typesCons.ConsensusConfig) + consCfg, ok := cfg.GetConsensusConfig().(*typesCons.ConsensusConfig) if !ok { return fmt.Errorf("cannot cast to TelemetryConfig") } diff --git a/consensus/types/consensus_genesis.go b/consensus/types/consensus_genesis.go new file mode 100644 index 000000000..8c9fe1d93 --- /dev/null +++ b/consensus/types/consensus_genesis.go @@ -0,0 +1,5 @@ +package types + +import "github.com/pokt-network/pocket/shared/modules" + +var _ modules.ConsensusGenesisState = &ConsensusGenesisState{} diff --git a/p2p/module.go b/p2p/module.go index 946562dc4..cec6c1285 100644 --- a/p2p/module.go +++ b/p2p/module.go @@ -41,19 +41,19 @@ func (m *p2pModule) GetAddress() (cryptoPocket.Address, error) { return m.address, nil } -func Create(runtime modules.Runtime) (modules.Module, error) { - return new(p2pModule).Create(runtime) +func Create(runtimeMgr modules.RuntimeMgr) (modules.Module, error) { + return new(p2pModule).Create(runtimeMgr) } -func (*p2pModule) Create(runtime modules.Runtime) (modules.Module, error) { +func (*p2pModule) Create(runtimeMgr modules.RuntimeMgr) (modules.Module, error) { log.Println("Creating network module") var m *p2pModule - cfg := runtime.GetConfig() + cfg := runtimeMgr.GetConfig() if err := m.ValidateConfig(cfg); err != nil { log.Fatalf("config validation failed: %v", err) } - p2pCfg := cfg.P2P.(*typesP2P.P2PConfig) + p2pCfg := cfg.GetP2PConfig().(*typesP2P.P2PConfig) l, err := CreateListener(p2pCfg) if err != nil { @@ -167,7 +167,7 @@ func (m *p2pModule) Send(addr cryptoPocket.Address, msg *anypb.Any, topic debug. } func (*p2pModule) ValidateConfig(cfg modules.Config) error { - if _, ok := cfg.P2P.(*typesP2P.P2PConfig); !ok { + if _, ok := cfg.GetP2PConfig().(*typesP2P.P2PConfig); !ok { return fmt.Errorf("cannot cast to P2PConfig") } return nil diff --git a/p2p/module_raintree_test.go b/p2p/module_raintree_test.go index ac09b0034..ee0f89277 100644 --- a/p2p/module_raintree_test.go +++ b/p2p/module_raintree_test.go @@ -3,30 +3,25 @@ package p2p import ( "crypto/ed25519" "encoding/binary" - "encoding/json" "fmt" - "io/ioutil" "log" "os" "path/filepath" "sort" - "strconv" "sync" "testing" "time" "github.com/pokt-network/pocket/runtime" "github.com/pokt-network/pocket/shared/debug" - "github.com/pokt-network/pocket/shared/test_artifacts" + "github.com/stretchr/testify/require" "github.com/golang/mock/gomock" typesP2P "github.com/pokt-network/pocket/p2p/types" mocksP2P "github.com/pokt-network/pocket/p2p/types/mocks" - typesPers "github.com/pokt-network/pocket/persistence/types" cryptoPocket "github.com/pokt-network/pocket/shared/crypto" "github.com/pokt-network/pocket/shared/modules" modulesMock "github.com/pokt-network/pocket/shared/modules/mocks" - "github.com/stretchr/testify/require" "google.golang.org/protobuf/types/known/anypb" ) @@ -186,7 +181,7 @@ func TestRainTreeCompleteTwentySevenNodes(t *testing.T) { func testRainTreeCalls(t *testing.T, origNode string, testCommConfig TestRainTreeCommConfig, isOriginatorPinged bool) { // Network configurations numValidators := len(testCommConfig) - configs, genesisState := createConfigs(t, numValidators) + runtimeConfigs := createMockRuntimeMgrs(t, numValidators) // Test configurations var messageHandeledWaitGroup sync.WaitGroup @@ -197,7 +192,7 @@ func testRainTreeCalls(t *testing.T, origNode string, testCommConfig TestRainTre } // Network initialization - consensusMock := prepareConsensusMock(t, genesisState) + consensusMock := prepareConsensusMock(t, runtimeConfigs[0].GetGenesis()) telemetryMock := prepareTelemetryMock(t) connMocks := make(map[string]typesP2P.Transport) busMocks := make(map[string]modules.Bus) @@ -207,7 +202,7 @@ func testRainTreeCalls(t *testing.T, origNode string, testCommConfig TestRainTre } // Module injection - p2pModules := prepareP2PModules(t, configs) + p2pModules := prepareP2PModules(t, runtimeConfigs) for validatorId, p2pMod := range p2pModules { p2pMod.listener = connMocks[validatorId] p2pMod.SetBus(busMocks[validatorId]) @@ -305,7 +300,7 @@ func prepareConsensusMock(t *testing.T, genesisState modules.GenesisState) *modu ctrl := gomock.NewController(t) consensusMock := modulesMock.NewMockConsensusModule(ctrl) - validators := genesisState.PersistenceGenesisState.GetVals() + validators := genesisState.GetPersistenceGenesisState().GetVals() m := make(modules.ValidatorMap, len(validators)) for _, v := range validators { m[v.GetAddress()] = v @@ -375,91 +370,75 @@ func prepareConnMock(t *testing.T, expectedNumNetworkReads, expectedNumNetworkWr return connMock } -func prepareP2PModules(t *testing.T, configs []modules.Config) (p2pModules map[string]*p2pModule) { - p2pModules = make(map[string]*p2pModule, len(configs)) - for i, config := range configs { - createTestingGenesisAndConfigFiles(t, config, modules.GenesisState{}, i) - - configFilePath := testingConfigFilePath + strconv.Itoa(i) + jsonPosfix - genesisFilePath := testingGenesisFilePath + jsonPosfix - - runtimeCfg := runtime.New(configFilePath, genesisFilePath) - - p2pMod, err := Create(runtimeCfg) +func prepareP2PModules(t *testing.T, runtimeConfigs []modules.RuntimeMgr) (p2pModules map[string]*p2pModule) { + p2pModules = make(map[string]*p2pModule, len(runtimeConfigs)) + for i, runtimeConfig := range runtimeConfigs { + p2pMod, err := Create(runtimeConfig) require.NoError(t, err) p2pModules[validatorId(t, i+1)] = p2pMod.(*p2pModule) } return } -func createTestingGenesisAndConfigFiles(t *testing.T, cfg modules.Config, genesisState modules.GenesisState, n int) { - config, err := json.Marshal(cfg.P2P) - require.NoError(t, err) - - genesis, err := json.Marshal(genesisState.ConsensusGenesisState) - require.NoError(t, err) - - genesisFile := make(map[string]json.RawMessage) - configFile := make(map[string]json.RawMessage) - moduleName := new(p2pModule).GetModuleName() - - genesisFile[test_artifacts.GetGenesisFileName(moduleName)] = genesis - configFile[moduleName] = config - genesisFileBz, err := json.MarshalIndent(genesisFile, "", " ") - require.NoError(t, err) - - p2pFileBz, err := json.MarshalIndent(configFile, "", " ") - require.NoError(t, err) - require.NoError(t, ioutil.WriteFile(testingGenesisFilePath+jsonPosfix, genesisFileBz, 0777)) - require.NoError(t, ioutil.WriteFile(testingConfigFilePath+strconv.Itoa(n)+jsonPosfix, p2pFileBz, 0777)) -} - -func createConfigs(t *testing.T, numValidators int) (configs []modules.Config, genesisState modules.GenesisState) { - configs = make([]modules.Config, numValidators) +func createMockRuntimeMgrs(t *testing.T, numValidators int) []modules.RuntimeMgr { + ctrl := gomock.NewController(t) + mockRuntimeMgrs := make([]modules.RuntimeMgr, numValidators) valKeys := make([]cryptoPocket.PrivateKey, numValidators) copy(valKeys[:], keys[:numValidators]) - genesisState = createGenesisState(t, valKeys) - for i := range configs { - configs[i] = modules.Config{ - Base: &modules.BaseConfig{ - RootDirectory: "", - PrivateKey: valKeys[i].String(), - }, - P2P: &typesP2P.P2PConfig{ - PrivateKey: valKeys[i].String(), - ConsensusPort: 8080, - UseRainTree: true, - IsEmptyConnectionType: true, - }, - } + mockGenesisState := createMockGenesisState(t, valKeys) + for i := range mockRuntimeMgrs { + mockConfig := modulesMock.NewMockConfig(ctrl) + mockConfig.EXPECT().GetBaseConfig().Return(&runtime.BaseConfig{ + RootDirectory: "", + PrivateKey: valKeys[i].String(), + }).AnyTimes() + mockConfig.EXPECT().GetP2PConfig().Return(&typesP2P.P2PConfig{ + PrivateKey: valKeys[i].String(), + ConsensusPort: 8080, + UseRainTree: true, + IsEmptyConnectionType: true, + }).AnyTimes() + + mockRuntimeMgr := modulesMock.NewMockRuntimeMgr(ctrl) + mockRuntimeMgr.EXPECT().GetConfig().Return(mockConfig).AnyTimes() + mockRuntimeMgr.EXPECT().GetGenesis().Return(mockGenesisState).AnyTimes() + mockRuntimeMgrs[i] = mockRuntimeMgr } - return + return mockRuntimeMgrs } func validatorId(_ *testing.T, i int) string { return fmt.Sprintf(serviceUrlFormat, i) } -func createGenesisState(t *testing.T, valKeys []cryptoPocket.PrivateKey) modules.GenesisState { +func createMockGenesisState(t *testing.T, valKeys []cryptoPocket.PrivateKey) modules.GenesisState { + ctrl := gomock.NewController(t) + validators := make([]modules.Actor, len(valKeys)) for i, valKey := range valKeys { addr := valKey.Address().String() - val := &typesPers.Actor{ - Address: addr, - PublicKey: valKey.PublicKey().String(), - GenericParam: validatorId(t, i+1), - StakedAmount: "1000000000000000", - PausedHeight: 0, - UnstakingHeight: 0, - Output: addr, - } - validators[i] = val - } - return modules.GenesisState{ - PersistenceGenesisState: &typesPers.PersistenceGenesisState{ - Validators: typesPers.ToPersistenceActors(validators), - }, + mockActor := modulesMock.NewMockActor(ctrl) + mockActor.EXPECT().GetAddress().Return(addr).AnyTimes() + mockActor.EXPECT().GetPublicKey().Return(valKey.PublicKey().String()).AnyTimes() + mockActor.EXPECT().GetGenericParam().Return(validatorId(t, i+1)).AnyTimes() + mockActor.EXPECT().GetStakedAmount().Return("1000000000000000").AnyTimes() + mockActor.EXPECT().GetPausedHeight().Return(int64(0)).AnyTimes() + mockActor.EXPECT().GetUnstakingHeight().Return(int64(0)).AnyTimes() + mockActor.EXPECT().GetOutput().Return(addr).AnyTimes() + validators[i] = mockActor } + + mockPersistenceGenesisState := modulesMock.NewMockPersistenceGenesisState(ctrl) + mockPersistenceGenesisState.EXPECT(). + GetVals(). + Return(validators).AnyTimes() + + mockGenesisState := modulesMock.NewMockGenesisState(ctrl) + mockGenesisState.EXPECT(). + GetPersistenceGenesisState(). + Return(mockPersistenceGenesisState).AnyTimes() + return mockGenesisState + } func TestMain(m *testing.M) { diff --git a/p2p/raintree/addrbook_utils_test.go b/p2p/raintree/addrbook_utils_test.go index 5e9f55c73..45b6c40e6 100644 --- a/p2p/raintree/addrbook_utils_test.go +++ b/p2p/raintree/addrbook_utils_test.go @@ -114,7 +114,8 @@ func BenchmarkAddrBookUpdates(b *testing.B) { require.Equal(b, testCase.numExpectedLevels, int(network.maxNumLevels)) for i := 0; i < numAddressessToBeAdded; i++ { - newAddr, _ := crypto.GenerateAddress() + newAddr, err := crypto.GenerateAddress() + require.NoError(b, err) network.AddPeerToAddrBook(&types.NetworkPeer{Address: newAddr}) } diff --git a/persistence/debug.go b/persistence/debug.go index 19e1d6d21..2032f7659 100644 --- a/persistence/debug.go +++ b/persistence/debug.go @@ -15,7 +15,7 @@ func (m *persistenceModule) HandleDebugMessage(debugMessage *debug.DebugMessage) m.showLatestBlockInStore(debugMessage) case debug.DebugMessageAction_DEBUG_CLEAR_STATE: m.clearState(debugMessage) - g := m.GetBus().GetGenesis().PersistenceGenesisState.(*types.PersistenceGenesisState) + g := m.GetBus().GetGenesis().GetPersistenceGenesisState().(*types.PersistenceGenesisState) m.populateGenesisState(g) default: log.Printf("Debug message not handled by persistence module: %s \n", debugMessage.Message) diff --git a/persistence/module.go b/persistence/module.go index 1cd07e911..9e71293ed 100644 --- a/persistence/module.go +++ b/persistence/module.go @@ -35,25 +35,25 @@ const ( PersistenceModuleName = "persistence" ) -func Create(runtime modules.Runtime) (modules.Module, error) { - return new(persistenceModule).Create(runtime) +func Create(runtimeMgr modules.RuntimeMgr) (modules.Module, error) { + return new(persistenceModule).Create(runtimeMgr) } -func (*persistenceModule) Create(runtime modules.Runtime) (modules.Module, error) { +func (*persistenceModule) Create(runtimeMgr modules.RuntimeMgr) (modules.Module, error) { var m *persistenceModule - cfg := runtime.GetConfig() + cfg := runtimeMgr.GetConfig() if err := m.ValidateConfig(cfg); err != nil { log.Fatalf("config validation failed: %v", err) } - persistenceCfg := cfg.Persistence.(*types.PersistenceConfig) + persistenceCfg := cfg.GetPersistenceConfig().(*types.PersistenceConfig) - genesis := runtime.GetGenesis() + genesis := runtimeMgr.GetGenesis() if err := m.ValidateGenesis(genesis); err != nil { log.Fatalf("genesis validation failed: %v", err) } - persistenceGenesis := genesis.PersistenceGenesisState.(*types.PersistenceGenesisState) + persistenceGenesis := genesis.GetPersistenceGenesisState().(*types.PersistenceGenesisState) conn, err := connectToDatabase(persistenceCfg.GetPostgresUrl(), persistenceCfg.GetNodeSchema()) if err != nil { @@ -118,14 +118,14 @@ func (m *persistenceModule) GetBus() modules.Bus { } func (*persistenceModule) ValidateConfig(cfg modules.Config) error { - if _, ok := cfg.Persistence.(*types.PersistenceConfig); !ok { + if _, ok := cfg.GetPersistenceConfig().(*types.PersistenceConfig); !ok { return fmt.Errorf("cannot cast to PersistenceConfig") } return nil } func (*persistenceModule) ValidateGenesis(genesis modules.GenesisState) error { - if _, ok := genesis.PersistenceGenesisState.(*types.PersistenceGenesisState); !ok { + if _, ok := genesis.GetPersistenceGenesisState().(*types.PersistenceGenesisState); !ok { return fmt.Errorf("cannot cast to PersistenceGenesisState") } return nil diff --git a/persistence/test/setup_test.go b/persistence/test/setup_test.go index 09921b3de..1ebc5982e 100644 --- a/persistence/test/setup_test.go +++ b/persistence/test/setup_test.go @@ -2,13 +2,10 @@ package test import ( "encoding/hex" - "encoding/json" "fmt" - "io/ioutil" "log" "math/big" "math/rand" - "os" "strings" "testing" "time" @@ -57,8 +54,8 @@ func TestMain(m *testing.M) { pool, resource, dbUrl := sharedTest.SetupPostgresDocker() testPersistenceMod = newTestPersistenceModule(dbUrl) m.Run() - os.Remove(testingConfigFilePath) - os.Remove(testingGenesisFilePath) + // os.Remove(testingConfigFilePath) + // os.Remove(testingGenesisFilePath) sharedTest.CleanupPostgresDocker(m, pool, resource) } @@ -102,17 +99,20 @@ func NewFuzzTestPostgresContext(f *testing.F, height int64) *persistence.Postgre // TODO(andrew): Take in `t testing.T` as a parameter and error if there's an issue func newTestPersistenceModule(databaseUrl string) modules.PersistenceModule { - cfg := modules.Config{ - Persistence: &types.PersistenceConfig{ - PostgresUrl: databaseUrl, - NodeSchema: testSchema, - BlockStorePath: "", - }, - } + // cfg := runtime.NewRuntimeConfig{nil, nil, nil, + // &types.PersistenceConfig{ + // PostgresUrl: databaseUrl, + // NodeSchema: testSchema, + // BlockStorePath: "", + // },nil, nil) + + cfg := runtime.NewConfig(&runtime.BaseConfig{}, runtime.WithPersistenceConfig(&types.PersistenceConfig{ + PostgresUrl: databaseUrl, + NodeSchema: testSchema, + BlockStorePath: "", + })) genesisState, _ := test_artifacts.NewGenesisState(5, 1, 1, 1) - createTestingGenesisAndConfigFiles(cfg, genesisState) - - runtimeCfg := runtime.New(testingConfigFilePath, testingGenesisFilePath) + runtimeCfg := runtime.NewManager(cfg, genesisState) persistenceMod, err := persistence.Create(runtimeCfg) if err != nil { @@ -294,40 +294,40 @@ func fuzzSingleProtocolActor( // TODO(olshansky): Make these functions & variables more functional to avoid having "unexpected" // // side effects and making it clearer to the reader. -const ( - testingGenesisFilePath = "genesis.json" - testingConfigFilePath = "config.json" -) - -func createTestingGenesisAndConfigFiles(cfg modules.Config, genesisState modules.GenesisState) { - config, err := json.Marshal(cfg.Persistence) - if err != nil { - log.Fatal(err) - } - genesis, err := json.Marshal(genesisState.PersistenceGenesisState) - if err != nil { - log.Fatal(err) - } - genesisFile := make(map[string]json.RawMessage) - configFile := make(map[string]json.RawMessage) - persistenceModuleName := persistence.PersistenceModuleName - genesisFile[test_artifacts.GetGenesisFileName(persistenceModuleName)] = genesis - configFile[persistenceModuleName] = config - genesisFileBz, err := json.MarshalIndent(genesisFile, "", " ") - if err != nil { - log.Fatal(err) - } - configFileBz, err := json.MarshalIndent(configFile, "", " ") - if err != nil { - log.Fatal(err) - } - if err := ioutil.WriteFile(testingGenesisFilePath, genesisFileBz, 0777); err != nil { - log.Fatal(err) - } - if err := ioutil.WriteFile(testingConfigFilePath, configFileBz, 0777); err != nil { - log.Fatal(err) - } -} +// const ( +// testingGenesisFilePath = "genesis.json" +// testingConfigFilePath = "config.json" +// ) + +// func createTestingGenesisAndConfigFiles(cfg modules.Config, genesisState modules.GenesisState) { +// config, err := json.Marshal(cfg.GetPersistenceConfig()) +// if err != nil { +// log.Fatal(err) +// } +// genesis, err := json.Marshal(genesisState.GetPersistenceGenesisState()) +// if err != nil { +// log.Fatal(err) +// } +// genesisFile := make(map[string]json.RawMessage) +// configFile := make(map[string]json.RawMessage) +// persistenceModuleName := persistence.PersistenceModuleName +// genesisFile[test_artifacts.GetGenesisFileName(persistenceModuleName)] = genesis +// configFile[persistenceModuleName] = config +// genesisFileBz, err := json.MarshalIndent(genesisFile, "", " ") +// if err != nil { +// log.Fatal(err) +// } +// configFileBz, err := json.MarshalIndent(configFile, "", " ") +// if err != nil { +// log.Fatal(err) +// } +// if err := ioutil.WriteFile(testingGenesisFilePath, genesisFileBz, 0777); err != nil { +// log.Fatal(err) +// } +// if err := ioutil.WriteFile(testingConfigFilePath, configFileBz, 0777); err != nil { +// log.Fatal(err) +// } +// } func getRandomChains() (chains []string) { setRandomSeed() diff --git a/persistence/types/persistence_genesis.go b/persistence/types/persistence_genesis.go index a7250a5ae..3abc5379d 100644 --- a/persistence/types/persistence_genesis.go +++ b/persistence/types/persistence_genesis.go @@ -8,6 +8,8 @@ import ( "github.com/pokt-network/pocket/utility/types" ) +var _ modules.PersistenceGenesisState = &PersistenceGenesisState{} + // TODO (Research) is there anyway to not have to name these protobuf files uniquely? // not a fan of _config/genesis.go would rather just config/genesis.go diff --git a/runtime/base_config.go b/runtime/base_config.go new file mode 100644 index 000000000..dede2a201 --- /dev/null +++ b/runtime/base_config.go @@ -0,0 +1,18 @@ +package runtime + +import "github.com/pokt-network/pocket/shared/modules" + +var _ modules.BaseConfig = &BaseConfig{} + +type BaseConfig struct { + RootDirectory string `json:"root_directory"` + PrivateKey string `json:"private_key"` // TODO (pocket/issues/150) better architecture for key management (keybase, keyfiles, etc.) +} + +func (c *BaseConfig) GetRootDirectory() string { + return c.RootDirectory +} + +func (c *BaseConfig) GetPrivateKey() string { + return c.PrivateKey +} diff --git a/runtime/config.go b/runtime/config.go index 277fb3505..ccbe32b5e 100644 --- a/runtime/config.go +++ b/runtime/config.go @@ -1,9 +1,6 @@ package runtime import ( - "encoding/json" - "os" - typesCons "github.com/pokt-network/pocket/consensus/types" typesP2P "github.com/pokt-network/pocket/p2p/types" typesPers "github.com/pokt-network/pocket/persistence/types" @@ -12,13 +9,9 @@ import ( typesUtil "github.com/pokt-network/pocket/utility/types" ) -var _ modules.ConsensusConfig = &Config{} -var _ modules.P2PConfig = &Config{} -var _ modules.PersistenceConfig = &Config{} -var _ modules.TelemetryConfig = &Config{} -var _ modules.UtilityConfig = &Config{} +var _ modules.Config = &runtimeConfig{} -type Config struct { +type runtimeConfig struct { Base *BaseConfig `json:"base"` Consensus *typesCons.ConsensusConfig `json:"consensus"` Utility *typesUtil.UtilityConfig `json:"utility"` @@ -27,76 +20,72 @@ type Config struct { Telemetry *typesTelemetry.TelemetryConfig `json:"telemetry"` } -func (c *Config) ToShared() modules.Config { - return modules.Config{ - Base: (*modules.BaseConfig)(c.Base), - Consensus: c.Consensus, - Utility: c.Utility, - Persistence: c.Persistence, - P2P: c.P2P, - Telemetry: c.Telemetry, +func NewConfig(base *BaseConfig, otherConfigs ...func(modules.Config)) *runtimeConfig { + rc := &runtimeConfig{ + Base: base, + } + for _, oc := range otherConfigs { + oc(rc) } + return rc } -type BaseConfig struct { - RootDirectory string `json:"root_directory"` - PrivateKey string `json:"private_key"` // TODO (pocket/issues/150) better architecture for key management (keybase, keyfiles, etc.) - ConfigPath string `json:"config_path"` - GenesisPath string `json:"genesis_path"` +func WithConsensusConfig(consensusConfig modules.ConsensusConfig) func(modules.Config) { + return func(rc modules.Config) { + rc.(*runtimeConfig).Consensus = consensusConfig.(*typesCons.ConsensusConfig) + } } -func ParseConfigJSON(configPath string) (config *Config, err error) { - data, err := os.ReadFile(configPath) - if err != nil { - return +func WithUtilityConfig(utilityConfig modules.UtilityConfig) func(modules.Config) { + return func(rc modules.Config) { + rc.(*runtimeConfig).Utility = utilityConfig.(*typesUtil.UtilityConfig) } - - // general configuration file - config = new(Config) - err = json.Unmarshal(data, &config) - return } -// modules.ConsensusConfig - -func (c *Config) GetMaxMempoolBytes() uint64 { - return c.Consensus.MaxMempoolBytes +func WithPersistenceConfig(persistenceConfig modules.UtilityConfig) func(modules.Config) { + return func(rc modules.Config) { + rc.(*runtimeConfig).Persistence = persistenceConfig.(*typesPers.PersistenceConfig) + } } -// modules.P2PConfig - -func (c *Config) GetConsensusPort() uint32 { - return c.P2P.ConsensusPort +func WithP2PConfig(p2pConfig modules.P2PConfig) func(modules.Config) { + return func(rc modules.Config) { + rc.(*runtimeConfig).P2P = p2pConfig.(*typesP2P.P2PConfig) + } } - -func (c *Config) IsEmptyConnType() bool { // TODO (team) make enum - return c.P2P.IsEmptyConnectionType +func WithTelemetryConfig(telemetryConfig modules.TelemetryConfig) func(modules.Config) { + return func(rc modules.Config) { + rc.(*runtimeConfig).Telemetry = telemetryConfig.(*typesTelemetry.TelemetryConfig) + } } -// modules.PersistenceConfig - -func (c *Config) GetPostgresUrl() string { - return c.Persistence.PostgresUrl +func (c *runtimeConfig) GetBaseConfig() modules.BaseConfig { + return c.Base } - -func (c *Config) GetNodeSchema() string { - return c.Persistence.NodeSchema +func (c *runtimeConfig) GetConsensusConfig() modules.ConsensusConfig { + return c.Consensus } - -func (c *Config) GetBlockStorePath() string { - return c.Persistence.BlockStorePath +func (c *runtimeConfig) GetUtilityConfig() modules.UtilityConfig { + return c.Utility } - -// modules.TelemetryConfig - -func (c *Config) GetEnabled() bool { - return c.Telemetry.Enabled +func (c *runtimeConfig) GetPersistenceConfig() modules.PersistenceConfig { + return c.Persistence } - -func (c *Config) GetAddress() string { - return c.Telemetry.Address +func (c *runtimeConfig) GetP2PConfig() modules.P2PConfig { + return c.P2P } - -func (c *Config) GetEndpoint() string { - return c.Telemetry.Endpoint +func (c *runtimeConfig) GetTelemetryConfig() modules.TelemetryConfig { + return c.Telemetry } + +// func ParseConfigJSON(configPath string) (config *runtimeConfig, err error) { +// data, err := os.ReadFile(configPath) +// if err != nil { +// return +// } + +// general configuration file +// config = new(runtimeConfig) +// err = json.Unmarshal(data, &config) +// return +// } diff --git a/runtime/genesis.go b/runtime/genesis.go index 98947e27a..8616ec389 100644 --- a/runtime/genesis.go +++ b/runtime/genesis.go @@ -7,74 +7,40 @@ import ( typesCons "github.com/pokt-network/pocket/consensus/types" typesPers "github.com/pokt-network/pocket/persistence/types" "github.com/pokt-network/pocket/shared/modules" - "google.golang.org/protobuf/types/known/timestamppb" ) -var _ modules.ConsensusGenesisState = &Genesis{} -var _ modules.PersistenceGenesisState = &Genesis{} +var _ modules.GenesisState = &runtimeGenesis{} -type Genesis struct { +type runtimeGenesis struct { ConsensusGenesisState *typesCons.ConsensusGenesisState `json:"consensus_genesis_state"` PersistenceGenesisState *typesPers.PersistenceGenesisState `json:"persistence_genesis_state"` } -func (g *Genesis) ToShared() modules.GenesisState { - return modules.GenesisState{ - PersistenceGenesisState: g.PersistenceGenesisState, - ConsensusGenesisState: g.ConsensusGenesisState, +func NewGenesis( + consensusGenesisState modules.ConsensusGenesisState, + persistenceGenesisState modules.PersistenceGenesisState, +) *runtimeGenesis { + return &runtimeGenesis{ + ConsensusGenesisState: consensusGenesisState.(*typesCons.ConsensusGenesisState), + PersistenceGenesisState: persistenceGenesisState.(*typesPers.PersistenceGenesisState), } } -func ParseGenesisJSON(genesisPath string) (genesis *Genesis, err error) { +func (g *runtimeGenesis) GetPersistenceGenesisState() modules.PersistenceGenesisState { + return g.PersistenceGenesisState +} +func (g *runtimeGenesis) GetConsensusGenesisState() modules.ConsensusGenesisState { + return g.ConsensusGenesisState +} + +func parseGenesisJSON(genesisPath string) (g *runtimeGenesis, err error) { data, err := os.ReadFile(genesisPath) if err != nil { return } // general genesis file - genesis = new(Genesis) - err = json.Unmarshal(data, &genesis) + g = new(runtimeGenesis) + err = json.Unmarshal(data, &g) return } - -// modules.ConsensusGenesisState - -func (g *Genesis) GetGenesisTime() *timestamppb.Timestamp { - return g.ConsensusGenesisState.GenesisTime -} -func (g *Genesis) GetChainId() string { - return g.ConsensusGenesisState.ChainId -} -func (g *Genesis) GetMaxBlockBytes() uint64 { - return g.ConsensusGenesisState.MaxBlockBytes -} - -// modules.PersistenceGenesisState - -func (g *Genesis) GetAccs() []modules.Account { - return g.PersistenceGenesisState.GetAccs() -} - -func (g *Genesis) GetAccPools() []modules.Account { - return g.PersistenceGenesisState.GetAccPools() -} - -func (g *Genesis) GetApps() []modules.Actor { - return g.PersistenceGenesisState.GetApps() -} - -func (g *Genesis) GetVals() []modules.Actor { - return g.PersistenceGenesisState.GetVals() -} - -func (g *Genesis) GetFish() []modules.Actor { - return g.PersistenceGenesisState.GetFish() -} - -func (g *Genesis) GetNodes() []modules.Actor { - return g.PersistenceGenesisState.GetNodes() -} - -func (g *Genesis) GetParameters() modules.Params { - return g.PersistenceGenesisState.GetParameters() -} diff --git a/runtime/runtime.go b/runtime/manager.go similarity index 53% rename from runtime/runtime.go rename to runtime/manager.go index 4cec6c090..9ea1483cc 100644 --- a/runtime/runtime.go +++ b/runtime/manager.go @@ -1,6 +1,8 @@ package runtime import ( + "encoding/json" + "io" "log" "path" "path/filepath" @@ -14,23 +16,17 @@ import ( "github.com/spf13/viper" ) -var _ modules.Runtime = &runtimeConfig{} +var _ modules.RuntimeMgr = &Manager{} -type runtimeConfig struct { - configPath string - genesisPath string - - config *Config - genesis *Genesis +type Manager struct { + config *runtimeConfig + genesis *runtimeGenesis } -func New(configPath, genesisPath string, options ...func(*runtimeConfig)) *runtimeConfig { - rc := &runtimeConfig{ - configPath: configPath, - genesisPath: genesisPath, - } +func NewManagerFromFiles(configPath, genesisPath string, options ...func(*Manager)) *Manager { + rc := &Manager{} - cfg, genesis, err := rc.init() + cfg, genesis, err := rc.init(configPath, genesisPath) if err != nil { log.Fatalf("[ERROR] Failed to initialize runtime builder: %v", err) } @@ -44,8 +40,29 @@ func New(configPath, genesisPath string, options ...func(*runtimeConfig)) *runti return rc } -func (rc *runtimeConfig) init() (config *Config, genesis *Genesis, err error) { - dir, file := path.Split(rc.configPath) +func NewManagerFromReaders(configReader, genesisReader io.Reader, options ...func(*Manager)) *Manager { + var cfg *runtimeConfig + parse(configReader, cfg) + + var genesis *runtimeGenesis + parse(genesisReader, genesis) + + rc := &Manager{ + config: cfg, + genesis: genesis, + } + return rc +} + +func NewManager(config modules.Config, genesis modules.GenesisState) *Manager { + return &Manager{ + config: config.(*runtimeConfig), + genesis: genesis.(*runtimeGenesis), + } +} + +func (rc *Manager) init(configPath, genesisPath string) (config *runtimeConfig, genesis *runtimeGenesis, err error) { + dir, file := path.Split(configPath) filename := strings.TrimSuffix(file, filepath.Ext(file)) viper.AddConfigPath(".") @@ -75,24 +92,37 @@ func (rc *runtimeConfig) init() (config *Config, genesis *Genesis, err error) { if config.Base == nil { config.Base = &BaseConfig{} } - config.Base.ConfigPath = rc.configPath - config.Base.GenesisPath = rc.genesisPath - genesis, err = ParseGenesisJSON(rc.genesisPath) + genesis, err = parseGenesisJSON(genesisPath) return } -func (b *runtimeConfig) GetConfig() modules.Config { - return b.config.ToShared() +func (b *Manager) GetConfig() modules.Config { + return b.config +} + +func (b *Manager) GetGenesis() modules.GenesisState { + return b.genesis +} + +type supportedStructs interface { + *runtimeConfig | *runtimeGenesis } -func (b *runtimeConfig) GetGenesis() modules.GenesisState { - return b.genesis.ToShared() +func parse[T supportedStructs](reader io.Reader, target T) { + bz, err := io.ReadAll(reader) + if err != nil { + log.Fatalf("[ERROR] Failed to read from reader: %v", err) + + } + if err := json.Unmarshal(bz, &target); err != nil { + log.Fatalf("[ERROR] Failed to unmarshal: %v", err) + } } -// RuntimeConfig option helpers +// Manager option helpers -func WithRandomPK() func(*runtimeConfig) { +func WithRandomPK() func(*Manager) { privateKey, err := cryptoPocket.GeneratePrivateKey() if err != nil { log.Fatalf("unable to generate private key") @@ -101,8 +131,8 @@ func WithRandomPK() func(*runtimeConfig) { return WithPK(privateKey.String()) } -func WithPK(pk string) func(*runtimeConfig) { - return func(b *runtimeConfig) { +func WithPK(pk string) func(*Manager) { + return func(b *Manager) { if b.config.Consensus == nil { b.config.Consensus = &types.ConsensusConfig{} } diff --git a/shared/bus.go b/shared/bus.go index 5bf555afb..d35ea2f5e 100644 --- a/shared/bus.go +++ b/shared/bus.go @@ -32,7 +32,7 @@ const ( ) func CreateBus( - runtime modules.Runtime, + runtime modules.RuntimeMgr, persistence modules.PersistenceModule, p2p modules.P2PModule, utility modules.UtilityModule, @@ -83,7 +83,7 @@ func CreateBus( // Using `CreateBusWithOptionalModules`, we can create a bus with only pre2p and a NOOP telemetry module // so that we can the pre2p module without any issues. func CreateBusWithOptionalModules( - runtime modules.Runtime, + runtime modules.RuntimeMgr, persistence modules.PersistenceModule, p2p modules.P2PModule, utility modules.UtilityModule, diff --git a/persistence/types/converters.go b/shared/converters/converters.go similarity index 93% rename from persistence/types/converters.go rename to shared/converters/converters.go index e118e66cb..3140a633f 100644 --- a/persistence/types/converters.go +++ b/shared/converters/converters.go @@ -1,9 +1,12 @@ -package types +package converters -import "github.com/pokt-network/pocket/shared/modules" +import ( + typesPers "github.com/pokt-network/pocket/persistence/types" + "github.com/pokt-network/pocket/shared/modules" +) -func toPersistenceActor(actor modules.Actor) *Actor { - return &Actor{ +func toPersistenceActor(actor modules.Actor) *typesPers.Actor { + return &typesPers.Actor{ Address: actor.GetAddress(), PublicKey: actor.GetPublicKey(), StakedAmount: actor.GetStakedAmount(), @@ -11,31 +14,31 @@ func toPersistenceActor(actor modules.Actor) *Actor { } } -func ToPersistenceActors(actors []modules.Actor) []*Actor { - r := make([]*Actor, 0) +func ToPersistenceActors(actors []modules.Actor) []*typesPers.Actor { + r := make([]*typesPers.Actor, 0) for _, a := range actors { r = append(r, toPersistenceActor(a)) } return r } -func toPersistenceAccount(account modules.Account) *Account { - return &Account{ +func toPersistenceAccount(account modules.Account) *typesPers.Account { + return &typesPers.Account{ Address: account.GetAddress(), Amount: account.GetAmount(), } } -func ToPersistenceAccounts(accounts []modules.Account) []*Account { - r := make([]*Account, 0) +func ToPersistenceAccounts(accounts []modules.Account) []*typesPers.Account { + r := make([]*typesPers.Account, 0) for _, a := range accounts { r = append(r, toPersistenceAccount(a)) } return r } -func ToPersistenceParams(params modules.Params) *Params { - return &Params{ +func ToPersistenceParams(params modules.Params) *typesPers.Params { + return &typesPers.Params{ BlocksPerSession: params.GetBlocksPerSession(), AppMinimumStake: params.GetAppMinimumStake(), AppMaxChains: params.GetAppMaxChains(), diff --git a/shared/modules/module.go b/shared/modules/module.go index 9882357a3..c7dacf408 100644 --- a/shared/modules/module.go +++ b/shared/modules/module.go @@ -26,7 +26,7 @@ type InterruptableModule interface { type InitializableModule interface { GetModuleName() string - Create(runtime Runtime) (Module, error) + Create(runtime RuntimeMgr) (Module, error) } type ConfigurableModule interface { @@ -38,7 +38,7 @@ type GenesisDependentModule interface { } type KeyholderModule interface { - GetPrivateKey(Runtime) (crypto.PrivateKey, error) + GetPrivateKey(RuntimeMgr) (crypto.PrivateKey, error) } type P2PAddressableModule interface { diff --git a/shared/modules/runtime_module.go b/shared/modules/runtime_module.go index 64001f814..915e2c449 100644 --- a/shared/modules/runtime_module.go +++ b/shared/modules/runtime_module.go @@ -1,6 +1,8 @@ package modules -type Runtime interface { +//go:generate mockgen -source=$GOFILE -destination=./mocks/runtime_module_mock.go -aux_files=github.com/pokt-network/pocket/shared/modules=module.go + +type RuntimeMgr interface { GetConfig() Config GetGenesis() GenesisState } diff --git a/shared/modules/types.go b/shared/modules/types.go index ee23d9b4d..88b28b5bf 100644 --- a/shared/modules/types.go +++ b/shared/modules/types.go @@ -1,5 +1,7 @@ package modules +//go:generate mockgen -source=$GOFILE -destination=./mocks/types_mock.go -aux_files=github.com/pokt-network/pocket/shared/modules=module.go + import ( "google.golang.org/protobuf/types/known/timestamppb" ) @@ -7,25 +9,23 @@ import ( // This file contains the minimum shared structures (GenesisState) and the many shared interfaces the modules implement // the main purpose of this structure is to ensure the ownership of the -type GenesisState struct { - PersistenceGenesisState PersistenceGenesisState `json:"persistence_genesis_state"` - ConsensusGenesisState ConsensusGenesisState `json:"consensus_genesis_state"` +type GenesisState interface { + GetPersistenceGenesisState() PersistenceGenesisState + GetConsensusGenesisState() ConsensusGenesisState } -type BaseConfig struct { - RootDirectory string `json:"root_directory"` - PrivateKey string `json:"private_key"` // TODO (pocket/issues/150) better architecture for key management (keybase, keyfiles, etc.) - ConfigPath string `json:"config_path"` - GenesisPath string `json:"genesis_path"` +type Config interface { + GetBaseConfig() BaseConfig + GetConsensusConfig() ConsensusConfig + GetUtilityConfig() UtilityConfig + GetPersistenceConfig() PersistenceConfig + GetP2PConfig() P2PConfig + GetTelemetryConfig() TelemetryConfig } -type Config struct { - Base *BaseConfig `json:"base"` - Consensus ConsensusConfig `json:"consensus"` - Utility UtilityConfig `json:"utility"` - Persistence PersistenceConfig `json:"persistence"` - P2P P2PConfig `json:"p2p"` - Telemetry TelemetryConfig `json:"telemetry"` +type BaseConfig interface { + GetRootDirectory() string + GetPrivateKey() string // TODO (pocket/issues/150) better architecture for key management (keybase, keyfiles, etc.) } type ConsensusConfig interface { @@ -222,19 +222,6 @@ type Params interface { GetMessageChangeParameterFeeOwner() string } -var _ IConfig = PacemakerConfig(nil) -var _ IConfig = PersistenceConfig(nil) -var _ IConfig = P2PConfig(nil) -var _ IConfig = TelemetryConfig(nil) -var _ IConfig = UtilityConfig(nil) - -var _ IGenesis = PersistenceGenesisState(nil) -var _ IGenesis = ConsensusGenesisState(nil) - -// TODO think of a way to enforce these configuration interfaces as true configs/genesis, this is merely decorative at this point -type IConfig interface{} -type IGenesis interface{} - // TODO (Team) move to use proto string() and deprecate #149 const ( BlocksPerSessionParamName = "blocks_per_session" diff --git a/shared/node.go b/shared/node.go index e3fc68066..8d6a7d501 100644 --- a/shared/node.go +++ b/shared/node.go @@ -33,10 +33,10 @@ func NewNodeWithAddress(address cryptoPocket.Address) *Node { } func Create(configPath, genesisPath string) (modules.Module, error) { - return new(Node).Create(runtime.New(configPath, genesisPath)) + return new(Node).Create(runtime.NewManagerFromFiles(configPath, genesisPath)) } -func (m *Node) Create(runtime modules.Runtime) (modules.Module, error) { +func (m *Node) Create(runtime modules.RuntimeMgr) (modules.Module, error) { persistenceMod, err := persistence.Create(runtime) if err != nil { return nil, err diff --git a/shared/test_artifacts/generator.go b/shared/test_artifacts/generator.go index 9ea3ed250..d418e75c8 100644 --- a/shared/test_artifacts/generator.go +++ b/shared/test_artifacts/generator.go @@ -7,6 +7,7 @@ import ( typesPersistence "github.com/pokt-network/pocket/persistence/types" "github.com/pokt-network/pocket/runtime" + "github.com/pokt-network/pocket/shared/converters" "github.com/pokt-network/pocket/shared/modules" "github.com/pokt-network/pocket/utility/types" @@ -45,65 +46,66 @@ func NewGenesisState(numValidators, numServiceNodes, numApplications, numFisherm vals, validatorPrivateKeys := NewActors(types.UtilActorType_Val, numValidators) serviceNodes, snPrivateKeys := NewActors(types.UtilActorType_Node, numServiceNodes) fish, fishPrivateKeys := NewActors(types.UtilActorType_Fish, numFisherman) - return modules.GenesisState{ - ConsensusGenesisState: &typesCons.ConsensusGenesisState{ + + return runtime.NewGenesis(&typesCons.ConsensusGenesisState{ GenesisTime: timestamppb.Now(), ChainId: DefaultChainID, MaxBlockBytes: DefaultMaxBlockBytes, Validators: typesCons.ToConsensusValidators(vals), }, - PersistenceGenesisState: &typesPers.PersistenceGenesisState{ - Pools: typesPers.ToPersistenceAccounts(NewPools()), - Accounts: typesPers.ToPersistenceAccounts(NewAccounts(numValidators+numServiceNodes+numApplications+numFisherman, append(append(append(validatorPrivateKeys, snPrivateKeys...), fishPrivateKeys...), appsPrivateKeys...)...)), // TODO(olshansky): clean this up - Applications: typesPers.ToPersistenceActors(apps), - Validators: typesPers.ToPersistenceActors(vals), - ServiceNodes: typesPers.ToPersistenceActors(serviceNodes), - Fishermen: typesPers.ToPersistenceActors(fish), - Params: typesPers.ToPersistenceParams(DefaultParams()), - }, - }, validatorPrivateKeys + &typesPers.PersistenceGenesisState{ + Pools: converters.ToPersistenceAccounts(NewPools()), + Accounts: converters.ToPersistenceAccounts(NewAccounts(numValidators+numServiceNodes+numApplications+numFisherman, append(append(append(validatorPrivateKeys, snPrivateKeys...), fishPrivateKeys...), appsPrivateKeys...)...)), // TODO(olshansky): clean this up + Applications: converters.ToPersistenceActors(apps), + Validators: converters.ToPersistenceActors(vals), + ServiceNodes: converters.ToPersistenceActors(serviceNodes), + Fishermen: converters.ToPersistenceActors(fish), + Params: converters.ToPersistenceParams(DefaultParams()), + }), + validatorPrivateKeys } -func NewDefaultConfigs(privateKeys []string) (configs []runtime.Config) { +func NewDefaultConfigs(privateKeys []string) (configs []modules.Config) { for i, pk := range privateKeys { configs = append(configs, NewDefaultConfig(i, pk)) } return } -func NewDefaultConfig(i int, pk string) runtime.Config { - return runtime.Config{ - Base: &runtime.BaseConfig{ +func NewDefaultConfig(i int, pk string) modules.Config { + return runtime.NewConfig( + &runtime.BaseConfig{ RootDirectory: "/go/src/github.com/pocket-network", PrivateKey: pk, }, - Consensus: &typesCons.ConsensusConfig{ - MaxMempoolBytes: 500000000, - PacemakerConfig: &typesCons.PacemakerConfig{ - TimeoutMsec: 5000, - Manual: true, - DebugTimeBetweenStepsMsec: 1000, - }, - PrivateKey: pk, - }, - Utility: &typesUtil.UtilityConfig{}, - Persistence: &typesPers.PersistenceConfig{ + runtime.WithConsensusConfig( + &typesCons.ConsensusConfig{ + MaxMempoolBytes: 500000000, + PacemakerConfig: &typesCons.PacemakerConfig{ + TimeoutMsec: 5000, + Manual: true, + DebugTimeBetweenStepsMsec: 1000, + }, + PrivateKey: pk, + }), + runtime.WithUtilityConfig(&typesUtil.UtilityConfig{}), + runtime.WithPersistenceConfig(&typesPers.PersistenceConfig{ PostgresUrl: "postgres://postgres:postgres@pocket-db:5432/postgres", NodeSchema: "node" + strconv.Itoa(i+1), BlockStorePath: "/var/blockstore", - }, - P2P: &typesP2P.P2PConfig{ + }), + runtime.WithP2PConfig(&typesP2P.P2PConfig{ ConsensusPort: 8080, UseRainTree: true, IsEmptyConnectionType: false, PrivateKey: pk, - }, - Telemetry: &typesTelemetry.TelemetryConfig{ + }), + runtime.WithTelemetryConfig(&typesTelemetry.TelemetryConfig{ Enabled: true, Address: "0.0.0.0:9000", Endpoint: "/metrics", - }, - } + }), + ) } func NewPools() (pools []modules.Account) { // TODO (Team) in the real testing suite, we need to populate the pool amounts dependent on the actors diff --git a/telemetry/module.go b/telemetry/module.go index 18c97fa7f..1a42a5631 100644 --- a/telemetry/module.go +++ b/telemetry/module.go @@ -13,15 +13,15 @@ const ( TelemetryModuleName = "telemetry" ) -func Create(runtime modules.Runtime) (modules.Module, error) { +func Create(runtime modules.RuntimeMgr) (modules.Module, error) { return new(telemetryModule).Create(runtime) } // TODO(pocket/issues/99): Add a switch statement and configuration variable when support for other telemetry modules is added. -func (*telemetryModule) Create(runtime modules.Runtime) (modules.Module, error) { +func (*telemetryModule) Create(runtime modules.RuntimeMgr) (modules.Module, error) { cfg := runtime.GetConfig() - telemetryCfg := cfg.Telemetry.(*TelemetryConfig) + telemetryCfg := cfg.GetTelemetryConfig().(*TelemetryConfig) if telemetryCfg.GetEnabled() { return CreatePrometheusTelemetryModule(runtime) @@ -32,9 +32,9 @@ func (*telemetryModule) Create(runtime modules.Runtime) (modules.Module, error) type telemetryModule struct{} -func (t *telemetryModule) GetModuleName() string { return TelemetryModuleName } -func (t *telemetryModule) InitGenesis(_ string) (genesis modules.IGenesis, err error) { return } -func (t *telemetryModule) SetBus(bus modules.Bus) {} -func (t *telemetryModule) GetBus() modules.Bus { return nil } -func (t *telemetryModule) Start() error { return nil } -func (t *telemetryModule) Stop() error { return nil } +func (t *telemetryModule) GetModuleName() string { return TelemetryModuleName } +func (t *telemetryModule) InitGenesis(_ string) (genesis modules.GenesisState, err error) { return } +func (t *telemetryModule) SetBus(bus modules.Bus) {} +func (t *telemetryModule) GetBus() modules.Bus { return nil } +func (t *telemetryModule) Start() error { return nil } +func (t *telemetryModule) Stop() error { return nil } diff --git a/telemetry/noop_module.go b/telemetry/noop_module.go index 18a92a5fe..5d7bc0d00 100644 --- a/telemetry/noop_module.go +++ b/telemetry/noop_module.go @@ -26,12 +26,12 @@ func NOOP() { log.Printf("\n[telemetry=noop]\n") } -func CreateNoopTelemetryModule(runtime modules.Runtime) (modules.Module, error) { +func CreateNoopTelemetryModule(runtime modules.RuntimeMgr) (modules.Module, error) { var m NoopTelemetryModule return m.Create(runtime) } -func (m *NoopTelemetryModule) Create(runtime modules.Runtime) (modules.Module, error) { +func (m *NoopTelemetryModule) Create(runtime modules.RuntimeMgr) (modules.Module, error) { return &NoopTelemetryModule{}, nil } @@ -61,7 +61,7 @@ func (m *NoopTelemetryModule) GetBus() modules.Bus { } func (*NoopTelemetryModule) ValidateConfig(cfg modules.Config) error { - if _, ok := cfg.Telemetry.(*TelemetryConfig); !ok { + if _, ok := cfg.GetTelemetryConfig().(*TelemetryConfig); !ok { return fmt.Errorf("cannot cast to TelemetryConfig") } return nil diff --git a/telemetry/prometheus_module.go b/telemetry/prometheus_module.go index 33bc08577..9ed7af8b6 100644 --- a/telemetry/prometheus_module.go +++ b/telemetry/prometheus_module.go @@ -34,17 +34,17 @@ const ( PrometheusModuleName = "prometheus" ) -func CreatePrometheusTelemetryModule(runtime modules.Runtime) (modules.Module, error) { +func CreatePrometheusTelemetryModule(runtime modules.RuntimeMgr) (modules.Module, error) { var m PrometheusTelemetryModule return m.Create(runtime) } -func (m *PrometheusTelemetryModule) Create(runtime modules.Runtime) (modules.Module, error) { +func (m *PrometheusTelemetryModule) Create(runtime modules.RuntimeMgr) (modules.Module, error) { cfg := runtime.GetConfig() if err := m.ValidateConfig(cfg); err != nil { log.Fatalf("config validation failed: %v", err) } - telemetryCfg := cfg.Telemetry.(*TelemetryConfig) + telemetryCfg := cfg.GetTelemetryConfig().(*TelemetryConfig) return &PrometheusTelemetryModule{ config: telemetryCfg, @@ -85,7 +85,7 @@ func (m *PrometheusTelemetryModule) GetBus() modules.Bus { } func (*PrometheusTelemetryModule) ValidateConfig(cfg modules.Config) error { - if _, ok := cfg.Telemetry.(*TelemetryConfig); !ok { + if _, ok := cfg.GetTelemetryConfig().(*TelemetryConfig); !ok { return fmt.Errorf("cannot cast to TelemetryConfig") } return nil diff --git a/utility/module.go b/utility/module.go index 936b68a90..b212759fd 100644 --- a/utility/module.go +++ b/utility/module.go @@ -24,18 +24,18 @@ const ( UtilityModuleName = "utility" ) -func Create(runtime modules.Runtime) (modules.Module, error) { +func Create(runtime modules.RuntimeMgr) (modules.Module, error) { return new(utilityModule).Create(runtime) } -func (*utilityModule) Create(runtime modules.Runtime) (modules.Module, error) { +func (*utilityModule) Create(runtime modules.RuntimeMgr) (modules.Module, error) { var m *utilityModule cfg := runtime.GetConfig() if err := m.ValidateConfig(cfg); err != nil { log.Fatalf("config validation failed: %v", err) } - utilityCfg := cfg.Utility.(*types.UtilityConfig) + utilityCfg := cfg.GetUtilityConfig().(*types.UtilityConfig) return &utilityModule{ config: utilityCfg, @@ -68,7 +68,7 @@ func (u *utilityModule) GetBus() modules.Bus { } func (*utilityModule) ValidateConfig(cfg modules.Config) error { - if _, ok := cfg.Utility.(*types.UtilityConfig); !ok { + if _, ok := cfg.GetUtilityConfig().(*types.UtilityConfig); !ok { return fmt.Errorf("cannot cast to UtilityConfig") } return nil diff --git a/utility/test/module_test.go b/utility/test/module_test.go index 5d33507b5..327471a0f 100644 --- a/utility/test/module_test.go +++ b/utility/test/module_test.go @@ -1,15 +1,13 @@ package test import ( - "encoding/json" - "io/ioutil" "log" "math/big" - "os" "testing" - typesPers "github.com/pokt-network/pocket/persistence/types" + "github.com/golang/mock/gomock" "github.com/pokt-network/pocket/runtime" + mock_modules "github.com/pokt-network/pocket/shared/modules/mocks" "github.com/pokt-network/pocket/shared/test_artifacts" utilTypes "github.com/pokt-network/pocket/utility/types" @@ -28,7 +26,7 @@ var ( testSchema = "test_schema" ) -var testPersistenceMod modules.PersistenceModule +var persistenceDbUrl string func NewTestingMempool(_ *testing.T) utilTypes.Mempool { return utilTypes.NewMempool(1000000, 1000) @@ -36,14 +34,16 @@ func NewTestingMempool(_ *testing.T) utilTypes.Mempool { func TestMain(m *testing.M) { pool, resource, dbUrl := test_artifacts.SetupPostgresDocker() - testPersistenceMod = newTestPersistenceModule(dbUrl) + persistenceDbUrl = dbUrl m.Run() - os.Remove(testingConfigFilePath) - os.Remove(testingGenesisFilePath) + // os.Remove(testingConfigFilePath) + // os.Remove(testingGenesisFilePath) test_artifacts.CleanupPostgresDocker(m, pool, resource) } func NewTestingUtilityContext(t *testing.T, height int64) utility.UtilityContext { + testPersistenceMod := newTestPersistenceModule(t, persistenceDbUrl) + persistenceContext, err := testPersistenceMod.NewRWContext(height) require.NoError(t, err) @@ -62,59 +62,27 @@ func NewTestingUtilityContext(t *testing.T, height int64) utility.UtilityContext } } -// TODO(andrew): Take in `t` and fail the test if there's an error -func newTestPersistenceModule(databaseUrl string) modules.PersistenceModule { - cfg := modules.Config{ - Persistence: &typesPers.PersistenceConfig{ - PostgresUrl: databaseUrl, - NodeSchema: testSchema, - BlockStorePath: "", - }, - } - // TODO(andrew): Move the number of actors into local constants +func newTestPersistenceModule(t *testing.T, databaseUrl string) modules.PersistenceModule { + ctrl := gomock.NewController(t) + + mockPersistenceConfig := mock_modules.NewMockPersistenceConfig(ctrl) + mockPersistenceConfig.EXPECT().GetPostgresUrl().Return(databaseUrl).AnyTimes() + mockPersistenceConfig.EXPECT().GetNodeSchema().Return(testSchema).AnyTimes() + mockPersistenceConfig.EXPECT().GetBlockStorePath().Return("").AnyTimes() + + mockRuntimeMgr := mock_modules.NewMockRuntimeMgr(ctrl) + mockRuntimeMgr.EXPECT().GetConfig().Return(runtime.NewConfig(&runtime.BaseConfig{}, + runtime.WithPersistenceConfig(mockPersistenceConfig), + )).AnyTimes() + genesisState, _ := test_artifacts.NewGenesisState(5, 1, 1, 1) - createTestingGenesisAndConfigFiles(cfg, genesisState) - runtimeCfg := runtime.New(testingConfigFilePath, testingGenesisFilePath) + mockRuntimeMgr.EXPECT().GetGenesis().Return(genesisState).AnyTimes() - persistenceMod, err := persistence.Create(runtimeCfg) // TODO (Drewsky) this is the last remaining cross module import and needs a fix... + persistenceMod, err := persistence.Create(mockRuntimeMgr) if err != nil { log.Fatalf("Error creating persistence module: %s", err) } - persistenceMod.Start() // TODO: Check for error + err = persistenceMod.Start() + require.NoError(t, err) return persistenceMod.(modules.PersistenceModule) } - -const ( - testingGenesisFilePath = "genesis.json" - testingConfigFilePath = "config.json" -) - -func createTestingGenesisAndConfigFiles(cfg modules.Config, genesisState modules.GenesisState) { - config, err := json.Marshal(cfg.Persistence) - if err != nil { - log.Fatal(err) - } - genesis, err := json.Marshal(genesisState.PersistenceGenesisState) - if err != nil { - log.Fatal(err) - } - genesisFile := make(map[string]json.RawMessage) - configFile := make(map[string]json.RawMessage) - persistenceModuleName := persistence.PersistenceModuleName - genesisFile[test_artifacts.GetGenesisFileName(persistenceModuleName)] = genesis - configFile[persistenceModuleName] = config - genesisFileBz, err := json.MarshalIndent(genesisFile, "", " ") - if err != nil { - log.Fatal(err) - } - configFileBz, err := json.MarshalIndent(configFile, "", " ") - if err != nil { - log.Fatal(err) - } - if err := ioutil.WriteFile(testingGenesisFilePath, genesisFileBz, 0777); err != nil { - log.Fatal(err) - } - if err := ioutil.WriteFile(testingConfigFilePath, configFileBz, 0777); err != nil { - log.Fatal(err) - } -} From 281bb4d4d6504ba9d0af7d64f4b5c7ddecaf23d1 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 29 Sep 2022 16:19:52 +0100 Subject: [PATCH 43/90] fix(Shared): bugfixes --- consensus/consensus_tests/utils_test.go | 8 ----- consensus/module.go | 29 +++++++++--------- consensus/pacemaker.go | 18 +++++------ consensus/types/consensus_genesis.go | 13 ++++++++ consensus/types/types.go | 9 +++--- p2p/module.go | 8 ++--- p2p/module_raintree_test.go | 3 -- persistence/module.go | 22 +++++++------- persistence/test/setup_test.go | 40 ------------------------- runtime/config.go | 2 +- shared/modules/module.go | 2 +- shared/modules/types.go | 2 ++ telemetry/noop_module.go | 8 ++--- telemetry/prometheus_module.go | 8 ++--- utility/module.go | 8 ++--- utility/test/module_test.go | 10 +++---- 16 files changed, 78 insertions(+), 112 deletions(-) diff --git a/consensus/consensus_tests/utils_test.go b/consensus/consensus_tests/utils_test.go index 2a27425d6..50db9e1ad 100644 --- a/consensus/consensus_tests/utils_test.go +++ b/consensus/consensus_tests/utils_test.go @@ -6,7 +6,6 @@ import ( "flag" "fmt" "log" - "os" "reflect" "sort" "testing" @@ -30,8 +29,6 @@ import ( func TestMain(m *testing.M) { m.Run() - os.Remove(testingConfigFilePath) - os.Remove(testingGenesisFilePath) } // If this is set to true, consensus unit tests will fail if additional unexpected messages are received. @@ -128,11 +125,6 @@ func CreateTestConsensusPocketNodesNew( return } -const ( - testingGenesisFilePath = "genesis.json" - testingConfigFilePath = "config.json" -) - // Creates a pocket node where all the primary modules, exception for consensus, are mocked func CreateTestConsensusPocketNode( t *testing.T, diff --git a/consensus/module.go b/consensus/module.go index 0f0789143..65a748f44 100644 --- a/consensus/module.go +++ b/consensus/module.go @@ -5,7 +5,6 @@ import ( "log" "github.com/pokt-network/pocket/consensus/leader_election" - "github.com/pokt-network/pocket/consensus/types" typesCons "github.com/pokt-network/pocket/consensus/types" cryptoPocket "github.com/pokt-network/pocket/shared/crypto" "google.golang.org/protobuf/proto" @@ -31,7 +30,7 @@ var ( type consensusModule struct { bus modules.Bus privateKey cryptoPocket.Ed25519PrivateKey - config *types.ConsensusConfig + config modules.ConsensusConfig // Hotstuff Height uint64 @@ -75,13 +74,13 @@ func (*consensusModule) Create(runtimeMgr modules.RuntimeMgr) (modules.Module, e if err := m.ValidateConfig(cfg); err != nil { log.Fatalf("config validation failed: %v", err) } - consensusCfg := cfg.GetConsensusConfig().(*typesCons.ConsensusConfig) + consensusCfg := cfg.GetConsensusConfig() genesis := runtimeMgr.GetGenesis() if err := m.ValidateGenesis(genesis); err != nil { log.Fatalf("genesis validation failed: %v", err) } - consensusGenesis := genesis.GetConsensusGenesisState().(*typesCons.ConsensusGenesisState) + consensusGenesis := genesis.GetConsensusGenesisState() leaderElectionMod, err := leader_election.Create(runtimeMgr) if err != nil { @@ -94,9 +93,9 @@ func (*consensusModule) Create(runtimeMgr modules.RuntimeMgr) (modules.Module, e return nil, err } - valMap := typesCons.ValidatorListToMap(consensusGenesis.Validators) + valMap := typesCons.ActorListToMap(consensusGenesis.GetVals()) - privateKey, err := m.GetPrivateKey(runtimeMgr) + privateKey, err := cryptoPocket.NewPrivateKey(consensusCfg.GetPrivateKey()) if err != nil { return nil, err } @@ -188,21 +187,23 @@ func (m *consensusModule) SetBus(pocketBus modules.Bus) { } func (*consensusModule) ValidateConfig(cfg modules.Config) error { - if _, ok := cfg.GetConsensusConfig().(*typesCons.ConsensusConfig); !ok { - return fmt.Errorf("cannot cast to ConsensusConfig") - } + // DISCUSS (team): we cannot cast if we want to use mocks and rely on interfaces + // if _, ok := cfg.GetConsensusConfig().(*typesCons.ConsensusConfig); !ok { + // return fmt.Errorf("cannot cast to ConsensusConfig") + // } return nil } func (*consensusModule) ValidateGenesis(genesis modules.GenesisState) error { - if _, ok := genesis.GetConsensusGenesisState().(*typesCons.ConsensusGenesisState); !ok { - return fmt.Errorf("cannot cast to ConsensusGenesisState") - } + // DISCUSS (team): we cannot cast if we want to use mocks and rely on interfaces + // if _, ok := genesis.GetConsensusGenesisState().(*typesCons.ConsensusGenesisState); !ok { + // return fmt.Errorf("cannot cast to ConsensusGenesisState") + // } return nil } -func (*consensusModule) GetPrivateKey(runtime modules.RuntimeMgr) (cryptoPocket.PrivateKey, error) { - return cryptoPocket.NewPrivateKey(runtime.GetConfig().GetConsensusConfig().(*typesCons.ConsensusConfig).PrivateKey) +func (m *consensusModule) GetPrivateKey() (cryptoPocket.PrivateKey, error) { + return cryptoPocket.NewPrivateKey(m.config.GetPrivateKey()) } func (m *consensusModule) loadPersistedState() error { diff --git a/consensus/pacemaker.go b/consensus/pacemaker.go index 4be30611f..6f5b15441 100644 --- a/consensus/pacemaker.go +++ b/consensus/pacemaker.go @@ -2,7 +2,6 @@ package consensus import ( "context" - "fmt" "log" "time" @@ -108,14 +107,15 @@ func (m *paceMaker) GetBus() modules.Bus { } func (*paceMaker) ValidateConfig(cfg modules.Config) error { - consCfg, ok := cfg.GetConsensusConfig().(*typesCons.ConsensusConfig) - if !ok { - return fmt.Errorf("cannot cast to TelemetryConfig") - } - - if consCfg.PacemakerConfig == nil { - return fmt.Errorf("PacemakerConfig is nil") - } + // DISCUSS (team): we cannot cast if we want to use mocks and rely on interfaces + // consCfg, ok := cfg.GetConsensusConfig().(*typesCons.ConsensusConfig) + // if !ok { + // return fmt.Errorf("cannot cast to ConsensusConfig") + // } + + // if consCfg.PacemakerConfig == nil { + // return fmt.Errorf("PacemakerConfig is nil") + // } return nil } diff --git a/consensus/types/consensus_genesis.go b/consensus/types/consensus_genesis.go index 8c9fe1d93..24c005499 100644 --- a/consensus/types/consensus_genesis.go +++ b/consensus/types/consensus_genesis.go @@ -3,3 +3,16 @@ package types import "github.com/pokt-network/pocket/shared/modules" var _ modules.ConsensusGenesisState = &ConsensusGenesisState{} + +func (x *ConsensusGenesisState) GetVals() []modules.Actor { + return ActorsToActorsInterface(x.GetValidators()) +} + +func ActorsToActorsInterface(a []*Validator) (actorI []modules.Actor) { + for _, actor := range a { + actorI = append(actorI, actor) + } + return +} + +var _ modules.Actor = &Validator{} diff --git a/consensus/types/types.go b/consensus/types/types.go index 36896cfaf..909b2ece9 100644 --- a/consensus/types/types.go +++ b/consensus/types/types.go @@ -1,8 +1,9 @@ package types import ( - "github.com/pokt-network/pocket/shared/modules" "sort" + + "github.com/pokt-network/pocket/shared/modules" ) type NodeId uint64 @@ -55,9 +56,9 @@ func ValidatorMapToModulesValidatorMap(validatorMap ValidatorMap) (vm modules.Va return } -func ValidatorListToMap(validators []*Validator) (m ValidatorMap) { - m = make(ValidatorMap, len(validators)) - for _, v := range validators { +func ActorListToMap(actors []modules.Actor) (m ValidatorMap) { + m = make(ValidatorMap, len(actors)) + for _, v := range actors { m[v.GetAddress()] = v } return diff --git a/p2p/module.go b/p2p/module.go index cec6c1285..cbede9c56 100644 --- a/p2p/module.go +++ b/p2p/module.go @@ -5,7 +5,6 @@ package p2p // to be a "real" replacement for now. import ( - "fmt" "log" "github.com/pokt-network/pocket/shared/debug" @@ -167,9 +166,10 @@ func (m *p2pModule) Send(addr cryptoPocket.Address, msg *anypb.Any, topic debug. } func (*p2pModule) ValidateConfig(cfg modules.Config) error { - if _, ok := cfg.GetP2PConfig().(*typesP2P.P2PConfig); !ok { - return fmt.Errorf("cannot cast to P2PConfig") - } + // DISCUSS (team): we cannot cast if we want to use mocks and rely on interfaces + // if _, ok := cfg.GetP2PConfig().(*typesP2P.P2PConfig); !ok { + // return fmt.Errorf("cannot cast to P2PConfig") + // } return nil } diff --git a/p2p/module_raintree_test.go b/p2p/module_raintree_test.go index ee0f89277..74802eaba 100644 --- a/p2p/module_raintree_test.go +++ b/p2p/module_raintree_test.go @@ -241,9 +241,6 @@ const ( maxNumKeys = 42 // The number of keys generated for all the unit tests. Optimization to avoid regenerating every time. serviceUrlFormat = "val_%d" testChannelSize = 10000 - testingGenesisFilePath = "genesis" - testingConfigFilePath = "config" - jsonPosfix = ".json" ) // TODO(olshansky): Add configurations tests for dead and partially visible nodes diff --git a/persistence/module.go b/persistence/module.go index 9e71293ed..fbe05ea73 100644 --- a/persistence/module.go +++ b/persistence/module.go @@ -23,7 +23,7 @@ var ( type persistenceModule struct { bus modules.Bus - config *types.PersistenceConfig + config modules.PersistenceConfig blockStore kvstore.KVStore // INVESTIGATE: We may need to create a custom `BlockStore` package in the future @@ -47,7 +47,7 @@ func (*persistenceModule) Create(runtimeMgr modules.RuntimeMgr) (modules.Module, if err := m.ValidateConfig(cfg); err != nil { log.Fatalf("config validation failed: %v", err) } - persistenceCfg := cfg.GetPersistenceConfig().(*types.PersistenceConfig) + persistenceCfg := cfg.GetPersistenceConfig() genesis := runtimeMgr.GetGenesis() if err := m.ValidateGenesis(genesis); err != nil { @@ -118,16 +118,18 @@ func (m *persistenceModule) GetBus() modules.Bus { } func (*persistenceModule) ValidateConfig(cfg modules.Config) error { - if _, ok := cfg.GetPersistenceConfig().(*types.PersistenceConfig); !ok { - return fmt.Errorf("cannot cast to PersistenceConfig") - } + // DISCUSS (team): we cannot cast if we want to use mocks and rely on interfaces + // if _, ok := cfg.GetPersistenceConfig().(*types.PersistenceConfig); !ok { + // return fmt.Errorf("cannot cast to PersistenceConfig") + // } return nil } func (*persistenceModule) ValidateGenesis(genesis modules.GenesisState) error { - if _, ok := genesis.GetPersistenceGenesisState().(*types.PersistenceGenesisState); !ok { - return fmt.Errorf("cannot cast to PersistenceGenesisState") - } + // DISCUSS (team): we cannot cast if we want to use mocks and rely on interfaces + // if _, ok := genesis.GetPersistenceGenesisState().(*types.PersistenceGenesisState); !ok { + // return fmt.Errorf("cannot cast to PersistenceGenesisState") + // } return nil } @@ -135,7 +137,7 @@ func (m *persistenceModule) NewRWContext(height int64) (modules.PersistenceRWCon if m.writeContext != nil && !m.writeContext.conn.IsClosed() { return nil, fmt.Errorf("write context already exists") } - conn, err := connectToDatabase(m.config.PostgresUrl, m.config.NodeSchema) + conn, err := connectToDatabase(m.config.GetPostgresUrl(), m.config.GetNodeSchema()) if err != nil { return nil, err } @@ -160,7 +162,7 @@ func (m *persistenceModule) NewRWContext(height int64) (modules.PersistenceRWCon } func (m *persistenceModule) NewReadContext(height int64) (modules.PersistenceReadContext, error) { - conn, err := connectToDatabase(m.config.PostgresUrl, m.config.NodeSchema) + conn, err := connectToDatabase(m.config.GetPostgresUrl(), m.config.GetNodeSchema()) if err != nil { return nil, err } diff --git a/persistence/test/setup_test.go b/persistence/test/setup_test.go index 1ebc5982e..19492f970 100644 --- a/persistence/test/setup_test.go +++ b/persistence/test/setup_test.go @@ -54,8 +54,6 @@ func TestMain(m *testing.M) { pool, resource, dbUrl := sharedTest.SetupPostgresDocker() testPersistenceMod = newTestPersistenceModule(dbUrl) m.Run() - // os.Remove(testingConfigFilePath) - // os.Remove(testingGenesisFilePath) sharedTest.CleanupPostgresDocker(m, pool, resource) } @@ -291,44 +289,6 @@ func fuzzSingleProtocolActor( }) } -// TODO(olshansky): Make these functions & variables more functional to avoid having "unexpected" -// -// side effects and making it clearer to the reader. -// const ( -// testingGenesisFilePath = "genesis.json" -// testingConfigFilePath = "config.json" -// ) - -// func createTestingGenesisAndConfigFiles(cfg modules.Config, genesisState modules.GenesisState) { -// config, err := json.Marshal(cfg.GetPersistenceConfig()) -// if err != nil { -// log.Fatal(err) -// } -// genesis, err := json.Marshal(genesisState.GetPersistenceGenesisState()) -// if err != nil { -// log.Fatal(err) -// } -// genesisFile := make(map[string]json.RawMessage) -// configFile := make(map[string]json.RawMessage) -// persistenceModuleName := persistence.PersistenceModuleName -// genesisFile[test_artifacts.GetGenesisFileName(persistenceModuleName)] = genesis -// configFile[persistenceModuleName] = config -// genesisFileBz, err := json.MarshalIndent(genesisFile, "", " ") -// if err != nil { -// log.Fatal(err) -// } -// configFileBz, err := json.MarshalIndent(configFile, "", " ") -// if err != nil { -// log.Fatal(err) -// } -// if err := ioutil.WriteFile(testingGenesisFilePath, genesisFileBz, 0777); err != nil { -// log.Fatal(err) -// } -// if err := ioutil.WriteFile(testingConfigFilePath, configFileBz, 0777); err != nil { -// log.Fatal(err) -// } -// } - func getRandomChains() (chains []string) { setRandomSeed() diff --git a/runtime/config.go b/runtime/config.go index ccbe32b5e..e7cbcab24 100644 --- a/runtime/config.go +++ b/runtime/config.go @@ -42,7 +42,7 @@ func WithUtilityConfig(utilityConfig modules.UtilityConfig) func(modules.Config) } } -func WithPersistenceConfig(persistenceConfig modules.UtilityConfig) func(modules.Config) { +func WithPersistenceConfig(persistenceConfig modules.PersistenceConfig) func(modules.Config) { return func(rc modules.Config) { rc.(*runtimeConfig).Persistence = persistenceConfig.(*typesPers.PersistenceConfig) } diff --git a/shared/modules/module.go b/shared/modules/module.go index c7dacf408..9ffd64755 100644 --- a/shared/modules/module.go +++ b/shared/modules/module.go @@ -38,7 +38,7 @@ type GenesisDependentModule interface { } type KeyholderModule interface { - GetPrivateKey(RuntimeMgr) (crypto.PrivateKey, error) + GetPrivateKey() (crypto.PrivateKey, error) } type P2PAddressableModule interface { diff --git a/shared/modules/types.go b/shared/modules/types.go index 88b28b5bf..9a9fd0a00 100644 --- a/shared/modules/types.go +++ b/shared/modules/types.go @@ -30,6 +30,7 @@ type BaseConfig interface { type ConsensusConfig interface { GetMaxMempoolBytes() uint64 + GetPrivateKey() string // TODO (pocket/issues/150) better architecture for key management (keybase, keyfiles, etc.) } type PacemakerConfig interface { @@ -72,6 +73,7 @@ type ConsensusGenesisState interface { GetGenesisTime() *timestamppb.Timestamp GetChainId() string GetMaxBlockBytes() uint64 + GetVals() []Actor } type Account interface { diff --git a/telemetry/noop_module.go b/telemetry/noop_module.go index 5d7bc0d00..e407584d5 100644 --- a/telemetry/noop_module.go +++ b/telemetry/noop_module.go @@ -1,7 +1,6 @@ package telemetry import ( - "fmt" "log" "github.com/pokt-network/pocket/shared/modules" @@ -61,9 +60,10 @@ func (m *NoopTelemetryModule) GetBus() modules.Bus { } func (*NoopTelemetryModule) ValidateConfig(cfg modules.Config) error { - if _, ok := cfg.GetTelemetryConfig().(*TelemetryConfig); !ok { - return fmt.Errorf("cannot cast to TelemetryConfig") - } + // DISCUSS (team): we cannot cast if we want to use mocks and rely on interfaces + // if _, ok := cfg.GetTelemetryConfig().(*TelemetryConfig); !ok { + // return fmt.Errorf("cannot cast to TelemetryConfig") + // } return nil } diff --git a/telemetry/prometheus_module.go b/telemetry/prometheus_module.go index 9ed7af8b6..329681500 100644 --- a/telemetry/prometheus_module.go +++ b/telemetry/prometheus_module.go @@ -1,7 +1,6 @@ package telemetry import ( - "fmt" "log" "net/http" @@ -85,9 +84,10 @@ func (m *PrometheusTelemetryModule) GetBus() modules.Bus { } func (*PrometheusTelemetryModule) ValidateConfig(cfg modules.Config) error { - if _, ok := cfg.GetTelemetryConfig().(*TelemetryConfig); !ok { - return fmt.Errorf("cannot cast to TelemetryConfig") - } + // DISCUSS (team): we cannot cast if we want to use mocks and rely on interfaces + // if _, ok := cfg.GetTelemetryConfig().(*TelemetryConfig); !ok { + // return fmt.Errorf("cannot cast to TelemetryConfig") + // } return nil } diff --git a/utility/module.go b/utility/module.go index b212759fd..9935bee84 100644 --- a/utility/module.go +++ b/utility/module.go @@ -1,7 +1,6 @@ package utility import ( - "fmt" "log" "github.com/pokt-network/pocket/utility/types" @@ -68,8 +67,9 @@ func (u *utilityModule) GetBus() modules.Bus { } func (*utilityModule) ValidateConfig(cfg modules.Config) error { - if _, ok := cfg.GetUtilityConfig().(*types.UtilityConfig); !ok { - return fmt.Errorf("cannot cast to UtilityConfig") - } + // DISCUSS (team): we cannot cast if we want to use mocks and rely on interfaces + // if _, ok := cfg.GetUtilityConfig().(*types.UtilityConfig); !ok { + // return fmt.Errorf("cannot cast to UtilityConfig") + // } return nil } diff --git a/utility/test/module_test.go b/utility/test/module_test.go index bd7613db3..df512b2f1 100644 --- a/utility/test/module_test.go +++ b/utility/test/module_test.go @@ -6,7 +6,6 @@ import ( "testing" "github.com/golang/mock/gomock" - "github.com/pokt-network/pocket/runtime" mock_modules "github.com/pokt-network/pocket/shared/modules/mocks" "github.com/pokt-network/pocket/shared/test_artifacts" utilTypes "github.com/pokt-network/pocket/utility/types" @@ -43,8 +42,6 @@ func TestMain(m *testing.M) { pool, resource, dbUrl := test_artifacts.SetupPostgresDocker() persistenceDbUrl = dbUrl m.Run() - // os.Remove(testingConfigFilePath) - // os.Remove(testingGenesisFilePath) test_artifacts.CleanupPostgresDocker(m, pool, resource) } @@ -77,10 +74,11 @@ func newTestPersistenceModule(t *testing.T, databaseUrl string) modules.Persiste mockPersistenceConfig.EXPECT().GetNodeSchema().Return(testSchema).AnyTimes() mockPersistenceConfig.EXPECT().GetBlockStorePath().Return("").AnyTimes() + mockRuntimeConfig := mock_modules.NewMockConfig(ctrl) + mockRuntimeConfig.EXPECT().GetPersistenceConfig().Return(mockPersistenceConfig).AnyTimes() + mockRuntimeMgr := mock_modules.NewMockRuntimeMgr(ctrl) - mockRuntimeMgr.EXPECT().GetConfig().Return(runtime.NewConfig(&runtime.BaseConfig{}, - runtime.WithPersistenceConfig(mockPersistenceConfig), - )).AnyTimes() + mockRuntimeMgr.EXPECT().GetConfig().Return(mockRuntimeConfig).AnyTimes() genesisState, _ := test_artifacts.NewGenesisState(5, 1, 1, 1) mockRuntimeMgr.EXPECT().GetGenesis().Return(genesisState).AnyTimes() From 7f6d428bc8ca2428158293b80b0c9d0540c365a7 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 29 Sep 2022 16:25:42 +0100 Subject: [PATCH 44/90] fix(Consensus): merge fix --- consensus/block.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/consensus/block.go b/consensus/block.go index ce5bf6a7b..c85fc620e 100644 --- a/consensus/block.go +++ b/consensus/block.go @@ -135,7 +135,7 @@ func (m *consensusModule) commitBlock(block *typesCons.Block) error { return nil } -func (m *ConsensusModule) storeBlock(block *typesCons.Block, blockProtoBytes []byte) error { +func (m *consensusModule) storeBlock(block *typesCons.Block, blockProtoBytes []byte) error { store := m.utilityContext.GetPersistenceContext() // Store in KV Store if err := store.StoreBlock(blockProtoBytes); err != nil { From f0a7c7da10ee1eaa4d034bcca11df6cdfdc031a5 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 29 Sep 2022 16:35:51 +0100 Subject: [PATCH 45/90] test(Consensus): fixed tests (StoreBlock and InsertBlock mocks cfg) --- consensus/consensus_tests/utils_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/consensus/consensus_tests/utils_test.go b/consensus/consensus_tests/utils_test.go index 50db9e1ad..a6ded4bb8 100644 --- a/consensus/consensus_tests/utils_test.go +++ b/consensus/consensus_tests/utils_test.go @@ -359,6 +359,8 @@ func baseUtilityMock(t *testing.T, _ modules.EventsChannel) *modulesMock.MockUti utilityContextMock.EXPECT().StoreBlock(gomock.Any()).AnyTimes().Return(nil) persistenceContextMock.EXPECT().Commit().Return(nil).AnyTimes() + persistenceContextMock.EXPECT().StoreBlock(gomock.Any()).AnyTimes().Return(nil) + persistenceContextMock.EXPECT().InsertBlock(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil) return utilityMock } From 2837b5aff91973a67988c6d5db9ae893a6445437 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 29 Sep 2022 16:53:30 +0100 Subject: [PATCH 46/90] fix(Shared): removed config and genesis from bus --- app/client/main.go | 2 +- consensus/consensus_tests/utils_test.go | 2 +- persistence/debug.go | 2 +- persistence/module.go | 6 ++++-- shared/bus.go | 18 ------------------ shared/modules/bus_module.go | 4 ---- shared/node.go | 2 +- 7 files changed, 8 insertions(+), 28 deletions(-) diff --git a/app/client/main.go b/app/client/main.go index 7be62aa11..cab34d78c 100644 --- a/app/client/main.go +++ b/app/client/main.go @@ -72,7 +72,7 @@ func main() { } telemetryMod := telemetryM.(modules.TelemetryModule) - _ = shared.CreateBusWithOptionalModules(runtimeMgr, nil, p2pMod, nil, consensusMod, telemetryMod) + _ = shared.CreateBusWithOptionalModules(nil, p2pMod, nil, consensusMod, telemetryMod) p2pMod.Start() diff --git a/consensus/consensus_tests/utils_test.go b/consensus/consensus_tests/utils_test.go index a6ded4bb8..9a80e7aba 100644 --- a/consensus/consensus_tests/utils_test.go +++ b/consensus/consensus_tests/utils_test.go @@ -142,7 +142,7 @@ func CreateTestConsensusPocketNode( utilityMock := baseUtilityMock(t, testChannel) telemetryMock := baseTelemetryMock(t, testChannel) - bus, err := shared.CreateBus(runtimeMgr, persistenceMock, p2pMock, utilityMock, consensusMod.(modules.ConsensusModule), telemetryMock) + bus, err := shared.CreateBus(persistenceMock, p2pMock, utilityMock, consensusMod.(modules.ConsensusModule), telemetryMock) require.NoError(t, err) pk, err := cryptoPocket.NewPrivateKey(runtimeMgr.GetConfig().GetBaseConfig().GetPrivateKey()) diff --git a/persistence/debug.go b/persistence/debug.go index 2032f7659..1f49f99c2 100644 --- a/persistence/debug.go +++ b/persistence/debug.go @@ -15,7 +15,7 @@ func (m *persistenceModule) HandleDebugMessage(debugMessage *debug.DebugMessage) m.showLatestBlockInStore(debugMessage) case debug.DebugMessageAction_DEBUG_CLEAR_STATE: m.clearState(debugMessage) - g := m.GetBus().GetGenesis().GetPersistenceGenesisState().(*types.PersistenceGenesisState) + g := m.genesisState.(*types.PersistenceGenesisState) m.populateGenesisState(g) default: log.Printf("Debug message not handled by persistence module: %s \n", debugMessage.Message) diff --git a/persistence/module.go b/persistence/module.go index fbe05ea73..be16a686a 100644 --- a/persistence/module.go +++ b/persistence/module.go @@ -22,8 +22,9 @@ var ( ) type persistenceModule struct { - bus modules.Bus - config modules.PersistenceConfig + bus modules.Bus + config modules.PersistenceConfig + genesisState modules.PersistenceGenesisState blockStore kvstore.KVStore // INVESTIGATE: We may need to create a custom `BlockStore` package in the future @@ -72,6 +73,7 @@ func (*persistenceModule) Create(runtimeMgr modules.RuntimeMgr) (modules.Module, m = &persistenceModule{ bus: nil, config: persistenceCfg, + genesisState: persistenceGenesis, blockStore: blockStore, writeContext: nil, } diff --git a/shared/bus.go b/shared/bus.go index d35ea2f5e..c81c24644 100644 --- a/shared/bus.go +++ b/shared/bus.go @@ -22,9 +22,6 @@ type bus struct { utility modules.UtilityModule consensus modules.ConsensusModule telemetry modules.TelemetryModule - - config modules.Config - genesis modules.GenesisState } const ( @@ -32,7 +29,6 @@ const ( ) func CreateBus( - runtime modules.RuntimeMgr, persistence modules.PersistenceModule, p2p modules.P2PModule, utility modules.UtilityModule, @@ -47,9 +43,6 @@ func CreateBus( utility: utility, consensus: consensus, telemetry: telemetry, - - config: runtime.GetConfig(), - genesis: runtime.GetGenesis(), } modules := map[string]modules.Module{ @@ -83,7 +76,6 @@ func CreateBus( // Using `CreateBusWithOptionalModules`, we can create a bus with only pre2p and a NOOP telemetry module // so that we can the pre2p module without any issues. func CreateBusWithOptionalModules( - runtime modules.RuntimeMgr, persistence modules.PersistenceModule, p2p modules.P2PModule, utility modules.UtilityModule, @@ -97,8 +89,6 @@ func CreateBusWithOptionalModules( utility: utility, consensus: consensus, telemetry: telemetry, - config: runtime.GetConfig(), - genesis: runtime.GetGenesis(), } maybeSetModuleBus := func(mod modules.Module) { @@ -148,11 +138,3 @@ func (m bus) GetConsensusModule() modules.ConsensusModule { func (m bus) GetTelemetryModule() modules.TelemetryModule { return m.telemetry } - -func (m bus) GetConfig() modules.Config { - return m.config -} - -func (m bus) GetGenesis() modules.GenesisState { - return m.genesis -} diff --git a/shared/modules/bus_module.go b/shared/modules/bus_module.go index 3bd0a433d..68dc5c704 100644 --- a/shared/modules/bus_module.go +++ b/shared/modules/bus_module.go @@ -23,8 +23,4 @@ type Bus interface { GetUtilityModule() UtilityModule GetConsensusModule() ConsensusModule GetTelemetryModule() TelemetryModule - - // Configuration - GetConfig() Config - GetGenesis() GenesisState } diff --git a/shared/node.go b/shared/node.go index 8d6a7d501..50157ac3d 100644 --- a/shared/node.go +++ b/shared/node.go @@ -62,7 +62,7 @@ func (m *Node) Create(runtime modules.RuntimeMgr) (modules.Module, error) { return nil, err } - bus, err := CreateBus(runtime, + bus, err := CreateBus( persistenceMod.(modules.PersistenceModule), p2pMod.(modules.P2PModule), utilityMod.(modules.UtilityModule), From 3af1aa9c4be5d89a37e9022b422dd06f0d77429f Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 29 Sep 2022 16:59:36 +0100 Subject: [PATCH 47/90] style(Runtime): cleanup --- runtime/config.go | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/runtime/config.go b/runtime/config.go index e7cbcab24..5cddd4057 100644 --- a/runtime/config.go +++ b/runtime/config.go @@ -77,15 +77,3 @@ func (c *runtimeConfig) GetP2PConfig() modules.P2PConfig { func (c *runtimeConfig) GetTelemetryConfig() modules.TelemetryConfig { return c.Telemetry } - -// func ParseConfigJSON(configPath string) (config *runtimeConfig, err error) { -// data, err := os.ReadFile(configPath) -// if err != nil { -// return -// } - -// general configuration file -// config = new(runtimeConfig) -// err = json.Unmarshal(data, &config) -// return -// } From a8530d2ab589c248d53352a8df2b37d351b362ac Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 29 Sep 2022 17:17:05 +0100 Subject: [PATCH 48/90] style(Runtime): import alias --- runtime/manager.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/manager.go b/runtime/manager.go index 9ea1483cc..04943f24b 100644 --- a/runtime/manager.go +++ b/runtime/manager.go @@ -9,7 +9,7 @@ import ( "strings" "github.com/mitchellh/mapstructure" - "github.com/pokt-network/pocket/consensus/types" + typesCons "github.com/pokt-network/pocket/consensus/types" typesP2P "github.com/pokt-network/pocket/p2p/types" cryptoPocket "github.com/pokt-network/pocket/shared/crypto" "github.com/pokt-network/pocket/shared/modules" @@ -134,7 +134,7 @@ func WithRandomPK() func(*Manager) { func WithPK(pk string) func(*Manager) { return func(b *Manager) { if b.config.Consensus == nil { - b.config.Consensus = &types.ConsensusConfig{} + b.config.Consensus = &typesCons.ConsensusConfig{} } b.config.Consensus.PrivateKey = pk From 2a686db94104405ac0ff6b2320c740bbf32a11ae Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 29 Sep 2022 17:24:37 +0100 Subject: [PATCH 49/90] fix(Shared): module creation failures returns err --- consensus/module.go | 4 ++-- p2p/module.go | 3 ++- persistence/module.go | 4 ++-- utility/module.go | 3 ++- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/consensus/module.go b/consensus/module.go index 65a748f44..b0fa5c134 100644 --- a/consensus/module.go +++ b/consensus/module.go @@ -72,13 +72,13 @@ func (*consensusModule) Create(runtimeMgr modules.RuntimeMgr) (modules.Module, e cfg := runtimeMgr.GetConfig() if err := m.ValidateConfig(cfg); err != nil { - log.Fatalf("config validation failed: %v", err) + return nil, fmt.Errorf("config validation failed: %w", err) } consensusCfg := cfg.GetConsensusConfig() genesis := runtimeMgr.GetGenesis() if err := m.ValidateGenesis(genesis); err != nil { - log.Fatalf("genesis validation failed: %v", err) + return nil, fmt.Errorf("genesis validation failed: %w", err) } consensusGenesis := genesis.GetConsensusGenesisState() diff --git a/p2p/module.go b/p2p/module.go index cbede9c56..8b06e3acb 100644 --- a/p2p/module.go +++ b/p2p/module.go @@ -5,6 +5,7 @@ package p2p // to be a "real" replacement for now. import ( + "fmt" "log" "github.com/pokt-network/pocket/shared/debug" @@ -50,7 +51,7 @@ func (*p2pModule) Create(runtimeMgr modules.RuntimeMgr) (modules.Module, error) cfg := runtimeMgr.GetConfig() if err := m.ValidateConfig(cfg); err != nil { - log.Fatalf("config validation failed: %v", err) + return nil, fmt.Errorf("config validation failed: %w", err) } p2pCfg := cfg.GetP2PConfig().(*typesP2P.P2PConfig) diff --git a/persistence/module.go b/persistence/module.go index be16a686a..87b77efa2 100644 --- a/persistence/module.go +++ b/persistence/module.go @@ -46,13 +46,13 @@ func (*persistenceModule) Create(runtimeMgr modules.RuntimeMgr) (modules.Module, cfg := runtimeMgr.GetConfig() if err := m.ValidateConfig(cfg); err != nil { - log.Fatalf("config validation failed: %v", err) + return nil, fmt.Errorf("config validation failed: %w", err) } persistenceCfg := cfg.GetPersistenceConfig() genesis := runtimeMgr.GetGenesis() if err := m.ValidateGenesis(genesis); err != nil { - log.Fatalf("genesis validation failed: %v", err) + return nil, fmt.Errorf("genesis validation failed: %w", err) } persistenceGenesis := genesis.GetPersistenceGenesisState().(*types.PersistenceGenesisState) diff --git a/utility/module.go b/utility/module.go index 9935bee84..7eae28fd0 100644 --- a/utility/module.go +++ b/utility/module.go @@ -1,6 +1,7 @@ package utility import ( + "fmt" "log" "github.com/pokt-network/pocket/utility/types" @@ -32,7 +33,7 @@ func (*utilityModule) Create(runtime modules.RuntimeMgr) (modules.Module, error) cfg := runtime.GetConfig() if err := m.ValidateConfig(cfg); err != nil { - log.Fatalf("config validation failed: %v", err) + return nil, fmt.Errorf("config validation failed: %w", err) } utilityCfg := cfg.GetUtilityConfig().(*types.UtilityConfig) From fbe4a7fa23c1d9598df5d13bd3ed58bd3651cd5f Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 29 Sep 2022 17:44:51 +0100 Subject: [PATCH 50/90] refactor(Persistence): reverted converters to shared --- .../types}/converters.go | 23 +++++++-------- shared/test_artifacts/generator.go | 29 +++++++++---------- 2 files changed, 24 insertions(+), 28 deletions(-) rename {shared/converters => persistence/types}/converters.go (94%) diff --git a/shared/converters/converters.go b/persistence/types/converters.go similarity index 94% rename from shared/converters/converters.go rename to persistence/types/converters.go index 3140a633f..33fae5f48 100644 --- a/shared/converters/converters.go +++ b/persistence/types/converters.go @@ -1,12 +1,11 @@ -package converters +package types import ( - typesPers "github.com/pokt-network/pocket/persistence/types" "github.com/pokt-network/pocket/shared/modules" ) -func toPersistenceActor(actor modules.Actor) *typesPers.Actor { - return &typesPers.Actor{ +func toPersistenceActor(actor modules.Actor) *Actor { + return &Actor{ Address: actor.GetAddress(), PublicKey: actor.GetPublicKey(), StakedAmount: actor.GetStakedAmount(), @@ -14,31 +13,31 @@ func toPersistenceActor(actor modules.Actor) *typesPers.Actor { } } -func ToPersistenceActors(actors []modules.Actor) []*typesPers.Actor { - r := make([]*typesPers.Actor, 0) +func ToPersistenceActors(actors []modules.Actor) []*Actor { + r := make([]*Actor, 0) for _, a := range actors { r = append(r, toPersistenceActor(a)) } return r } -func toPersistenceAccount(account modules.Account) *typesPers.Account { - return &typesPers.Account{ +func toPersistenceAccount(account modules.Account) *Account { + return &Account{ Address: account.GetAddress(), Amount: account.GetAmount(), } } -func ToPersistenceAccounts(accounts []modules.Account) []*typesPers.Account { - r := make([]*typesPers.Account, 0) +func ToPersistenceAccounts(accounts []modules.Account) []*Account { + r := make([]*Account, 0) for _, a := range accounts { r = append(r, toPersistenceAccount(a)) } return r } -func ToPersistenceParams(params modules.Params) *typesPers.Params { - return &typesPers.Params{ +func ToPersistenceParams(params modules.Params) *Params { + return &Params{ BlocksPerSession: params.GetBlocksPerSession(), AppMinimumStake: params.GetAppMinimumStake(), AppMaxChains: params.GetAppMaxChains(), diff --git a/shared/test_artifacts/generator.go b/shared/test_artifacts/generator.go index d418e75c8..613af292c 100644 --- a/shared/test_artifacts/generator.go +++ b/shared/test_artifacts/generator.go @@ -5,17 +5,14 @@ import ( "math/big" "strconv" - typesPersistence "github.com/pokt-network/pocket/persistence/types" - "github.com/pokt-network/pocket/runtime" - "github.com/pokt-network/pocket/shared/converters" - "github.com/pokt-network/pocket/shared/modules" - "github.com/pokt-network/pocket/utility/types" - typesCons "github.com/pokt-network/pocket/consensus/types" typesP2P "github.com/pokt-network/pocket/p2p/types" typesPers "github.com/pokt-network/pocket/persistence/types" + "github.com/pokt-network/pocket/runtime" "github.com/pokt-network/pocket/shared/crypto" + "github.com/pokt-network/pocket/shared/modules" typesTelemetry "github.com/pokt-network/pocket/telemetry" + "github.com/pokt-network/pocket/utility/types" typesUtil "github.com/pokt-network/pocket/utility/types" "google.golang.org/protobuf/types/known/timestamppb" ) @@ -54,13 +51,13 @@ func NewGenesisState(numValidators, numServiceNodes, numApplications, numFisherm Validators: typesCons.ToConsensusValidators(vals), }, &typesPers.PersistenceGenesisState{ - Pools: converters.ToPersistenceAccounts(NewPools()), - Accounts: converters.ToPersistenceAccounts(NewAccounts(numValidators+numServiceNodes+numApplications+numFisherman, append(append(append(validatorPrivateKeys, snPrivateKeys...), fishPrivateKeys...), appsPrivateKeys...)...)), // TODO(olshansky): clean this up - Applications: converters.ToPersistenceActors(apps), - Validators: converters.ToPersistenceActors(vals), - ServiceNodes: converters.ToPersistenceActors(serviceNodes), - Fishermen: converters.ToPersistenceActors(fish), - Params: converters.ToPersistenceParams(DefaultParams()), + Pools: typesPers.ToPersistenceAccounts(NewPools()), + Accounts: typesPers.ToPersistenceAccounts(NewAccounts(numValidators+numServiceNodes+numApplications+numFisherman, append(append(append(validatorPrivateKeys, snPrivateKeys...), fishPrivateKeys...), appsPrivateKeys...)...)), // TODO(olshansky): clean this up + Applications: typesPers.ToPersistenceActors(apps), + Validators: typesPers.ToPersistenceActors(vals), + ServiceNodes: typesPers.ToPersistenceActors(serviceNodes), + Fishermen: typesPers.ToPersistenceActors(fish), + Params: typesPers.ToPersistenceParams(DefaultParams()), }), validatorPrivateKeys } @@ -109,8 +106,8 @@ func NewDefaultConfig(i int, pk string) modules.Config { } func NewPools() (pools []modules.Account) { // TODO (Team) in the real testing suite, we need to populate the pool amounts dependent on the actors - for _, name := range typesPersistence.Pool_Names_name { - if name == typesPersistence.Pool_Names_FeeCollector.String() { + for _, name := range typesPers.Pool_Names_name { + if name == typesPers.Pool_Names_FeeCollector.String() { pools = append(pools, &typesPers.Account{ Address: name, Amount: "0", @@ -156,7 +153,7 @@ func NewActors(actorType typesUtil.UtilActorType, n int) (actors []modules.Actor func NewDefaultActor(actorType int32, genericParam string) (actor modules.Actor, privateKey string) { privKey, pubKey, addr := GenerateNewKeysStrings() chains := DefaultChains - if actorType == int32(typesPersistence.ActorType_Val) { + if actorType == int32(typesPers.ActorType_Val) { chains = nil } else if actorType == int32(types.UtilActorType_App) { genericParam = DefaultMaxRelaysString From d532c30c87cc8a007134a988713c112681021e82 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 29 Sep 2022 19:08:45 +0100 Subject: [PATCH 51/90] feat(Tooling): check_cross_module_imports makefile target --- Makefile | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Makefile b/Makefile index 86b768812..1e74b826f 100644 --- a/Makefile +++ b/Makefile @@ -383,3 +383,24 @@ gen_genesis_and_config: ## Clear the genesis and config files for LocalNet clear_genesis_and_config: rm build/config/gen.*.json + +.PHONY: check_cross_module_imports +## Lists cross-module imports +check_cross_module_imports: + echo "persistence:\n" + grep --exclude-dir=persistence -r "github.com/pokt-network/pocket/persistence" + echo "-----------------------" + echo "utility:\n" + grep --exclude-dir=utility -r "github.com/pokt-network/pocket/utility" + echo "-----------------------" + echo "consensus:\n" + grep --exclude-dir=consensus -r "github.com/pokt-network/pocket/consensus" + echo "-----------------------" + echo "telemetry:\n" + grep --exclude-dir=telemetry -r "github.com/pokt-network/pocket/telemetry" + echo "-----------------------" + echo "p2p:\n" + grep --exclude-dir=p2p -r "github.com/pokt-network/pocket/p2p" + echo "-----------------------" + echo "runtime:\n" + grep --exclude-dir=runtime -r "github.com/pokt-network/pocket/runtime" From deaf36d67bd60d5055e276d94e919fb12b16e859 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 29 Sep 2022 19:10:00 +0100 Subject: [PATCH 52/90] style(Runtime): import names --- runtime/genesis.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/runtime/genesis.go b/runtime/genesis.go index 8616ec389..8627e0649 100644 --- a/runtime/genesis.go +++ b/runtime/genesis.go @@ -4,16 +4,16 @@ import ( "encoding/json" "os" - typesCons "github.com/pokt-network/pocket/consensus/types" - typesPers "github.com/pokt-network/pocket/persistence/types" + consTypes "github.com/pokt-network/pocket/consensus/types" + persTypes "github.com/pokt-network/pocket/persistence/types" "github.com/pokt-network/pocket/shared/modules" ) var _ modules.GenesisState = &runtimeGenesis{} type runtimeGenesis struct { - ConsensusGenesisState *typesCons.ConsensusGenesisState `json:"consensus_genesis_state"` - PersistenceGenesisState *typesPers.PersistenceGenesisState `json:"persistence_genesis_state"` + ConsensusGenesisState *consTypes.ConsensusGenesisState `json:"consensus_genesis_state"` + PersistenceGenesisState *persTypes.PersistenceGenesisState `json:"persistence_genesis_state"` } func NewGenesis( @@ -21,8 +21,8 @@ func NewGenesis( persistenceGenesisState modules.PersistenceGenesisState, ) *runtimeGenesis { return &runtimeGenesis{ - ConsensusGenesisState: consensusGenesisState.(*typesCons.ConsensusGenesisState), - PersistenceGenesisState: persistenceGenesisState.(*typesPers.PersistenceGenesisState), + ConsensusGenesisState: consensusGenesisState.(*consTypes.ConsensusGenesisState), + PersistenceGenesisState: persistenceGenesisState.(*persTypes.PersistenceGenesisState), } } From 704ff7123ec6cdcc7b396da99c733533d4b967cb Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Fri, 30 Sep 2022 10:47:07 +0100 Subject: [PATCH 53/90] refactor(Shared): refactored clock to be sourced from runtimeMgr --- app/client/main.go | 2 +- consensus/consensus_tests/hotstuff_test.go | 6 +-- consensus/consensus_tests/pacemaker_test.go | 10 ++--- consensus/consensus_tests/utils_test.go | 13 +++--- consensus/pacemaker.go | 2 +- runtime/manager.go | 48 ++++++++++++++++----- shared/bus.go | 19 ++++---- shared/modules/bus_module.go | 5 +-- shared/modules/runtime_module.go | 3 ++ shared/node.go | 28 ++++-------- 10 files changed, 78 insertions(+), 58 deletions(-) diff --git a/app/client/main.go b/app/client/main.go index cab34d78c..7be62aa11 100644 --- a/app/client/main.go +++ b/app/client/main.go @@ -72,7 +72,7 @@ func main() { } telemetryMod := telemetryM.(modules.TelemetryModule) - _ = shared.CreateBusWithOptionalModules(nil, p2pMod, nil, consensusMod, telemetryMod) + _ = shared.CreateBusWithOptionalModules(runtimeMgr, nil, p2pMod, nil, consensusMod, telemetryMod) p2pMod.Start() diff --git a/consensus/consensus_tests/hotstuff_test.go b/consensus/consensus_tests/hotstuff_test.go index 3e1045ffd..8ac97b4e6 100644 --- a/consensus/consensus_tests/hotstuff_test.go +++ b/consensus/consensus_tests/hotstuff_test.go @@ -13,16 +13,16 @@ import ( ) func TestHotstuff4Nodes1BlockHappyPath(t *testing.T) { + clockMock := clock.NewMock() // Test configs numNodes := 4 - runtimeMgrs := GenerateNodeRuntimeMgrs(t, numNodes) + runtimeMgrs := GenerateNodeRuntimeMgrs(t, numNodes, clockMock) - clockMock := clock.NewMock() go timeReminder(clockMock, 100*time.Millisecond) // Create & start test pocket nodes testChannel := make(modules.EventsChannel, 100) - pocketNodes := CreateTestConsensusPocketNodes(t, runtimeMgrs, clockMock, testChannel) + pocketNodes := CreateTestConsensusPocketNodes(t, runtimeMgrs, testChannel) StartAllTestPocketNodes(t, pocketNodes) // Debug message to start consensus by triggering first view change diff --git a/consensus/consensus_tests/pacemaker_test.go b/consensus/consensus_tests/pacemaker_test.go index 732c0c017..d846121aa 100644 --- a/consensus/consensus_tests/pacemaker_test.go +++ b/consensus/consensus_tests/pacemaker_test.go @@ -25,7 +25,7 @@ func TestTinyPacemakerTimeouts(t *testing.T) { numNodes := 4 paceMakerTimeoutMsec := uint64(50) // Set a very small pacemaker timeout paceMakerTimeout := 50 * time.Millisecond - runtimeMgrs := GenerateNodeRuntimeMgrs(t, numNodes) + runtimeMgrs := GenerateNodeRuntimeMgrs(t, numNodes, clockMock) for _, runtimeConfig := range runtimeMgrs { if consCfg, ok := runtimeConfig.GetConfig().GetConsensusConfig().(*typesCons.ConsensusConfig); ok { consCfg.GetPaceMakerConfig().SetTimeoutMsec(paceMakerTimeoutMsec) @@ -34,7 +34,7 @@ func TestTinyPacemakerTimeouts(t *testing.T) { // Create & start test pocket nodes testChannel := make(modules.EventsChannel, 100) - pocketNodes := CreateTestConsensusPocketNodes(t, runtimeMgrs, clockMock, testChannel) + pocketNodes := CreateTestConsensusPocketNodes(t, runtimeMgrs, testChannel) StartAllTestPocketNodes(t, pocketNodes) // Debug message to start consensus by triggering next view. @@ -125,15 +125,15 @@ func TestTinyPacemakerTimeouts(t *testing.T) { } func TestPacemakerCatchupSameStepDifferentRounds(t *testing.T) { + clockMock := clock.NewMock() numNodes := 4 - runtimeConfigs := GenerateNodeRuntimeMgrs(t, numNodes) + runtimeConfigs := GenerateNodeRuntimeMgrs(t, numNodes, clockMock) - clockMock := clock.NewMock() timeReminder(clockMock, 100*time.Millisecond) // Create & start test pocket nodes testChannel := make(modules.EventsChannel, 100) - pocketNodes := CreateTestConsensusPocketNodes(t, runtimeConfigs, clockMock, testChannel) + pocketNodes := CreateTestConsensusPocketNodes(t, runtimeConfigs, testChannel) StartAllTestPocketNodes(t, pocketNodes) // Starting point diff --git a/consensus/consensus_tests/utils_test.go b/consensus/consensus_tests/utils_test.go index a38f758ee..d7186ab6e 100644 --- a/consensus/consensus_tests/utils_test.go +++ b/consensus/consensus_tests/utils_test.go @@ -58,7 +58,7 @@ type IdToNodeMapping map[typesCons.NodeId]modules.NodeModule /*** Node Generation Helpers ***/ -func GenerateNodeRuntimeMgrs(_ *testing.T, validatorCount int) []runtime.Manager { +func GenerateNodeRuntimeMgrs(_ *testing.T, validatorCount int, clockMgr clock.Clock) []runtime.Manager { runtimeMgrs := make([]runtime.Manager, 0) var keys []string genesisState, keys := test_artifacts.NewGenesisState(validatorCount, 1, 1, 1) @@ -73,7 +73,7 @@ func GenerateNodeRuntimeMgrs(_ *testing.T, validatorCount int) []runtime.Manager DebugTimeBetweenStepsMsec: 0, }, })(config) - runtimeMgrs = append(runtimeMgrs, *runtime.NewManager(config, genesisState)) + runtimeMgrs = append(runtimeMgrs, *runtime.NewManager(config, genesisState, runtime.WithClock(clockMgr))) } return runtimeMgrs } @@ -81,7 +81,6 @@ func GenerateNodeRuntimeMgrs(_ *testing.T, validatorCount int) []runtime.Manager func CreateTestConsensusPocketNodes( t *testing.T, runtimeMgrs []runtime.Manager, - clock clock.Clock, testChannel modules.EventsChannel, ) (pocketNodes IdToNodeMapping) { pocketNodes = make(IdToNodeMapping, len(runtimeMgrs)) @@ -94,8 +93,8 @@ func CreateTestConsensusPocketNodes( require.NoError(t, err) return pk.Address().String() < pk2.Address().String() }) - for i, cfg := range configs { - pocketNode := CreateTestConsensusPocketNode(t, &runtimeConfig, clock, testChannel) + for i, runtimeMgr := range runtimeMgrs { + pocketNode := CreateTestConsensusPocketNode(t, &runtimeMgr, testChannel) // TODO(olshansky): Figure this part out. pocketNodes[typesCons.NodeId(i+1)] = pocketNode } @@ -131,7 +130,6 @@ func CreateTestConsensusPocketNode( t *testing.T, runtimeMgr *runtime.Manager, testChannel modules.EventsChannel, - clock clock.Clock, ) *shared.Node { //createTestingGenesisAndConfigFiles(t, cfg, genesisState) @@ -144,7 +142,7 @@ func CreateTestConsensusPocketNode( utilityMock := baseUtilityMock(t, testChannel) telemetryMock := baseTelemetryMock(t, testChannel) - bus, err := shared.CreateBus(persistenceMock, p2pMock, utilityMock, consensusMod.(modules.ConsensusModule), telemetryMock, clock) + bus, err := shared.CreateBus(runtimeMgr, persistenceMock, p2pMock, utilityMock, consensusMod.(modules.ConsensusModule), telemetryMock) require.NoError(t, err) pk, err := cryptoPocket.NewPrivateKey(runtimeMgr.GetConfig().GetBaseConfig().GetPrivateKey()) @@ -152,7 +150,6 @@ func CreateTestConsensusPocketNode( pocketNode := shared.NewNodeWithAddress(pk.Address()) - pocketNode.SetClock(clock) pocketNode.SetBus(bus) return pocketNode diff --git a/consensus/pacemaker.go b/consensus/pacemaker.go index f6ea26fde..1f159d1cb 100644 --- a/consensus/pacemaker.go +++ b/consensus/pacemaker.go @@ -182,7 +182,7 @@ func (p *paceMaker) RestartTimer() { stepTimeout := p.getStepTimeout(p.consensusMod.Round) - clock := p.bus.GetClock() + clock := p.bus.GetRuntimeMgr().GetClock() ctx, cancel := clock.WithTimeout(context.TODO(), stepTimeout) p.stepCancelFunc = cancel diff --git a/runtime/manager.go b/runtime/manager.go index 04943f24b..0cbcd9f50 100644 --- a/runtime/manager.go +++ b/runtime/manager.go @@ -8,6 +8,7 @@ import ( "path/filepath" "strings" + "github.com/benbjohnson/clock" "github.com/mitchellh/mapstructure" typesCons "github.com/pokt-network/pocket/consensus/types" typesP2P "github.com/pokt-network/pocket/p2p/types" @@ -21,23 +22,27 @@ var _ modules.RuntimeMgr = &Manager{} type Manager struct { config *runtimeConfig genesis *runtimeGenesis + + clock clock.Clock } func NewManagerFromFiles(configPath, genesisPath string, options ...func(*Manager)) *Manager { - rc := &Manager{} + mgr := &Manager{ + clock: clock.New(), + } - cfg, genesis, err := rc.init(configPath, genesisPath) + cfg, genesis, err := mgr.init(configPath, genesisPath) if err != nil { log.Fatalf("[ERROR] Failed to initialize runtime builder: %v", err) } - rc.config = cfg - rc.genesis = genesis + mgr.config = cfg + mgr.genesis = genesis for _, o := range options { - o(rc) + o(mgr) } - return rc + return mgr } func NewManagerFromReaders(configReader, genesisReader io.Reader, options ...func(*Manager)) *Manager { @@ -47,18 +52,31 @@ func NewManagerFromReaders(configReader, genesisReader io.Reader, options ...fun var genesis *runtimeGenesis parse(genesisReader, genesis) - rc := &Manager{ + mgr := &Manager{ config: cfg, genesis: genesis, + clock: clock.New(), + } + + for _, o := range options { + o(mgr) } - return rc + + return mgr } -func NewManager(config modules.Config, genesis modules.GenesisState) *Manager { - return &Manager{ +func NewManager(config modules.Config, genesis modules.GenesisState, options ...func(*Manager)) *Manager { + mgr := &Manager{ config: config.(*runtimeConfig), genesis: genesis.(*runtimeGenesis), + clock: clock.New(), + } + + for _, o := range options { + o(mgr) } + + return mgr } func (rc *Manager) init(configPath, genesisPath string) (config *runtimeConfig, genesis *runtimeGenesis, err error) { @@ -105,6 +123,10 @@ func (b *Manager) GetGenesis() modules.GenesisState { return b.genesis } +func (b *Manager) GetClock() clock.Clock { + return b.clock +} + type supportedStructs interface { *runtimeConfig | *runtimeGenesis } @@ -144,3 +166,9 @@ func WithPK(pk string) func(*Manager) { b.config.P2P.PrivateKey = pk } } + +func WithClock(clockMgr clock.Clock) func(*Manager) { + return func(b *Manager) { + b.clock = clockMgr + } +} diff --git a/shared/bus.go b/shared/bus.go index f6141811a..7a85e4c75 100644 --- a/shared/bus.go +++ b/shared/bus.go @@ -3,7 +3,6 @@ package shared import ( "log" - "github.com/benbjohnson/clock" "github.com/pokt-network/pocket/shared/debug" "github.com/pokt-network/pocket/shared/modules" ) @@ -23,7 +22,7 @@ type bus struct { consensus modules.ConsensusModule telemetry modules.TelemetryModule - clock clock.Clock + runtimeMgr modules.RuntimeMgr } const ( @@ -31,23 +30,23 @@ const ( ) func CreateBus( + runtimeMgr modules.RuntimeMgr, persistence modules.PersistenceModule, p2p modules.P2PModule, utility modules.UtilityModule, consensus modules.ConsensusModule, telemetry modules.TelemetryModule, - clock clock.Clock, ) (modules.Bus, error) { bus := &bus{ channel: make(modules.EventsChannel, DefaultPocketBusBufferSize), + runtimeMgr: runtimeMgr, + persistence: persistence, p2p: p2p, utility: utility, consensus: consensus, telemetry: telemetry, - - clock: clock, } modules := map[string]modules.Module{ @@ -81,6 +80,7 @@ func CreateBus( // Using `CreateBusWithOptionalModules`, we can create a bus with only pre2p and a NOOP telemetry module // so that we can the pre2p module without any issues. func CreateBusWithOptionalModules( + runtimeMgr modules.RuntimeMgr, persistence modules.PersistenceModule, p2p modules.P2PModule, utility modules.UtilityModule, @@ -88,7 +88,10 @@ func CreateBusWithOptionalModules( telemetry modules.TelemetryModule, ) modules.Bus { bus := &bus{ - channel: make(modules.EventsChannel, DefaultPocketBusBufferSize), + channel: make(modules.EventsChannel, DefaultPocketBusBufferSize), + + runtimeMgr: runtimeMgr, + persistence: persistence, p2p: p2p, utility: utility, @@ -144,6 +147,6 @@ func (m bus) GetTelemetryModule() modules.TelemetryModule { return m.telemetry } -func (m *bus) GetClock() clock.Clock { - return m.clock +func (m *bus) GetRuntimeMgr() modules.RuntimeMgr { + return m.runtimeMgr } diff --git a/shared/modules/bus_module.go b/shared/modules/bus_module.go index 107148e32..cf0e09e6c 100644 --- a/shared/modules/bus_module.go +++ b/shared/modules/bus_module.go @@ -3,7 +3,6 @@ package modules //go:generate mockgen -source=$GOFILE -destination=./mocks/bus_module_mock.go -aux_files=github.com/pokt-network/pocket/shared/modules=module.go import ( - "github.com/benbjohnson/clock" "github.com/pokt-network/pocket/shared/debug" ) @@ -25,6 +24,6 @@ type Bus interface { GetConsensusModule() ConsensusModule GetTelemetryModule() TelemetryModule - // Time - GetClock() clock.Clock + // Runtime + GetRuntimeMgr() RuntimeMgr } diff --git a/shared/modules/runtime_module.go b/shared/modules/runtime_module.go index 915e2c449..ef4ca8e9a 100644 --- a/shared/modules/runtime_module.go +++ b/shared/modules/runtime_module.go @@ -1,8 +1,11 @@ package modules +import "github.com/benbjohnson/clock" + //go:generate mockgen -source=$GOFILE -destination=./mocks/runtime_module_mock.go -aux_files=github.com/pokt-network/pocket/shared/modules=module.go type RuntimeMgr interface { GetConfig() Config GetGenesis() GenesisState + GetClock() clock.Clock } diff --git a/shared/node.go b/shared/node.go index 573a31e88..7d9172b0f 100644 --- a/shared/node.go +++ b/shared/node.go @@ -3,13 +3,11 @@ package shared import ( "log" - "github.com/pokt-network/pocket/runtime" - "github.com/pokt-network/pocket/shared/debug" - "github.com/pokt-network/pocket/telemetry" "github.com/benbjohnson/clock" "github.com/pokt-network/pocket/consensus" "github.com/pokt-network/pocket/p2p" "github.com/pokt-network/pocket/persistence" + "github.com/pokt-network/pocket/runtime" cryptoPocket "github.com/pokt-network/pocket/shared/crypto" "github.com/pokt-network/pocket/shared/debug" "github.com/pokt-network/pocket/shared/modules" @@ -34,43 +32,43 @@ func NewNodeWithAddress(address cryptoPocket.Address) *Node { return &Node{p2pAddress: address} } -func Create(configPath, genesisPath string) (modules.Module, error) { +func Create(configPath, genesisPath string, clock clock.Clock) (modules.Module, error) { return new(Node).Create(runtime.NewManagerFromFiles(configPath, genesisPath)) } -func (m *Node) Create(runtime modules.RuntimeMgr, clock clock.Clock) (modules.Module, error) { - persistenceMod, err := persistence.Create(runtime) +func (m *Node) Create(runtimeMgr modules.RuntimeMgr) (modules.Module, error) { + persistenceMod, err := persistence.Create(runtimeMgr) if err != nil { return nil, err } - p2pMod, err := p2p.Create(runtime) + p2pMod, err := p2p.Create(runtimeMgr) if err != nil { return nil, err } - utilityMod, err := utility.Create(runtime) + utilityMod, err := utility.Create(runtimeMgr) if err != nil { return nil, err } - consensusMod, err := consensus.Create(runtime) + consensusMod, err := consensus.Create(runtimeMgr) if err != nil { return nil, err } - telemetryMod, err := telemetry.Create(runtime) + telemetryMod, err := telemetry.Create(runtimeMgr) if err != nil { return nil, err } bus, err := CreateBus( + runtimeMgr, persistenceMod.(modules.PersistenceModule), p2pMod.(modules.P2PModule), utilityMod.(modules.UtilityModule), consensusMod.(modules.ConsensusModule), telemetryMod.(modules.TelemetryModule), - clock ) if err != nil { return nil, err @@ -186,11 +184,3 @@ func (node *Node) GetModuleName() string { func (node *Node) GetP2PAddress() cryptoPocket.Address { return node.p2pAddress } - -func (m *Node) SetClock(clock clock.Clock) { - m.clock = clock -} - -func (m *Node) GetClock() clock.Clock { - return m.clock -} From 8601df9efb153db2867eff5e56b9917b518d14c0 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Fri, 30 Sep 2022 10:50:33 +0100 Subject: [PATCH 54/90] fix(Makefile): excluding makefile! --- Makefile | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index fee01e518..d60faff12 100644 --- a/Makefile +++ b/Makefile @@ -397,19 +397,19 @@ clear_genesis_and_config: ## Lists cross-module imports check_cross_module_imports: echo "persistence:\n" - grep --exclude-dir=persistence -r "github.com/pokt-network/pocket/persistence" + grep --exclude=Makefile --exclude-dir=persistence -r "github.com/pokt-network/pocket/persistence" echo "-----------------------" echo "utility:\n" - grep --exclude-dir=utility -r "github.com/pokt-network/pocket/utility" + grep --exclude=Makefile --exclude-dir=utility -r "github.com/pokt-network/pocket/utility" echo "-----------------------" echo "consensus:\n" - grep --exclude-dir=consensus -r "github.com/pokt-network/pocket/consensus" + grep --exclude=Makefile --exclude-dir=consensus -r "github.com/pokt-network/pocket/consensus" echo "-----------------------" echo "telemetry:\n" - grep --exclude-dir=telemetry -r "github.com/pokt-network/pocket/telemetry" + grep --exclude=Makefile --exclude-dir=telemetry -r "github.com/pokt-network/pocket/telemetry" echo "-----------------------" echo "p2p:\n" - grep --exclude-dir=p2p -r "github.com/pokt-network/pocket/p2p" + grep --exclude=Makefile --exclude-dir=p2p -r "github.com/pokt-network/pocket/p2p" echo "-----------------------" echo "runtime:\n" - grep --exclude-dir=runtime -r "github.com/pokt-network/pocket/runtime" + grep --exclude=Makefile --exclude-dir=runtime -r "github.com/pokt-network/pocket/runtime" From 72f6c90272ac9f901450fb077e06020c04c6fd98 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Fri, 30 Sep 2022 11:06:26 +0100 Subject: [PATCH 55/90] feat(Tooling): improved check_cross_module_imports --- Makefile | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index d60faff12..f89b9d587 100644 --- a/Makefile +++ b/Makefile @@ -397,19 +397,19 @@ clear_genesis_and_config: ## Lists cross-module imports check_cross_module_imports: echo "persistence:\n" - grep --exclude=Makefile --exclude-dir=persistence -r "github.com/pokt-network/pocket/persistence" + grep --exclude=Makefile --exclude-dir=shared --exclude-dir=app --exclude-dir=runtime --exclude-dir=persistence -r "github.com/pokt-network/pocket/persistence" || echo "✅ OK!" echo "-----------------------" echo "utility:\n" - grep --exclude=Makefile --exclude-dir=utility -r "github.com/pokt-network/pocket/utility" + grep --exclude=Makefile --exclude-dir=shared --exclude-dir=app --exclude-dir=runtime --exclude-dir=utility -r "github.com/pokt-network/pocket/utility" || echo "✅ OK!" echo "-----------------------" echo "consensus:\n" - grep --exclude=Makefile --exclude-dir=consensus -r "github.com/pokt-network/pocket/consensus" + grep --exclude=Makefile --exclude-dir=shared --exclude-dir=app --exclude-dir=runtime --exclude-dir=consensus -r "github.com/pokt-network/pocket/consensus" || echo "✅ OK!" echo "-----------------------" echo "telemetry:\n" - grep --exclude=Makefile --exclude-dir=telemetry -r "github.com/pokt-network/pocket/telemetry" + grep --exclude=Makefile --exclude-dir=shared --exclude-dir=app --exclude-dir=runtime --exclude-dir=telemetry -r "github.com/pokt-network/pocket/telemetry" || echo "✅ OK!" echo "-----------------------" echo "p2p:\n" - grep --exclude=Makefile --exclude-dir=p2p -r "github.com/pokt-network/pocket/p2p" + grep --exclude=Makefile --exclude-dir=shared --exclude-dir=app --exclude-dir=runtime --exclude-dir=p2p -r "github.com/pokt-network/pocket/p2p" || echo "✅ OK!" echo "-----------------------" echo "runtime:\n" - grep --exclude=Makefile --exclude-dir=runtime -r "github.com/pokt-network/pocket/runtime" + grep --exclude=Makefile --exclude-dir=shared --exclude-dir=app --exclude-dir=runtime --exclude-dir=runtime -r "github.com/pokt-network/pocket/runtime" || echo "✅ OK!" From dee736c4e965bf08e434ca3136e8aa66317a1e0b Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Fri, 30 Sep 2022 12:52:11 +0100 Subject: [PATCH 56/90] docs(Shared): Updated README and CHANGELOGs --- consensus/CHANGELOG.md | 9 +++++++++ p2p/CHANGELOG.md | 8 ++++++++ persistence/docs/CHANGELOG.md | 9 +++++++++ runtime/docs/CHANGELOG.md | 17 ++++++++++++++++ runtime/docs/README.md | 38 +++++++++++++++++++++++++++++++++++ shared/CHANGELOG.md | 7 +++++++ shared/README.md | 7 ++++--- utility/doc/CHANGELOG.md | 8 ++++++++ 8 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 runtime/docs/CHANGELOG.md create mode 100644 runtime/docs/README.md diff --git a/consensus/CHANGELOG.md b/consensus/CHANGELOG.md index a41773b18..3631f62b0 100644 --- a/consensus/CHANGELOG.md +++ b/consensus/CHANGELOG.md @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.0.0.4] - 2022-09-30 + +### [#235](https://github.com/pokt-network/pocket/pull/235) Config and genesis handling + +- Updated to use `RuntimeMgr` +- Made `ConsensusModule` struct unexported +- Updated tests and mocks +- Removed some cross-module dependencies + ## [0.0.0.3] - 2022-09-28 - `consensusModule` stores block directly to prevent shared structure in the `utilityModule` diff --git a/p2p/CHANGELOG.md b/p2p/CHANGELOG.md index 98a388960..206b96c4a 100644 --- a/p2p/CHANGELOG.md +++ b/p2p/CHANGELOG.md @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.0.0.3] - 2022-09-30 + +### [#235](https://github.com/pokt-network/pocket/pull/235) Config and genesis handling + +- Updated to use `RuntimeMgr` +- Updated tests and mocks +- Removed some cross-module dependencies + ## [0.0.0.2] - 2022-08-25 **Encapsulate structures previously in shared [#163](github.com/pokt-network/pocket/issues/163)** - Ensured proto structures implement shared interfaces diff --git a/persistence/docs/CHANGELOG.md b/persistence/docs/CHANGELOG.md index 71205cef2..8ce12eed6 100644 --- a/persistence/docs/CHANGELOG.md +++ b/persistence/docs/CHANGELOG.md @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.0.0.7] - 2022-09-30 + +### [#235](https://github.com/pokt-network/pocket/pull/235) Config and genesis handling + +- Updated to use `RuntimeMgr` +- Made `PersistenceModule` struct unexported +- Updated tests and mocks +- Removed some cross-module dependencies + ## [0.0.0.6] - 2022-09-22 - Removed no-op `DeleteActor` code diff --git a/runtime/docs/CHANGELOG.md b/runtime/docs/CHANGELOG.md new file mode 100644 index 000000000..8c2668d19 --- /dev/null +++ b/runtime/docs/CHANGELOG.md @@ -0,0 +1,17 @@ +# Changelog + +All notable changes to this module will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +## [0.0.0] - 2022-09-30 + +### [#235](https://github.com/pokt-network/pocket/pull/235) Config and genesis handling + +- Abstracted config and genesis handling +- Mockable runtime +- Refactored all modules to use RuntimeMgr +- Updated RuntimeMgr to manage clock as well diff --git a/runtime/docs/README.md b/runtime/docs/README.md new file mode 100644 index 000000000..cc6cac0cc --- /dev/null +++ b/runtime/docs/README.md @@ -0,0 +1,38 @@ +# RuntimeMgr + +This document outlines the purpose of this module, its components and how they all interact with the other modules. + +## Contents + +- [Overview](#overview) +- [Components](#components) + +### Overview + +The `RuntimeMgr`'s purpose is to abstract the runtime so that it's easier to test and reason about various configuration scenarios. + +It works like a black-box that takes the current environment/machine and therefore the configuration files, flags supplied to the binary, etc. and returns a structure that can be queried for settings that are relevant for the functioning of the modules and the system as a whole. + +### Components + +This module includes the following components: + +- **Config** + + As the name says, it includes, in the form of properties, module specific configurations. + + It also has a `Base` configuration that is supposed to contain more cross-functional settings that cannot really find place in module-specific "subconfigs" (as another way to define module-specific configurations). + +- **Genesis** + + The genesis represents the initial state of the blockchain. + + This allows the binary to start with a specific initial state. + + Similarly to `Config`, these are scoped by module as well and currently we have `Persistence` and `Consensus` specific `GenesisState`s + +- **Clock** + + Clock is a drop-in replacement for some of the features offered by the `time` package, it acts as an injectable clock implementation used to provide time manipulation while testing. + + By default, the **real** clock is used and while testing it's possible to override it by using the "option" `WithClock(...)` diff --git a/shared/CHANGELOG.md b/shared/CHANGELOG.md index 758de960a..aa8ac350c 100644 --- a/shared/CHANGELOG.md +++ b/shared/CHANGELOG.md @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.0.2] - 2022-09-30 + +### [#235](https://github.com/pokt-network/pocket/pull/235) Config and genesis handling + +- Updated to use `RuntimeMgr`, available via `GetRuntimeMgr()` +- Segregate interfaces (eg: `GenesisDependentModule`, `P2PAddressableModule`, etc) +- Updated tests and mocks ## [0.0.1] - 2022-09-24 - Add unit test for `SharedCodec()` diff --git a/shared/README.md b/shared/README.md index 8fdefaf6b..5e38bf376 100644 --- a/shared/README.md +++ b/shared/README.md @@ -45,7 +45,8 @@ The key things to keep in mind are: - Receive asynchronous events from the **main events channel** - The **Persistence** module is the only module that communicates with the local database - The **P2P** module is the only one that communicates with the outside world -- **Clock** is a drop-in replacement for some of the features offered by the `time` package, it acts as an injectable clock implementation used to provide time manipulation while testing. +- **RuntimeMgr** abstracts the runtime and it's injected in the modules (more details [here](../runtime/docs/README.md)) + ```mermaid @@ -54,11 +55,11 @@ flowchart TD subgraph Pocket's Application Specific Bus B("Bus") E("Main Events Channel") - Clock("Clock") B <-.-> E - B <-.-> Clock end subgraph Pocket's Core Modules + RMGR(RuntimeMgr) + RMGR .- P(Persistence) & C(Consensus) & U(Utility) & P2P(P2P) end P <--> B diff --git a/utility/doc/CHANGELOG.md b/utility/doc/CHANGELOG.md index 3cc9ef57a..393fd9d81 100644 --- a/utility/doc/CHANGELOG.md +++ b/utility/doc/CHANGELOG.md @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.0.0.5] - 2022-09-30 + +### [#235](https://github.com/pokt-network/pocket/pull/235) Config and genesis handling + +- Updated to use `RuntimeMgr` +- Made `UtilityModule` struct unexported +- Updated tests and mocks +- Removed some cross-module dependencies ## [0.0.0.4] - 2022-09-21 From fa07890e1a15513b39e6a55eed4e50eefe7e2426 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Fri, 30 Sep 2022 12:54:20 +0100 Subject: [PATCH 57/90] docs(Runtime): missing items --- runtime/docs/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/runtime/docs/CHANGELOG.md b/runtime/docs/CHANGELOG.md index 8c2668d19..507ca9467 100644 --- a/runtime/docs/CHANGELOG.md +++ b/runtime/docs/CHANGELOG.md @@ -15,3 +15,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Mockable runtime - Refactored all modules to use RuntimeMgr - Updated RuntimeMgr to manage clock as well +- Modules now accept `interfaces` instead of paths. +- Unmarshalling is done in a new `runtime` package (runtime because what we do in there affects the runtime of the application) +- We are now able to accept configuration via environment variables (thanks to @okdas for inspiration and [sp13 for Viper]("github.com/spf13/viper")) From 1bfa97417131c847341a802b35aeea7e6182347f Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Mon, 3 Oct 2022 08:42:45 +0100 Subject: [PATCH 58/90] test(Persistence): Tests: added missing mock configuration Closes 262 --- consensus/consensus_tests/utils_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/consensus/consensus_tests/utils_test.go b/consensus/consensus_tests/utils_test.go index 8929dd07e..908271487 100644 --- a/consensus/consensus_tests/utils_test.go +++ b/consensus/consensus_tests/utils_test.go @@ -372,6 +372,8 @@ func baseUtilityMock(t *testing.T, _ modules.EventsChannel) *modulesMock.MockUti utilityContextMock.EXPECT().StoreBlock(gomock.Any()).AnyTimes().Return(nil) persistenceContextMock.EXPECT().Commit().Return(nil).AnyTimes() + persistenceContextMock.EXPECT().StoreBlock(gomock.Any()).AnyTimes().Return(nil) + persistenceContextMock.EXPECT().InsertBlock(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil) return utilityMock } From c04500a5932f3900ce2b8a1e7ce12f8c4658e1fe Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Mon, 3 Oct 2022 16:40:13 +0100 Subject: [PATCH 59/90] fix(Tooling): execute tests sequentially fix (wrong syntax) --- Makefile | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 4cdc29a03..a9e0f2738 100644 --- a/Makefile +++ b/Makefile @@ -240,17 +240,17 @@ protogen_docker: docker_check .PHONY: test_all ## Run all go unit tests test_all: # generate_mocks - go test -p=1 -count=1 ./... + go test -p 1 -count=1 ./... .PHONY: test_all_with_json ## Run all go unit tests, output results in json file test_all_with_json: # generate_mocks - go test -p=1 -json ./... > test_results.json + go test -p 1 -json ./... > test_results.json .PHONY: test_all_with_coverage ## Run all go unit tests, output results & coverage into files test_all_with_coverage: # generate_mocks - go test -p=1 -v ./... -covermode=count -coverprofile=coverage.out + go test -p 1 -v ./... -covermode=count -coverprofile=coverage.out go tool cover -func=coverage.out -o=coverage.out .PHONY: test_race @@ -261,7 +261,7 @@ test_race: # generate_mocks .PHONY: test_utility_module ## Run all go utility module unit tests test_utility_module: # generate_mocks - go test ${VERBOSE_TEST} -p=1 -count=1 ./shared/tests/utility_module/... + go test ${VERBOSE_TEST} -p 1 -count=1 ./shared/tests/utility_module/... .PHONY: test_utility_types ## Run all go utility types module unit tests @@ -271,7 +271,7 @@ test_utility_types: # generate_mocks .PHONY: test_shared ## Run all go unit tests in the shared module test_shared: # generate_mocks - go test ${VERBOSE_TEST} -p=1 ./shared/... + go test ${VERBOSE_TEST} -p 1 ./shared/... .PHONY: test_consensus ## Run all go unit tests in the Consensus module @@ -309,7 +309,7 @@ test_sortition: .PHONY: test_persistence ## Run all go unit tests in the Persistence module test_persistence: - go test ${VERBOSE_TEST} -p=1 -count=1 ./persistence/... + go test ${VERBOSE_TEST} -p 1 -count=1 ./persistence/... .PHONY: test_p2p_types ## Run p2p subcomponents' tests From 902c524a6a37b95b42c3bd14e46f1814307bc648 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 4 Oct 2022 22:10:35 +0100 Subject: [PATCH 60/90] style(Makefile): check_cross_module_imports --- Makefile | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 6a71e3d2a..d9b942642 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ EXTRA_MSG_FAIL ?= false # `VERBOSE_TEST="" make test_persistence` is an easy way to run the same tests without verbose output VERBOSE_TEST ?= -v -.SILENT: +# .SILENT: help: printf "Available targets\n\n" @@ -396,20 +396,21 @@ clear_genesis_and_config: .PHONY: check_cross_module_imports ## Lists cross-module imports check_cross_module_imports: + $(eval exclude_common=--exclude=Makefile --exclude-dir=shared --exclude-dir=app --exclude-dir=runtime) echo "persistence:\n" - grep --exclude=Makefile --exclude-dir=shared --exclude-dir=app --exclude-dir=runtime --exclude-dir=persistence -r "github.com/pokt-network/pocket/persistence" || echo "✅ OK!" + grep ${exclude_common} --exclude-dir=persistence -r "github.com/pokt-network/pocket/persistence" || echo "✅ OK!" echo "-----------------------" echo "utility:\n" - grep --exclude=Makefile --exclude-dir=shared --exclude-dir=app --exclude-dir=runtime --exclude-dir=utility -r "github.com/pokt-network/pocket/utility" || echo "✅ OK!" + grep ${exclude_common} --exclude-dir=utility -r "github.com/pokt-network/pocket/utility" || echo "✅ OK!" echo "-----------------------" echo "consensus:\n" - grep --exclude=Makefile --exclude-dir=shared --exclude-dir=app --exclude-dir=runtime --exclude-dir=consensus -r "github.com/pokt-network/pocket/consensus" || echo "✅ OK!" + grep ${exclude_common} --exclude-dir=consensus -r "github.com/pokt-network/pocket/consensus" || echo "✅ OK!" echo "-----------------------" echo "telemetry:\n" - grep --exclude=Makefile --exclude-dir=shared --exclude-dir=app --exclude-dir=runtime --exclude-dir=telemetry -r "github.com/pokt-network/pocket/telemetry" || echo "✅ OK!" + grep ${exclude_common} --exclude-dir=telemetry -r "github.com/pokt-network/pocket/telemetry" || echo "✅ OK!" echo "-----------------------" echo "p2p:\n" - grep --exclude=Makefile --exclude-dir=shared --exclude-dir=app --exclude-dir=runtime --exclude-dir=p2p -r "github.com/pokt-network/pocket/p2p" || echo "✅ OK!" + grep ${exclude_common} --exclude-dir=p2p -r "github.com/pokt-network/pocket/p2p" || echo "✅ OK!" echo "-----------------------" echo "runtime:\n" - grep --exclude=Makefile --exclude-dir=shared --exclude-dir=app --exclude-dir=runtime --exclude-dir=runtime -r "github.com/pokt-network/pocket/runtime" || echo "✅ OK!" + grep ${exclude_common} --exclude-dir=runtime -r "github.com/pokt-network/pocket/runtime" || echo "✅ OK!" From 998538158ad432d6d096bf623f482fece0fcdf9e Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 4 Oct 2022 22:11:08 +0100 Subject: [PATCH 61/90] Update app/client/main.go Co-authored-by: Daniel Olshansky --- app/client/main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/app/client/main.go b/app/client/main.go index 7be62aa11..eb305535d 100644 --- a/app/client/main.go +++ b/app/client/main.go @@ -62,6 +62,7 @@ func main() { log.Fatalf("[ERROR] Failed to create p2p module: %v", err.Error()) } p2pMod = p2pM.(modules.P2PModule) + // This telemetry module instance is a NOOP because the 'enable_telemetry' flag in the `cfg` above is set to false. // Since this client mimics partial - networking only - functionality of a full node, some of the telemetry-related // code paths are executed. To avoid those messages interfering with the telemetry data collected, a non-nil telemetry From bfea53de035a422646b374a4b6380ca5793c9e41 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 4 Oct 2022 22:11:56 +0100 Subject: [PATCH 62/90] refactor(Consensus): keys -> validatorKeys --- consensus/consensus_tests/utils_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/consensus/consensus_tests/utils_test.go b/consensus/consensus_tests/utils_test.go index f9b90e97b..98f1c7ee0 100644 --- a/consensus/consensus_tests/utils_test.go +++ b/consensus/consensus_tests/utils_test.go @@ -60,9 +60,9 @@ type IdToNodeMapping map[typesCons.NodeId]modules.NodeModule func GenerateNodeRuntimeMgrs(_ *testing.T, validatorCount int, clockMgr clock.Clock) []runtime.Manager { runtimeMgrs := make([]runtime.Manager, 0) - var keys []string - genesisState, keys := test_artifacts.NewGenesisState(validatorCount, 1, 1, 1) - configs := test_artifacts.NewDefaultConfigs(keys) + var validatorKeys []string + genesisState, validatorKeys := test_artifacts.NewGenesisState(validatorCount, 1, 1, 1) + configs := test_artifacts.NewDefaultConfigs(validatorKeys) for _, config := range configs { runtime.WithConsensusConfig(&typesCons.ConsensusConfig{ PrivateKey: config.GetBaseConfig().GetPrivateKey(), From d14aedb0420799d52a0703329d9f2729035ee4c4 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 4 Oct 2022 22:12:04 +0100 Subject: [PATCH 63/90] Update consensus/consensus_tests/utils_test.go Co-authored-by: Daniel Olshansky --- consensus/consensus_tests/utils_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/consensus/consensus_tests/utils_test.go b/consensus/consensus_tests/utils_test.go index f9b90e97b..9861a13e6 100644 --- a/consensus/consensus_tests/utils_test.go +++ b/consensus/consensus_tests/utils_test.go @@ -110,7 +110,6 @@ func CreateTestConsensusPocketNodesNew( // TODO(design): The order here is important in order for NodeId to be set correctly below. // This logic will need to change once proper leader election is implemented. sort.Slice(runtimeMgrs, func(i, j int) bool { - pk, err := cryptoPocket.NewPrivateKey(runtimeMgrs[i].GetConfig().GetBaseConfig().GetPrivateKey()) require.NoError(t, err) pk2, err := cryptoPocket.NewPrivateKey(runtimeMgrs[j].GetConfig().GetBaseConfig().GetPrivateKey()) From 59df16746a1cd56f80ebbee21f71ea78e985e723 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 4 Oct 2022 22:12:57 +0100 Subject: [PATCH 64/90] Update consensus/consensus_tests/utils_test.go Co-authored-by: Daniel Olshansky --- consensus/consensus_tests/utils_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/consensus/consensus_tests/utils_test.go b/consensus/consensus_tests/utils_test.go index a4fb55957..c9a340bf3 100644 --- a/consensus/consensus_tests/utils_test.go +++ b/consensus/consensus_tests/utils_test.go @@ -130,8 +130,6 @@ func CreateTestConsensusPocketNode( runtimeMgr *runtime.Manager, testChannel modules.EventsChannel, ) *shared.Node { - //createTestingGenesisAndConfigFiles(t, cfg, genesisState) - consensusMod, err := consensus.Create(runtimeMgr) require.NoError(t, err) // TODO(olshansky): At the moment we are using the same base mocks for all the tests, From 50090d14bb330de12fff505ae4ec5c09e69115a8 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 4 Oct 2022 22:13:39 +0100 Subject: [PATCH 65/90] refactor(Consensu): NewNodeWithAddress -> NewNodeWithP2PAddress --- consensus/consensus_tests/utils_test.go | 2 +- shared/node.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/consensus/consensus_tests/utils_test.go b/consensus/consensus_tests/utils_test.go index a4fb55957..a1c5868e2 100644 --- a/consensus/consensus_tests/utils_test.go +++ b/consensus/consensus_tests/utils_test.go @@ -147,7 +147,7 @@ func CreateTestConsensusPocketNode( pk, err := cryptoPocket.NewPrivateKey(runtimeMgr.GetConfig().GetBaseConfig().GetPrivateKey()) require.NoError(t, err) - pocketNode := shared.NewNodeWithAddress(pk.Address()) + pocketNode := shared.NewNodeWithP2PAddress(pk.Address()) pocketNode.SetBus(bus) diff --git a/shared/node.go b/shared/node.go index 7d9172b0f..4222c4b51 100644 --- a/shared/node.go +++ b/shared/node.go @@ -28,7 +28,7 @@ type Node struct { p2pAddress cryptoPocket.Address } -func NewNodeWithAddress(address cryptoPocket.Address) *Node { +func NewNodeWithP2PAddress(address cryptoPocket.Address) *Node { return &Node{p2pAddress: address} } From fe3a4c7caf4fc176fce45dee3aacb74faed0fd22 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 4 Oct 2022 22:21:03 +0100 Subject: [PATCH 66/90] fix(Makefile): revert silent --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index d9b942642..abe3e3561 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ EXTRA_MSG_FAIL ?= false # `VERBOSE_TEST="" make test_persistence` is an easy way to run the same tests without verbose output VERBOSE_TEST ?= -v -# .SILENT: +.SILENT: help: printf "Available targets\n\n" From eff5c0ce85a4186e2dee5b024eb0e8f4b832dfca Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 4 Oct 2022 22:21:39 +0100 Subject: [PATCH 67/90] style(Shared): init var before return --- shared/test_artifacts/generator.go | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/shared/test_artifacts/generator.go b/shared/test_artifacts/generator.go index 9fb404a7b..44fb6a105 100644 --- a/shared/test_artifacts/generator.go +++ b/shared/test_artifacts/generator.go @@ -38,28 +38,31 @@ var ( // TODO (Team) this is meant to be a **temporary** replacement for the recently deprecated // 'genesis config' option. We need to implement a real suite soon! -func NewGenesisState(numValidators, numServiceNodes, numApplications, numFisherman int) (genesisState modules.GenesisState, validatorPrivateKeys []string) { +func NewGenesisState(numValidators, numServiceNodes, numApplications, numFisherman int) (modules.GenesisState, []string) { apps, appsPrivateKeys := NewActors(types.UtilActorType_App, numApplications) vals, validatorPrivateKeys := NewActors(types.UtilActorType_Val, numValidators) serviceNodes, snPrivateKeys := NewActors(types.UtilActorType_Node, numServiceNodes) fish, fishPrivateKeys := NewActors(types.UtilActorType_Fish, numFisherman) - return runtime.NewGenesis(&typesCons.ConsensusGenesisState{ + genesisState := runtime.NewGenesis( + &typesCons.ConsensusGenesisState{ GenesisTime: timestamppb.Now(), ChainId: DefaultChainID, MaxBlockBytes: DefaultMaxBlockBytes, Validators: typesCons.ToConsensusValidators(vals), }, - &typesPers.PersistenceGenesisState{ - Pools: typesPers.ToPersistenceAccounts(NewPools()), - Accounts: typesPers.ToPersistenceAccounts(NewAccounts(numValidators+numServiceNodes+numApplications+numFisherman, append(append(append(validatorPrivateKeys, snPrivateKeys...), fishPrivateKeys...), appsPrivateKeys...)...)), // TODO(olshansky): clean this up - Applications: typesPers.ToPersistenceActors(apps), - Validators: typesPers.ToPersistenceActors(vals), - ServiceNodes: typesPers.ToPersistenceActors(serviceNodes), - Fishermen: typesPers.ToPersistenceActors(fish), - Params: typesPers.ToPersistenceParams(DefaultParams()), - }), - validatorPrivateKeys + &typesPers.PersistenceGenesisState{ + Pools: typesPers.ToPersistenceAccounts(NewPools()), + Accounts: typesPers.ToPersistenceAccounts(NewAccounts(numValidators+numServiceNodes+numApplications+numFisherman, append(append(append(validatorPrivateKeys, snPrivateKeys...), fishPrivateKeys...), appsPrivateKeys...)...)), // TODO(olshansky): clean this up + Applications: typesPers.ToPersistenceActors(apps), + Validators: typesPers.ToPersistenceActors(vals), + ServiceNodes: typesPers.ToPersistenceActors(serviceNodes), + Fishermen: typesPers.ToPersistenceActors(fish), + Params: typesPers.ToPersistenceParams(DefaultParams()), + }, + ) + + return genesisState, validatorPrivateKeys } func NewDefaultConfigs(privateKeys []string) (configs []modules.Config) { From 8182b82c1c38f326f606fc971df601bf37ce26ca Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 4 Oct 2022 22:21:47 +0100 Subject: [PATCH 68/90] Update shared/test_artifacts/generator.go Co-authored-by: Daniel Olshansky --- shared/test_artifacts/generator.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shared/test_artifacts/generator.go b/shared/test_artifacts/generator.go index 9fb404a7b..bf8653c22 100644 --- a/shared/test_artifacts/generator.go +++ b/shared/test_artifacts/generator.go @@ -1,5 +1,7 @@ package test_artifacts +// TODO: Move `root/shared/test_artifacts` to `root/test/test_artifacts` +// Cross module imports are okay because this is only used for testing and not business logic import ( "fmt" "math/big" From ae97b0383c10618924ffd51d6ee1175bc72b23e4 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 4 Oct 2022 22:22:02 +0100 Subject: [PATCH 69/90] Update utility/test/module_test.go Co-authored-by: Daniel Olshansky --- utility/test/module_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/utility/test/module_test.go b/utility/test/module_test.go index 692bb7de4..620fbf9d7 100644 --- a/utility/test/module_test.go +++ b/utility/test/module_test.go @@ -87,5 +87,6 @@ func newTestPersistenceModule(t *testing.T, databaseUrl string) modules.Persiste err = persistenceMod.Start() require.NoError(t, err) + return persistenceMod.(modules.PersistenceModule) } From 6f25100b0ea80d3c567e21419cbd5fa756a6ec09 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 4 Oct 2022 22:23:59 +0100 Subject: [PATCH 70/90] Update consensus/module.go Co-authored-by: Daniel Olshansky --- consensus/module.go | 1 - 1 file changed, 1 deletion(-) diff --git a/consensus/module.go b/consensus/module.go index 12ed96883..f6a41c6df 100644 --- a/consensus/module.go +++ b/consensus/module.go @@ -27,7 +27,6 @@ var ( ) // TODO(olshansky): Any reason to make all of these attributes local only (i.e. not exposed outside the struct)? -// TODO(olshansky): Look for a way to not externalize the `consensusModule` struct type consensusModule struct { bus modules.Bus privateKey cryptoPocket.Ed25519PrivateKey From 8ea935535643e34b216280ea57464a4216c35997 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 4 Oct 2022 22:27:22 +0100 Subject: [PATCH 71/90] Update consensus/types/converters.go Co-authored-by: Daniel Olshansky --- consensus/types/converters.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/consensus/types/converters.go b/consensus/types/converters.go index b5c950a56..05e0f3b16 100644 --- a/consensus/types/converters.go +++ b/consensus/types/converters.go @@ -12,9 +12,9 @@ func actorToValidator(actor modules.Actor) *Validator { } func ToConsensusValidators(actors []modules.Actor) []*Validator { - r := make([]*Validator, 0) - for _, a := range actors { - r = append(r, actorToValidator(a)) + vals = make([]*Validator, len(actors)) + for i, actor := range actors { + vals[i] = actor } - return r + return } From 5657b41fdf73a69e7eca6949a5e5a8c91b6745c2 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 4 Oct 2022 22:30:58 +0100 Subject: [PATCH 72/90] refactor(Consensus): s/ActorListToMap/ActorListToValidatorMap --- consensus/module.go | 2 +- consensus/types/types.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/consensus/module.go b/consensus/module.go index f6a41c6df..2a90dcb12 100644 --- a/consensus/module.go +++ b/consensus/module.go @@ -100,7 +100,7 @@ func (*consensusModule) Create(runtimeMgr modules.RuntimeMgr) (modules.Module, e return nil, err } - valMap := typesCons.ActorListToMap(consensusGenesis.GetVals()) + valMap := typesCons.ActorListToValidatorMap(consensusGenesis.GetVals()) privateKey, err := cryptoPocket.NewPrivateKey(consensusCfg.GetPrivateKey()) if err != nil { diff --git a/consensus/types/types.go b/consensus/types/types.go index 909b2ece9..5d9c47c75 100644 --- a/consensus/types/types.go +++ b/consensus/types/types.go @@ -56,7 +56,7 @@ func ValidatorMapToModulesValidatorMap(validatorMap ValidatorMap) (vm modules.Va return } -func ActorListToMap(actors []modules.Actor) (m ValidatorMap) { +func ActorListToValidatorMap(actors []modules.Actor) (m ValidatorMap) { m = make(ValidatorMap, len(actors)) for _, v := range actors { m[v.GetAddress()] = v From 0aabc23aeed9cb23d48fac910b722bdc4721f7cc Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 4 Oct 2022 22:31:31 +0100 Subject: [PATCH 73/90] Update consensus/types/converters.go Co-authored-by: Daniel Olshansky --- consensus/types/converters.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/consensus/types/converters.go b/consensus/types/converters.go index 05e0f3b16..de7704ef8 100644 --- a/consensus/types/converters.go +++ b/consensus/types/converters.go @@ -11,7 +11,7 @@ func actorToValidator(actor modules.Actor) *Validator { } } -func ToConsensusValidators(actors []modules.Actor) []*Validator { +func ToConsensusValidators(actors []modules.Actor) (vals []*Validator) { vals = make([]*Validator, len(actors)) for i, actor := range actors { vals[i] = actor From f7ffe6ec9668f79538a49a75c51c4be374f4d622 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 4 Oct 2022 22:37:08 +0100 Subject: [PATCH 74/90] fix(Consensus): converters fix --- consensus/types/converters.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/consensus/types/converters.go b/consensus/types/converters.go index de7704ef8..f11d42bc2 100644 --- a/consensus/types/converters.go +++ b/consensus/types/converters.go @@ -1,6 +1,8 @@ package types -import "github.com/pokt-network/pocket/shared/modules" +import ( + "github.com/pokt-network/pocket/shared/modules" +) func actorToValidator(actor modules.Actor) *Validator { return &Validator{ @@ -14,7 +16,7 @@ func actorToValidator(actor modules.Actor) *Validator { func ToConsensusValidators(actors []modules.Actor) (vals []*Validator) { vals = make([]*Validator, len(actors)) for i, actor := range actors { - vals[i] = actor + vals[i] = actorToValidator(actor) } return } From 25a8607f85dd4c6274be2ae3ff829d99ad9296ab Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 4 Oct 2022 22:50:37 +0100 Subject: [PATCH 75/90] refactor(Shared): preallocations --- consensus/consensus_tests/utils_test.go | 6 +++--- consensus/types/consensus_genesis.go | 7 ++++--- persistence/types/converters.go | 16 ++++++++-------- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/consensus/consensus_tests/utils_test.go b/consensus/consensus_tests/utils_test.go index e65a5c811..324e4c2e1 100644 --- a/consensus/consensus_tests/utils_test.go +++ b/consensus/consensus_tests/utils_test.go @@ -59,11 +59,11 @@ type IdToNodeMapping map[typesCons.NodeId]modules.NodeModule /*** Node Generation Helpers ***/ func GenerateNodeRuntimeMgrs(_ *testing.T, validatorCount int, clockMgr clock.Clock) []runtime.Manager { - runtimeMgrs := make([]runtime.Manager, 0) + runtimeMgrs := make([]runtime.Manager, validatorCount) var validatorKeys []string genesisState, validatorKeys := test_artifacts.NewGenesisState(validatorCount, 1, 1, 1) configs := test_artifacts.NewDefaultConfigs(validatorKeys) - for _, config := range configs { + for i, config := range configs { runtime.WithConsensusConfig(&typesCons.ConsensusConfig{ PrivateKey: config.GetBaseConfig().GetPrivateKey(), MaxMempoolBytes: 500000000, @@ -73,7 +73,7 @@ func GenerateNodeRuntimeMgrs(_ *testing.T, validatorCount int, clockMgr clock.Cl DebugTimeBetweenStepsMsec: 0, }, })(config) - runtimeMgrs = append(runtimeMgrs, *runtime.NewManager(config, genesisState, runtime.WithClock(clockMgr))) + runtimeMgrs[i] = *runtime.NewManager(config, genesisState, runtime.WithClock(clockMgr)) } return runtimeMgrs } diff --git a/consensus/types/consensus_genesis.go b/consensus/types/consensus_genesis.go index 24c005499..4ec59e476 100644 --- a/consensus/types/consensus_genesis.go +++ b/consensus/types/consensus_genesis.go @@ -8,9 +8,10 @@ func (x *ConsensusGenesisState) GetVals() []modules.Actor { return ActorsToActorsInterface(x.GetValidators()) } -func ActorsToActorsInterface(a []*Validator) (actorI []modules.Actor) { - for _, actor := range a { - actorI = append(actorI, actor) +func ActorsToActorsInterface(vals []*Validator) (actorI []modules.Actor) { + actorI = make([]modules.Actor, len(vals)) + for i, actor := range vals { + actorI[i] = actor } return } diff --git a/persistence/types/converters.go b/persistence/types/converters.go index 33fae5f48..ab5ccfdc2 100644 --- a/persistence/types/converters.go +++ b/persistence/types/converters.go @@ -14,11 +14,11 @@ func toPersistenceActor(actor modules.Actor) *Actor { } func ToPersistenceActors(actors []modules.Actor) []*Actor { - r := make([]*Actor, 0) - for _, a := range actors { - r = append(r, toPersistenceActor(a)) + perActors := make([]*Actor, len(actors)) + for i, actor := range actors { + perActors[i] = toPersistenceActor(actor) } - return r + return perActors } func toPersistenceAccount(account modules.Account) *Account { @@ -29,11 +29,11 @@ func toPersistenceAccount(account modules.Account) *Account { } func ToPersistenceAccounts(accounts []modules.Account) []*Account { - r := make([]*Account, 0) - for _, a := range accounts { - r = append(r, toPersistenceAccount(a)) + perAccounts := make([]*Account, len(accounts)) + for i, account := range accounts { + perAccounts[i] = toPersistenceAccount(account) } - return r + return perAccounts } func ToPersistenceParams(params modules.Params) *Params { From fb07ed3e107f7358293665dfcf7962db1eeb0c68 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 4 Oct 2022 23:01:08 +0100 Subject: [PATCH 76/90] docs(Shared): nits --- runtime/docs/CHANGELOG.md | 4 ++-- shared/README.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/runtime/docs/CHANGELOG.md b/runtime/docs/CHANGELOG.md index 507ca9467..16725f07d 100644 --- a/runtime/docs/CHANGELOG.md +++ b/runtime/docs/CHANGELOG.md @@ -13,8 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Abstracted config and genesis handling - Mockable runtime -- Refactored all modules to use RuntimeMgr -- Updated RuntimeMgr to manage clock as well +- Refactored all modules to use `RuntimeMgr` +- Updated `RuntimeMgr` to manage clock as well - Modules now accept `interfaces` instead of paths. - Unmarshalling is done in a new `runtime` package (runtime because what we do in there affects the runtime of the application) - We are now able to accept configuration via environment variables (thanks to @okdas for inspiration and [sp13 for Viper]("github.com/spf13/viper")) diff --git a/shared/README.md b/shared/README.md index 5e38bf376..dbe312a6e 100644 --- a/shared/README.md +++ b/shared/README.md @@ -45,7 +45,7 @@ The key things to keep in mind are: - Receive asynchronous events from the **main events channel** - The **Persistence** module is the only module that communicates with the local database - The **P2P** module is the only one that communicates with the outside world -- **RuntimeMgr** abstracts the runtime and it's injected in the modules (more details [here](../runtime/docs/README.md)) +- **RuntimeMgr** abstracts the runtime and is injected in the modules (more details [here](../runtime/docs/README.md)) From f9f3c1ce3df4f62f0e63ffd87a345aaca5c30c0d Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 4 Oct 2022 23:03:30 +0100 Subject: [PATCH 77/90] style(Persistence): cleanup --- persistence/test/setup_test.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/persistence/test/setup_test.go b/persistence/test/setup_test.go index 19492f970..abb873150 100644 --- a/persistence/test/setup_test.go +++ b/persistence/test/setup_test.go @@ -97,13 +97,6 @@ func NewFuzzTestPostgresContext(f *testing.F, height int64) *persistence.Postgre // TODO(andrew): Take in `t testing.T` as a parameter and error if there's an issue func newTestPersistenceModule(databaseUrl string) modules.PersistenceModule { - // cfg := runtime.NewRuntimeConfig{nil, nil, nil, - // &types.PersistenceConfig{ - // PostgresUrl: databaseUrl, - // NodeSchema: testSchema, - // BlockStorePath: "", - // },nil, nil) - cfg := runtime.NewConfig(&runtime.BaseConfig{}, runtime.WithPersistenceConfig(&types.PersistenceConfig{ PostgresUrl: databaseUrl, NodeSchema: testSchema, From 2c078af0b02364de2f97f9fdf6ae15f47f8e7996 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 4 Oct 2022 23:07:04 +0100 Subject: [PATCH 78/90] Update shared/modules/module.go Co-authored-by: Daniel Olshansky --- shared/modules/module.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/shared/modules/module.go b/shared/modules/module.go index 9ffd64755..708fc1e3a 100644 --- a/shared/modules/module.go +++ b/shared/modules/module.go @@ -5,8 +5,7 @@ import ( cryptoPocket "github.com/pokt-network/pocket/shared/crypto" ) -// TODO(olshansky): Show an example of `TypicalUsage` -// TODO(drewsky): Add `Create` function; pocket/issues/163 +// TODO: Show an example of `TypicalUsage` // TODO(drewsky): Do not embed this inside of modules but force it via an implicit cast at compile time type Module interface { InitializableModule From 30af85d12e41617f0c08366856de409270f588a1 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 4 Oct 2022 23:09:52 +0100 Subject: [PATCH 79/90] fix(Telemetry): error management --- telemetry/prometheus_module.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/telemetry/prometheus_module.go b/telemetry/prometheus_module.go index 329681500..4a15fb7fe 100644 --- a/telemetry/prometheus_module.go +++ b/telemetry/prometheus_module.go @@ -1,6 +1,7 @@ package telemetry import ( + "fmt" "log" "net/http" @@ -41,7 +42,7 @@ func CreatePrometheusTelemetryModule(runtime modules.RuntimeMgr) (modules.Module func (m *PrometheusTelemetryModule) Create(runtime modules.RuntimeMgr) (modules.Module, error) { cfg := runtime.GetConfig() if err := m.ValidateConfig(cfg); err != nil { - log.Fatalf("config validation failed: %v", err) + return nil, fmt.Errorf("config validation failed: %w", err) } telemetryCfg := cfg.GetTelemetryConfig().(*TelemetryConfig) From 05762ea64f3430134ce9b30f86a3cad144d9e2b9 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Tue, 4 Oct 2022 23:10:58 +0100 Subject: [PATCH 80/90] Update shared/modules/module.go Co-authored-by: Daniel Olshansky --- shared/modules/module.go | 1 - 1 file changed, 1 deletion(-) diff --git a/shared/modules/module.go b/shared/modules/module.go index 708fc1e3a..a27459fb5 100644 --- a/shared/modules/module.go +++ b/shared/modules/module.go @@ -6,7 +6,6 @@ import ( ) // TODO: Show an example of `TypicalUsage` -// TODO(drewsky): Do not embed this inside of modules but force it via an implicit cast at compile time type Module interface { InitializableModule IntegratableModule From 8061a4ce24c7873bd020c34d72e5197a3c56b17b Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 6 Oct 2022 16:01:51 +0100 Subject: [PATCH 81/90] style(P2P): renamed p2pCfg --- p2p/module.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/p2p/module.go b/p2p/module.go index 258f4d554..c13ad15aa 100644 --- a/p2p/module.go +++ b/p2p/module.go @@ -26,8 +26,8 @@ const ( ) type p2pModule struct { - bus modules.Bus - p2pConfig *typesP2P.P2PConfig // TODO (olshansky): to remove this since it'll be available via the bus + bus modules.Bus + p2pCfg *typesP2P.P2PConfig // TODO (olshansky): to remove this since it'll be available via the bus listener typesP2P.Transport address cryptoPocket.Address @@ -63,7 +63,7 @@ func (*p2pModule) Create(runtimeMgr modules.RuntimeMgr) (modules.Module, error) return nil, err } m = &p2pModule{ - p2pConfig: p2pCfg, + p2pCfg: p2pCfg, listener: l, address: privateKey.Address(), @@ -100,12 +100,12 @@ func (m *p2pModule) Start() error { telemetry.P2P_NODE_STARTED_TIMESERIES_METRIC_DESCRIPTION, ) - addrBook, err := ValidatorMapToAddrBook(m.p2pConfig, m.bus.GetConsensusModule().ValidatorMap()) + addrBook, err := ValidatorMapToAddrBook(m.p2pCfg, m.bus.GetConsensusModule().ValidatorMap()) if err != nil { return err } - if m.p2pConfig.UseRainTree { + if m.p2pCfg.UseRainTree { m.network = raintree.NewRainTreeNetwork(m.address, addrBook) } else { m.network = stdnetwork.NewNetwork(addrBook) From 85bb1cf3f0a5c2595167dc49fcef84aca9b54e9b Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Wed, 12 Oct 2022 23:11:54 +0100 Subject: [PATCH 82/90] fix(Tooling): reverted a line change introduced in #282 It was causing test failures because of inconsistent Postgresql state --- shared/test_artifacts/util.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/shared/test_artifacts/util.go b/shared/test_artifacts/util.go index 8b47b2daf..ac7cfd8fb 100644 --- a/shared/test_artifacts/util.go +++ b/shared/test_artifacts/util.go @@ -25,7 +25,8 @@ const ( ) // DISCUSS(team) both the persistence module and the utility module share this code which is less than ideal -// (see call to action in generator.go to try to remove the cross module testing code) +// +// (see call to action in generator.go to try to remove the cross module testing code) func SetupPostgresDocker() (*dockertest.Pool, *dockertest.Resource, string) { opts := dockertest.RunOptions{ Repository: "postgres", @@ -92,6 +93,7 @@ func CleanupPostgresDocker(_ *testing.M, pool *dockertest.Pool, resource *docker if err := pool.Purge(resource); err != nil { log.Fatalf("could not purge resource: %s", err) } + os.Exit(0) } // TODO(drewsky): Remove this in favor of a golang specific solution From 7508fd2748955bcc1376354e95643ecbf3f43531 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Wed, 12 Oct 2022 23:18:16 +0100 Subject: [PATCH 83/90] refactor(Shared): modules use interfaces for config and genesis --- consensus/block.go | 4 ++-- consensus/module.go | 4 ++-- p2p/module.go | 4 ++-- telemetry/prometheus_module.go | 8 ++++---- utility/module.go | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/consensus/block.go b/consensus/block.go index bf2e083e4..878cc1fe7 100644 --- a/consensus/block.go +++ b/consensus/block.go @@ -66,8 +66,8 @@ func (m *consensusModule) validateBlockBasic(block *typesCons.Block) error { return typesCons.ErrBlockExists } - if block != nil && unsafe.Sizeof(*block) > uintptr(m.consGenesis.MaxBlockBytes) { - return typesCons.ErrInvalidBlockSize(uint64(unsafe.Sizeof(*block)), m.consGenesis.MaxBlockBytes) + if block != nil && unsafe.Sizeof(*block) > uintptr(m.consGenesis.GetMaxBlockBytes()) { + return typesCons.ErrInvalidBlockSize(uint64(unsafe.Sizeof(*block)), m.consGenesis.GetMaxBlockBytes()) } // If the current block being processed (i.e. voted on) by consensus is non nil, we need to make diff --git a/consensus/module.go b/consensus/module.go index cdfecbac2..e2ab3c506 100644 --- a/consensus/module.go +++ b/consensus/module.go @@ -31,8 +31,8 @@ type consensusModule struct { bus modules.Bus privateKey cryptoPocket.Ed25519PrivateKey - consCfg *typesCons.ConsensusConfig - consGenesis *typesCons.ConsensusGenesisState + consCfg modules.ConsensusConfig + consGenesis modules.ConsensusGenesisState // m is a mutex used to control synchronization when multiple goroutines are accessing the struct and its fields / properties. // diff --git a/p2p/module.go b/p2p/module.go index c13ad15aa..3c09b6b7e 100644 --- a/p2p/module.go +++ b/p2p/module.go @@ -27,7 +27,7 @@ const ( type p2pModule struct { bus modules.Bus - p2pCfg *typesP2P.P2PConfig // TODO (olshansky): to remove this since it'll be available via the bus + p2pCfg modules.P2PConfig // TODO (olshansky): to remove this since it'll be available via the bus listener typesP2P.Transport address cryptoPocket.Address @@ -105,7 +105,7 @@ func (m *p2pModule) Start() error { return err } - if m.p2pCfg.UseRainTree { + if m.p2pCfg.GetUseRainTree() { m.network = raintree.NewRainTreeNetwork(m.address, addrBook) } else { m.network = stdnetwork.NewNetwork(addrBook) diff --git a/telemetry/prometheus_module.go b/telemetry/prometheus_module.go index 4a15fb7fe..0302bc990 100644 --- a/telemetry/prometheus_module.go +++ b/telemetry/prometheus_module.go @@ -23,7 +23,7 @@ var ( type PrometheusTelemetryModule struct { bus modules.Bus - config *TelemetryConfig + config modules.TelemetryConfig counters map[string]prometheus.Counter gauges map[string]prometheus.Gauge @@ -55,10 +55,10 @@ func (m *PrometheusTelemetryModule) Create(runtime modules.RuntimeMgr) (modules. } func (m *PrometheusTelemetryModule) Start() error { - log.Printf("\nPrometheus metrics exporter: Starting at %s%s...\n", m.config.Address, m.config.Endpoint) + log.Printf("\nPrometheus metrics exporter: Starting at %s%s...\n", m.config.GetAddress(), m.config.GetEndpoint()) - http.Handle(m.config.Endpoint, promhttp.Handler()) - go http.ListenAndServe(m.config.Address, nil) + http.Handle(m.config.GetEndpoint(), promhttp.Handler()) + go http.ListenAndServe(m.config.GetAddress(), nil) log.Println("Prometheus metrics exporter started: OK") diff --git a/utility/module.go b/utility/module.go index f33b4ecf3..8ba38b4a1 100644 --- a/utility/module.go +++ b/utility/module.go @@ -15,7 +15,7 @@ var _ modules.Module = &utilityModule{} type utilityModule struct { bus modules.Bus - config *types.UtilityConfig + config modules.UtilityConfig Mempool types.Mempool } From 516f0ea117ef8ce68d9b8296242a118eef4c1d50 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 13 Oct 2022 00:14:52 +0100 Subject: [PATCH 84/90] refactor(Shared): using interfaces vs structs --- consensus/consensus_tests/pacemaker_test.go | 4 ++-- consensus/module.go | 18 +++++++-------- consensus/pacemaker.go | 25 +++++++++------------ p2p/module.go | 8 ++----- p2p/transport.go | 8 +++---- persistence/module.go | 4 ---- shared/modules/types.go | 8 +++++-- telemetry/module.go | 2 +- telemetry/noop_module.go | 4 ---- telemetry/prometheus_module.go | 6 +---- utility/module.go | 8 ++----- 11 files changed, 36 insertions(+), 59 deletions(-) diff --git a/consensus/consensus_tests/pacemaker_test.go b/consensus/consensus_tests/pacemaker_test.go index 10f604032..109ad2acf 100644 --- a/consensus/consensus_tests/pacemaker_test.go +++ b/consensus/consensus_tests/pacemaker_test.go @@ -27,8 +27,8 @@ func TestTinyPacemakerTimeouts(t *testing.T) { paceMakerTimeout := 50 * time.Millisecond runtimeMgrs := GenerateNodeRuntimeMgrs(t, numNodes, clockMock) for _, runtimeConfig := range runtimeMgrs { - if consCfg, ok := runtimeConfig.GetConfig().GetConsensusConfig().(*typesCons.ConsensusConfig); ok { - consCfg.GetPaceMakerConfig().SetTimeoutMsec(paceMakerTimeoutMsec) + if consCfg, ok := runtimeConfig.GetConfig().GetConsensusConfig().(consensus.HasPacemakerConfig); ok { + consCfg.GetPacemakerConfig().SetTimeoutMsec(paceMakerTimeoutMsec) } } diff --git a/consensus/module.go b/consensus/module.go index e2ab3c506..aad862427 100644 --- a/consensus/module.go +++ b/consensus/module.go @@ -118,8 +118,8 @@ func (*consensusModule) Create(runtimeMgr modules.RuntimeMgr) (modules.Module, e bus: nil, privateKey: privateKey.(cryptoPocket.Ed25519PrivateKey), - consCfg: cfg.GetConsensusConfig().(*typesCons.ConsensusConfig), - consGenesis: genesis.GetConsensusGenesisState().(*typesCons.ConsensusGenesisState), + consCfg: cfg.GetConsensusConfig(), + consGenesis: genesis.GetConsensusGenesisState(), Height: 0, Round: 0, @@ -197,18 +197,10 @@ func (m *consensusModule) SetBus(pocketBus modules.Bus) { } func (*consensusModule) ValidateConfig(cfg modules.Config) error { - // DISCUSS (team): we cannot cast if we want to use mocks and rely on interfaces - // if _, ok := cfg.GetConsensusConfig().(*typesCons.ConsensusConfig); !ok { - // return fmt.Errorf("cannot cast to ConsensusConfig") - // } return nil } func (*consensusModule) ValidateGenesis(genesis modules.GenesisState) error { - // DISCUSS (team): we cannot cast if we want to use mocks and rely on interfaces - // if _, ok := genesis.GetConsensusGenesisState().(*typesCons.ConsensusGenesisState); !ok { - // return fmt.Errorf("cannot cast to ConsensusGenesisState") - // } return nil } @@ -283,3 +275,9 @@ func (m *consensusModule) loadPersistedState() error { m.nodeLog(fmt.Sprintf("Starting node at height %d", latestHeight)) return nil } + +// HasPacemakerConfig is used to determine if a ConsensusConfig includes a PacemakerConfig without having to cast to the struct +// (which would break mocks and/or pollute the codebase with mock types casts and checks) +type HasPacemakerConfig interface { + GetPacemakerConfig() *typesCons.PacemakerConfig +} diff --git a/consensus/pacemaker.go b/consensus/pacemaker.go index fbf323d7e..4f0651418 100644 --- a/consensus/pacemaker.go +++ b/consensus/pacemaker.go @@ -2,6 +2,7 @@ package consensus import ( "context" + "fmt" "log" timePkg "time" @@ -46,7 +47,7 @@ type paceMaker struct { // a great idea in production code. consensusMod *consensusModule - pacemakerConfigs modules.PacemakerConfig + pacemakerCfg modules.PacemakerConfig stepCancelFunc context.CancelFunc @@ -65,19 +66,19 @@ func (m *paceMaker) Create(runtimeMgr modules.RuntimeMgr) (modules.Module, error log.Fatalf("config validation failed: %v", err) } - pacemakerConfig := cfg.GetConsensusConfig().(*typesCons.ConsensusConfig).PacemakerConfig + pacemakerCfg := cfg.GetConsensusConfig().(HasPacemakerConfig).GetPacemakerConfig() return &paceMaker{ bus: nil, consensusMod: nil, - pacemakerConfigs: pacemakerConfig, + pacemakerCfg: pacemakerCfg, stepCancelFunc: nil, // Only set on restarts paceMakerDebug: paceMakerDebug{ - manualMode: pacemakerConfig.GetManual(), - debugTimeBetweenStepsMsec: pacemakerConfig.GetDebugTimeBetweenStepsMsec(), + manualMode: pacemakerCfg.GetManual(), + debugTimeBetweenStepsMsec: pacemakerCfg.GetDebugTimeBetweenStepsMsec(), quorumCertificate: nil, }, }, nil @@ -107,15 +108,9 @@ func (m *paceMaker) GetBus() modules.Bus { } func (*paceMaker) ValidateConfig(cfg modules.Config) error { - // DISCUSS (team): we cannot cast if we want to use mocks and rely on interfaces - // consCfg, ok := cfg.GetConsensusConfig().(*typesCons.ConsensusConfig) - // if !ok { - // return fmt.Errorf("cannot cast to ConsensusConfig") - // } - - // if consCfg.PacemakerConfig == nil { - // return fmt.Errorf("PacemakerConfig is nil") - // } + if _, ok := cfg.GetConsensusConfig().(HasPacemakerConfig); !ok { + return fmt.Errorf("cannot cast to PacemakeredConsensus") + } return nil } @@ -256,6 +251,6 @@ func (p *paceMaker) startNextView(qc *typesCons.QuorumCertificate, forceNextView // TODO(olshansky): Increase timeout using exponential backoff. func (p *paceMaker) getStepTimeout(round uint64) timePkg.Duration { - baseTimeout := timePkg.Duration(int64(timePkg.Millisecond) * int64(p.pacemakerConfigs.GetTimeoutMsec())) + baseTimeout := timePkg.Duration(int64(timePkg.Millisecond) * int64(p.pacemakerCfg.GetTimeoutMsec())) return baseTimeout } diff --git a/p2p/module.go b/p2p/module.go index 3c09b6b7e..01c8447e2 100644 --- a/p2p/module.go +++ b/p2p/module.go @@ -52,13 +52,13 @@ func (*p2pModule) Create(runtimeMgr modules.RuntimeMgr) (modules.Module, error) if err := m.ValidateConfig(cfg); err != nil { return nil, fmt.Errorf("config validation failed: %w", err) } - p2pCfg := cfg.GetP2PConfig().(*typesP2P.P2PConfig) + p2pCfg := cfg.GetP2PConfig() l, err := CreateListener(p2pCfg) if err != nil { return nil, err } - privateKey, err := cryptoPocket.NewPrivateKey(p2pCfg.PrivateKey) + privateKey, err := cryptoPocket.NewPrivateKey(p2pCfg.GetPrivateKey()) if err != nil { return nil, err } @@ -166,10 +166,6 @@ func (m *p2pModule) Send(addr cryptoPocket.Address, msg *anypb.Any, topic debug. } func (*p2pModule) ValidateConfig(cfg modules.Config) error { - // DISCUSS (team): we cannot cast if we want to use mocks and rely on interfaces - // if _, ok := cfg.GetP2PConfig().(*typesP2P.P2PConfig); !ok { - // return fmt.Errorf("cannot cast to P2PConfig") - // } return nil } diff --git a/p2p/transport.go b/p2p/transport.go index 44ac1e238..cf836ea19 100644 --- a/p2p/transport.go +++ b/p2p/transport.go @@ -14,24 +14,24 @@ const ( ) func CreateListener(cfg modules.P2PConfig) (typesP2P.Transport, error) { - switch cfg.IsEmptyConnType() { // TECHDEBT kept in switch format because this should be an enum not a bool + switch cfg.GetIsEmptyConnectionType() { // TECHDEBT kept in switch format because this should be an enum not a bool case true: return createEmptyListener(cfg) case false: return createTCPListener(cfg) default: - return nil, fmt.Errorf("unsupported connection type for listener: %v", cfg.IsEmptyConnType()) + return nil, fmt.Errorf("unsupported connection type for listener: %v", cfg.GetIsEmptyConnectionType()) } } func CreateDialer(cfg modules.P2PConfig, url string) (typesP2P.Transport, error) { - switch cfg.IsEmptyConnType() { + switch cfg.GetIsEmptyConnectionType() { case true: return createEmptyDialer(cfg, url) case false: return createTCPDialer(cfg, url) default: - return nil, fmt.Errorf("unsupported connection type for dialer: %v", cfg.IsEmptyConnType()) + return nil, fmt.Errorf("unsupported connection type for dialer: %v", cfg.GetIsEmptyConnectionType()) } } diff --git a/persistence/module.go b/persistence/module.go index 0255c4b66..79eca5fb5 100644 --- a/persistence/module.go +++ b/persistence/module.go @@ -122,10 +122,6 @@ func (m *persistenceModule) GetBus() modules.Bus { } func (*persistenceModule) ValidateConfig(cfg modules.Config) error { - // DISCUSS (team): we cannot cast if we want to use mocks and rely on interfaces - // if _, ok := cfg.GetPersistenceConfig().(*types.PersistenceConfig); !ok { - // return fmt.Errorf("cannot cast to PersistenceConfig") - // } return nil } diff --git a/shared/modules/types.go b/shared/modules/types.go index d4a420b0a..7c9dc419b 100644 --- a/shared/modules/types.go +++ b/shared/modules/types.go @@ -47,9 +47,10 @@ type PersistenceConfig interface { } type P2PConfig interface { + GetPrivateKey() string GetConsensusPort() uint32 GetUseRainTree() bool - IsEmptyConnType() bool // TODO : make enum + GetIsEmptyConnectionType() bool // TODO : make enum } type TelemetryConfig interface { @@ -58,7 +59,10 @@ type TelemetryConfig interface { GetEndpoint() string } -type UtilityConfig interface{} +type UtilityConfig interface { + GetMaxMempoolTransactionBytes() uint64 + GetMaxMempoolTransactions() uint32 +} type PersistenceGenesisState interface { GetAccs() []Account diff --git a/telemetry/module.go b/telemetry/module.go index 1a42a5631..50d385f37 100644 --- a/telemetry/module.go +++ b/telemetry/module.go @@ -21,7 +21,7 @@ func Create(runtime modules.RuntimeMgr) (modules.Module, error) { func (*telemetryModule) Create(runtime modules.RuntimeMgr) (modules.Module, error) { cfg := runtime.GetConfig() - telemetryCfg := cfg.GetTelemetryConfig().(*TelemetryConfig) + telemetryCfg := cfg.GetTelemetryConfig() if telemetryCfg.GetEnabled() { return CreatePrometheusTelemetryModule(runtime) diff --git a/telemetry/noop_module.go b/telemetry/noop_module.go index e407584d5..1866e86a3 100644 --- a/telemetry/noop_module.go +++ b/telemetry/noop_module.go @@ -60,10 +60,6 @@ func (m *NoopTelemetryModule) GetBus() modules.Bus { } func (*NoopTelemetryModule) ValidateConfig(cfg modules.Config) error { - // DISCUSS (team): we cannot cast if we want to use mocks and rely on interfaces - // if _, ok := cfg.GetTelemetryConfig().(*TelemetryConfig); !ok { - // return fmt.Errorf("cannot cast to TelemetryConfig") - // } return nil } diff --git a/telemetry/prometheus_module.go b/telemetry/prometheus_module.go index 0302bc990..c81b3b70e 100644 --- a/telemetry/prometheus_module.go +++ b/telemetry/prometheus_module.go @@ -44,7 +44,7 @@ func (m *PrometheusTelemetryModule) Create(runtime modules.RuntimeMgr) (modules. if err := m.ValidateConfig(cfg); err != nil { return nil, fmt.Errorf("config validation failed: %w", err) } - telemetryCfg := cfg.GetTelemetryConfig().(*TelemetryConfig) + telemetryCfg := cfg.GetTelemetryConfig() return &PrometheusTelemetryModule{ config: telemetryCfg, @@ -85,10 +85,6 @@ func (m *PrometheusTelemetryModule) GetBus() modules.Bus { } func (*PrometheusTelemetryModule) ValidateConfig(cfg modules.Config) error { - // DISCUSS (team): we cannot cast if we want to use mocks and rely on interfaces - // if _, ok := cfg.GetTelemetryConfig().(*TelemetryConfig); !ok { - // return fmt.Errorf("cannot cast to TelemetryConfig") - // } return nil } diff --git a/utility/module.go b/utility/module.go index 8ba38b4a1..1e2fd619f 100644 --- a/utility/module.go +++ b/utility/module.go @@ -35,11 +35,11 @@ func (*utilityModule) Create(runtime modules.RuntimeMgr) (modules.Module, error) if err := m.ValidateConfig(cfg); err != nil { return nil, fmt.Errorf("config validation failed: %w", err) } - utilityCfg := cfg.GetUtilityConfig().(*types.UtilityConfig) + utilityCfg := cfg.GetUtilityConfig() return &utilityModule{ config: utilityCfg, - Mempool: types.NewMempool(utilityCfg.MaxMempoolTransactionBytes, utilityCfg.MaxMempoolTransactions), + Mempool: types.NewMempool(utilityCfg.GetMaxMempoolTransactionBytes(), utilityCfg.GetMaxMempoolTransactions()), }, nil } @@ -67,9 +67,5 @@ func (u *utilityModule) GetBus() modules.Bus { } func (*utilityModule) ValidateConfig(cfg modules.Config) error { - // DISCUSS (team): we cannot cast if we want to use mocks and rely on interfaces - // if _, ok := cfg.GetUtilityConfig().(*types.UtilityConfig); !ok { - // return fmt.Errorf("cannot cast to UtilityConfig") - // } return nil } From 7352d39c8d75b387012be2ba735068f3f22bc26c Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 13 Oct 2022 00:15:38 +0100 Subject: [PATCH 85/90] style(Shared): formatting Goland is the offender... weird format on save rules... investigate --- consensus/hotstuff_leader.go | 7 +++---- p2p/module_raintree_test.go | 10 ++++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/consensus/hotstuff_leader.go b/consensus/hotstuff_leader.go index 93d8bc4fa..19183830b 100644 --- a/consensus/hotstuff_leader.go +++ b/consensus/hotstuff_leader.go @@ -315,10 +315,9 @@ func (m *consensusModule) validatePartialSignature(msg *typesCons.HotstuffMessag } // TODO: This is just a placeholder at the moment for indexing hotstuff messages ONLY. -// -// It doesn't actually work because SizeOf returns the size of the map pointer, -// and does not recursively determine the size of all the underlying elements -// Add proper tests and implementation once the mempool is implemented. +// It doesn't actually work because SizeOf returns the size of the map pointer, +// and does not recursively determine the size of all the underlying elements +// Add proper tests and implementation once the mempool is implemented. func (m *consensusModule) tempIndexHotstuffMessage(msg *typesCons.HotstuffMessage) { if m.consCfg.GetMaxMempoolBytes() < uint64(unsafe.Sizeof(m.messagePool)) { m.nodeLogError(typesCons.DisregardHotstuffMessage, typesCons.ErrConsensusMempoolFull) diff --git a/p2p/module_raintree_test.go b/p2p/module_raintree_test.go index 9efff20a4..6eac6b1b5 100644 --- a/p2p/module_raintree_test.go +++ b/p2p/module_raintree_test.go @@ -274,8 +274,7 @@ func generateKeys(_ *testing.T, numValidators int) []cryptoPocket.PrivateKey { // A mock of the application specific to know if a message was sent to be handled by the application // INVESTIGATE(olshansky): Double check that how the expected calls are counted is accurate per the -// -// expectation with RainTree by comparing with Telemetry after updating specs. +// expectation with RainTree by comparing with Telemetry after updating specs. func prepareBusMock(t *testing.T, wg *sync.WaitGroup, consensusMock *modulesMock.MockConsensusModule, telemetryMock *modulesMock.MockTelemetryModule) *modulesMock.MockBus { ctrl := gomock.NewController(t) busMock := modulesMock.NewMockBus(ctrl) @@ -341,8 +340,7 @@ func prepareEventMetricsAgentMock(t *testing.T) *modulesMock.MockEventMetricsAge // is a race condition here, but it is okay because our goal is to achieve max coverage with an upper limit // on the number of expected messages propagated. // INVESTIGATE(olshansky): Double check that how the expected calls are counted is accurate per the -// -// expectation with RainTree by comparing with Telemetry after updating specs. +// expectation with RainTree by comparing with Telemetry after updating specs. func prepareConnMock(t *testing.T, expectedNumNetworkReads, expectedNumNetworkWrites uint16) typesP2P.Transport { testChannel := make(chan []byte, testChannelSize) ctrl := gomock.NewController(t) @@ -363,6 +361,7 @@ func prepareConnMock(t *testing.T, expectedNumNetworkReads, expectedNumNetworkWr return connMock } +// prepareP2PModules returns a map of configured p2pModules keyed by an incremental naming convention (eg: `val_1`, `val_2`, etc.) func prepareP2PModules(t *testing.T, runtimeConfigs []modules.RuntimeMgr) (p2pModules map[string]*p2pModule) { p2pModules = make(map[string]*p2pModule, len(runtimeConfigs)) for i, runtimeConfig := range runtimeConfigs { @@ -373,6 +372,8 @@ func prepareP2PModules(t *testing.T, runtimeConfigs []modules.RuntimeMgr) (p2pMo return } +// createMockRuntimeMgrs creates `numValidators` instances of mocked `RuntimeMgr` that are essentially +// representing the runtime environments of the validators that we will use in our tests func createMockRuntimeMgrs(t *testing.T, numValidators int) []modules.RuntimeMgr { ctrl := gomock.NewController(t) mockRuntimeMgrs := make([]modules.RuntimeMgr, numValidators) @@ -404,6 +405,7 @@ func validatorId(_ *testing.T, i int) string { return fmt.Sprintf(serviceUrlFormat, i) } +// createMockGenesisState configures and returns a mocked GenesisState func createMockGenesisState(t *testing.T, valKeys []cryptoPocket.PrivateKey) modules.GenesisState { ctrl := gomock.NewController(t) From 9664cba8de2fe699d205d4399c74a316ff4c5ca8 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 13 Oct 2022 00:15:52 +0100 Subject: [PATCH 86/90] style(Consensus): cleanup --- consensus/types/types.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/consensus/types/types.go b/consensus/types/types.go index 67f9fa380..a73637d6a 100644 --- a/consensus/types/types.go +++ b/consensus/types/types.go @@ -41,10 +41,6 @@ func GetValAddrToIdMap(validatorMap ValidatorMap) (ValAddrToIdMap, IdToValAddrMa return valToIdMap, idToValMap } -func (x *ConsensusConfig) GetPaceMakerConfig() modules.PacemakerConfig { - return x.GetPacemakerConfig() -} - func (x *PacemakerConfig) SetTimeoutMsec(u uint64) { x.TimeoutMsec = u } From 1c482afe794b0d6aff07ef8366d8b2a20688d07e Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 13 Oct 2022 00:22:13 +0100 Subject: [PATCH 87/90] docs(Runtime): NewManagerFromReaders comment --- runtime/manager.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/runtime/manager.go b/runtime/manager.go index 0cbcd9f50..1110de3c1 100644 --- a/runtime/manager.go +++ b/runtime/manager.go @@ -45,6 +45,11 @@ func NewManagerFromFiles(configPath, genesisPath string, options ...func(*Manage return mgr } +// NewManagerFromReaders returns a *Manager given io.Readers for the config and the genesis. +// +// Ideally useful when the user doesn't want to rely on the filesystem and instead intends plugging in different configuration management system. +// +// Note: currently unused, here as a reference func NewManagerFromReaders(configReader, genesisReader io.Reader, options ...func(*Manager)) *Manager { var cfg *runtimeConfig parse(configReader, cfg) From a8909bbfd565dd77abe4fb67dc2200b919608cf6 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 13 Oct 2022 00:31:17 +0100 Subject: [PATCH 88/90] refactor(Shared): shared.test_artifacts -> runtime.test_artifacts --- build/config/main.go | 2 +- consensus/consensus_tests/utils_test.go | 2 +- persistence/test/setup_test.go | 10 ++++------ {shared => runtime}/test_artifacts/generator.go | 0 {shared => runtime}/test_artifacts/genesis.go | 0 {shared => runtime}/test_artifacts/gov.go | 0 {shared => runtime}/test_artifacts/util.go | 0 utility/test/account_test.go | 7 +++---- utility/test/actor_test.go | 2 +- utility/test/block_test.go | 5 ++--- utility/test/gov_test.go | 6 +++--- utility/test/module_test.go | 7 +++---- utility/test/transaction_test.go | 3 +-- 13 files changed, 19 insertions(+), 25 deletions(-) rename {shared => runtime}/test_artifacts/generator.go (100%) rename {shared => runtime}/test_artifacts/genesis.go (100%) rename {shared => runtime}/test_artifacts/gov.go (100%) rename {shared => runtime}/test_artifacts/util.go (100%) diff --git a/build/config/main.go b/build/config/main.go index 77a46f023..c1848e17e 100644 --- a/build/config/main.go +++ b/build/config/main.go @@ -6,7 +6,7 @@ import ( "fmt" "io/ioutil" - "github.com/pokt-network/pocket/shared/test_artifacts" + "github.com/pokt-network/pocket/runtime/test_artifacts" ) // Utility to generate config and genesis files diff --git a/consensus/consensus_tests/utils_test.go b/consensus/consensus_tests/utils_test.go index b7a293ef8..59889b7a1 100644 --- a/consensus/consensus_tests/utils_test.go +++ b/consensus/consensus_tests/utils_test.go @@ -19,12 +19,12 @@ import ( "github.com/pokt-network/pocket/consensus" typesCons "github.com/pokt-network/pocket/consensus/types" "github.com/pokt-network/pocket/runtime" + "github.com/pokt-network/pocket/runtime/test_artifacts" "github.com/pokt-network/pocket/shared" cryptoPocket "github.com/pokt-network/pocket/shared/crypto" "github.com/pokt-network/pocket/shared/debug" "github.com/pokt-network/pocket/shared/modules" modulesMock "github.com/pokt-network/pocket/shared/modules/mocks" - "github.com/pokt-network/pocket/shared/test_artifacts" "github.com/stretchr/testify/require" "google.golang.org/protobuf/types/known/anypb" ) diff --git a/persistence/test/setup_test.go b/persistence/test/setup_test.go index 1101d46a2..e685c6e67 100644 --- a/persistence/test/setup_test.go +++ b/persistence/test/setup_test.go @@ -11,13 +11,11 @@ import ( "testing" "time" + "github.com/pokt-network/pocket/persistence" "github.com/pokt-network/pocket/persistence/types" "github.com/pokt-network/pocket/runtime" - "github.com/pokt-network/pocket/shared/test_artifacts" - - "github.com/pokt-network/pocket/persistence" + "github.com/pokt-network/pocket/runtime/test_artifacts" "github.com/pokt-network/pocket/shared/modules" - sharedTest "github.com/pokt-network/pocket/shared/test_artifacts" "github.com/stretchr/testify/require" "golang.org/x/exp/slices" ) @@ -52,10 +50,10 @@ var testPersistenceMod modules.PersistenceModule // initialized in TestMain // See https://github.com/ory/dockertest as reference for the template of this code // Postgres example can be found here: https://github.com/ory/dockertest/blob/v3/examples/PostgreSQL.md func TestMain(m *testing.M) { - pool, resource, dbUrl := sharedTest.SetupPostgresDocker() + pool, resource, dbUrl := test_artifacts.SetupPostgresDocker() testPersistenceMod = newTestPersistenceModule(dbUrl) exitCode := m.Run() - sharedTest.CleanupPostgresDocker(m, pool, resource) + test_artifacts.CleanupPostgresDocker(m, pool, resource) os.Exit(exitCode) } diff --git a/shared/test_artifacts/generator.go b/runtime/test_artifacts/generator.go similarity index 100% rename from shared/test_artifacts/generator.go rename to runtime/test_artifacts/generator.go diff --git a/shared/test_artifacts/genesis.go b/runtime/test_artifacts/genesis.go similarity index 100% rename from shared/test_artifacts/genesis.go rename to runtime/test_artifacts/genesis.go diff --git a/shared/test_artifacts/gov.go b/runtime/test_artifacts/gov.go similarity index 100% rename from shared/test_artifacts/gov.go rename to runtime/test_artifacts/gov.go diff --git a/shared/test_artifacts/util.go b/runtime/test_artifacts/util.go similarity index 100% rename from shared/test_artifacts/util.go rename to runtime/test_artifacts/util.go diff --git a/utility/test/account_test.go b/utility/test/account_test.go index 55c56b755..8a825dab7 100644 --- a/utility/test/account_test.go +++ b/utility/test/account_test.go @@ -6,12 +6,11 @@ import ( "sort" "testing" - "github.com/pokt-network/pocket/shared/modules" - "github.com/pokt-network/pocket/shared/test_artifacts" - "github.com/pokt-network/pocket/utility/types" - + "github.com/pokt-network/pocket/runtime/test_artifacts" "github.com/pokt-network/pocket/shared/crypto" + "github.com/pokt-network/pocket/shared/modules" "github.com/pokt-network/pocket/utility" + "github.com/pokt-network/pocket/utility/types" "github.com/stretchr/testify/require" ) diff --git a/utility/test/actor_test.go b/utility/test/actor_test.go index 52fd6e55b..fdb922f37 100644 --- a/utility/test/actor_test.go +++ b/utility/test/actor_test.go @@ -8,9 +8,9 @@ import ( "sort" "testing" + "github.com/pokt-network/pocket/runtime/test_artifacts" "github.com/pokt-network/pocket/shared/crypto" "github.com/pokt-network/pocket/shared/modules" - "github.com/pokt-network/pocket/shared/test_artifacts" "github.com/pokt-network/pocket/utility" typesUtil "github.com/pokt-network/pocket/utility/types" "github.com/stretchr/testify/require" diff --git a/utility/test/block_test.go b/utility/test/block_test.go index 3f5a8bb18..a181d0311 100644 --- a/utility/test/block_test.go +++ b/utility/test/block_test.go @@ -6,10 +6,9 @@ import ( "math/big" "testing" - "github.com/pokt-network/pocket/shared/test_artifacts" - "github.com/stretchr/testify/require" - + "github.com/pokt-network/pocket/runtime/test_artifacts" typesUtil "github.com/pokt-network/pocket/utility/types" + "github.com/stretchr/testify/require" ) func TestUtilityContext_ApplyBlock(t *testing.T) { diff --git a/utility/test/gov_test.go b/utility/test/gov_test.go index 41eee1e2d..42dacb8ed 100644 --- a/utility/test/gov_test.go +++ b/utility/test/gov_test.go @@ -2,11 +2,11 @@ package test import ( "encoding/hex" - "github.com/pokt-network/pocket/shared/codec" - "github.com/pokt-network/pocket/shared/modules" - "github.com/pokt-network/pocket/shared/test_artifacts" "testing" + "github.com/pokt-network/pocket/runtime/test_artifacts" + "github.com/pokt-network/pocket/shared/codec" + "github.com/pokt-network/pocket/shared/modules" typesUtil "github.com/pokt-network/pocket/utility/types" "github.com/stretchr/testify/require" "google.golang.org/protobuf/types/known/wrapperspb" diff --git a/utility/test/module_test.go b/utility/test/module_test.go index cc612eb8f..0d76e2857 100644 --- a/utility/test/module_test.go +++ b/utility/test/module_test.go @@ -6,13 +6,12 @@ import ( "testing" "github.com/golang/mock/gomock" - mock_modules "github.com/pokt-network/pocket/shared/modules/mocks" - "github.com/pokt-network/pocket/shared/test_artifacts" - utilTypes "github.com/pokt-network/pocket/utility/types" - "github.com/pokt-network/pocket/persistence" + "github.com/pokt-network/pocket/runtime/test_artifacts" "github.com/pokt-network/pocket/shared/modules" + mock_modules "github.com/pokt-network/pocket/shared/modules/mocks" "github.com/pokt-network/pocket/utility" + utilTypes "github.com/pokt-network/pocket/utility/types" "github.com/stretchr/testify/require" ) diff --git a/utility/test/transaction_test.go b/utility/test/transaction_test.go index 42da82f16..be38645c8 100644 --- a/utility/test/transaction_test.go +++ b/utility/test/transaction_test.go @@ -5,9 +5,8 @@ import ( "math/big" "testing" + "github.com/pokt-network/pocket/runtime/test_artifacts" "github.com/pokt-network/pocket/shared/codec" - "github.com/pokt-network/pocket/shared/test_artifacts" - "github.com/pokt-network/pocket/shared/crypto" "github.com/pokt-network/pocket/utility" typesUtil "github.com/pokt-network/pocket/utility/types" From 5b5461951df2117c5c9df767bd0e22548e5f16e2 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 13 Oct 2022 00:35:56 +0100 Subject: [PATCH 89/90] fix(Persistence): leftover casts --- persistence/genesis.go | 2 +- persistence/module.go | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/persistence/genesis.go b/persistence/genesis.go index 0d9d592df..65d2fd197 100644 --- a/persistence/genesis.go +++ b/persistence/genesis.go @@ -12,7 +12,7 @@ import ( // TODO(andrew): generalize with the `actors interface` // WARNING: This function crashes the process if there is an error populating the genesis state. -func (m *persistenceModule) populateGenesisState(state *types.PersistenceGenesisState) { +func (m *persistenceModule) populateGenesisState(state modules.PersistenceGenesisState) { log.Println("Populating genesis state...") // REFACTOR: This business logic should probably live in `types/genesis.go` diff --git a/persistence/module.go b/persistence/module.go index 79eca5fb5..d6a08e763 100644 --- a/persistence/module.go +++ b/persistence/module.go @@ -56,7 +56,7 @@ func (*persistenceModule) Create(runtimeMgr modules.RuntimeMgr) (modules.Module, if err := m.ValidateGenesis(genesis); err != nil { return nil, fmt.Errorf("genesis validation failed: %w", err) } - persistenceGenesis := genesis.GetPersistenceGenesisState().(*types.PersistenceGenesisState) + persistenceGenesis := genesis.GetPersistenceGenesisState() conn, err := connectToDatabase(persistenceCfg.GetPostgresUrl(), persistenceCfg.GetNodeSchema()) if err != nil { @@ -126,10 +126,6 @@ func (*persistenceModule) ValidateConfig(cfg modules.Config) error { } func (*persistenceModule) ValidateGenesis(genesis modules.GenesisState) error { - // DISCUSS (team): we cannot cast if we want to use mocks and rely on interfaces - // if _, ok := genesis.GetPersistenceGenesisState().(*types.PersistenceGenesisState); !ok { - // return fmt.Errorf("cannot cast to PersistenceGenesisState") - // } return nil } From d38b0e107e238c3bb8623b9fca76e2a25c5c96fd Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Thu, 13 Oct 2022 00:55:37 +0100 Subject: [PATCH 90/90] refactor(Shared): removed NodeModule --- consensus/consensus_tests/utils_test.go | 14 +++++++------- shared/modules/node_module.go | 8 -------- shared/node.go | 2 -- 3 files changed, 7 insertions(+), 17 deletions(-) delete mode 100644 shared/modules/node_module.go diff --git a/consensus/consensus_tests/utils_test.go b/consensus/consensus_tests/utils_test.go index 59889b7a1..4b8b2539b 100644 --- a/consensus/consensus_tests/utils_test.go +++ b/consensus/consensus_tests/utils_test.go @@ -57,7 +57,7 @@ func init() { } } -type IdToNodeMapping map[typesCons.NodeId]modules.NodeModule +type IdToNodeMapping map[typesCons.NodeId]*shared.Node /*** Node Generation Helpers ***/ @@ -168,25 +168,25 @@ func StartAllTestPocketNodes(t *testing.T, pocketNodes IdToNodeMapping) { // TODO(discuss): Should we use reflections inside the testing module as being done here or explicitly // define the interfaces used for debug/development. The latter will probably scale more but will // require more effort and pollute the source code with debugging information. -func GetConsensusNodeState(node modules.NodeModule) typesCons.ConsensusNodeState { +func GetConsensusNodeState(node *shared.Node) typesCons.ConsensusNodeState { return GetConsensusModImpl(node).MethodByName("GetNodeState").Call([]reflect.Value{})[0].Interface().(typesCons.ConsensusNodeState) } -func GetConsensusModElem(node modules.NodeModule) reflect.Value { +func GetConsensusModElem(node *shared.Node) reflect.Value { return reflect.ValueOf(node.GetBus().GetConsensusModule()).Elem() } -func GetConsensusModImpl(node modules.NodeModule) reflect.Value { +func GetConsensusModImpl(node *shared.Node) reflect.Value { return reflect.ValueOf(node.GetBus().GetConsensusModule()) } /*** Debug/Development Message Helpers ***/ -func TriggerNextView(t *testing.T, node modules.NodeModule) { +func TriggerNextView(t *testing.T, node *shared.Node) { triggerDebugMessage(t, node, debug.DebugMessageAction_DEBUG_CONSENSUS_TRIGGER_NEXT_VIEW) } -func triggerDebugMessage(t *testing.T, node modules.NodeModule, action debug.DebugMessageAction) { +func triggerDebugMessage(t *testing.T, node *shared.Node, action debug.DebugMessageAction) { debugMessage := &debug.DebugMessage{ Action: debug.DebugMessageAction_DEBUG_CONSENSUS_TRIGGER_NEXT_VIEW, Message: nil, @@ -207,7 +207,7 @@ func P2PBroadcast(_ *testing.T, nodes IdToNodeMapping, any *anypb.Any) { } } -func P2PSend(_ *testing.T, node modules.NodeModule, any *anypb.Any) { +func P2PSend(_ *testing.T, node *shared.Node, any *anypb.Any) { e := &debug.PocketEvent{Topic: debug.PocketTopic_CONSENSUS_MESSAGE_TOPIC, Data: any} node.GetBus().PublishEventToBus(e) } diff --git a/shared/modules/node_module.go b/shared/modules/node_module.go deleted file mode 100644 index 16366da66..000000000 --- a/shared/modules/node_module.go +++ /dev/null @@ -1,8 +0,0 @@ -package modules - -//go:generate mockgen -source=$GOFILE -destination=./mocks/node_module_mock.go -aux_files=github.com/pokt-network/pocket/shared/modules=module.go - -type NodeModule interface { - Module - P2PAddressableModule -} diff --git a/shared/node.go b/shared/node.go index 4222c4b51..026a2c9f8 100644 --- a/shared/node.go +++ b/shared/node.go @@ -17,8 +17,6 @@ import ( "google.golang.org/protobuf/types/known/anypb" ) -var _ modules.NodeModule = &Node{} - const ( MainModuleName = "main" )