forked from DeFiCh/ain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_median_time.py
executable file
·51 lines (41 loc) · 2.08 KB
/
feature_median_time.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
# Copyright (c) 2015-2019 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
"""Test median time change"""
from test_framework.test_framework import DefiTestFramework
from test_framework.util import assert_equal
import time
from random import randint
class MedianTimeTest(DefiTestFramework):
def set_test_params(self):
self.num_nodes = 1
self.extra_args = [ "-dummypos=1", '-amkheight=0', "-dakotaheight=1", "-fortcanningheight=100"],
self.setup_clean_chain = True
def CalcMedianTime(self):
medianTime = 11
times = []
for i in range(medianTime):
times.append(self.nodes[0].getblock(self.nodes[0].getblockhash(self.nodes[0].getblockcount() - i))['time'])
times.sort()
if self.nodes[0].getblockcount() >= 100:
return times[8]
return times[int(medianTime / 2)]
def GenerateBlocks(self, blocks):
for _ in range(blocks):
self.nodes[0].set_mocktime(int(time.time()) + randint(10, 60))
self.nodes[0].generate(1)
def run_test(self):
self.nodes[0].generate(11)
assert_equal(self.nodes[0].getblock(self.nodes[0].getblockhash(self.nodes[0].getblockcount()))['mediantime'], self.CalcMedianTime())
# Test some random block times pre-fork
self.GenerateBlocks(11)
assert_equal(self.nodes[0].getblock(self.nodes[0].getblockhash(self.nodes[0].getblockcount()))['mediantime'], self.CalcMedianTime())
# Move to hard fork
self.nodes[0].generate(100 - self.nodes[0].getblockcount())
assert_equal(self.nodes[0].getblock(self.nodes[0].getblockhash(self.nodes[0].getblockcount()))['mediantime'], self.CalcMedianTime())
# Test some random block times post-fork
self.GenerateBlocks(5)
assert_equal(self.nodes[0].getblock(self.nodes[0].getblockhash(self.nodes[0].getblockcount()))['mediantime'], self.CalcMedianTime())
if __name__ == '__main__':
MedianTimeTest().main()