|
|
@ -48,7 +48,7 @@ func CalcDifficulty(block, parent *types.Header) *big.Int { |
|
|
|
return diff |
|
|
|
return diff |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func CalculateTD(block, parent *types.Block) *big.Int { |
|
|
|
func CalcTD(block, parent *types.Block) *big.Int { |
|
|
|
if parent == nil { |
|
|
|
if parent == nil { |
|
|
|
return block.Difficulty() |
|
|
|
return block.Difficulty() |
|
|
|
} |
|
|
|
} |
|
|
@ -59,14 +59,20 @@ func CalculateTD(block, parent *types.Block) *big.Int { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func CalcGasLimit(parent *types.Block) *big.Int { |
|
|
|
func CalcGasLimit(parent *types.Block) *big.Int { |
|
|
|
// ((1024-1) * parent.gasLimit + (gasUsed * 6 / 5)) / 1024
|
|
|
|
decay := new(big.Int).Div(parent.GasLimit(), params.GasLimitBoundDivisor) |
|
|
|
previous := new(big.Int).Mul(big.NewInt(1024-1), parent.GasLimit()) |
|
|
|
contrib := new(big.Int).Mul(parent.GasUsed(), big.NewInt(3)) |
|
|
|
current := new(big.Rat).Mul(new(big.Rat).SetInt(parent.GasUsed()), big.NewRat(6, 5)) |
|
|
|
contrib = contrib.Div(contrib, big.NewInt(2)) |
|
|
|
curInt := new(big.Int).Div(current.Num(), current.Denom()) |
|
|
|
contrib = contrib.Div(contrib, params.GasLimitBoundDivisor) |
|
|
|
|
|
|
|
|
|
|
|
result := new(big.Int).Add(previous, curInt) |
|
|
|
gl := new(big.Int).Sub(parent.GasLimit(), decay) |
|
|
|
result.Div(result, big.NewInt(1024)) |
|
|
|
gl = gl.Add(gl, contrib) |
|
|
|
return common.BigMax(params.GenesisGasLimit, result) |
|
|
|
gl = common.BigMax(gl, params.MinGasLimit) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if gl.Cmp(params.GenesisGasLimit) < 0 { |
|
|
|
|
|
|
|
gl2 := new(big.Int).Add(parent.GasLimit(), decay) |
|
|
|
|
|
|
|
return common.BigMin(params.GenesisGasLimit, gl2) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return gl |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type ChainManager struct { |
|
|
|
type ChainManager struct { |
|
|
@ -525,7 +531,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { |
|
|
|
} |
|
|
|
} |
|
|
|
// Setting block.Td regardless of error (known for example) prevents errors down the line
|
|
|
|
// Setting block.Td regardless of error (known for example) prevents errors down the line
|
|
|
|
// in the protocol handler
|
|
|
|
// in the protocol handler
|
|
|
|
block.Td = new(big.Int).Set(CalculateTD(block, self.GetBlock(block.ParentHash()))) |
|
|
|
block.Td = new(big.Int).Set(CalcTD(block, self.GetBlock(block.ParentHash()))) |
|
|
|
|
|
|
|
|
|
|
|
// Call in to the block processor and check for errors. It's likely that if one block fails
|
|
|
|
// Call in to the block processor and check for errors. It's likely that if one block fails
|
|
|
|
// all others will fail too (unless a known block is returned).
|
|
|
|
// all others will fail too (unless a known block is returned).
|
|
|
|