From a6c5df0c4550882b77d4ab2c25f2f668096090e2 Mon Sep 17 00:00:00 2001
From: Benjamin Bollen <ben@erisindustries.com>
Date: Thu, 9 Jun 2016 17:40:42 +0200
Subject: [PATCH] Create ConsensusEngine interface to hand to Pipe;

---
 consensus/consensus.go             | 34 +++++++-----------------------
 consensus/tendermint/tendermint.go |  5 +++++
 core/core.go                       |  8 +++----
 definitions/pipe.go                |  1 +
 manager/eris-mint/pipe.go          | 33 ++++++++++++++++++++---------
 manager/eris-mint/state/state.go   |  2 +-
 6 files changed, 42 insertions(+), 41 deletions(-)

diff --git a/consensus/consensus.go b/consensus/consensus.go
index cb1a4fc7..2bc719b3 100644
--- a/consensus/consensus.go
+++ b/consensus/consensus.go
@@ -17,43 +17,25 @@
 package consensus
 
 import (
-  config "github.com/eris-ltd/eris-db/config"
+  "fmt"
 
+  config      "github.com/eris-ltd/eris-db/config"
   definitions "github.com/eris-ltd/eris-db/definitions"
   tendermint  "github.com/eris-ltd/eris-db/consensus/tendermint"
 )
 
-type ConsensusEngine struct {
-  //
-  Communicator
-
-  // application manager
-
-}
-
 func LoadConsensusEngineInPipe(moduleConfig *config.ModuleConfig,
   pipe definitions.Pipe) error {
   switch moduleConfig.Name {
   case "tendermint" :
-    _, err := tendermint.NewTendermintNode(moduleConfig, pipe.GetApplication())
+    tendermintNode, err := tendermint.NewTendermintNode(moduleConfig,
+      pipe.GetApplication())
     if err != nil {
-      return err
+      return fmt.Errorf("Failed to load Tendermint node: %v", err)
+    }
+    if err := pipe.SetConsensusEngine(tendermintNode); err != nil {
+      return fmt.Errorf("Failed to hand Tendermint node to pipe: %v", err)
     }
   }
   return nil
 }
-
-func NewConsensusEngine(moduleConfig *config.ModuleConfig) {
-
-}
-
-type Communicator interface {
-  // Unicast()
-  Broadcast()
-}
-
-type ConsensusModule interface {
-  Start()
-}
-
-// func (consensusEngine *ConsensusEngine) setCommunicator (communicator *)
diff --git a/consensus/tendermint/tendermint.go b/consensus/tendermint/tendermint.go
index fbdf94f3..b57b75fc 100644
--- a/consensus/tendermint/tendermint.go
+++ b/consensus/tendermint/tendermint.go
@@ -33,6 +33,7 @@ import (
   log "github.com/eris-ltd/eris-logger"
 
   config        "github.com/eris-ltd/eris-db/config"
+  definitions   "github.com/eris-ltd/eris-db/definitions"
   manager_types "github.com/eris-ltd/eris-db/manager/types"
   // files  "github.com/eris-ltd/eris-db/files"
 )
@@ -42,6 +43,10 @@ type TendermintNode struct {
   tmintConfig *TendermintConfig
 }
 
+// NOTE [ben] Compiler check to ensure TendermintNode successfully implements
+// eris-db/definitions.Consensus
+var _ definitions.ConsensusEngine = (*TendermintNode)(nil)
+
 func NewTendermintNode(moduleConfig *config.ModuleConfig,
   application manager_types.Application) (*TendermintNode, error) {
   // re-assert proper configuration for module
diff --git a/core/core.go b/core/core.go
index fc0941e4..9aca7585 100644
--- a/core/core.go
+++ b/core/core.go
@@ -58,10 +58,10 @@ func NewCore(chainId string, consensusConfig *config.ModuleConfig,
   // [x] from genesis
   // [x] create event switch
   // [x] give state and evsw to app
-  // give app to consensus
-  // create new Pipe
-  // give app
-  // create servers
+  // [x] give app to consensus
+  // [x] create new Pipe
+  // [x] give consensus to pipe
+  // [ ] create servers
   return &Core{}, fmt.Errorf("PLACEHOLDER")
 }
 
diff --git a/definitions/pipe.go b/definitions/pipe.go
index cb6df44f..1db83165 100644
--- a/definitions/pipe.go
+++ b/definitions/pipe.go
@@ -44,6 +44,7 @@ type Pipe interface {
   Transactor() Transactor
   // NOTE: [ben] added to Pipe interface on 0.12 refactor
   GetApplication() manager_types.Application
+  SetConsensusEngine(consensus ConsensusEngine) error
 }
 
 type Accounts interface {
diff --git a/manager/eris-mint/pipe.go b/manager/eris-mint/pipe.go
index ecb8ccd3..74bc6373 100644
--- a/manager/eris-mint/pipe.go
+++ b/manager/eris-mint/pipe.go
@@ -34,17 +34,19 @@ import (
 )
 
 type ErisMintPipe struct {
-  erisMintState *state.State
-  eventSwitch   *tendermint_events.EventSwitch
-  erisMint      *ErisMint
+  erisMintState   *state.State
+  eventSwitch     *tendermint_events.EventSwitch
+  erisMint        *ErisMint
   // Pipe implementations
-  accounts      definitions.Accounts
-  blockchain    definitions.Blockchain
-  consensus     definitions.Consensus
-  events        definitions.EventEmitter
-  namereg       definitions.NameReg
-  net           definitions.Net
-  transactor    definitions.Transactor
+  accounts        definitions.Accounts
+  blockchain      definitions.Blockchain
+  consensus       definitions.Consensus
+  events          definitions.EventEmitter
+  namereg         definitions.NameReg
+  net             definitions.Net
+  transactor      definitions.Transactor
+  // Consensus interface
+  consensusEngine definitions.ConsensusEngine
 }
 
 // NOTE [ben] Compiler check to ensure ErisMintPipe successfully implements
@@ -96,6 +98,7 @@ func NewErisMintPipe(moduleConfig *config.ModuleConfig,
     events:        events,
     namereg:       namereg,
     transactor:    transactor,
+    consensus:     nil,
   }, nil
 }
 
@@ -179,3 +182,13 @@ func (pipe *ErisMintPipe) Transactor() definitions.Transactor {
 func (pipe *ErisMintPipe) GetApplication() manager_types.Application {
   return pipe.erisMint
 }
+
+func (pipe *ErisMintPipe) SetConsensusEngine(
+  consensus definitions.ConsensusEngine) error {
+  if pipe.consensusEngine == nil {
+    pipe.consensusEngine = consensus
+  } else {
+    return fmt.Errorf("Failed to set consensus engine for pipe; already set")
+  }
+  return nil
+}
diff --git a/manager/eris-mint/state/state.go b/manager/eris-mint/state/state.go
index f22beb3e..9c7b4928 100644
--- a/manager/eris-mint/state/state.go
+++ b/manager/eris-mint/state/state.go
@@ -398,7 +398,7 @@ func (s *State) SetFireable(evc events.Fireable) {
 // Genesis
 
 func MakeGenesisStateFromFile(db dbm.DB, genDocFile string) (*GenesisDoc, *State) {
-	jsonBlob, err := ioutil.ReadFile(genDocFile)
+  jsonBlob, err := ioutil.ReadFile(genDocFile)
 	if err != nil {
 		Exit(Fmt("Couldn't read GenesisDoc file: %v", err))
 	}
-- 
GitLab