Ejemplo n.º 1
0
 def get_candidate(self, chain):
     ''' return a block object that is a candidate for the next block '''
     new_grachten_header = self.header.get_candidate(chain, self)
     new_tree_list = [
         self.merkle_tree.leaves[0],
         new_grachten_header.get_hash(),
         global_hash(b'some_message'),
         global_hash(b'another_message?')
     ]
     return GrachtenBlock.make(leaves=new_tree_list,
                               header=new_grachten_header,
                               uncles=[])
Ejemplo n.º 2
0
    def add_header_to_state(self, header):
        block_hash = header.get_hash()
        if self.state[block_hash] != 0:
            raise ValidationError('Chainheaders: block header already added')
        self.state[block_hash + self._BLOCK_HEADER_BYTES] = header.to_bytes()
        header_height = self.state[header.parent_hash + self._BLOCK_HEIGHT] + 1
        self.state[block_hash + self._BLOCK_HEIGHT] = header_height
        sigma_diff = self.state[
            header.parent_hash +
            self._BLOCK_SIGMA_DIFF] + BitcoinHeader.bits_to_diff(header.bits)
        self.state[block_hash + self._BLOCK_SIGMA_DIFF] = sigma_diff

        # This section rolls up an incremental merkle tree
        previous_ancestors_n = self.state[header.parent_hash +
                                          self._BLOCK_ANCESTORS_N]
        previous_ancestors = []
        previous_ancestors_start_index = header.parent_hash + self._BLOCK_ANCESTORS
        for i in range(previous_ancestors_n):
            previous_ancestors.append(
                self.state[previous_ancestors_start_index + i])
        ancestors = previous_ancestors[:]
        j = 2
        while header_height % j == 0:
            ancestors = ancestors[:-2] + [
                global_hash(ancestors[-2].to_bytes(32, 'big') +
                            ancestors[-1].to_bytes(32, 'big'))
            ]
            j *= 2
        for i in range(len(ancestors)):
            self.state[block_hash + self._BLOCK_ANCESTORS + i] = ancestors[i]

        # If new head
        if sigma_diff > self.state[self._CHAIN_TOP_SIGMA_DIFF]:
            self.state[self._CHAIN_TOP_BLOCK] = block_hash
            self.state[self._CHAIN_TOP_SIGMA_DIFF] = sigma_diff
Ejemplo n.º 3
0
 def get_hash(self):
     if self.my_hash == None:
         #debug('StateDelta.get_hash, complete_kvs', self.complete_kvs())
         keys = list(self.all_keys())
         # TODO: keys.sort() definition unknown ATM, needs to be specific so
         # identical states generate identical hashes (ints and bytes may be
         # being used as keys, not checked currently).
         keys.sort()
         leaves = []
         for k in keys:
             if isinstance(self[k], Encodium):
                 leaves.extend([global_hash(k), self[k].get_hash()])
             else:
                 leaves.extend([global_hash(k), global_hash(self[k])])
         merkle_tree = MerkleLeavesToRoot(leaves=leaves)
         self.my_hash = merkle_tree.get_hash()
     return self.my_hash
Ejemplo n.º 4
0
 def get_hash(self):
     if self.my_hash == None:
         #debug('StateDelta.get_hash, complete_kvs', self.complete_kvs())
         keys = list(self.all_keys())
         # TODO: keys.sort() definition unknown ATM, needs to be specific so
         # identical states generate identical hashes (ints and bytes may be
         # being used as keys, not checked currently).
         keys.sort()
         leaves = []
         for k in keys:
             if isinstance(self[k], Encodium):
                 leaves.extend([global_hash(k), self[k].get_hash()])
             else:
                 leaves.extend([global_hash(k), global_hash(self[k])])
         merkle_tree = MerkleLeavesToRoot(leaves=leaves)
         self.my_hash = merkle_tree.get_hash()
     return self.my_hash
Ejemplo n.º 5
0
 def assert_valid_signature(self, message):
     '''
     '''
     # TODO assert pubkey is valid for curve
     if not pycoin.ecdsa.verify(pycoin.ecdsa.generator_secp256k1,
                                (self.pubkey_x, self.pubkey_y),
                                global_hash(message), (self.r, self.s)):
         raise ValidationError('Signature failed to verify')
     debug('Signature.assert_valid_signature', message)
Ejemplo n.º 6
0
 def get_hash(self):
     leaves = []
     names = list(self.state_dict.keys())
     # all names are bytes, need to investigate exactly how these are sorted.
     names.sort()
     for n in names:
         leaves.extend([global_hash(n), self.state_dict[n].get_hash()])
     merkle_root = MerkleLeavesToRoot(leaves=leaves)
     debug('SuperState: root: ', merkle_root.get_hash(), [self.state_dict[n].complete_kvs() for n in names])
     return merkle_root.get_hash()
Ejemplo n.º 7
0
 def assert_valid_signature(self, message):
     '''
     '''
     # TODO assert pubkey is valid for curve
     if not pycoin.ecdsa.verify(pycoin.ecdsa.generator_secp256k1,
                                (self.pubkey_x, self.pubkey_y),
                                global_hash(message),
                                (self.r, self.s)):
         raise ValidationError('Signature failed to verify')
     debug('Signature.assert_valid_signature', message)
