Official Go implementation of the Ethereum protocol
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-ethereum/block_manager.go

90 lines
2.0 KiB

11 years ago
package main
import (
11 years ago
"fmt"
11 years ago
)
11 years ago
type BlockChain struct {
lastBlock *Block
genesisBlock *Block
}
func NewBlockChain() *BlockChain {
bc := &BlockChain{}
bc.genesisBlock = NewBlock( Encode(Genesis) )
return bc
}
11 years ago
type BlockManager struct {
vm *Vm
11 years ago
blockChain *BlockChain
11 years ago
}
func NewBlockManager() *BlockManager {
bm := &BlockManager{vm: NewVm()}
return bm
}
// Process a block.
func (bm *BlockManager) ProcessBlock(block *Block) error {
11 years ago
// TODO Validation (Or move to other part of the application)
if err := bm.ValidateBlock(block); err != nil {
return err
}
11 years ago
// Get the tx count. Used to create enough channels to 'join' the go routines
11 years ago
txCount := len(block.transactions)
11 years ago
// Locking channel. When it has been fully buffered this method will return
11 years ago
lockChan := make(chan bool, txCount)
11 years ago
// Process each transaction/contract
11 years ago
for _, tx := range block.transactions {
11 years ago
// If there's no recipient, it's a contract
if tx.IsContract() {
11 years ago
go bm.ProcessContract(tx, block, lockChan)
} else {
// "finish" tx which isn't a contract
lockChan <- true
}
11 years ago
}
// Wait for all Tx to finish processing
for i := 0; i < txCount; i++ {
<- lockChan
}
return nil
}
11 years ago
func (bm *BlockManager) ValidateBlock(block *Block) error {
return nil
}
11 years ago
func (bm *BlockManager) ProcessContract(tx *Transaction, block *Block, lockChan chan bool) {
// Recovering function in case the VM had any errors
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered from VM execution with err =", r)
// Let the channel know where done even though it failed (so the execution may resume normally)
lockChan <- true
}
}()
// Process contract
bm.vm.ProcContract(tx, block, func(opType OpType) bool {
11 years ago
// TODO turn on once big ints are in place
//if !block.PayFee(tx.Hash(), StepFee.Uint64()) {
// return false
//}
11 years ago
return true // Continue
})
11 years ago
// Broadcast we're done
lockChan <- true
}