def calculate_new_bits(previous_bits, time_differential): if time_differential > TWO_WEEKS * 4: time_differential = TWO_WEEKS * 4 if time_differential < TWO_WEEKS // 4: time_differential = TWO_WEEKS // 4 new_target = bits_to_target(previous_bits) * time_differential // TWO_WEEKS return target_to_bits(new_target)
def new_bits(self, beginning_block): time_differential = self.timestamp - beginning_block.timestamp if time_differential > TWO_WEEKS * 4: time_differential = TWO_WEEKS * 4 if time_differential < TWO_WEEKS // 4: time_differential = TWO_WEEKS // 4 new_target = self.target() * time_differential // TWO_WEEKS if new_target > MAX_TARGET: new_target = MAX_TARGET return target_to_bits(new_target)
def test_exercise_8(self): block1_hex = '000000203471101bbda3fe307664b3283a9ef0e97d9a38a7eacd8800000000000000000010c8aba8479bbaa5e0848152fd3c2289ca50e1c3e58c9a4faaafbdf5803c5448ddb845597e8b0118e43a81d3' block2_hex = '02000020f1472d9db4b563c35f97c428ac903f23b7fc055d1cfc26000000000000000000b3f449fcbe1bc4cfbcb8283a0d2c037f961a3fdf2b8bedc144973735eea707e1264258597e8b0118e5f00474' last_block = Block.parse(BytesIO(bytes.fromhex(block1_hex))) first_block = Block.parse(BytesIO(bytes.fromhex(block2_hex))) time_differential = last_block.timestamp - first_block.timestamp if time_differential > TWO_WEEKS * 4: time_differential = TWO_WEEKS * 4 if time_differential < TWO_WEEKS // 4: time_differential = TWO_WEEKS // 4 new_target = last_block.target() * time_differential // TWO_WEEKS new_bits = target_to_bits(new_target) self.assertEqual(new_bits.hex(), '80df6217')
def new_bits(self, beginning_block): """Calculates the new bits for a 2016-block epoch. Assumes current block is the last of the 2016-block epoch. Requires the first block of the epoch.""" # calculate the time differential time_differential = self.timestamp - beginning_block.timestamp # if the time differential is greater than 8 weeks, set to 8 weeks if time_differential > TWO_WEEKS * 4: time_differential = TWO_WEEKS * 4 # if the time differential is less than half a week, set to half a week if time_differential < TWO_WEEKS // 4: time_differential = TWO_WEEKS // 4 # the new target is the current target * time differential / two weeks new_target = self.target() * time_differential // TWO_WEEKS # if the new target is bigger than MAX_TARGET, set to MAX_TARGET if new_target > MAX_TARGET: new_target = MAX_TARGET # convert the new target to bits using the target_to_bits function return target_to_bits(new_target)
from io import BytesIO from block import Block from helper import TWO_WEEKS, target_to_bits block_hex1 = "000000203471101bbda3fe307664b3283a9ef0e97d9a38a7eacd8800000000000000000010c8ab\ a8479bbaa5e0848152fd3c2289ca50e1c3e58c9a4faaafbdf5803c5448ddb845597e8b0118e43a\ 81d3" block_hex2 = "02000020f1472d9db4b563c35f97c428ac903f23b7fc055d1cfc26000000000000000000b3f449\ fcbe1bc4cfbcb8283a0d2c037f961a3fdf2b8bedc144973735eea707e1264258597e8b0118e5f0\ 0474" last_block = Block.parse(BytesIO(bytes.fromhex(block_hex1))) first_block = Block.parse(BytesIO(bytes.fromhex(block_hex2))) time_diferencial = last_block.timestamp - first_block.timestamp if time_diferencial > TWO_WEEKS * 4: time_diferencial = TWO_WEEKS * 4 if time_diferencial < TWO_WEEKS // 4: time_diferencial = TWO_WEEKS // 4 new_target = last_block.target() * time_diferencial // TWO_WEEKS new_bits = target_to_bits(new_target) print(new_bits.hex())
# # Exercise 12 # block1_hex = '000000203471101bbda3fe307664b3283a9ef0e97d9a38a7eacd8800000000000000000010c8aba8479bbaa5e0848152fd3c2289ca50e1c3e58c9a4faaafbdf5803c5448ddb845597e8b0118e43a81d3' block2_hex = '02000020f1472d9db4b563c35f97c428ac903f23b7fc055d1cfc26000000000000000000b3f449fcbe1bc4cfbcb8283a0d2c037f961a3fdf2b8bedc144973735eea707e1264258597e8b0118e5f00474' block1_stream = BytesIO(bytes.fromhex(block1_hex)) block2_stream = BytesIO(bytes.fromhex(block2_hex)) block1 = Block.parse(block1_stream) block2 = Block.parse(block2_stream) time_differential = block2.time_differential(block1) print("time differential: ", time_differential) new_target = block1.target() * time_differential // helper.TWO_WEEKS print("new target: ", new_target) new_bits = helper.target_to_bits(new_target) print(new_bits.hex()) print("Doing the other one: *****************") block1_hex = '000000203471101bbda3fe307664b3283a9ef0e97d9a38a7eacd8800000000000000000010c8aba8479bbaa5e0848152fd3c2289ca50e1c3e58c9a4faaafbdf5803c5448ddb845597e8b0118e43a81d3' block2_hex = '02000020f1472d9db4b563c35f97c428ac903f23b7fc055d1cfc26000000000000000000b3f449fcbe1bc4cfbcb8283a0d2c037f961a3fdf2b8bedc144973735eea707e1264258597e8b0118e5f00474' last_block = Block.parse(BytesIO(bytes.fromhex(block2_hex))) first_block = Block.parse(BytesIO(bytes.fromhex(block1_hex))) time_differential = last_block.timestamp - first_block.timestamp print("time differential: ", time_differential) if time_differential > TWO_WEEKS * 4: time_differential = TWO_WEEKS * 4 if time_differential < TWO_WEEKS // 4: time_differential = TWO_WEEKS // 4 new_target = last_block.target() * time_differential // TWO_WEEKS