Skip to content

Commit

Permalink
Override install phase in travis
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
jordanschalm committed May 15, 2017
1 parent 9b3028a commit 1a6a1e2
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ sudo: false
go:
- 1.7
before_install:
- bash scripts/install_glide.sh
- go get github.com/mattn/goveralls
install:
- bash scripts/install_glide.sh
script:
- $HOME/gopath/bin/goveralls -service=travis-ci
20 changes: 20 additions & 0 deletions blockchain/block_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package blockchain

import (
"bytes"
"testing"
)

func TestEncodeBlock(t *testing.T) {
b1 := newBlock()
b2 := Block{}

buf := bytes.NewBuffer(make([]byte, 0, b1.Len()))

b1.Encode(buf)
b2.Decode(buf)

if b1.Hash() != b2.Hash() {
t.Fail()
}
}
79 changes: 79 additions & 0 deletions blockchain/test_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package blockchain

import (
"crypto/ecdsa"
crand "crypto/rand"
"crypto/sha256"
mrand "math/rand"
)

func newHash() Hash {
message := make([]byte, 256)
crand.Read(message)
return sha256.Sum256(message)
}

func newWallet() Wallet {
priv, _ := ecdsa.GenerateKey(curve, crand.Reader)
return (*wallet)(priv)
}

func newTxHashPointer() TxHashPointer {
return TxHashPointer{
BlockNumber: mrand.Uint32(),
Hash: newHash(),
}
}

func newTxOutput() TxOutput {
return TxOutput{
Amount: uint64(mrand.Int63()),
Recipient: newWallet().Public(),
}
}

func newTxBody() TxBody {
// Uniform distribution on [1, 4]
nOutputs := mrand.Intn(4) + 1
body := TxBody{
Sender: newWallet().Public(),
Input: newTxHashPointer(),
Outputs: make([]TxOutput, nOutputs),
}
for i := 0; i < nOutputs; i++ {
body.Outputs[i] = newTxOutput()
}
return body
}

func newTransaction() *Transaction {
sender := newWallet()
tbody := newTxBody()
digest := tbody.Hash()
sig := sender.Sign(digest, crand.Reader)
return &Transaction{
TxBody: tbody,
Sig: sig,
}
}

func newBlockHeader() BlockHeader {
return BlockHeader{
BlockNumber: mrand.Uint32(),
LastBlock: newHash(),
Miner: newWallet().Public(),
}
}

func newBlock() *Block {
// Uniform distribution on [500, 999]
nTransactions := mrand.Intn(500) + 500
b := Block{
BlockHeader: newBlockHeader(),
Transactions: make([]*Transaction, nTransactions),
}
for i := 0; i < nTransactions; i++ {
b.Transactions[i] = newTransaction()
}
return &b
}

0 comments on commit 1a6a1e2

Please sign in to comment.