Skip to content

Commit

Permalink
Merge branch 'dev' into 90-app-peer-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bfbachmann committed Jul 15, 2017
2 parents d91db57 + 36824e2 commit 90846ca
Show file tree
Hide file tree
Showing 21 changed files with 417 additions and 105 deletions.
19 changes: 19 additions & 0 deletions PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## Related Issue
[link]()

## Description
* Write a few sentences describing the overall goal of this PR.
* List the general components of the application that this PR will affect.

## WIKI Updates
* [link]()

## Todos

General:
- [ ] Tests
- [ ] Documentation
- [ ] Wiki

Other (links to TODOs in code):
* [link]()
19 changes: 5 additions & 14 deletions blockchain/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package blockchain

// BlockHeader contains metadata about a block
import (
"encoding/binary"
"encoding/gob"
"fmt"
"io"

"github.com/ubclaunchpad/cumulus/common/util"
)

// BlockHeader contains metadata about a block
Expand All @@ -29,21 +30,11 @@ type BlockHeader struct {
// Marshal converts a BlockHeader to a byte slice
func (bh *BlockHeader) Marshal() []byte {
var buf []byte

tempBufBlockNumber := make([]byte, 4)
binary.LittleEndian.PutUint32(tempBufBlockNumber, bh.BlockNumber)

tempBufTime := make([]byte, 4)
binary.LittleEndian.PutUint32(tempBufTime, bh.Time)

tempBufNonce := make([]byte, 8)
binary.LittleEndian.PutUint64(tempBufNonce, bh.Nonce)

buf = append(buf, tempBufBlockNumber...)
buf = util.AppendUint32(buf, bh.BlockNumber)
buf = append(buf, bh.LastBlock.Marshal()...)
buf = append(buf, bh.Target.Marshal()...)
buf = append(buf, tempBufTime...)
buf = append(buf, tempBufNonce...)
buf = util.AppendUint32(buf, bh.Time)
buf = util.AppendUint64(buf, bh.Nonce)
buf = append(buf, bh.ExtraData...)

return buf
Expand Down
7 changes: 4 additions & 3 deletions blockchain/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package blockchain
import (
"bytes"
"testing"
"time"

"github.com/ubclaunchpad/cumulus/common/util"
)

func TestEncodeDecodeBlock(t *testing.T) {
Expand Down Expand Up @@ -32,7 +33,7 @@ func TestBlockHeaderLen(t *testing.T) {
0,
NewTestHash(),
NewValidTestTarget(),
uint32(time.Now().Unix()),
util.UnixNow(),
0,
[]byte{0x00, 0x01, 0x02},
}
Expand All @@ -47,7 +48,7 @@ func TestBlockHeaderLen(t *testing.T) {
0,
NewTestHash(),
NewValidTestTarget(),
uint32(time.Now().Unix()),
util.UnixNow(),
0,
[]byte{},
}
Expand Down
6 changes: 2 additions & 4 deletions blockchain/genesis.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package blockchain

import (
"time"
)
import "github.com/ubclaunchpad/cumulus/common/util"

// Genesis creates the Genesis block and returns is.
//
Expand Down Expand Up @@ -36,7 +34,7 @@ func Genesis(miner Address, target Hash, blockReward uint64, extraData []byte) *
BlockNumber: 0,
LastBlock: NilHash,
Target: target,
Time: uint32(time.Now().Unix()),
Time: util.UnixNow(),
Nonce: 0,
ExtraData: extraData,
},
Expand Down
5 changes: 3 additions & 2 deletions blockchain/genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package blockchain

import (
"math/big"
"testing"

c "github.com/ubclaunchpad/cumulus/common/constants"
)

func TestGenesis(t *testing.T) {
Expand All @@ -22,7 +23,7 @@ func TestGenesis(t *testing.T) {
}

// Check if the genesis block's last block hash is equal to 0
if HashToBigInt(gb.BlockHeader.LastBlock).Cmp(big.NewInt(0)) != 0 {
if HashToBigInt(gb.BlockHeader.LastBlock).Cmp(c.Big0) != 0 {
t.Fail()
}

Expand Down
7 changes: 5 additions & 2 deletions blockchain/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package blockchain
import (
"crypto/sha256"
"math/big"

c "github.com/ubclaunchpad/cumulus/common/constants"
)

const (
Expand All @@ -12,7 +14,7 @@ const (

var (
// NilHash represents a nil hash
NilHash = BigIntToHash(big.NewInt(0))
NilHash = BigIntToHash(c.Big0)
)

// Hash represents a 256-bit hash of a block or transaction
Expand Down Expand Up @@ -58,7 +60,8 @@ func HashToBigInt(h Hash) *big.Int {
return new(big.Int).SetBytes(h[:])
}

// BigIntToHash converts a big integer to a hash
// BigIntToHash converts a big integer to a hash. If the size of the big int
// is larger than the size of hash, the function will return a hash set to 0.
func BigIntToHash(x *big.Int) Hash {
bytes := x.Bytes()

Expand Down
32 changes: 31 additions & 1 deletion blockchain/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,41 @@ package blockchain
import (
"math/big"
"testing"

c "github.com/ubclaunchpad/cumulus/common/constants"
"github.com/ubclaunchpad/cumulus/common/util"
)

func TestBigIntToHash(t *testing.T) {
x := util.BigAdd(util.BigExp(2, 256), c.Big1)
h := BigIntToHash(x)

for i := 0; i < HashLen; i++ {
if h[i] != 0 {
t.Fail()
}
}

h = BigIntToHash(c.Big0)

for i := 0; i < HashLen; i++ {
if h[i] != 0 {
t.Fail()
}
}

h = BigIntToHash(c.MaxUint256)

for i := 0; i < HashLen; i++ {
if h[i] != 255 {
t.Fail()
}
}
}

func TestHashToBigInt(t *testing.T) {
// Max hash value
x := new(big.Int).Sub(new(big.Int).Exp(big.NewInt(int64(2)), big.NewInt(int64(256)), big.NewInt(0)), big.NewInt(1))
x := c.MaxUint256
h := BigIntToHash(x)

if HashToBigInt(h).Cmp(x) != 0 {
Expand Down
11 changes: 7 additions & 4 deletions blockchain/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"math/big"
mrand "math/rand"
"time"

c "github.com/ubclaunchpad/cumulus/common/constants"
"github.com/ubclaunchpad/cumulus/common/util"
)

// NewTestHash produces a hash.
Expand Down Expand Up @@ -99,7 +102,7 @@ func NewTestInputBlock(t []*Transaction) *Block {
BlockNumber: 0,
LastBlock: NewTestHash(),
Target: NewValidTestTarget(),
Time: uint32(time.Now().Unix()),
Time: util.UnixNow(),
Nonce: 0,
},
Transactions: t,
Expand All @@ -114,7 +117,7 @@ func NewTestOutputBlock(t []*Transaction, input *Block) *Block {
BlockNumber: input.BlockNumber + 1,
LastBlock: HashSum(input),
Target: NewValidTestTarget(),
Time: uint32(time.Now().Unix()),
Time: util.UnixNow(),
Nonce: 0,
},
Transactions: t,
Expand Down Expand Up @@ -204,9 +207,9 @@ func NewValidTestChainAndBlock() (*BlockChain, *Block) {
func NewValidTestTarget() Hash {
r := new(big.Int).Rand(
mrand.New(mrand.NewSource(time.Now().Unix())),
new(big.Int).Add(MaxTarget, big.NewInt(1)),
util.BigAdd(MaxTarget, c.Big1),
)
r.Add(r, big.NewInt(1))
r.Add(r, c.Big1)
return BigIntToHash(r)
}

Expand Down
10 changes: 5 additions & 5 deletions blockchain/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package blockchain
import (
"encoding/binary"
"io"

"github.com/ubclaunchpad/cumulus/common/util"
)

// TxHashPointer is a reference to a transaction on the blockchain.
Expand All @@ -14,12 +16,10 @@ type TxHashPointer struct {

// Marshal converts a TxHashPointer to a byte slice
func (thp TxHashPointer) Marshal() []byte {
buf := make([]byte, 4)
binary.LittleEndian.PutUint32(buf, thp.BlockNumber)
var buf []byte
buf = util.AppendUint32(buf, thp.BlockNumber)
buf = append(buf, thp.Hash.Marshal()...)
tempBufIndex := make([]byte, 4)
binary.LittleEndian.PutUint32(tempBufIndex, thp.Index)
buf = append(buf, tempBufIndex...)
buf = util.AppendUint32(buf, thp.Index)
return buf
}

Expand Down
15 changes: 5 additions & 10 deletions blockchain/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package blockchain
// ValidTransaction checks whether a transaction is valid, assuming the
import (
"crypto/ecdsa"
"math/big"

log "github.com/Sirupsen/logrus"
c "github.com/ubclaunchpad/cumulus/common/constants"
"github.com/ubclaunchpad/cumulus/common/util"
)

// TransactionCode is returned from ValidTransaction.
Expand Down Expand Up @@ -110,9 +111,9 @@ const (

var (
// MaxTarget is the minimum difficulty
MaxTarget = new(big.Int).Sub(BigExp(2, 232), big.NewInt(1))
MaxTarget = util.BigSub(util.BigExp(2, 232), c.Big1)
// MinTarget is the maximum difficulty value
MinTarget = big.NewInt(1)
MinTarget = c.Big1
)

// ValidTransaction tests whether a transaction valid.
Expand Down Expand Up @@ -203,7 +204,7 @@ func (bc *BlockChain) ValidGenesisBlock(gb *Block) (bool, GenesisBlockCode) {
}

// Check if the genesis block's last block hash is equal to 0.
if HashToBigInt(gb.BlockHeader.LastBlock).Cmp(big.NewInt(0)) != 0 {
if HashToBigInt(gb.BlockHeader.LastBlock).Cmp(c.Big0) != 0 {
return false, BadGenesisLastBlock
}

Expand Down Expand Up @@ -304,9 +305,3 @@ func (bc *BlockChain) ValidBlock(b *Block) (bool, BlockCode) {

return true, ValidBlock
}

// BigExp returns an big int pointer with the result set to base**exp,
// if exp <= 0, the result is 1
func BigExp(base, exp int) *big.Int {
return new(big.Int).Exp(big.NewInt(int64(base)), big.NewInt(int64(exp)), nil)
}
39 changes: 7 additions & 32 deletions blockchain/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package blockchain
import (
"crypto/rand"
"fmt"
"math/big"
"testing"

c "github.com/ubclaunchpad/cumulus/common/constants"
"github.com/ubclaunchpad/cumulus/common/util"
)

func TestValidTransactionNilTransaction(t *testing.T) {
Expand Down Expand Up @@ -116,7 +118,7 @@ func TestValidBlockBadGenesisBlock(t *testing.T) {
currentTarget := BigIntToHash(MaxTarget)
currentBlockReward := uint64(25)
gb := Genesis(miner.Public(), currentTarget, currentBlockReward, []byte{})
gb.Target = BigIntToHash(BigExp(2, 255))
gb.Target = BigIntToHash(util.BigExp(2, 255))
bc := &BlockChain{
Blocks: []*Block{gb},
Head: HashSum(gb),
Expand Down Expand Up @@ -191,7 +193,7 @@ func TestValidBlockBadTime(t *testing.T) {

func TestValidBlockBadTarget(t *testing.T) {
bc, b := NewValidTestChainAndBlock()
b.Target = BigIntToHash(new(big.Int).Add(MaxTarget, big.NewInt(1)))
b.Target = BigIntToHash(util.BigAdd(MaxTarget, c.Big1))
valid, code := bc.ValidBlock(b)

if valid {
Expand All @@ -201,7 +203,7 @@ func TestValidBlockBadTarget(t *testing.T) {
t.Fail()
}

b.Target = BigIntToHash(new(big.Int).Sub(MinTarget, big.NewInt(1)))
b.Target = BigIntToHash(util.BigSub(MinTarget, c.Big1))
valid, code = bc.ValidBlock(b)

if valid {
Expand Down Expand Up @@ -552,7 +554,7 @@ func TestValidGenesisBlockBadGenesisTarget(t *testing.T) {
currentTarget := BigIntToHash(MaxTarget)
currentBlockReward := uint64(25)
gb := Genesis(miner.Public(), currentTarget, currentBlockReward, []byte{})
gb.Target = BigIntToHash(BigExp(2, 255))
gb.Target = BigIntToHash(util.BigExp(2, 255))
bc := &BlockChain{
Blocks: []*Block{gb},
Head: HashSum(gb),
Expand Down Expand Up @@ -590,30 +592,3 @@ func TestValidGenesisBlockBadGenesisTime(t *testing.T) {
t.Fail()
}
}

func TestBigExp(t *testing.T) {
a := big.NewInt(1)
b := BigExp(0, 0)

if a.Cmp(b) != 0 {
t.Fail()
}

a = big.NewInt(1)
b = BigExp(10, -2)

if a.Cmp(b) != 0 {
t.Fail()
}

a = new(big.Int).Exp(
big.NewInt(int64(2)),
big.NewInt(int64(256)),
big.NewInt(0),
)
b = BigExp(2, 256)

if a.Cmp(b) != 0 {
t.Fail()
}
}
Loading

0 comments on commit 90846ca

Please sign in to comment.