@@ -48,6 +48,8 @@ type BlockGen struct {
4848 engine consensus.Engine
4949}
5050
51+ type headerGenFn func (chain consensus.ChainReader , parent * types.Block , state * state.StateDB , engine consensus.Engine ) * types.Header
52+
5153// SetCoinbase sets the coinbase of the generated block.
5254// It can be called at most once.
5355func (b * BlockGen ) SetCoinbase (addr common.Address ) {
@@ -170,14 +172,19 @@ func (b *BlockGen) OffsetTime(seconds int64) {
170172// values. Inserting them into BlockChain requires use of FakePow or
171173// a similar non-validating proof of work implementation.
172174func GenerateChain (config * params.ChainConfig , parent * types.Block , engine consensus.Engine , db ethdb.Database , n int , gen func (int , * BlockGen )) ([]* types.Block , []types.Receipts ) {
175+ return generateChain (config , parent , engine , db , n , gen , makeHeader )
176+ }
177+
178+ func generateChain (config * params.ChainConfig , parent * types.Block , engine consensus.Engine , db ethdb.Database , n int , gen func (int , * BlockGen ), headerGen headerGenFn ) ([]* types.Block , []types.Receipts ) {
173179 if config == nil {
174180 config = params .TestChainConfig
175181 }
176182 blocks , receipts := make (types.Blocks , n ), make ([]types.Receipts , n )
177183 chainreader := & fakeChainReader {config : config }
178184 genblock := func (i int , parent * types.Block , statedb * state.StateDB ) (* types.Block , types.Receipts ) {
179185 b := & BlockGen {i : i , chain : blocks , parent : parent , statedb : statedb , config : config , engine : engine }
180- b .header = makeHeader (chainreader , parent , statedb , b .engine )
186+ //b.header = makeHeader(chainreader, parent, statedb, b.engine)
187+ b .header = headerGen (chainreader , parent , statedb , b .engine )
181188
182189 // Mutate the state and block according to any hard-fork specs
183190 if daoBlock := config .DAOForkBlock ; daoBlock != nil {
@@ -248,6 +255,54 @@ func makeHeader(chain consensus.ChainReader, parent *types.Block, state *state.S
248255 }
249256}
250257
258+ func makeHeaderWithLargeDifficulty (chain consensus.ChainReader , parent * types.Block , state * state.StateDB , engine consensus.Engine ) * types.Header {
259+ var time * big.Int
260+ if parent .Time () == nil {
261+ time = big .NewInt (1 )
262+ } else {
263+ time = new (big.Int ).Add (parent .Time (), big .NewInt (1 )) // block time is fixed at 10 seconds
264+ }
265+
266+ return & types.Header {
267+ Root : state .IntermediateRoot (chain .Config ().IsEIP158 (parent .Number ())),
268+ ParentHash : parent .Hash (),
269+ Coinbase : parent .Coinbase (),
270+ Difficulty : engine .CalcDifficulty (chain , time .Uint64 (), & types.Header {
271+ Number : parent .Number (),
272+ Time : new (big.Int ).Sub (time , big .NewInt (1 )),
273+ Difficulty : parent .Difficulty (),
274+ UncleHash : parent .UncleHash (),
275+ }),
276+ GasLimit : CalcGasLimit (parent , parent .GasLimit (), parent .GasLimit ()),
277+ Number : new (big.Int ).Add (parent .Number (), common .Big1 ),
278+ Time : time ,
279+ }
280+ }
281+
282+ func makeHeaderWithSmallDifficulty (chain consensus.ChainReader , parent * types.Block , state * state.StateDB , engine consensus.Engine ) * types.Header {
283+ var time * big.Int
284+ if parent .Time () == nil {
285+ time = big .NewInt (30 )
286+ } else {
287+ time = new (big.Int ).Add (parent .Time (), big .NewInt (30 )) // block time is fixed at 10 seconds
288+ }
289+
290+ return & types.Header {
291+ Root : state .IntermediateRoot (chain .Config ().IsEIP158 (parent .Number ())),
292+ ParentHash : parent .Hash (),
293+ Coinbase : parent .Coinbase (),
294+ Difficulty : engine .CalcDifficulty (chain , time .Uint64 (), & types.Header {
295+ Number : parent .Number (),
296+ Time : new (big.Int ).Sub (time , big .NewInt (30 )),
297+ Difficulty : parent .Difficulty (),
298+ UncleHash : parent .UncleHash (),
299+ }),
300+ GasLimit : CalcGasLimit (parent , parent .GasLimit (), parent .GasLimit ()),
301+ Number : new (big.Int ).Add (parent .Number (), common .Big1 ),
302+ Time : time ,
303+ }
304+ }
305+
251306// makeHeaderChain creates a deterministic chain of headers rooted at parent.
252307func makeHeaderChain (parent * types.Header , n int , engine consensus.Engine , db ethdb.Database , seed int ) []* types.Header {
253308 blocks := makeBlockChain (types .NewBlockWithHeader (parent ), n , engine , db , seed )
0 commit comments