2013-12-26 11:45:52 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-01-11 14:27:08 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/ethereum/ethutil-go"
|
2014-01-12 00:51:52 +00:00
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
"math/big"
|
2013-12-26 11:45:52 +00:00
|
|
|
)
|
|
|
|
|
2014-01-08 22:43:07 +00:00
|
|
|
type BlockChain struct {
|
2014-01-12 00:51:52 +00:00
|
|
|
LastBlock *ethutil.Block
|
2014-01-08 22:43:07 +00:00
|
|
|
|
2014-01-11 14:27:08 +00:00
|
|
|
genesisBlock *ethutil.Block
|
2014-01-12 00:51:52 +00:00
|
|
|
|
|
|
|
TD *big.Int
|
2014-01-08 22:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewBlockChain() *BlockChain {
|
2014-01-11 14:27:08 +00:00
|
|
|
bc := &BlockChain{}
|
|
|
|
bc.genesisBlock = ethutil.NewBlock(ethutil.Encode(ethutil.Genesis))
|
2014-01-08 22:43:07 +00:00
|
|
|
|
2014-01-12 00:51:52 +00:00
|
|
|
// Set the last know difficulty (might be 0x0 as initial value, Genesis)
|
|
|
|
bc.TD = new(big.Int)
|
|
|
|
bc.TD.SetBytes(ethutil.Config.Db.LastKnownTD())
|
|
|
|
|
2014-01-12 15:50:09 +00:00
|
|
|
|
|
|
|
// TODO get last block from the database
|
|
|
|
//bc.LastBlock = bc.genesisBlock
|
|
|
|
|
2014-01-11 14:27:08 +00:00
|
|
|
return bc
|
2014-01-08 22:43:07 +00:00
|
|
|
}
|
|
|
|
|
2014-01-12 00:51:52 +00:00
|
|
|
func (bc *BlockChain) HasBlock(hash string) bool {
|
2014-01-12 15:50:09 +00:00
|
|
|
data, _ := ethutil.Config.Db.Get([]byte(hash))
|
|
|
|
return len(data) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bc *BlockChain) GenesisBlock() *ethutil.Block {
|
|
|
|
return bc.genesisBlock
|
2014-01-12 00:51:52 +00:00
|
|
|
}
|
|
|
|
|
2013-12-26 11:45:52 +00:00
|
|
|
type BlockManager struct {
|
2014-01-12 00:51:52 +00:00
|
|
|
// Ethereum virtual machine for processing contracts
|
2014-01-11 14:27:08 +00:00
|
|
|
vm *Vm
|
2014-01-12 00:51:52 +00:00
|
|
|
// The block chain :)
|
|
|
|
bc *BlockChain
|
2013-12-26 11:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewBlockManager() *BlockManager {
|
2014-01-12 00:51:52 +00:00
|
|
|
bm := &BlockManager{
|
|
|
|
vm: NewVm(),
|
|
|
|
bc: NewBlockChain(),
|
|
|
|
}
|
2013-12-26 11:45:52 +00:00
|
|
|
|
2014-01-11 14:27:08 +00:00
|
|
|
return bm
|
2013-12-26 11:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process a block.
|
2014-01-10 21:44:53 +00:00
|
|
|
func (bm *BlockManager) ProcessBlock(block *ethutil.Block) error {
|
2014-01-12 00:51:52 +00:00
|
|
|
// Block validation
|
2014-01-11 14:27:08 +00:00
|
|
|
if err := bm.ValidateBlock(block); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-01-12 00:51:52 +00:00
|
|
|
// I'm not sure, but I don't know if there should be thrown
|
|
|
|
// any errors at this time.
|
|
|
|
if err := bm.AccumelateRewards(block); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-01-11 14:27:08 +00:00
|
|
|
// 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() {
|
|
|
|
// If there's no recipient, it's a contract
|
|
|
|
if tx.IsContract() {
|
|
|
|
go bm.ProcessContract(tx, block, lockChan)
|
|
|
|
} else {
|
|
|
|
// "finish" tx which isn't a contract
|
|
|
|
lockChan <- true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all Tx to finish processing
|
|
|
|
for i := 0; i < txCount; i++ {
|
|
|
|
<-lockChan
|
|
|
|
}
|
|
|
|
|
2014-01-12 00:51:52 +00:00
|
|
|
if bm.CalculateTD(block) {
|
|
|
|
ethutil.Config.Db.Put(block.Hash(), block.MarshalRlp())
|
|
|
|
bm.bc.LastBlock = block
|
|
|
|
}
|
|
|
|
|
2014-01-11 14:27:08 +00:00
|
|
|
return nil
|
2013-12-26 11:45:52 +00:00
|
|
|
}
|
|
|
|
|
2014-01-12 00:51:52 +00:00
|
|
|
func (bm *BlockManager) CalculateTD(block *ethutil.Block) bool {
|
|
|
|
uncleDiff := new(big.Int)
|
|
|
|
for _, uncle := range block.Uncles {
|
|
|
|
uncleDiff = uncleDiff.Add(uncleDiff, uncle.Difficulty)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TD(genesis_block) = 0 and TD(B) = TD(B.parent) + sum(u.difficulty for u in B.uncles) + B.difficulty
|
|
|
|
td := new(big.Int)
|
|
|
|
td = td.Add(bm.bc.TD, uncleDiff)
|
|
|
|
td = td.Add(td, block.Difficulty)
|
|
|
|
|
|
|
|
// The new TD will only be accepted if the new difficulty is
|
|
|
|
// is greater than the previous.
|
|
|
|
if td.Cmp(bm.bc.TD) > 0 {
|
|
|
|
bm.bc.LastBlock = block
|
|
|
|
// Set the new total difficulty back to the block chain
|
|
|
|
bm.bc.TD = td
|
|
|
|
|
2014-01-12 15:50:09 +00:00
|
|
|
if Debug {
|
|
|
|
log.Println("TD(block) =", td)
|
|
|
|
}
|
|
|
|
|
2014-01-12 00:51:52 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validates the current block. Returns an error if the block was invalid,
|
2014-01-12 01:36:41 +00:00
|
|
|
// an uncle or anything that isn't on the current block chain.
|
|
|
|
// Validation validates easy over difficult (dagger takes longer time = difficult)
|
2014-01-10 21:44:53 +00:00
|
|
|
func (bm *BlockManager) ValidateBlock(block *ethutil.Block) error {
|
2014-01-12 00:51:52 +00:00
|
|
|
// TODO
|
|
|
|
// 2. Check if the difficulty is correct
|
|
|
|
|
|
|
|
// Check if we have the parent hash, if it isn't known we discard it
|
|
|
|
// Reasons might be catching up or simply an invalid block
|
2014-01-12 15:50:09 +00:00
|
|
|
if bm.bc.LastBlock != nil && block.PrevHash == "" &&
|
|
|
|
!bm.bc.HasBlock(block.PrevHash) {
|
2014-01-12 01:36:41 +00:00
|
|
|
return errors.New("Block's parent unknown")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check each uncle's previous hash. In order for it to be valid
|
|
|
|
// is if it has the same block hash as the current
|
|
|
|
for _, uncle := range block.Uncles {
|
|
|
|
if uncle.PrevHash != block.PrevHash {
|
|
|
|
if Debug {
|
|
|
|
log.Printf("Uncle prvhash mismatch %x %x\n", block.PrevHash, uncle.PrevHash)
|
2014-01-12 00:51:52 +00:00
|
|
|
}
|
2014-01-12 01:36:41 +00:00
|
|
|
|
|
|
|
return errors.New("Mismatching Prvhash from uncle")
|
2014-01-12 00:51:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-12 01:36:41 +00:00
|
|
|
// Verify the nonce of the block. Return an error if it's not valid
|
2014-01-12 15:50:09 +00:00
|
|
|
if bm.bc.LastBlock != nil && block.PrevHash == "" &&
|
|
|
|
!DaggerVerify(ethutil.BigD(block.Hash()), block.Difficulty, block.Nonce) {
|
2014-01-12 01:36:41 +00:00
|
|
|
return errors.New("Block's nonce is invalid")
|
|
|
|
}
|
2014-01-12 00:51:52 +00:00
|
|
|
|
2014-01-12 15:50:09 +00:00
|
|
|
log.Println("Block validation PASSED")
|
|
|
|
|
2014-01-12 00:51:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bm *BlockManager) AccumelateRewards(block *ethutil.Block) error {
|
|
|
|
// Get the coinbase rlp data
|
|
|
|
d := block.State().Get(block.Coinbase)
|
|
|
|
|
|
|
|
ether := ethutil.NewEtherFromData([]byte(d))
|
|
|
|
|
|
|
|
// Reward amount of ether to the coinbase address
|
|
|
|
ether.AddFee(ethutil.CalculateBlockReward(block, len(block.Uncles)))
|
|
|
|
block.State().Update(block.Coinbase, string(ether.MarshalRlp()))
|
|
|
|
|
|
|
|
// TODO Reward each uncle
|
|
|
|
|
|
|
|
|
2014-01-11 14:27:08 +00:00
|
|
|
return nil
|
2014-01-08 22:43:07 +00:00
|
|
|
}
|
|
|
|
|
2014-01-10 21:44:53 +00:00
|
|
|
func (bm *BlockManager) ProcessContract(tx *ethutil.Transaction, block *ethutil.Block, lockChan chan bool) {
|
2014-01-11 14:27:08 +00: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 {
|
|
|
|
// TODO turn on once big ints are in place
|
|
|
|
//if !block.PayFee(tx.Hash(), StepFee.Uint64()) {
|
|
|
|
// return false
|
|
|
|
//}
|
|
|
|
|
|
|
|
return true // Continue
|
|
|
|
})
|
|
|
|
|
|
|
|
// Broadcast we're done
|
|
|
|
lockChan <- true
|
2013-12-26 11:45:52 +00:00
|
|
|
}
|