From a1c5d5acac542ab877aeec7814338e7638d55dbf Mon Sep 17 00:00:00 2001 From: obscuren Date: Sun, 29 Dec 2013 01:36:59 +0100 Subject: [PATCH] Comments --- big.go | 6 ++++++ block.go | 22 ++++++++++++++++++---- block_manager.go | 3 +++ ethereum.go | 1 - transaction.go | 1 + 5 files changed, 28 insertions(+), 5 deletions(-) diff --git a/big.go b/big.go index 71e7573e1..44bfee2e4 100644 --- a/big.go +++ b/big.go @@ -4,6 +4,9 @@ import ( "math/big" ) +/* + * Returns the power of two integers + */ func BigPow(a,b int) *big.Int { c := new(big.Int) c.Exp(big.NewInt(int64(a)), big.NewInt(int64(b)), big.NewInt(0)) @@ -11,6 +14,9 @@ func BigPow(a,b int) *big.Int { return c } +/* + * Like big.NewInt(uint64); this takes a string instead. + */ func Big(num string) *big.Int { n := new(big.Int) n.SetString(num, 0) diff --git a/block.go b/block.go index 5c089137d..8c4d263fa 100644 --- a/block.go +++ b/block.go @@ -7,17 +7,23 @@ import ( ) type Block struct { + // The number of this block number uint32 + // Hash to the previous block prevHash string + // Uncles of this block uncles []*Block coinbase string // state xxx difficulty uint32 + // Creation time time time.Time nonce uint32 + // List of transactions and/or contracts transactions []*Transaction } +// Creates a new block. This is currently for testing func NewBlock(/* TODO use raw data */transactions []*Transaction) *Block { block := &Block{ // Slice of transactions to include in this block @@ -37,14 +43,16 @@ func NewBlock(/* TODO use raw data */transactions []*Transaction) *Block { func (block *Block) Update() { } +// Returns a hash of the block func (block *Block) Hash() string { return Sha256Hex(block.MarshalRlp()) } func (block *Block) MarshalRlp() []byte { - // Encoding method requires []interface{} type. It's actual a slice of strings + // Marshal the transactions of this block encTx := make([]string, len(block.transactions)) for i, tx := range block.transactions { + // Cast it to a string (safe) encTx[i] = string(tx.MarshalRlp()) } @@ -64,14 +72,16 @@ func (block *Block) MarshalRlp() []byte { // extra? } - encoded := Encode([]interface{}{header, encTx}) - - return encoded + // Encode a slice interface which contains the header and the list of transactions. + return Encode([]interface{}{header, encTx}) } func (block *Block) UnmarshalRlp(data []byte) { t, _ := Decode(data,0) + + // interface slice assertion if slice, ok := t.([]interface{}); ok { + // interface slice assertion if header, ok := slice[0].([]interface{}); ok { if number, ok := header[0].(uint8); ok { block.number = uint32(number) @@ -109,11 +119,15 @@ func (block *Block) UnmarshalRlp(data []byte) { } if txSlice, ok := slice[1].([]interface{}); ok { + // Create transaction slice equal to decoded tx interface slice block.transactions = make([]*Transaction, len(txSlice)) + // Unmarshal transactions for i, tx := range txSlice { if t, ok := tx.([]byte); ok { tx := &Transaction{} + // Use the unmarshaled data to unmarshal the transaction + // t is still decoded. tx.UnmarshalRlp(t) block.transactions[i] = tx diff --git a/block_manager.go b/block_manager.go index 80b30eff6..a60d4340d 100644 --- a/block_manager.go +++ b/block_manager.go @@ -26,9 +26,12 @@ func NewBlockManager() *BlockManager { // Process a block. func (bm *BlockManager) ProcessBlock(block *Block) error { + // Get the tx count. Used to create enough channels to 'join' the go routines txCount := len(block.transactions) + // Locking channel. When it has been fully buffered this method will return lockChan := make(chan bool, txCount) + // Process each transaction/contract for _, tx := range block.transactions { go bm.ProcessTransaction(tx, lockChan) } diff --git a/ethereum.go b/ethereum.go index 3e01f89b2..52e6e3046 100644 --- a/ethereum.go +++ b/ethereum.go @@ -37,7 +37,6 @@ func main() { bm.ProcessBlock( blck ) - t := blck.MarshalRlp() copyBlock := &Block{} copyBlock.UnmarshalRlp(t) diff --git a/transaction.go b/transaction.go index b6e9eaa8f..562593c96 100644 --- a/transaction.go +++ b/transaction.go @@ -125,6 +125,7 @@ func (tx *Transaction) UnmarshalRlp(data []byte) { tx.fee = uint32(fee) } + // Encode the data/instructions if data, ok := slice[5].([]interface{}); ok { tx.data = make([]string, len(data)) for i, d := range data {