Added generic big to 256 method. Implemented new iterator

This commit is contained in:
obscuren
2014-10-10 17:00:06 +02:00
parent 9b494c6869
commit e02c0fa808
8 changed files with 151 additions and 120 deletions

View File

@@ -40,13 +40,11 @@ func (bc *BlockChain) Genesis() *Block {
func (bc *BlockChain) NewBlock(coinbase []byte) *Block {
var root interface{}
var lastBlockTime int64
hash := ZeroHash256
if bc.CurrentBlock != nil {
root = bc.CurrentBlock.state.Trie.Root
hash = bc.LastBlockHash
lastBlockTime = bc.CurrentBlock.Time
}
block := CreateBlock(
@@ -61,15 +59,7 @@ func (bc *BlockChain) NewBlock(coinbase []byte) *Block {
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 {
diff.Add(parent.Difficulty, adjust)
}
block.Difficulty = diff
block.Difficulty = CalcDifficulty(block, parent)
block.Number = new(big.Int).Add(bc.CurrentBlock.Number, ethutil.Big1)
block.GasLimit = block.CalcGasLimit(bc.CurrentBlock)
@@ -78,6 +68,19 @@ func (bc *BlockChain) NewBlock(coinbase []byte) *Block {
return block
}
func CalcDifficulty(block, parent *Block) *big.Int {
diff := new(big.Int)
adjust := new(big.Int).Rsh(parent.Difficulty, 10)
if block.Time >= parent.Time+5 {
diff.Sub(parent.Difficulty, adjust)
} else {
diff.Add(parent.Difficulty, adjust)
}
return diff
}
func (bc *BlockChain) Reset() {
AddTestNetFunds(bc.genesisBlock)

View File

@@ -346,9 +346,6 @@ func (sm *StateManager) CalculateTD(block *Block) bool {
// an uncle or anything that isn't on the current block chain.
// Validation validates easy over difficult (dagger takes longer time = difficult)
func (sm *StateManager) ValidateBlock(block *Block) error {
// TODO
// 2. Check if the difficulty is correct
// Check each uncle's previous hash. In order for it to be valid
// is if it has the same block hash as the current
parent := sm.bc.GetBlock(block.PrevHash)
@@ -360,6 +357,11 @@ func (sm *StateManager) ValidateBlock(block *Block) error {
}
*/
expd := CalcDifficulty(block, parent)
if expd.Cmp(block.Difficulty) < 0 {
return fmt.Errorf("Difficulty check failed for block %v, %v", block.Difficulty, expd)
}
diff := block.Time - parent.Time
if diff < 0 {
return ValidationError("Block timestamp less then prev block %v (%v - %v)", diff, block.Time, sm.bc.CurrentBlock.Time)