Skip to content

Commit

Permalink
util and constants folders
Browse files Browse the repository at this point in the history
  • Loading branch information
david-julien committed Jul 14, 2017
1 parent 06cb4a9 commit f933cf9
Show file tree
Hide file tree
Showing 17 changed files with 45 additions and 43 deletions.
8 changes: 4 additions & 4 deletions blockchain/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"io"

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

// BlockHeader contains metadata about a block
Expand All @@ -30,11 +30,11 @@ type BlockHeader struct {
// Marshal converts a BlockHeader to a byte slice
func (bh *BlockHeader) Marshal() []byte {
var buf []byte
buf = common.AppendUint32(buf, bh.BlockNumber)
buf = util.AppendUint32(buf, bh.BlockNumber)
buf = append(buf, bh.LastBlock.Marshal()...)
buf = append(buf, bh.Target.Marshal()...)
buf = common.AppendUint32(buf, bh.Time)
buf = common.AppendUint64(buf, bh.Nonce)
buf = util.AppendUint32(buf, bh.Time)
buf = util.AppendUint64(buf, bh.Nonce)
buf = append(buf, bh.ExtraData...)

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

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

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

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

// Genesis creates the Genesis block and returns is.
//
Expand Down Expand Up @@ -34,7 +34,7 @@ func Genesis(miner Address, target Hash, blockReward uint64, extraData []byte) *
BlockNumber: 0,
LastBlock: NilHash,
Target: target,
Time: common.UnixNow(),
Time: util.UnixNow(),
Nonce: 0,
ExtraData: extraData,
},
Expand Down
4 changes: 2 additions & 2 deletions blockchain/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"testing"

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

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

for i := 0; i < HashLen; i++ {
Expand Down
9 changes: 4 additions & 5 deletions blockchain/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
mrand "math/rand"
"time"

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

// NewTestHash produces a hash.
Expand Down Expand Up @@ -103,7 +102,7 @@ func NewTestInputBlock(t []*Transaction) *Block {
BlockNumber: 0,
LastBlock: NewTestHash(),
Target: NewValidTestTarget(),
Time: common.UnixNow(),
Time: util.UnixNow(),
Nonce: 0,
},
Transactions: t,
Expand All @@ -118,7 +117,7 @@ func NewTestOutputBlock(t []*Transaction, input *Block) *Block {
BlockNumber: input.BlockNumber + 1,
LastBlock: HashSum(input),
Target: NewValidTestTarget(),
Time: common.UnixNow(),
Time: util.UnixNow(),
Nonce: 0,
},
Transactions: t,
Expand Down Expand Up @@ -208,7 +207,7 @@ func NewValidTestChainAndBlock() (*BlockChain, *Block) {
func NewValidTestTarget() Hash {
r := new(big.Int).Rand(
mrand.New(mrand.NewSource(time.Now().Unix())),
math.BigAdd(MaxTarget, c.Big1),
util.BigAdd(MaxTarget, c.Big1),
)
r.Add(r, c.Big1)
return BigIntToHash(r)
Expand Down
6 changes: 3 additions & 3 deletions blockchain/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/binary"
"io"

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

// TxHashPointer is a reference to a transaction on the blockchain.
Expand All @@ -17,9 +17,9 @@ type TxHashPointer struct {
// Marshal converts a TxHashPointer to a byte slice
func (thp TxHashPointer) Marshal() []byte {
var buf []byte
buf = common.AppendUint32(buf, thp.BlockNumber)
buf = util.AppendUint32(buf, thp.BlockNumber)
buf = append(buf, thp.Hash.Marshal()...)
buf = common.AppendUint32(buf, thp.Index)
buf = util.AppendUint32(buf, thp.Index)
return buf
}

Expand Down
4 changes: 2 additions & 2 deletions blockchain/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

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

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

var (
// MaxTarget is the minimum difficulty
MaxTarget = math.BigSub(math.BigExp(2, 232), c.Big1)
MaxTarget = util.BigSub(util.BigExp(2, 232), c.Big1)
// MinTarget is the maximum difficulty value
MinTarget = c.Big1
)
Expand Down
10 changes: 5 additions & 5 deletions blockchain/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"

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

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

func TestValidBlockBadTarget(t *testing.T) {
bc, b := NewValidTestChainAndBlock()
b.Target = BigIntToHash(math.BigAdd(MaxTarget, c.Big1))
b.Target = BigIntToHash(util.BigAdd(MaxTarget, c.Big1))
valid, code := bc.ValidBlock(b)

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

b.Target = BigIntToHash(math.BigSub(MinTarget, c.Big1))
b.Target = BigIntToHash(util.BigSub(MinTarget, c.Big1))
valid, code = bc.ValidBlock(b)

if valid {
Expand Down Expand Up @@ -554,7 +554,7 @@ func TestValidGenesisBlockBadGenesisTarget(t *testing.T) {
currentTarget := BigIntToHash(MaxTarget)
currentBlockReward := uint64(25)
gb := Genesis(miner.Public(), currentTarget, currentBlockReward, []byte{})
gb.Target = BigIntToHash(math.BigExp(2, 255))
gb.Target = BigIntToHash(util.BigExp(2, 255))
bc := &BlockChain{
Blocks: []*Block{gb},
Head: HashSum(gb),
Expand Down
11 changes: 7 additions & 4 deletions common/constants/big.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package constants

import "github.com/ubclaunchpad/cumulus/common/math"
import "math/big"
import (
"math/big"

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

// Commonly used "math/big" type numbers
var (
Expand All @@ -10,11 +13,11 @@ var (
// Big1 is 1 represented as type "math/big"
Big1 = big.NewInt(1)
// Big2Exp256 is 2^256 respresented as type "math/big"
Big2Exp256 = math.BigExp(2, 256)
Big2Exp256 = util.BigExp(2, 256)
)

// Commonly used max values represented as type "math/big"
var (
// MaxUint256 is the maximum uint256 number represented as type "math/big"
MaxUint256 = math.BigSub(Big2Exp256, Big1)
MaxUint256 = util.BigSub(Big2Exp256, Big1)
)
2 changes: 1 addition & 1 deletion common/math/big.go → common/util/big.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package math
package util

import "math/big"

Expand Down
2 changes: 1 addition & 1 deletion common/math/big_test.go → common/util/big_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package math
package util

import (
"math/big"
Expand Down
2 changes: 1 addition & 1 deletion common/bytes.go → common/util/bytes.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package common
package util

import "encoding/binary"

Expand Down
2 changes: 1 addition & 1 deletion common/bytes_test.go → common/util/bytes_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package common
package util

import (
"math"
Expand Down
2 changes: 1 addition & 1 deletion common/time.go → common/util/time.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package common
package util

import "time"

Expand Down
4 changes: 2 additions & 2 deletions consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"math/rand"

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

func TestHalveReward(t *testing.T) {
Expand Down Expand Up @@ -154,7 +154,7 @@ func newValidBlockChainAndCloudBaseBlock() (
BlockNumber: bcSize,
LastBlock: blockchain.HashSum(bc.Blocks[bcSize-1]),
Target: CurrentTarget(),
Time: common.UnixNow(),
Time: util.UnixNow(),
Nonce: 0,
},
Transactions: make([]*blockchain.Transaction, 1),
Expand Down
4 changes: 2 additions & 2 deletions miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

log "github.com/Sirupsen/logrus"
"github.com/ubclaunchpad/cumulus/blockchain"
"github.com/ubclaunchpad/cumulus/common"
"github.com/ubclaunchpad/cumulus/common/util"
"github.com/ubclaunchpad/cumulus/consensus"
)

Expand Down Expand Up @@ -65,7 +65,7 @@ func Mine(bc *blockchain.BlockChain, b *blockchain.Block) *MiningResult {
}

// Timestamp and increase the nonce.
b.Time = common.UnixNow()
b.Time = util.UnixNow()
b.Nonce++
}

Expand Down
8 changes: 4 additions & 4 deletions miner/miner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

"github.com/stretchr/testify/assert"
"github.com/ubclaunchpad/cumulus/blockchain"
"github.com/ubclaunchpad/cumulus/common"
c "github.com/ubclaunchpad/cumulus/common/constants"
"github.com/ubclaunchpad/cumulus/common/util"
"github.com/ubclaunchpad/cumulus/consensus"
)

Expand All @@ -22,7 +22,7 @@ func TestMine(t *testing.T) {
// Set target to be as easy as possible so that we find a hash
// below the target straight away (2**256 - 1)
b.Target = blockchain.BigIntToHash(blockchain.MaxTarget)
b.Time = common.UnixNow()
b.Time = util.UnixNow()
mineResult := Mine(bc, b)
blockchain.MaxTarget = tempMaxTarget

Expand All @@ -43,7 +43,7 @@ func TestMineHaltMiner(t *testing.T) {

// Set target to be as hard as possible so that we stall.
b.Target = blockchain.BigIntToHash(blockchain.MinTarget)
b.Time = common.UnixNow()
b.Time = util.UnixNow()

// Use a thread to stop the miner a few moments after starting.
go func() {
Expand All @@ -67,7 +67,7 @@ func TestCloudBase(t *testing.T) {
BlockNumber: bcSize,
LastBlock: blockchain.HashSum(bc.Blocks[bcSize-1]),
Target: consensus.CurrentTarget(),
Time: common.UnixNow(),
Time: util.UnixNow(),
Nonce: 0,
},
Transactions: make([]*blockchain.Transaction, 0),
Expand Down

0 comments on commit f933cf9

Please sign in to comment.