Ejemplo n.º 8
0
 def get_hash(self):
     temp_cont = [
         self.version.to_bytes(4, 'big'),
         self.height.to_bytes(4, 'big'),
         self.target.to_bytes(32, 'big'),
         self.sigma_diff.to_bytes(32, 'big'),
         self.timestamp.to_bytes(5, 'big'),
         self.votes.to_bytes(1, 'big'),
         self.uncles_mr.to_bytes(32, 'big'),
     ]
     temp_cont.extend( [i.to_bytes(32, 'big') for i in self.previous_blocks] )
     return global_hash(b''.join(temp_cont))
Ejemplo n.º 9
0
 def get_hash(self):
     leaves = []
     names = list(self.state_dict.keys())
     # all names are bytes, need to investigate exactly how these are sorted.
     names.sort()
     debug('SuperState: names', names)
     for n in names:
         leaves.extend([global_hash(n), self.state_dict[n].get_hash()])  # h(name), h(state) pairs
     debug('SuperState: leaves', leaves)
     merkle_root = MerkleLeavesToRoot(leaves=leaves)
     debug('SuperState: MR', merkle_root.get_hash())
     return merkle_root.get_hash()
Ejemplo n.º 10
0
 def get_hash(self):
     temp_cont = [
         self.version.to_bytes(4, 'big'),
         self.height.to_bytes(4, 'big'),
         self.target.to_bytes(32, 'big'),
         self.sigma_diff.to_bytes(32, 'big'),
         self.timestamp.to_bytes(5, 'big'),
         self.votes.to_bytes(1, 'big'),
         self.uncles_mr.to_bytes(32, 'big'),
     ]
     temp_cont.extend([i.to_bytes(32, 'big') for i in self.previous_blocks])
     return global_hash(b''.join(temp_cont))
Ejemplo n.º 11
0
 def sign(self, secret_exponent, message):
     ''' Set r,s according to
     '''
     assert isinstance(message, int)
     # TODO check that we're doing this right.
     self.r, self.s = pycoin.ecdsa.sign(pycoin.ecdsa.generator_secp256k1,
                                        secret_exponent,
                                        global_hash(message))
     self.pubkey_x, self.pubkey_y = pycoin.ecdsa.public_pair_for_secret_exponent(
         pycoin.ecdsa.generator_secp256k1, secret_exponent)
     self.sender = self.pubkey_x
     self.assert_valid_signature(message)
     debug('Signature.sign', message)
Ejemplo n.º 12
0
 def sign(self, secret_exponent, message):
     ''' Set r,s according to
     '''
     assert isinstance(message, int)
     # TODO check that we're doing this right.
     self.r, self.s = pycoin.ecdsa.sign(pycoin.ecdsa.generator_secp256k1,
                                        secret_exponent,
                                        global_hash(message))
     self.pubkey_x, self.pubkey_y = pycoin.ecdsa.public_pair_for_secret_exponent(pycoin.ecdsa.generator_secp256k1,
                                                                                 secret_exponent)
     self.sender = self.pubkey_x
     self.assert_valid_signature(message)
     debug('Signature.sign', message)
Ejemplo n.º 13
0
 def get_hash(self):
     return global_hash(self.serialize())
Ejemplo n.º 14
0
 def my_hash(self, msg):
     return global_hash(msg)
Ejemplo n.º 15
0
 def get_hash(self):
     return global_hash(self.serialize())
Ejemplo n.º 16
0
 def my_hash(self, msg):
     return global_hash(msg)
 def get_hash(self):
     return global_hash(self.to_bytes())
Ejemplo n.º 18
0
 def get_candidate(self, chain):
     ''' return a block object that is a candidate for the next block '''
     new_grachten_header = self.header.get_candidate(chain, self)
     new_tree_list = [self.merkle_tree.leaves[0], new_grachten_header.get_hash(), global_hash(b'some_message'), global_hash(b'another_message?')]
     return GrachtenBlock.make(leaves=new_tree_list, header=new_grachten_header, uncles=[])
Ejemplo n.º 19
0
 def test_hashes(self):
     self.assertTrue(sha256(b'test') == unhexlify("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"))
     self.assertTrue(dsha256(b'test') == unhexlify("954d5a49fd70d9b8bcdb35d252267829957f7ef7fa6c74f88419bdc5e82209f4"))
     self.assertTrue(dsha256R(b'test') == unhexlify("f40922e8c5bd1984f8746cfaf77e7f9529782652d235dbbcb8d970fd495a4d95"))
     self.assertTrue(global_hash(b'test') == 70622639689279718371527342103894932928233838121221666359043189029713682937432)
Ejemplo n.º 20
0
 def __hash__(self):
     return global_hash(self.serialize())
Ejemplo n.º 21
0
 def get_hash(self):
     return global_hash(self.to_bytes())
Ejemplo n.º 22
0
 def __hash__(self):
     return global_hash(self.serialize())