diff options
author | obscuren <geffobscura@gmail.com> | 2014-09-15 21:42:12 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-09-15 21:42:12 +0800 |
commit | 33a0dec8a157b9687ca6038f4deb011f3f1f7bdc (patch) | |
tree | 197b792e06dc3952df93957a39fdf6e44582ac96 /ethchain | |
parent | 2f614900e82036e3e8f6f6a714efc43e09aca830 (diff) | |
download | go-tangerine-33a0dec8a157b9687ca6038f4deb011f3f1f7bdc.tar go-tangerine-33a0dec8a157b9687ca6038f4deb011f3f1f7bdc.tar.gz go-tangerine-33a0dec8a157b9687ca6038f4deb011f3f1f7bdc.tar.bz2 go-tangerine-33a0dec8a157b9687ca6038f4deb011f3f1f7bdc.tar.lz go-tangerine-33a0dec8a157b9687ca6038f4deb011f3f1f7bdc.tar.xz go-tangerine-33a0dec8a157b9687ca6038f4deb011f3f1f7bdc.tar.zst go-tangerine-33a0dec8a157b9687ca6038f4deb011f3f1f7bdc.zip |
Improved catching up and refactored
Diffstat (limited to 'ethchain')
-rw-r--r-- | ethchain/block.go | 33 | ||||
-rw-r--r-- | ethchain/block_chain.go | 25 | ||||
-rw-r--r-- | ethchain/state_manager.go | 8 |
3 files changed, 46 insertions, 20 deletions
diff --git a/ethchain/block.go b/ethchain/block.go index fde6ff04a..157be2a52 100644 --- a/ethchain/block.go +++ b/ethchain/block.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "math/big" + "sort" _ "strconv" "time" @@ -42,9 +43,32 @@ func (self Blocks) AsSet() ethutil.UniqueSet { return set } +type BlockBy func(b1, b2 *Block) bool + +func (self BlockBy) Sort(blocks Blocks) { + bs := blockSorter{ + blocks: blocks, + by: self, + } + sort.Sort(bs) +} + +type blockSorter struct { + blocks Blocks + by func(b1, b2 *Block) bool +} + +func (self blockSorter) Len() int { return len(self.blocks) } +func (self blockSorter) Swap(i, j int) { + self.blocks[i], self.blocks[j] = self.blocks[j], self.blocks[i] +} +func (self blockSorter) Less(i, j int) bool { return self.by(self.blocks[i], self.blocks[j]) } + +func Number(b1, b2 *Block) bool { return b1.Number.Cmp(b2.Number) < 0 } + type Block struct { // Hash to the previous block - PrevHash []byte + PrevHash ethutil.Bytes // Uncles of this block Uncles Blocks UncleSha []byte @@ -68,7 +92,7 @@ type Block struct { // Extra data Extra string // Block Nonce for verification - Nonce []byte + Nonce ethutil.Bytes // List of transactions and/or contracts transactions []*Transaction receipts []*Receipt @@ -117,8 +141,9 @@ func CreateBlock(root interface{}, } // Returns a hash of the block -func (block *Block) Hash() []byte { - return ethcrypto.Sha3Bin(block.Value().Encode()) +func (block *Block) Hash() ethutil.Bytes { + return ethcrypto.Sha3Bin(ethutil.NewValue(block.header()).Encode()) + //return ethcrypto.Sha3Bin(block.Value().Encode()) } func (block *Block) HashNoNonce() []byte { diff --git a/ethchain/block_chain.go b/ethchain/block_chain.go index 2d88a0f53..5d0d652df 100644 --- a/ethchain/block_chain.go +++ b/ethchain/block_chain.go @@ -58,24 +58,20 @@ func (bc *BlockChain) NewBlock(coinbase []byte) *Block { block.MinGasPrice = big.NewInt(10000000000000) - if bc.CurrentBlock != nil { - var mul *big.Int - if block.Time < lastBlockTime+5 { - mul = big.NewInt(1) + parent := bc.CurrentBlock + if parent != nil { + diff := new(big.Int) + + adjust := new(big.Int).Rsh(parent.Difficulty, 10) + if block.Time >= lastBlockTime+5 { + diff.Sub(parent.Difficulty, adjust) } else { - mul = big.NewInt(-1) + diff.Add(parent.Difficulty, adjust) } - - diff := new(big.Int) - diff.Add(diff, bc.CurrentBlock.Difficulty) - diff.Div(diff, big.NewInt(1024)) - diff.Mul(diff, mul) - diff.Add(diff, bc.CurrentBlock.Difficulty) block.Difficulty = diff - block.Number = new(big.Int).Add(bc.CurrentBlock.Number, ethutil.Big1) - block.GasLimit = block.CalcGasLimit(bc.CurrentBlock) + } return block @@ -159,6 +155,9 @@ func (bc *BlockChain) setLastBlock() { bc.LastBlockHash = block.Hash() bc.LastBlockNumber = block.Number.Uint64() + if bc.LastBlockNumber == 0 { + bc.genesisBlock = block + } } else { AddTestNetFunds(bc.genesisBlock) diff --git a/ethchain/state_manager.go b/ethchain/state_manager.go index a165ed79d..1ccaa560f 100644 --- a/ethchain/state_manager.go +++ b/ethchain/state_manager.go @@ -217,13 +217,13 @@ func (sm *StateManager) Process(block *Block, dontReact bool) (err error) { return err } - // 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, parent); err != nil { statelogger.Errorln("Error accumulating reward", err) return err } + state.Update() + if !block.State().Cmp(state) { err = fmt.Errorf("Invalid merkle root.\nrec: %x\nis: %x", block.State().Trie.Root, state.Trie.Root) return @@ -335,7 +335,7 @@ func (sm *StateManager) ValidateBlock(block *Block) error { } func (sm *StateManager) AccumelateRewards(state *ethstate.State, block, parent *Block) error { - reward := new(big.Int) + reward := new(big.Int).Set(BlockReward) knownUncles := ethutil.Set(parent.Uncles) nonces := ethutil.NewSet(block.Nonce) @@ -358,6 +358,8 @@ func (sm *StateManager) AccumelateRewards(state *ethstate.State, block, parent * return UncleError("Uncle in chain") } + nonces.Insert(uncle.Nonce) + r := new(big.Int) r.Mul(BlockReward, big.NewInt(15)).Div(r, big.NewInt(16)) |