From 6563c9650d8844068f8ec802856d4d35a08c96b7 Mon Sep 17 00:00:00 2001 From: David Julien Date: Wed, 12 Jul 2017 22:50:24 -0700 Subject: [PATCH 1/8] added common folder with big constants and math, and byte slice manipulation --- blockchain/block.go | 19 ++----- blockchain/genesis_test.go | 5 +- blockchain/hash.go | 4 +- blockchain/test_utils.go | 7 ++- blockchain/transaction.go | 10 ++-- blockchain/validation.go | 15 ++---- blockchain/validation_test.go | 39 +++----------- blockchain/wallet.go | 6 ++- common/bytes.go | 17 +++++++ common/bytes_test.go | 96 +++++++++++++++++++++++++++++++++++ common/constants/big.go | 18 +++++++ common/math/big.go | 20 ++++++++ common/math/big_test.go | 70 +++++++++++++++++++++++++ consensus/consensus_test.go | 9 ++-- miner/miner_test.go | 12 ++--- 15 files changed, 264 insertions(+), 83 deletions(-) create mode 100644 common/bytes.go create mode 100644 common/bytes_test.go create mode 100644 common/constants/big.go create mode 100644 common/math/big.go create mode 100644 common/math/big_test.go diff --git a/blockchain/block.go b/blockchain/block.go index 4d67a7f..8123e64 100644 --- a/blockchain/block.go +++ b/blockchain/block.go @@ -2,10 +2,11 @@ package blockchain // BlockHeader contains metadata about a block import ( - "encoding/binary" "encoding/gob" "fmt" "io" + + "github.com/ubclaunchpad/cumulus/common" ) // BlockHeader contains metadata about a block @@ -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 = common.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 = common.AppendUint32(buf, bh.Time) + buf = common.AppendUint64(buf, bh.Nonce) buf = append(buf, bh.ExtraData...) return buf diff --git a/blockchain/genesis_test.go b/blockchain/genesis_test.go index 945721d..fa8c518 100644 --- a/blockchain/genesis_test.go +++ b/blockchain/genesis_test.go @@ -1,8 +1,9 @@ package blockchain import ( - "math/big" "testing" + + c "github.com/ubclaunchpad/cumulus/common/constants" ) func TestGenesis(t *testing.T) { @@ -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() } diff --git a/blockchain/hash.go b/blockchain/hash.go index c9e7c00..467c1a5 100644 --- a/blockchain/hash.go +++ b/blockchain/hash.go @@ -3,6 +3,8 @@ package blockchain import ( "crypto/sha256" "math/big" + + c "github.com/ubclaunchpad/cumulus/common/constants" ) const ( @@ -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 diff --git a/blockchain/test_utils.go b/blockchain/test_utils.go index 6e747be..09c4198 100644 --- a/blockchain/test_utils.go +++ b/blockchain/test_utils.go @@ -6,6 +6,9 @@ import ( "math/big" mrand "math/rand" "time" + + c "github.com/ubclaunchpad/cumulus/common/constants" + "github.com/ubclaunchpad/cumulus/common/math" ) // NewTestHash produces a hash. @@ -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)), + math.BigAdd(MaxTarget, c.Big1), ) - r.Add(r, big.NewInt(1)) + r.Add(r, c.Big1) return BigIntToHash(r) } diff --git a/blockchain/transaction.go b/blockchain/transaction.go index efaf165..d13040e 100644 --- a/blockchain/transaction.go +++ b/blockchain/transaction.go @@ -3,6 +3,8 @@ package blockchain import ( "encoding/binary" "io" + + "github.com/ubclaunchpad/cumulus/common" ) // TxHashPointer is a reference to a transaction on the blockchain. @@ -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 = common.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 = common.AppendUint32(buf, thp.Index) return buf } diff --git a/blockchain/validation.go b/blockchain/validation.go index 7ec0007..d254dea 100644 --- a/blockchain/validation.go +++ b/blockchain/validation.go @@ -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/math" ) // TransactionCode is returned from ValidTransaction. @@ -110,9 +111,9 @@ const ( var ( // MaxTarget is the minimum difficulty - MaxTarget = new(big.Int).Sub(BigExp(2, 232), big.NewInt(1)) + MaxTarget = math.BigSub(math.BigExp(2, 232), c.Big1) // MinTarget is the maximum difficulty value - MinTarget = big.NewInt(1) + MinTarget = c.Big1 ) // ValidTransaction tests whether a transaction valid. @@ -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 } @@ -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) -} diff --git a/blockchain/validation_test.go b/blockchain/validation_test.go index 5277515..9ae366f 100644 --- a/blockchain/validation_test.go +++ b/blockchain/validation_test.go @@ -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/math" ) func TestValidTransactionNilTransaction(t *testing.T) { @@ -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(math.BigExp(2, 255)) bc := &BlockChain{ Blocks: []*Block{gb}, Head: HashSum(gb), @@ -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(math.BigAdd(MaxTarget, c.Big1)) valid, code := bc.ValidBlock(b) if valid { @@ -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(math.BigSub(MinTarget, c.Big1)) valid, code = bc.ValidBlock(b) if valid { @@ -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(math.BigExp(2, 255)) bc := &BlockChain{ Blocks: []*Block{gb}, Head: HashSum(gb), @@ -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() - } -} diff --git a/blockchain/wallet.go b/blockchain/wallet.go index 04a3d13..21502d4 100644 --- a/blockchain/wallet.go +++ b/blockchain/wallet.go @@ -6,6 +6,8 @@ import ( crand "crypto/rand" "io" "math/big" + + c "github.com/ubclaunchpad/cumulus/common/constants" ) const ( @@ -21,9 +23,9 @@ var ( // The curve we use for our ECC crypto. curve = elliptic.P256() // NilSig is a signature representing a failed Sign operation - NilSig = Signature{big.NewInt(0), big.NewInt(0)} + NilSig = Signature{c.Big0, c.Big0} // NilAddr is an address representing no address - NilAddr = Address{big.NewInt(0), big.NewInt(0)} + NilAddr = Address{c.Big0, c.Big0} ) // Address represents a wallet that can be a recipient in a transaction. diff --git a/common/bytes.go b/common/bytes.go new file mode 100644 index 0000000..595d7ed --- /dev/null +++ b/common/bytes.go @@ -0,0 +1,17 @@ +package common + +import "encoding/binary" + +// AppendUint32 appends a uint32 to a slice, and returns the appended slice +func AppendUint32(s []byte, n uint32) []byte { + temp := make([]byte, 4) + binary.LittleEndian.PutUint32(temp, n) + return append(s, temp...) +} + +// AppendUint64 appends a uint64 to a slice, and returns the appended slice +func AppendUint64(s []byte, n uint64) []byte { + temp := make([]byte, 8) + binary.LittleEndian.PutUint64(temp, n) + return append(s, temp...) +} diff --git a/common/bytes_test.go b/common/bytes_test.go new file mode 100644 index 0000000..f75a69f --- /dev/null +++ b/common/bytes_test.go @@ -0,0 +1,96 @@ +package common + +import ( + "math" + "testing" +) + +func TestAppendUint32(t *testing.T) { + var buf []byte + buf = AppendUint32(buf, math.MaxUint32) + + if len(buf) != 4 { + t.Fail() + } + + for i := 0; i < 4; i++ { + if buf[i] != 255 { + t.Fail() + } + } + + buf = AppendUint32(buf, math.MaxUint32) + + if len(buf) != 8 { + t.Fail() + } + + for i := 0; i < 8; i++ { + if buf[i] != 255 { + t.Fail() + } + } + + buf = AppendUint32(buf, 0) + + if len(buf) != 12 { + t.Fail() + } + + for i := 0; i < 8; i++ { + if buf[i] != 255 { + t.Fail() + } + } + + for i := 8; i < 12; i++ { + if buf[i] != 0 { + t.Fail() + } + } +} + +func TestAppendUint64(t *testing.T) { + var buf []byte + buf = AppendUint64(buf, math.MaxUint64) + + if len(buf) != 8 { + t.Fail() + } + + for i := 0; i < 8; i++ { + if buf[i] != 255 { + t.Fail() + } + } + + buf = AppendUint64(buf, math.MaxUint64) + + if len(buf) != 16 { + t.Fail() + } + + for i := 0; i < 16; i++ { + if buf[i] != 255 { + t.Fail() + } + } + + buf = AppendUint64(buf, 0) + + if len(buf) != 24 { + t.Fail() + } + + for i := 0; i < 16; i++ { + if buf[i] != 255 { + t.Fail() + } + } + + for i := 16; i < 24; i++ { + if buf[i] != 0 { + t.Fail() + } + } +} diff --git a/common/constants/big.go b/common/constants/big.go new file mode 100644 index 0000000..4ae45d0 --- /dev/null +++ b/common/constants/big.go @@ -0,0 +1,18 @@ +package constants + +import "github.com/ubclaunchpad/cumulus/common/math" +import "math/big" + +var ( + // Big0 is 0 represented as type big + Big0 = big.NewInt(0) + // Big1 is 1 represented as type big + Big1 = big.NewInt(1) + // Big2E256 is 2^256 respresented as type big + Big2E256 = math.BigExp(2, 256) +) + +var ( + // MaxUint256 is the maximum uint256 number + MaxUint256 = math.BigSub(Big2E256, Big1) +) diff --git a/common/math/big.go b/common/math/big.go new file mode 100644 index 0000000..eaec7f7 --- /dev/null +++ b/common/math/big.go @@ -0,0 +1,20 @@ +package math + +import "math/big" + +// BigExp returns a big int pointer to the result of base to the power of 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) +} + +// BigSub returns a big int pointer to the result of the subtraction of y from +// x. +func BigSub(x, y *big.Int) *big.Int { + return new(big.Int).Sub(x, y) +} + +// BigAdd returns a big int pointer to the result of the addition of x to y. +func BigAdd(x, y *big.Int) *big.Int { + return new(big.Int).Add(x, y) +} diff --git a/common/math/big_test.go b/common/math/big_test.go new file mode 100644 index 0000000..d1f4ff7 --- /dev/null +++ b/common/math/big_test.go @@ -0,0 +1,70 @@ +package math + +import ( + "math/big" + "testing" +) + +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() + } +} + +func TestBigSub(t *testing.T) { + a := big.NewInt(0) + b := BigSub(big.NewInt(0), big.NewInt(0)) + + if a.Cmp(b) != 0 { + t.Fail() + } + + a = big.NewInt(1) + b = BigSub(big.NewInt(1), big.NewInt(0)) + + if a.Cmp(b) != 0 { + t.Fail() + } + + a = big.NewInt(-1) + b = BigSub(big.NewInt(0), big.NewInt(1)) + + if a.Cmp(b) != 0 { + t.Fail() + } + + a = big.NewInt(2) + b = BigSub(big.NewInt(7), big.NewInt(5)) + + if a.Cmp(b) != 0 { + t.Fail() + } + + a = big.NewInt(2) + b = BigSub(big.NewInt(4), big.NewInt(2)) + + if a.Cmp(b) != 0 { + t.Fail() + } +} diff --git a/consensus/consensus_test.go b/consensus/consensus_test.go index dc5c388..58e9097 100644 --- a/consensus/consensus_test.go +++ b/consensus/consensus_test.go @@ -4,10 +4,10 @@ import ( "testing" "time" - "math/big" "math/rand" "github.com/ubclaunchpad/cumulus/blockchain" + c "github.com/ubclaunchpad/cumulus/common/constants" ) func TestHalveReward(t *testing.T) { @@ -88,7 +88,7 @@ func TestValidMinedBlockBadTarget(t *testing.T) { func TestValidMinedBlockBadNonce(t *testing.T) { bc, b, a := newValidBlockChainAndCloudBaseBlock() - b.Target = blockchain.BigIntToHash(big.NewInt(1)) + b.Target = blockchain.BigIntToHash(c.Big1) tempCurrentDifficulty := CurrentDifficulty CurrentDifficulty = blockchain.MaxTarget @@ -123,10 +123,7 @@ func TestValidMinedBlock(t *testing.T) { tempMaxTarget := blockchain.MaxTarget tempCurrentDifficulty := CurrentDifficulty - blockchain.MaxTarget = new(big.Int).Sub( - blockchain.BigExp(2, 256), - big.NewInt(1), - ) + blockchain.MaxTarget = c.MaxUint256 CurrentDifficulty = blockchain.MinTarget bc, b, a := newValidBlockChainAndCloudBaseBlock() diff --git a/miner/miner_test.go b/miner/miner_test.go index f4efc17..f3cf246 100644 --- a/miner/miner_test.go +++ b/miner/miner_test.go @@ -1,11 +1,11 @@ package miner import ( - "math/big" "testing" "time" "github.com/ubclaunchpad/cumulus/blockchain" + c "github.com/ubclaunchpad/cumulus/common/constants" "github.com/ubclaunchpad/cumulus/consensus" ) @@ -15,10 +15,7 @@ func TestMine(t *testing.T) { // Set min difficulty to be equal to the target so that the block validation // passes - blockchain.MaxTarget = new(big.Int).Sub( - blockchain.BigExp(2, 256), - big.NewInt(1), - ) + blockchain.MaxTarget = c.MaxUint256 // Set target to be as easy as possible so that we find a hash // below the target straight away (2**256 - 1) @@ -74,10 +71,7 @@ func TestCloudBase(t *testing.T) { func TestVerifyProofOfWork(t *testing.T) { _, b := blockchain.NewValidTestChainAndBlock() b.Target = blockchain.BigIntToHash( - new(big.Int).Sub( - blockchain.BigExp(2, 256), - big.NewInt(1), - ), + c.MaxUint256, ) if !VerifyProofOfWork(b) { From 4b6db99ef95c43ede78edea4cb5086061f660477 Mon Sep 17 00:00:00 2001 From: David Julien Date: Wed, 12 Jul 2017 22:54:28 -0700 Subject: [PATCH 2/8] TestBigAdd --- common/math/big_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/common/math/big_test.go b/common/math/big_test.go index d1f4ff7..7621199 100644 --- a/common/math/big_test.go +++ b/common/math/big_test.go @@ -68,3 +68,40 @@ func TestBigSub(t *testing.T) { t.Fail() } } + +func TestBigAdd(t *testing.T) { + a := big.NewInt(0) + b := BigAdd(big.NewInt(0), big.NewInt(0)) + + if a.Cmp(b) != 0 { + t.Fail() + } + + a = big.NewInt(1) + b = BigAdd(big.NewInt(1), big.NewInt(0)) + + if a.Cmp(b) != 0 { + t.Fail() + } + + a = big.NewInt(1) + b = BigAdd(big.NewInt(0), big.NewInt(1)) + + if a.Cmp(b) != 0 { + t.Fail() + } + + a = big.NewInt(12) + b = BigAdd(big.NewInt(7), big.NewInt(5)) + + if a.Cmp(b) != 0 { + t.Fail() + } + + a = big.NewInt(6) + b = BigAdd(big.NewInt(4), big.NewInt(2)) + + if a.Cmp(b) != 0 { + t.Fail() + } +} From 4ed7d6776ebf3719888f9726dcc5b0db027f5842 Mon Sep 17 00:00:00 2001 From: David Julien Date: Wed, 12 Jul 2017 23:02:47 -0700 Subject: [PATCH 3/8] Big2E256 to Big2Exp --- common/constants/big.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/common/constants/big.go b/common/constants/big.go index 4ae45d0..2b60fa0 100644 --- a/common/constants/big.go +++ b/common/constants/big.go @@ -3,16 +3,18 @@ package constants import "github.com/ubclaunchpad/cumulus/common/math" import "math/big" +// Commonly used "math/big" type numbers var ( // Big0 is 0 represented as type big Big0 = big.NewInt(0) // Big1 is 1 represented as type big Big1 = big.NewInt(1) - // Big2E256 is 2^256 respresented as type big - Big2E256 = math.BigExp(2, 256) + // Big2Exp256 is 2^256 respresented as type big + Big2Exp256 = math.BigExp(2, 256) ) +// Commonly used max values represented as type "math/big" var ( // MaxUint256 is the maximum uint256 number - MaxUint256 = math.BigSub(Big2E256, Big1) + MaxUint256 = math.BigSub(Big2Exp256, Big1) ) From f853ddc4f5e91d9a44ffb9ac0d1a9119424de607 Mon Sep 17 00:00:00 2001 From: David Julien Date: Wed, 12 Jul 2017 23:03:07 -0700 Subject: [PATCH 4/8] common big constants documentation update --- common/constants/big.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/common/constants/big.go b/common/constants/big.go index 2b60fa0..ed75c0a 100644 --- a/common/constants/big.go +++ b/common/constants/big.go @@ -5,16 +5,16 @@ import "math/big" // Commonly used "math/big" type numbers var ( - // Big0 is 0 represented as type big + // Big0 is 0 represented as type "math/big" Big0 = big.NewInt(0) - // Big1 is 1 represented as type big + // Big1 is 1 represented as type "math/big" Big1 = big.NewInt(1) - // Big2Exp256 is 2^256 respresented as type big + // Big2Exp256 is 2^256 respresented as type "math/big" Big2Exp256 = math.BigExp(2, 256) ) // Commonly used max values represented as type "math/big" var ( - // MaxUint256 is the maximum uint256 number + // MaxUint256 is the maximum uint256 number represented as type "math/big" MaxUint256 = math.BigSub(Big2Exp256, Big1) ) From d942014d5a0c0465a05474a2af4ee729f2d45d63 Mon Sep 17 00:00:00 2001 From: David Julien Date: Thu, 13 Jul 2017 11:35:33 -0700 Subject: [PATCH 5/8] Fixed TestTransactionLen and TestTxBodyLen random failures --- blockchain/wallet.go | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/blockchain/wallet.go b/blockchain/wallet.go index 21502d4..7cc62c4 100644 --- a/blockchain/wallet.go +++ b/blockchain/wallet.go @@ -36,8 +36,23 @@ type Address struct { // Marshal converts an Address to a byte slice. func (a Address) Marshal() []byte { buf := make([]byte, 0, AddrLen) - buf = append(buf, a.X.Bytes()...) - buf = append(buf, a.Y.Bytes()...) + xBytes := a.X.Bytes() + yBytes := a.Y.Bytes() + + if len(xBytes) < CoordLen { + for i := len(xBytes); i < CoordLen; i++ { + xBytes = append(xBytes, 0) + } + } + + if len(yBytes) < CoordLen { + for i := len(yBytes); i < CoordLen; i++ { + yBytes = append(yBytes, 0) + } + } + + buf = append(buf, xBytes...) + buf = append(buf, yBytes...) return buf } @@ -84,8 +99,23 @@ type Signature struct { // Marshal converts a signature to a byte slice. Should be 64 bytes long. func (s *Signature) Marshal() []byte { buf := make([]byte, 0, SigLen) - buf = append(buf, s.R.Bytes()...) - buf = append(buf, s.S.Bytes()...) + rBytes := s.R.Bytes() + sBytes := s.S.Bytes() + + if len(rBytes) < CoordLen { + for i := len(rBytes); i < CoordLen; i++ { + rBytes = append(rBytes, 0) + } + } + + if len(sBytes) < CoordLen { + for i := len(sBytes); i < CoordLen; i++ { + rBytes = append(sBytes, 0) + } + } + + buf = append(buf, rBytes...) + buf = append(buf, sBytes...) return buf } From fd911d0625398cc305e7f82ea1ace09523415b2b Mon Sep 17 00:00:00 2001 From: David Julien Date: Thu, 13 Jul 2017 11:49:16 -0700 Subject: [PATCH 6/8] BigIntToHash test --- blockchain/hash.go | 3 ++- blockchain/hash_test.go | 32 +++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/blockchain/hash.go b/blockchain/hash.go index 467c1a5..7ff3814 100644 --- a/blockchain/hash.go +++ b/blockchain/hash.go @@ -60,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() diff --git a/blockchain/hash_test.go b/blockchain/hash_test.go index 1df4247..a4320e9 100644 --- a/blockchain/hash_test.go +++ b/blockchain/hash_test.go @@ -3,11 +3,41 @@ package blockchain import ( "math/big" "testing" + + c "github.com/ubclaunchpad/cumulus/common/constants" + "github.com/ubclaunchpad/cumulus/common/math" ) +func TestBigIntToHash(t *testing.T) { + x := math.BigAdd(math.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 { From 06cb4a942cf99d7f0ddaafe039ac2e7fd046daa7 Mon Sep 17 00:00:00 2001 From: David Julien Date: Thu, 13 Jul 2017 12:52:01 -0700 Subject: [PATCH 7/8] uint32(time.Now().Unix()) wrapped --- blockchain/block_test.go | 7 ++++--- blockchain/genesis.go | 6 ++---- blockchain/test_utils.go | 5 +++-- common/time.go | 9 +++++++++ consensus/consensus_test.go | 4 ++-- miner/miner.go | 4 ++-- miner/miner_test.go | 7 ++++--- 7 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 common/time.go diff --git a/blockchain/block_test.go b/blockchain/block_test.go index 78d61a6..e018a75 100644 --- a/blockchain/block_test.go +++ b/blockchain/block_test.go @@ -3,7 +3,8 @@ package blockchain import ( "bytes" "testing" - "time" + + "github.com/ubclaunchpad/cumulus/common" ) func TestEncodeDecodeBlock(t *testing.T) { @@ -32,7 +33,7 @@ func TestBlockHeaderLen(t *testing.T) { 0, NewTestHash(), NewValidTestTarget(), - uint32(time.Now().Unix()), + common.UnixNow(), 0, []byte{0x00, 0x01, 0x02}, } @@ -47,7 +48,7 @@ func TestBlockHeaderLen(t *testing.T) { 0, NewTestHash(), NewValidTestTarget(), - uint32(time.Now().Unix()), + common.UnixNow(), 0, []byte{}, } diff --git a/blockchain/genesis.go b/blockchain/genesis.go index d6477e5..56bba26 100644 --- a/blockchain/genesis.go +++ b/blockchain/genesis.go @@ -1,8 +1,6 @@ package blockchain -import ( - "time" -) +import "github.com/ubclaunchpad/cumulus/common" // Genesis creates the Genesis block and returns is. // @@ -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: common.UnixNow(), Nonce: 0, ExtraData: extraData, }, diff --git a/blockchain/test_utils.go b/blockchain/test_utils.go index 09c4198..c178a7d 100644 --- a/blockchain/test_utils.go +++ b/blockchain/test_utils.go @@ -7,6 +7,7 @@ import ( mrand "math/rand" "time" + "github.com/ubclaunchpad/cumulus/common" c "github.com/ubclaunchpad/cumulus/common/constants" "github.com/ubclaunchpad/cumulus/common/math" ) @@ -102,7 +103,7 @@ func NewTestInputBlock(t []*Transaction) *Block { BlockNumber: 0, LastBlock: NewTestHash(), Target: NewValidTestTarget(), - Time: uint32(time.Now().Unix()), + Time: common.UnixNow(), Nonce: 0, }, Transactions: t, @@ -117,7 +118,7 @@ func NewTestOutputBlock(t []*Transaction, input *Block) *Block { BlockNumber: input.BlockNumber + 1, LastBlock: HashSum(input), Target: NewValidTestTarget(), - Time: uint32(time.Now().Unix()), + Time: common.UnixNow(), Nonce: 0, }, Transactions: t, diff --git a/common/time.go b/common/time.go new file mode 100644 index 0000000..8cb5ad6 --- /dev/null +++ b/common/time.go @@ -0,0 +1,9 @@ +package common + +import "time" + +// UnixNow returns the current Unix time as a uint32. This represents the number +// of seconds elapsed since January 1, 1970 UTC. +func UnixNow() uint32 { + return uint32(time.Now().Unix()) +} diff --git a/consensus/consensus_test.go b/consensus/consensus_test.go index 58e9097..c1a9e14 100644 --- a/consensus/consensus_test.go +++ b/consensus/consensus_test.go @@ -2,11 +2,11 @@ package consensus import ( "testing" - "time" "math/rand" "github.com/ubclaunchpad/cumulus/blockchain" + "github.com/ubclaunchpad/cumulus/common" c "github.com/ubclaunchpad/cumulus/common/constants" ) @@ -154,7 +154,7 @@ func newValidBlockChainAndCloudBaseBlock() ( BlockNumber: bcSize, LastBlock: blockchain.HashSum(bc.Blocks[bcSize-1]), Target: CurrentTarget(), - Time: uint32(time.Now().Unix()), + Time: common.UnixNow(), Nonce: 0, }, Transactions: make([]*blockchain.Transaction, 1), diff --git a/miner/miner.go b/miner/miner.go index e65169c..53e8246 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -3,10 +3,10 @@ package miner import ( "math" "sync" - "time" log "github.com/Sirupsen/logrus" "github.com/ubclaunchpad/cumulus/blockchain" + "github.com/ubclaunchpad/cumulus/common" "github.com/ubclaunchpad/cumulus/consensus" ) @@ -65,7 +65,7 @@ func Mine(bc *blockchain.BlockChain, b *blockchain.Block) *MiningResult { } // Timestamp and increase the nonce. - b.Time = uint32(time.Now().Unix()) + b.Time = common.UnixNow() b.Nonce++ } diff --git a/miner/miner_test.go b/miner/miner_test.go index 184b072..5ec2adf 100644 --- a/miner/miner_test.go +++ b/miner/miner_test.go @@ -6,6 +6,7 @@ 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/consensus" ) @@ -21,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 = uint32(time.Now().Unix()) + b.Time = common.UnixNow() mineResult := Mine(bc, b) blockchain.MaxTarget = tempMaxTarget @@ -42,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 = uint32(time.Now().Unix()) + b.Time = common.UnixNow() // Use a thread to stop the miner a few moments after starting. go func() { @@ -66,7 +67,7 @@ func TestCloudBase(t *testing.T) { BlockNumber: bcSize, LastBlock: blockchain.HashSum(bc.Blocks[bcSize-1]), Target: consensus.CurrentTarget(), - Time: uint32(time.Now().Unix()), + Time: common.UnixNow(), Nonce: 0, }, Transactions: make([]*blockchain.Transaction, 0), From f933cf99ba161378c33352acba6704b8332c0f0b Mon Sep 17 00:00:00 2001 From: David Julien Date: Thu, 13 Jul 2017 18:56:36 -0700 Subject: [PATCH 8/8] util and constants folders --- blockchain/block.go | 8 ++++---- blockchain/block_test.go | 6 +++--- blockchain/genesis.go | 4 ++-- blockchain/hash_test.go | 4 ++-- blockchain/test_utils.go | 9 ++++----- blockchain/transaction.go | 6 +++--- blockchain/validation.go | 4 ++-- blockchain/validation_test.go | 10 +++++----- common/constants/big.go | 11 +++++++---- common/{math => util}/big.go | 2 +- common/{math => util}/big_test.go | 2 +- common/{ => util}/bytes.go | 2 +- common/{ => util}/bytes_test.go | 2 +- common/{ => util}/time.go | 2 +- consensus/consensus_test.go | 4 ++-- miner/miner.go | 4 ++-- miner/miner_test.go | 8 ++++---- 17 files changed, 45 insertions(+), 43 deletions(-) rename common/{math => util}/big.go (97%) rename common/{math => util}/big_test.go (99%) rename common/{ => util}/bytes.go (96%) rename common/{ => util}/bytes_test.go (98%) rename common/{ => util}/time.go (93%) diff --git a/blockchain/block.go b/blockchain/block.go index 8123e64..4d7a9d8 100644 --- a/blockchain/block.go +++ b/blockchain/block.go @@ -6,7 +6,7 @@ import ( "fmt" "io" - "github.com/ubclaunchpad/cumulus/common" + "github.com/ubclaunchpad/cumulus/common/util" ) // BlockHeader contains metadata about a block @@ -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 diff --git a/blockchain/block_test.go b/blockchain/block_test.go index e018a75..400b660 100644 --- a/blockchain/block_test.go +++ b/blockchain/block_test.go @@ -4,7 +4,7 @@ import ( "bytes" "testing" - "github.com/ubclaunchpad/cumulus/common" + "github.com/ubclaunchpad/cumulus/common/util" ) func TestEncodeDecodeBlock(t *testing.T) { @@ -33,7 +33,7 @@ func TestBlockHeaderLen(t *testing.T) { 0, NewTestHash(), NewValidTestTarget(), - common.UnixNow(), + util.UnixNow(), 0, []byte{0x00, 0x01, 0x02}, } @@ -48,7 +48,7 @@ func TestBlockHeaderLen(t *testing.T) { 0, NewTestHash(), NewValidTestTarget(), - common.UnixNow(), + util.UnixNow(), 0, []byte{}, } diff --git a/blockchain/genesis.go b/blockchain/genesis.go index 56bba26..0e45413 100644 --- a/blockchain/genesis.go +++ b/blockchain/genesis.go @@ -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. // @@ -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, }, diff --git a/blockchain/hash_test.go b/blockchain/hash_test.go index a4320e9..73efc58 100644 --- a/blockchain/hash_test.go +++ b/blockchain/hash_test.go @@ -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++ { diff --git a/blockchain/test_utils.go b/blockchain/test_utils.go index c178a7d..bbcced2 100644 --- a/blockchain/test_utils.go +++ b/blockchain/test_utils.go @@ -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. @@ -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, @@ -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, @@ -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) diff --git a/blockchain/transaction.go b/blockchain/transaction.go index d13040e..6b5e464 100644 --- a/blockchain/transaction.go +++ b/blockchain/transaction.go @@ -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. @@ -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 } diff --git a/blockchain/validation.go b/blockchain/validation.go index d254dea..f94a722 100644 --- a/blockchain/validation.go +++ b/blockchain/validation.go @@ -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. @@ -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 ) diff --git a/blockchain/validation_test.go b/blockchain/validation_test.go index 9ae366f..3ffeb38 100644 --- a/blockchain/validation_test.go +++ b/blockchain/validation_test.go @@ -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) { @@ -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), @@ -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 { @@ -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 { @@ -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), diff --git a/common/constants/big.go b/common/constants/big.go index ed75c0a..56ad52e 100644 --- a/common/constants/big.go +++ b/common/constants/big.go @@ -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 ( @@ -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) ) diff --git a/common/math/big.go b/common/util/big.go similarity index 97% rename from common/math/big.go rename to common/util/big.go index eaec7f7..941fae4 100644 --- a/common/math/big.go +++ b/common/util/big.go @@ -1,4 +1,4 @@ -package math +package util import "math/big" diff --git a/common/math/big_test.go b/common/util/big_test.go similarity index 99% rename from common/math/big_test.go rename to common/util/big_test.go index 7621199..0838502 100644 --- a/common/math/big_test.go +++ b/common/util/big_test.go @@ -1,4 +1,4 @@ -package math +package util import ( "math/big" diff --git a/common/bytes.go b/common/util/bytes.go similarity index 96% rename from common/bytes.go rename to common/util/bytes.go index 595d7ed..28b4412 100644 --- a/common/bytes.go +++ b/common/util/bytes.go @@ -1,4 +1,4 @@ -package common +package util import "encoding/binary" diff --git a/common/bytes_test.go b/common/util/bytes_test.go similarity index 98% rename from common/bytes_test.go rename to common/util/bytes_test.go index f75a69f..7411b08 100644 --- a/common/bytes_test.go +++ b/common/util/bytes_test.go @@ -1,4 +1,4 @@ -package common +package util import ( "math" diff --git a/common/time.go b/common/util/time.go similarity index 93% rename from common/time.go rename to common/util/time.go index 8cb5ad6..c1f2463 100644 --- a/common/time.go +++ b/common/util/time.go @@ -1,4 +1,4 @@ -package common +package util import "time" diff --git a/consensus/consensus_test.go b/consensus/consensus_test.go index c1a9e14..07e3419 100644 --- a/consensus/consensus_test.go +++ b/consensus/consensus_test.go @@ -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) { @@ -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), diff --git a/miner/miner.go b/miner/miner.go index 53e8246..e5a777a 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -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" ) @@ -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++ } diff --git a/miner/miner_test.go b/miner/miner_test.go index 5ec2adf..3d752fe 100644 --- a/miner/miner_test.go +++ b/miner/miner_test.go @@ -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" ) @@ -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 @@ -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() { @@ -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),