Skip to content

Commit 6f3cb12

Browse files
committed
core: allow modifying test-chain block times
1 parent 58fbcaa commit 6f3cb12

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

core/chain_makers.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ func (b *BlockGen) PrevBlock(index int) *types.Block {
131131
return b.chain[index]
132132
}
133133

134+
// OffsetTime modifies the time instance of a block, implicitly changing its
135+
// associated difficulty. It's useful to test scenarios where forking is not
136+
// tied to chain length directly.
137+
func (b *BlockGen) OffsetTime(seconds int64) {
138+
b.header.Time.Add(b.header.Time, new(big.Int).SetInt64(seconds))
139+
if b.header.Time.Cmp(b.parent.Header().Time) <= 0 {
140+
panic("block time out of range")
141+
}
142+
b.header.Difficulty = CalcDifficulty(b.header.Time.Uint64(), b.parent.Time().Uint64(), b.parent.Number(), b.parent.Difficulty())
143+
}
144+
134145
// GenerateChain creates a chain of n blocks. The first block's
135146
// parent will be the provided parent. db is used to store
136147
// intermediate states and should contain the parent's state trie.

0 commit comments

Comments
 (0)