Skip to content

Commit

Permalink
included Cloudbase transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
chadlagore committed Jul 15, 2017
1 parent bc0d826 commit f78a5a9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
28 changes: 28 additions & 0 deletions app/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package app

import "github.com/ubclaunchpad/cumulus/blockchain"

// User holds basic user information.
type User struct {
// Wallet holds the users wallets (currently just support 1).
Wallet blockchain.Wallet
}

var currentUser *User

func init() {
// Temporary to create a new user for testing.
currentUser = NewUser()
}

// NewUser creates a new user
func NewUser() *User {
return &User{
Wallet: blockchain.NewWallet(),
}
}

// GetCurrentUser returns the current user.
func GetCurrentUser() *User {
return currentUser
}
7 changes: 4 additions & 3 deletions app/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@ func (w *AppWorker) HandleTransaction(work TransactionWork) {
func (w *AppWorker) HandleBlock(work BlockWork) {
validBlock := tpool.Update(work.Block, chain)

if ok {
if validBlock {
// Append to the chain before requesting
// the next block so that the block
// numbers make sense.
chain.AppendBlock(work.Block)
blk := tpool.NextBlock(chain)
address := GetCurrentUser().Wallet.Public()
blk := tpool.NextBlock(chain, address)
if miner.IsMining() {
miner.RestartMiner(chain, blk)
}
Expand All @@ -91,6 +92,6 @@ func (w *AppWorker) HandleBlock(work BlockWork) {
if work.Responder != nil {
work.Responder.Lock()
defer work.Responder.Unlock()
work.Responder.Send(ok)
work.Responder.Send(validBlock)
}
}
7 changes: 6 additions & 1 deletion pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/ubclaunchpad/cumulus/blockchain"
"github.com/ubclaunchpad/cumulus/miner"
)

// PooledTransaction is a Transaction with a timetamp.
Expand Down Expand Up @@ -126,7 +127,8 @@ func (p *Pool) Peek() *blockchain.Transaction {
}

// NextBlock produces a new block from the pool for mining.
func (p *Pool) NextBlock(chain *blockchain.BlockChain) *blockchain.Block {
func (p *Pool) NextBlock(chain *blockchain.BlockChain,
address blockchain.Address) *blockchain.Block {
var txns []*blockchain.Transaction

// Hash the last block in the chain.
Expand All @@ -143,6 +145,9 @@ func (p *Pool) NextBlock(chain *blockchain.BlockChain) *blockchain.Block {
}, Transactions: txns,
}

// Prepend the cloudbase transaction for this miner.
miner.CloudBase(b, chain, address)

// Try to grab as many transactions as the block will allow.
// Test each transaction to see if we break size before adding.
for p.Len() > 0 {
Expand Down
6 changes: 4 additions & 2 deletions pool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,13 @@ func TestNextBlock(t *testing.T) {
for i := 0; i < numTxns; i++ {
p.SetUnsafe(blockchain.NewTestTransaction())
}
b := p.NextBlock(chain)
b := p.NextBlock(chain, blockchain.NewWallet().Public())
assert.NotNil(t, b)
assert.True(t, b.Len() < blockchain.UserBlockSize)
assert.True(t, b.Len() > 0)
assert.Equal(t, len(b.Transactions), numTxns-p.Len())

// The difference is off by one thanks to cloud transaction.
assert.Equal(t, len(b.Transactions), numTxns-p.Len()+1)
assert.Equal(t, blockchain.HashSum(lastBlk), b.LastBlock)
assert.Equal(t, uint64(0), b.Nonce)
assert.Equal(t, uint32(nBlks), b.BlockNumber)
Expand Down

0 comments on commit f78a5a9

Please sign in to comment.