Skip to content

Commit

Permalink
ConnectAndDiscover method and log level
Browse files Browse the repository at this point in the history
  • Loading branch information
chadlagore committed Jul 1, 2017
1 parent dd580fe commit 8a7a92e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 33 deletions.
62 changes: 34 additions & 28 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,34 +53,38 @@ func Run(cfg conf.Config) {
}
}()

// If a target peer was supplied, connect to it and try discover and connect
// to its peers
if len(cfg.Target) > 0 {
// 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
go peer.MaintainConnections()

// Request the blockchain.
log.Info("Requesting blockchain from peers... ")
RequestBlockChain()

// Return to command line.
select {}
}

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

log.Infof("Dialing target %s", cfg.Target)
c, err := conn.Dial(cfg.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)
}

// Try maintain as close to peer.MaxPeers connections as possible while this
// peer is running
go peer.MaintainConnections()

// Request the blockchain.
log.Info("Requesting blockchain from peers... ")
RequestBlockChain()

// Return to command line.
select {}
}

// RequestHandler is called every time a peer sends us a request message except
Expand All @@ -92,16 +96,10 @@ func RequestHandler(req *msg.Request) msg.Response {
case msg.ResourcePeerInfo:
res.Resource = peer.PStore.Addrs()
case msg.ResourceBlock:
// Unmarshal request.
work := BlockWork{}
// Define callback
// Add to BlockWorkQueue
BlockWorkQueue <- work
case msg.ResourceTransaction:
// Unmarshal request.
work := TransactionWork{}
// Define callback.
// Add to TransactionWorkQueue
TransactionWorkQueue <- work
default:
res.Error = msg.NewProtocolError(msg.InvalidResourceType,
Expand All @@ -116,13 +114,21 @@ func RequestHandler(req *msg.Request) msg.Response {
func PushHandler(push *msg.Push) {
switch push.ResourceType {
case msg.ResourceBlock:
work := BlockWork{}
log.Info("Adding transaction.")
BlockWorkQueue <- work
blk, ok := push.Resource.(*blockchain.Block)
if ok {
log.Info("Adding block to work queue.")
BlockWorkQueue <- BlockWork{blk, nil}
} else {
log.Error("Could not cast resource to block.")
}
case msg.ResourceTransaction:
work := TransactionWork{}
log.Info("Adding block.")
TransactionWorkQueue <- work
txn, ok := push.Resource.(*blockchain.Transaction)
if ok {
log.Info("Adding transaction to work queue.")
TransactionWorkQueue <- TransactionWork{txn, nil}
} else {
log.Error("Could not cast resource to transaction.")
}
default:
// Invalid resource type. Ignore
}
Expand Down
25 changes: 20 additions & 5 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"time"

log "github.com/Sirupsen/logrus"
"github.com/ubclaunchpad/cumulus/blockchain"
"github.com/ubclaunchpad/cumulus/msg"
)

func init() {
log.SetLevel(log.DebugLevel)
log.SetLevel(log.InfoLevel)
}

func TestKillWorkers(t *testing.T) {
Expand Down Expand Up @@ -52,26 +53,40 @@ func TestInitializeNode(t *testing.T) {

func TestPushHandlerNewBlock(t *testing.T) {
intializeQueues()
push := msg.Push{ResourceType: msg.ResourceBlock}
_, b := blockchain.NewValidChainAndBlock()
push := msg.Push{
ResourceType: msg.ResourceBlock,
Resource: b,
}
PushHandler(&push)
select {
case _, ok := <-BlockWorkQueue:
case work, ok := <-BlockWorkQueue:
if !ok {
t.FailNow()
}
if work.Block != b {
t.FailNow()
}
}
// Add more here...
}

func TestPushHandlerNewTransaction(t *testing.T) {
intializeQueues()
push := msg.Push{ResourceType: msg.ResourceTransaction}
txn := blockchain.NewTransaction()
push := msg.Push{
ResourceType: msg.ResourceTransaction,
Resource: txn,
}
PushHandler(&push)
select {
case _, ok := <-TransactionWorkQueue:
case work, ok := <-TransactionWorkQueue:
if !ok {
t.FailNow()
}
if work.Transaction != txn {
t.FailNow()
}
}
// Add more here...
}
Expand Down

0 comments on commit 8a7a92e

Please sign in to comment.