-
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.
Add TxOutput struct, some Marshal/Hash functions and skeleton validit…
…y logic
- Loading branch information
1 parent
b39465a
commit 4475112
Showing
4 changed files
with
121 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package blockchain | ||
|
||
// BlockHeader contains metadata about a block | ||
import ( | ||
"crypto/sha256" | ||
"encoding/binary" | ||
"encoding/gob" | ||
"io" | ||
) | ||
|
||
// BlockHeader contains metadata about a block | ||
type BlockHeader struct { | ||
blockNumber uint32 | ||
lastBlock Hash | ||
miner Wallet | ||
} | ||
|
||
// Marshal converts a BlockHeader to a byte slice | ||
func (bh *BlockHeader) Marshal() []byte { | ||
buf := []byte{} | ||
binary.LittleEndian.PutUint32(buf, bh.blockNumber) | ||
for _, b := range bh.lastBlock { | ||
buf = append(buf, b) | ||
} | ||
buf = append(buf, bh.miner.Marshal()...) | ||
return buf | ||
} | ||
|
||
// Block represents a block in the blockchain. Contains transactions and header metadata. | ||
type Block struct { | ||
BlockHeader | ||
transactions []*Transaction | ||
} | ||
|
||
// Marshal converts a Block to a byte slice | ||
func (b *Block) Marshal() []byte { | ||
buf := b.BlockHeader.Marshal() | ||
for _, t := range b.transactions { | ||
buf = append(buf, t.Marshal()...) | ||
} | ||
return buf | ||
} | ||
|
||
// Encode writes the marshalled block to the given io.Writer | ||
func (b *Block) Encode(w io.Writer) { | ||
gob.NewEncoder(w).Encode(b) | ||
} | ||
|
||
// Decode reads the marshalled block from the given io.Reader | ||
func (b *Block) Decode(r io.Reader) { | ||
gob.NewDecoder(r).Decode(b) | ||
} | ||
|
||
// Hash computes and returns the SHA256 hash of the block | ||
func (b *Block) Hash() Hash { | ||
return sha256.Sum256(b.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 |
---|---|---|
@@ -1,75 +1,45 @@ | ||
package blockchain | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/binary" | ||
"encoding/gob" | ||
"io" | ||
) | ||
|
||
// Hash represents a hash of a block or transaction | ||
// Hash represents a 256-bit hash of a block or transaction | ||
type Hash [32]byte | ||
|
||
// BlockHeader contains metadata about a block | ||
type BlockHeader struct { | ||
blockNumber uint32 | ||
lastBlock Hash | ||
miner Wallet | ||
} | ||
|
||
// Marshal converts a BlockHeader to a byte slice | ||
func (bh *BlockHeader) Marshal() []byte { | ||
buf := []byte{} | ||
binary.LittleEndian.PutUint32(buf, bh.blockNumber) | ||
for _, b := range bh.lastBlock { | ||
buf = append(buf, b) | ||
} | ||
buf = append(buf, bh.miner.Marshal()...) | ||
return buf | ||
} | ||
|
||
// Block represents a block in the blockchain. Contains transactions and header metadata. | ||
type Block struct { | ||
BlockHeader | ||
transactions []*Transaction | ||
} | ||
|
||
// Marshal converts a Block to a byte slice | ||
func (b *Block) Marshal() []byte { | ||
buf := b.BlockHeader.Marshal() | ||
for _, t := range b.transactions { | ||
buf = append(buf, t.Marshal()...) | ||
} | ||
return buf | ||
} | ||
|
||
// Encode writes the marshalled block to the given io.Writer | ||
func (b *Block) Encode(w io.Writer) { | ||
gob.NewEncoder(w).Encode(b) | ||
} | ||
|
||
// Encode reads the marshalled block from the given io.Reader | ||
func (b *Block) Decode(r io.Reader) { | ||
gob.NewDecoder(r).Decode(b) | ||
} | ||
|
||
// Hash computes and returns the SHA256 hash of the block | ||
func (b *Block) Hash() Hash { | ||
return sha256.Sum256(b.Marshal()) | ||
} | ||
|
||
// BlockChain represents a linked list of blocks | ||
type BlockChain struct { | ||
blocks []*Block | ||
head Hash | ||
} | ||
|
||
// Encode writes the marshalled blockchain to the given io.Writer | ||
func (b *BlockChain) Encode(w io.Writer) { | ||
gob.NewEncoder(w).Encode(b) | ||
func (bc *BlockChain) Encode(w io.Writer) { | ||
gob.NewEncoder(w).Encode(bc) | ||
} | ||
|
||
// Encode reads the marshalled blockchain from the given io.Reader | ||
func (b *BlockChain) Decode(r io.Reader) { | ||
gob.NewDecoder(r).Decode(b) | ||
// Decode reads the marshalled blockchain from the given io.Reader | ||
func (bc *BlockChain) Decode(r io.Reader) { | ||
gob.NewDecoder(r).Decode(bc) | ||
} | ||
|
||
// ValidTransaction checks whether a transaction is valid, assuming the blockchain is valid. | ||
func (bc *BlockChain) ValidTransaction(t *Transaction) bool { | ||
// Find the transaction input (I) in the chain (by hash) | ||
// Check that output to sender in I is equal to outputs in T | ||
// Verify signature of T | ||
return false | ||
} | ||
|
||
// ValidBlock checks whether a block is valid | ||
func (bc *BlockChain) ValidBlock(b *Block) bool { | ||
for _, t := range b.transactions { | ||
if !bc.ValidTransaction(t) { | ||
return false | ||
} | ||
} | ||
// Check that block number is one greater than last block | ||
// Check that hash of last block is correct | ||
return false | ||
} |
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