From 2f614900e82036e3e8f6f6a714efc43e09aca830 Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 15 Sep 2014 01:11:01 +0200 Subject: Updated GHOST --- ethchain/block.go | 13 ++++++++- ethchain/block_chain.go | 2 +- ethchain/error.go | 18 ++++++++++++ ethchain/state_manager.go | 73 ++++++++++++++++++++--------------------------- 4 files changed, 62 insertions(+), 44 deletions(-) (limited to 'ethchain') diff --git a/ethchain/block.go b/ethchain/block.go index d2d012e55..fde6ff04a 100644 --- a/ethchain/block.go +++ b/ethchain/block.go @@ -31,11 +31,22 @@ func (bi *BlockInfo) RlpEncode() []byte { return ethutil.Encode([]interface{}{bi.Number, bi.Hash, bi.Parent}) } +type Blocks []*Block + +func (self Blocks) AsSet() ethutil.UniqueSet { + set := make(ethutil.UniqueSet) + for _, block := range self { + set.Insert(block.Hash()) + } + + return set +} + type Block struct { // Hash to the previous block PrevHash []byte // Uncles of this block - Uncles []*Block + Uncles Blocks UncleSha []byte // The coin base address Coinbase []byte diff --git a/ethchain/block_chain.go b/ethchain/block_chain.go index 74f47aa90..2d88a0f53 100644 --- a/ethchain/block_chain.go +++ b/ethchain/block_chain.go @@ -60,7 +60,7 @@ func (bc *BlockChain) NewBlock(coinbase []byte) *Block { if bc.CurrentBlock != nil { var mul *big.Int - if block.Time < lastBlockTime+42 { + if block.Time < lastBlockTime+5 { mul = big.NewInt(1) } else { mul = big.NewInt(-1) diff --git a/ethchain/error.go b/ethchain/error.go index 2cf09a1ec..82949141a 100644 --- a/ethchain/error.go +++ b/ethchain/error.go @@ -25,6 +25,24 @@ func IsParentErr(err error) bool { return ok } +type UncleErr struct { + Message string +} + +func (err *UncleErr) Error() string { + return err.Message +} + +func UncleError(str string) error { + return &UncleErr{Message: str} +} + +func IsUncleErr(err error) bool { + _, ok := err.(*UncleErr) + + return ok +} + // Block validation error. If any validation fails, this error will be thrown type ValidationErr struct { Message string diff --git a/ethchain/state_manager.go b/ethchain/state_manager.go index 33af259cf..a165ed79d 100644 --- a/ethchain/state_manager.go +++ b/ethchain/state_manager.go @@ -219,7 +219,7 @@ func (sm *StateManager) Process(block *Block, dontReact bool) (err error) { // I'm not sure, but I don't know if there should be thrown // any errors at this time. - if err = sm.AccumelateRewards(state, block); err != nil { + if err = sm.AccumelateRewards(state, block, parent); err != nil { statelogger.Errorln("Error accumulating reward", err) return err } @@ -334,36 +334,44 @@ func (sm *StateManager) ValidateBlock(block *Block) error { return nil } -func CalculateBlockReward(block *Block, uncleLength int) *big.Int { - base := new(big.Int) - for i := 0; i < uncleLength; i++ { - base.Add(base, UncleInclusionReward) - } +func (sm *StateManager) AccumelateRewards(state *ethstate.State, block, parent *Block) error { + reward := new(big.Int) - return base.Add(base, BlockReward) -} + knownUncles := ethutil.Set(parent.Uncles) + nonces := ethutil.NewSet(block.Nonce) + for _, uncle := range block.Uncles { + if nonces.Include(uncle.Nonce) { + // Error not unique + return UncleError("Uncle not unique") + } -func CalculateUncleReward(block *Block) *big.Int { - return UncleReward -} + uncleParent := sm.bc.GetBlock(uncle.PrevHash) + if uncleParent == nil { + return UncleError("Uncle's parent unknown") + } -func (sm *StateManager) AccumelateRewards(state *ethstate.State, block *Block) error { - // Get the account associated with the coinbase - account := state.GetAccount(block.Coinbase) - // Reward amount of ether to the coinbase address - account.AddAmount(CalculateBlockReward(block, len(block.Uncles))) + if uncleParent.Number.Cmp(new(big.Int).Sub(parent.Number, big.NewInt(6))) < 0 { + return UncleError("Uncle too old") + } - addr := make([]byte, len(block.Coinbase)) - copy(addr, block.Coinbase) - state.UpdateStateObject(account) + if knownUncles.Include(uncle.Hash()) { + return UncleError("Uncle in chain") + } + + r := new(big.Int) + r.Mul(BlockReward, big.NewInt(15)).Div(r, big.NewInt(16)) - for _, uncle := range block.Uncles { uncleAccount := state.GetAccount(uncle.Coinbase) - uncleAccount.AddAmount(CalculateUncleReward(uncle)) + uncleAccount.AddAmount(r) - state.UpdateStateObject(uncleAccount) + reward.Add(reward, new(big.Int).Div(BlockReward, big.NewInt(32))) } + // Get the account associated with the coinbase + account := state.GetAccount(block.Coinbase) + // Reward amount of ether to the coinbase address + account.AddAmount(reward) + return nil } @@ -375,14 +383,6 @@ func (sm *StateManager) Stop() { func (sm *StateManager) createBloomFilter(state *ethstate.State) *BloomFilter { bloomf := NewBloomFilter(nil) - /* - for addr, stateObject := range state.Manifest().ObjectChanges { - // Set the bloom filter's bin - bloomf.Set([]byte(addr)) - - sm.Ethereum.Reactor().Post("object:"+addr, stateObject) - } - */ for _, msg := range state.Manifest().Messages { bloomf.Set(msg.To) bloomf.Set(msg.From) @@ -390,17 +390,6 @@ func (sm *StateManager) createBloomFilter(state *ethstate.State) *BloomFilter { sm.Ethereum.Reactor().Post("messages", state.Manifest().Messages) - /* - for stateObjectAddr, mappedObjects := range state.Manifest().StorageChanges { - for addr, value := range mappedObjects { - // Set the bloom filter's bin - bloomf.Set(ethcrypto.Sha3Bin([]byte(stateObjectAddr + addr))) - - sm.Ethereum.Reactor().Post("storage:"+stateObjectAddr+":"+addr, ðstate.StorageState{[]byte(stateObjectAddr), []byte(addr), value}) - } - } - */ - return bloomf } @@ -420,7 +409,7 @@ func (sm *StateManager) GetMessages(block *Block) (messages []*ethstate.Message, sm.ApplyDiff(state, parent, block) - sm.AccumelateRewards(state, block) + sm.AccumelateRewards(state, block, parent) return state.Manifest().Messages, nil } -- cgit v1.2.3