Skip to content

Commit

Permalink
Revert "merge dev"
Browse files Browse the repository at this point in the history
This reverts commit c805f49, reversing
changes made to 3b1804f.
  • Loading branch information
chadlagore committed Jul 12, 2017
1 parent 9d8e836 commit 992236f
Show file tree
Hide file tree
Showing 23 changed files with 304 additions and 463 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,3 @@ vendor

# Text Editors and IDEs
.vscode

# Log files
logfile
74 changes: 21 additions & 53 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package app

import (
"fmt"
"os"
"os/signal"
"syscall"

log "github.com/Sirupsen/logrus"
"github.com/google/uuid"
Expand All @@ -18,9 +15,9 @@ import (
)

var (
config *conf.Config
chain *blockchain.BlockChain
logFile = os.Stdout
config *conf.Config
// TODO peer store once it's merged in
chain *blockchain.BlockChain
// A reference to the transaction pool
tpool *pool.Pool
)
Expand All @@ -31,24 +28,6 @@ func Run(cfg conf.Config) {
log.Info("Starting Cumulus node")
config = &cfg

// Set logging level
if cfg.Verbose {
log.SetLevel(log.DebugLevel)
}

// Start a goroutine that waits for program termination. Before the program
// exits it will flush logs and save the blockchain.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
log.Info("Saving blockchain and flushing logs...")
// TODO
logFile.Sync()
logFile.Close()
os.Exit(0)
}()

// Below we'll connect to peers. After which, requests could begin to
// stream in. We should first initalize our pool, workers to handle
// incoming messages.
Expand All @@ -63,7 +42,7 @@ func Run(cfg conf.Config) {
// Start listening on the given interface and port so we can receive
// conenctions from other peers
log.Infof("Starting listener on %s:%d", cfg.Interface, cfg.Port)
peer.ListenAddr = fmt.Sprintf("%s:%d", cfg.Interface, cfg.Port)
peer.LocalAddr = fmt.Sprintf("%s:%d", cfg.Interface, cfg.Port)
go func() {
address := fmt.Sprintf("%s:%d", cfg.Interface, cfg.Port)
err := conn.Listen(address, peer.ConnectionHandler)
Expand All @@ -74,21 +53,8 @@ func Run(cfg conf.Config) {
}
}()

// If the console flag was passed, redirect logs to a file and run the console
if cfg.Console {
logFile, err := os.OpenFile("logfile", os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
log.WithError(err).Fatal("Failed to redirect logs to log file")
}
log.Warn("Redirecting logs to logfile")
log.SetOutput(logFile)
go RunConsole()
}

if len(config.Target) > 0 {
// Connect to the target and discover its peers.
ConnectAndDiscover(cfg.Target)
}
// Connect to the target and discover its peers.
ConnectAndDiscover(cfg.Target)

// Try maintain as close to peer.MaxPeers connections as possible while this
// peer is running
Expand All @@ -101,28 +67,30 @@ func Run(cfg conf.Config) {
}

// Return to command line.
select {} // Hang main thread. Everything happens in goroutines from here
select {}
}

// ConnectAndDiscover tries to connect to a target and discover its peers.
func ConnectAndDiscover(target string) {
peerInfoRequest := msg.Request{
ID: uuid.New().String(),
ResourceType: msg.ResourcePeerInfo,
}
if len(target) > 0 {
peerInfoRequest := msg.Request{
ID: uuid.New().String(),
ResourceType: msg.ResourcePeerInfo,
}

log.Infof("Dialing target %s", target)
c, err := conn.Dial(target)
if err != nil {
log.WithError(err).Fatalf("Failed to connect to target")
log.Infof("Dialing target %s", target)
c, err := conn.Dial(target)
if err != nil {
log.WithError(err).Fatalf("Failed to connect to target")
}
peer.ConnectionHandler(c)
p := peer.PStore.Get(c.RemoteAddr().String())
p.Request(peerInfoRequest, peer.PeerInfoHandler)
}
peer.ConnectionHandler(c)
p := peer.PStore.Get(c.RemoteAddr().String())
p.Request(peerInfoRequest, peer.PeerInfoHandler)
}

// RequestHandler is called every time a peer sends us a request message except
// on peers whos RequestHandlers have been overridden.
// on peers whos PushHandlers have been overridden.
func RequestHandler(req *msg.Request) msg.Response {
res := msg.Response{ID: req.ID}
typeErr := msg.NewProtocolError(msg.InvalidResourceType,
Expand Down
20 changes: 3 additions & 17 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestInitializeNode(t *testing.T) {

func TestPushHandlerNewBlock(t *testing.T) {
intializeQueues()
_, b := blockchain.NewValidTestChainAndBlock()
_, b := blockchain.NewValidChainAndBlock()
push := msg.Push{
ResourceType: msg.ResourceBlock,
Resource: b,
Expand All @@ -81,9 +81,9 @@ func TestPushHandlerNewBlock(t *testing.T) {
// Add more here...
}

func TestPushHandlerNewTestTransaction(t *testing.T) {
func TestPushHandlerNewTransaction(t *testing.T) {
intializeQueues()
txn := blockchain.NewTestTransaction()
txn := blockchain.NewTransaction()
push := msg.Push{
ResourceType: msg.ResourceTransaction,
Resource: txn,
Expand Down Expand Up @@ -117,7 +117,6 @@ func TestRequestHandlerNewBlockOK(t *testing.T) {
"block number should be "+string(blockNumber))
}

<<<<<<< HEAD
func TestRequestHandlerNewBlockBadParams(t *testing.T) {
initializeChain()

Expand Down Expand Up @@ -161,17 +160,4 @@ func TestRequestHandlerPeerInfo(t *testing.T) {
// Make sure request did not fail.
assert.NotNil(t, res, "resource should contain peer info")
// Assert peer address returned valid.
=======
func TestRequestHandlerNewTestTransaction(t *testing.T) {
intializeQueues()
push := msg.Request{ResourceType: msg.ResourceTransaction}
RequestHandler(&push)
select {
case _, ok := <-TransactionWorkQueue:
if !ok {
t.FailNow()
}
}
// Add more here...
>>>>>>> dev
}
111 changes: 0 additions & 111 deletions app/console.go

This file was deleted.

6 changes: 3 additions & 3 deletions app/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ func (r *MockResponder) Unlock() {
}

func init() {
badTxn = bc.NewTestTransaction()
badBlk = bc.NewTestBlock()
badTxn = bc.NewTransaction()
badBlk = bc.NewBlock()
}

func reset() {
tpool = pool.New()
chain, legitBlock = bc.NewValidTestChainAndBlock()
chain, legitBlock = bc.NewValidChainAndBlock()
legitTransaction = legitBlock.Transactions[1]
realWorker = NewWorker(7)
mockResponder = MockResponder{
Expand Down
4 changes: 2 additions & 2 deletions blockchain/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func TestEncodeDecodeBlock(t *testing.T) {
b1 := NewTestBlock()
b1 := NewBlock()

buf := bytes.NewBuffer(make([]byte, 0, b1.Len()))

Expand All @@ -19,7 +19,7 @@ func TestEncodeDecodeBlock(t *testing.T) {
}

func TestContainsTransaction(t *testing.T) {
b := NewTestBlock()
b := NewBlock()

if exists, _ := b.ContainsTransaction(b.Transactions[0]); !exists {
t.Fail()
Expand Down
2 changes: 1 addition & 1 deletion blockchain/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestMain(t *testing.T) {
}

func TestEncodeDecodeBlockChain(t *testing.T) {
b1 := NewTestBlockChain()
b1 := NewBlockChain()

buf := bytes.NewBuffer(make([]byte, 0, b1.Len()))

Expand Down
Loading

0 comments on commit 992236f

Please sign in to comment.