-
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.
Showing
3 changed files
with
101 additions
and
1 deletion.
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,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() | ||
} | ||
} |
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,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 | ||
} |