-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from ubclaunchpad/30-msg-layer
Message layer
- Loading branch information
Showing
19 changed files
with
450 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
PACKAGES = `go list ./... | grep -v vendor/` | ||
|
||
all: cumulus | ||
|
||
cumulus: | ||
go build | ||
|
||
test: | ||
go test $(PACKAGES) | ||
|
||
deps: | ||
glide install | ||
|
||
clean: | ||
rm cumulus | ||
|
||
install-glide: | ||
sh scripts/install_glide.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package blockchain | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestEncodeDecodeBlock(t *testing.T) { | ||
b1 := newBlock() | ||
|
||
buf := bytes.NewBuffer(make([]byte, 0, b1.Len())) | ||
|
||
b1.Encode(buf) | ||
b2 := DecodeBlock(buf) | ||
|
||
if HashSum(b1) != HashSum(b2) { | ||
t.Fail() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package blockchain | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestEncodeDecodeBlockChain(t *testing.T) { | ||
b1 := newBlockChain() | ||
|
||
buf := bytes.NewBuffer(make([]byte, 0, b1.Len())) | ||
|
||
b1.Encode(buf) | ||
b2 := DecodeBlockChain(buf) | ||
|
||
if HashSum(b1) != HashSum(b2) { | ||
t.Fail() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package blockchain | ||
|
||
import "crypto/sha256" | ||
|
||
// HashLen is the length in bytes of a hash. | ||
const HashLen = 32 | ||
|
||
// Hash represents a 256-bit hash of a block or transaction | ||
type Hash [HashLen]byte | ||
|
||
// Marshal converts a Hash to a slice. | ||
func (h Hash) Marshal() []byte { | ||
buf := make([]byte, HashLen) | ||
for i, b := range h { | ||
buf[i] = b | ||
} | ||
return buf | ||
} | ||
|
||
// Marshaller is any type that can convert itself to a byte slice | ||
type Marshaller interface { | ||
Marshal() []byte | ||
} | ||
|
||
// HashSum computes the SHA256 hash of a Marshaller. | ||
func HashSum(m Marshaller) Hash { | ||
return sha256.Sum256(m.Marshal()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
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 := HashSum(tbody) | ||
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 | ||
} | ||
|
||
func newBlockChain() *BlockChain { | ||
// Uniform distribution on [10, 50] | ||
nBlocks := mrand.Intn(40) + 10 | ||
bc := BlockChain{Blocks: make([]*Block, nBlocks)} | ||
for i := 0; i < nBlocks; i++ { | ||
bc.Blocks[i] = newBlock() | ||
} | ||
bc.Head = HashSum(bc.Blocks[nBlocks-1]) | ||
return &bc | ||
} |
Oops, something went wrong.