Skip to content

Commit

Permalink
uint32(time.Now().Unix()) wrapped
Browse files Browse the repository at this point in the history
  • Loading branch information
david-julien committed Jul 13, 2017
1 parent fd911d0 commit 06cb4a9
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 16 deletions.
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"
)

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()),
common.UnixNow(),
0,
[]byte{0x00, 0x01, 0x02},
}
Expand All @@ -47,7 +48,7 @@ func TestBlockHeaderLen(t *testing.T) {
0,
NewTestHash(),
NewValidTestTarget(),
uint32(time.Now().Unix()),
common.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"

// 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: common.UnixNow(),
Nonce: 0,
ExtraData: extraData,
},
Expand Down
5 changes: 3 additions & 2 deletions blockchain/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions common/time.go
Original file line number Diff line number Diff line change
@@ -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())
}
4 changes: 2 additions & 2 deletions consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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++
}

Expand Down
7 changes: 4 additions & 3 deletions miner/miner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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

Expand All @@ -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() {
Expand All @@ -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),
Expand Down

0 comments on commit 06cb4a9

Please sign in to comment.