2013-12-26 05:45:52 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-01-03 06:40:25 -06:00
|
|
|
"fmt"
|
2014-01-10 15:44:53 -06:00
|
|
|
"github.com/ethereum/ethutil-go"
|
2013-12-26 05:45:52 -06:00
|
|
|
)
|
|
|
|
|
2014-01-08 16:43:07 -06:00
|
|
|
type BlockChain struct {
|
2014-01-10 15:44:53 -06:00
|
|
|
lastBlock *ethutil.Block
|
2014-01-08 16:43:07 -06:00
|
|
|
|
2014-01-10 15:44:53 -06:00
|
|
|
genesisBlock *ethutil.Block
|
2014-01-08 16:43:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewBlockChain() *BlockChain {
|
|
|
|
bc := &BlockChain{}
|
2014-01-10 15:44:53 -06:00
|
|
|
bc.genesisBlock = ethutil.NewBlock( ethutil.Encode(ethutil.Genesis) )
|
2014-01-08 16:43:07 -06:00
|
|
|
|
|
|
|
return bc
|
|
|
|
}
|
|
|
|
|
2013-12-26 05:45:52 -06:00
|
|
|
type BlockManager struct {
|
|
|
|
vm *Vm
|
2014-01-08 16:43:07 -06:00
|
|
|
|
|
|
|
blockChain *BlockChain
|
2013-12-26 05:45:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewBlockManager() *BlockManager {
|
|
|
|
bm := &BlockManager{vm: NewVm()}
|
|
|
|
|
|
|
|
return bm
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process a block.
|
2014-01-10 15:44:53 -06:00
|
|
|
func (bm *BlockManager) ProcessBlock(block *ethutil.Block) error {
|
2014-01-08 16:43:07 -06:00
|
|
|
// TODO Validation (Or move to other part of the application)
|
|
|
|
if err := bm.ValidateBlock(block); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-28 18:36:59 -06:00
|
|
|
// Get the tx count. Used to create enough channels to 'join' the go routines
|
2014-01-10 15:44:53 -06:00
|
|
|
txCount := len(block.Transactions())
|
2013-12-28 18:36:59 -06:00
|
|
|
// Locking channel. When it has been fully buffered this method will return
|
2013-12-26 05:45:52 -06:00
|
|
|
lockChan := make(chan bool, txCount)
|
|
|
|
|
2013-12-28 18:36:59 -06:00
|
|
|
// Process each transaction/contract
|
2014-01-10 15:44:53 -06:00
|
|
|
for _, tx := range block.Transactions() {
|
2014-01-03 06:40:25 -06:00
|
|
|
// If there's no recipient, it's a contract
|
2014-01-04 18:54:15 -06:00
|
|
|
if tx.IsContract() {
|
2014-01-03 06:40:25 -06:00
|
|
|
go bm.ProcessContract(tx, block, lockChan)
|
|
|
|
} else {
|
|
|
|
// "finish" tx which isn't a contract
|
|
|
|
lockChan <- true
|
|
|
|
}
|
2013-12-26 05:45:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all Tx to finish processing
|
|
|
|
for i := 0; i < txCount; i++ {
|
|
|
|
<- lockChan
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-10 15:44:53 -06:00
|
|
|
func (bm *BlockManager) ValidateBlock(block *ethutil.Block) error {
|
2014-01-08 16:43:07 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-10 15:44:53 -06:00
|
|
|
func (bm *BlockManager) ProcessContract(tx *ethutil.Transaction, block *ethutil.Block, lockChan chan bool) {
|
2014-01-03 06:40:25 -06:00
|
|
|
// 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 {
|
2014-01-03 17:31:42 -06:00
|
|
|
// TODO turn on once big ints are in place
|
|
|
|
//if !block.PayFee(tx.Hash(), StepFee.Uint64()) {
|
|
|
|
// return false
|
|
|
|
//}
|
2013-12-26 05:45:52 -06:00
|
|
|
|
2014-01-02 17:43:49 -06:00
|
|
|
return true // Continue
|
|
|
|
})
|
2013-12-26 05:45:52 -06:00
|
|
|
|
|
|
|
// Broadcast we're done
|
|
|
|
lockChan <- true
|
|
|
|
}
|