Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/messages.py
Show All 20 Lines | |||||
import copy | import copy | ||||
import hashlib | import hashlib | ||||
import random | import random | ||||
import socket | import socket | ||||
import struct | import struct | ||||
import time | import time | ||||
import unittest | import unittest | ||||
from base64 import b64decode, b64encode | from base64 import b64decode, b64encode | ||||
from codecs import encode | |||||
from enum import IntEnum | from enum import IntEnum | ||||
from io import BytesIO | from io import BytesIO | ||||
from typing import List | from typing import List | ||||
from test_framework.siphash import siphash256 | from test_framework.siphash import siphash256 | ||||
from test_framework.util import assert_equal, uint256_hex | from test_framework.util import assert_equal, uint256_hex | ||||
MAX_LOCATOR_SZ = 101 | MAX_LOCATOR_SZ = 101 | ||||
▲ Show 20 Lines • Show All 432 Lines • ▼ Show 20 Lines | def rehash(self): | ||||
self.sha256 = None | self.sha256 = None | ||||
self.calc_sha256() | self.calc_sha256() | ||||
return self.hash | return self.hash | ||||
# self.sha256 and self.hash -- those are expected to be the txid. | # self.sha256 and self.hash -- those are expected to be the txid. | ||||
def calc_sha256(self): | def calc_sha256(self): | ||||
if self.sha256 is None: | if self.sha256 is None: | ||||
self.sha256 = uint256_from_str(hash256(self.serialize())) | self.sha256 = uint256_from_str(hash256(self.serialize())) | ||||
self.hash = encode( | self.hash = hash256(self.serialize())[::-1].hex() | ||||
hash256(self.serialize())[::-1], 'hex_codec').decode('ascii') | |||||
def get_id(self): | def get_id(self): | ||||
# For now, just forward the hash. | # For now, just forward the hash. | ||||
self.calc_sha256() | self.calc_sha256() | ||||
return self.hash | return self.hash | ||||
def is_valid(self): | def is_valid(self): | ||||
self.calc_sha256() | self.calc_sha256() | ||||
▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | def calc_sha256(self): | ||||
r = b"" | r = b"" | ||||
r += struct.pack("<i", self.nVersion) | r += struct.pack("<i", self.nVersion) | ||||
r += ser_uint256(self.hashPrevBlock) | r += ser_uint256(self.hashPrevBlock) | ||||
r += ser_uint256(self.hashMerkleRoot) | r += ser_uint256(self.hashMerkleRoot) | ||||
r += struct.pack("<I", self.nTime) | r += struct.pack("<I", self.nTime) | ||||
r += struct.pack("<I", self.nBits) | r += struct.pack("<I", self.nBits) | ||||
r += struct.pack("<I", self.nNonce) | r += struct.pack("<I", self.nNonce) | ||||
self.sha256 = uint256_from_str(hash256(r)) | self.sha256 = uint256_from_str(hash256(r)) | ||||
self.hash = encode(hash256(r)[::-1], 'hex_codec').decode('ascii') | self.hash = hash256(r)[::-1].hex() | ||||
def rehash(self): | def rehash(self): | ||||
self.sha256 = None | self.sha256 = None | ||||
self.calc_sha256() | self.calc_sha256() | ||||
return self.sha256 | return self.sha256 | ||||
def __repr__(self): | def __repr__(self): | ||||
return "CBlockHeader(nVersion={} hashPrevBlock={:064x} hashMerkleRoot={:064x} nTime={} nBits={:08x} nNonce={:08x})".format( | return "CBlockHeader(nVersion={} hashPrevBlock={:064x} hashMerkleRoot={:064x} nTime={} nBits={:08x} nNonce={:08x})".format( | ||||
▲ Show 20 Lines • Show All 1,690 Lines • Show Last 20 Lines |