def get_example_genesis_block():
    return Block.deserialize(computer(
        """000000000000000000000000000000000000000000000000000000000000000000008278968af4bd613aa24a5ccd5280211b3101e3"""
        """ff62621bb11500509d1bbe2a956046240b0100000000000000000000000000000000000000000000000000000000000000000000d7"""
        """38f2c472180cb401f650b12be96ec25bfd9b4e9908c6c9089d9bf26401646f87000000000000000000000000000000000000000000"""
        """0000000000000000000000077a14cfbe21d47f367f23f9a464c765541b1b07bef9f5a95901e0bffe3a1a2f01000100000000000000"""
        """000000000000000000000000000000000000000000000000000000000001000000000001000000012a05f200027878787878787878"""
        """7878787878787878787878787878787878787878787878787878787878787878787878787878787878787878787878787878787878"""
        """787878"""))
Beispiel #2
0
def read_chain_from_disk() -> CoinState:
    if os.path.isfile('chain.cache'):
        print("Reading cached chain")
        with open('chain.cache', 'rb') as file:
            coinstate = CoinState.load(lambda: pickle.load(file))
    else:
        coinstate = CoinState.zero()

    rewrite = False

    if os.path.isdir('chain'):
        # the code below is no longer needed by normal users, but some old testcases still rely on it:
        for filename in sorted(os.listdir('chain')):
            (height_str, hash_str) = filename.split("-")
            (height, hash) = (int(height_str), computer(hash_str))

            if hash not in coinstate.block_by_hash:

                if height % 1000 == 0:
                    print(filename)

                if os.path.getsize(f"chain/{filename}") == 0:
                    print("Stopping at empty block file: %s" % filename)
                    break

                with open(Path("chain") / filename, 'rb') as f:
                    try:
                        block = Block.stream_deserialize(f)
                    except Exception as e:
                        raise Exception("Corrupted block on disk: %s" % filename) from e
                    try:
                        coinstate = coinstate.add_block_no_validation(block)
                    except Exception:
                        print("Failed to add block at height=%d, previous_hash=%s"
                              % (block.height, human(block.header.summary.previous_block_hash)))
                        break

                rewrite = True

    if rewrite:
        DiskInterface().write_chain_cache_to_disk(coinstate)

    return coinstate
Beispiel #3
0
                  signature=SECP256k1Signature(b"b" * 64))
        ],
        outputs=[Output(value=1582, public_key=SECP256k1PublicKey(b"g" * 64))],
    )

    assert repr(
        trans
    ) == "Transaction #4025f3f13790dc96d857562dabcdd257ee9dfd95ce126e11d8cbbe64ac1bbec4"


example_block_summary = BlockSummary(
    height=445,
    previous_block_hash=b"a" * 32,
    merkle_root_hash=b"b" * 32,
    timestamp=1231006505,
    target=computer(
        "00000000ffff0000000000000000000000000000000000000000000000000000"),
    nonce=1234,
)


def test_block_summary_serialization():
    serialize_and_deserialize(example_block_summary)


example_pow_evidence = PowEvidence(
    summary_hash=b"d" * 32,
    chain_sample=b"e" * 32,
    block_hash=b"f" * 32,
)

Beispiel #4
0
def read_chain_from_disk() -> CoinState:
    if os.path.isfile('chain.cache'):
        print("Reading cached chain")
        with open('chain.cache', 'rb') as file:
            coinstate = CoinState.load(lambda: pickle.load(file))
    else:
        try:
            print("Pre-download blockchain from trusted source to 'chain.zip'")
            with urllib.request.urlopen(TRUSTED_BLOCKCHAIN_ZIP) as resp:
                with open('chain.zip', 'wb') as outfile:
                    outfile.write(resp.read())
            print("Reading initial chain from zipfile")
            coinstate = CoinState.zero()
            with zipfile.ZipFile('chain.zip') as zip:
                for entry in zip.infolist():
                    if not entry.is_dir():
                        filename = entry.filename.split('/')[1]
                        height = int(filename.split("-")[0])
                        if height % 1000 == 0:
                            print(filename)

                        data = zip.read(entry)
                        block = Block.stream_deserialize(BytesIO(data))
                        coinstate = coinstate.add_block_no_validation(block)
        except Exception:
            print(
                "Error reading zip file. We'll start with an empty blockchain instead."
                + traceback.format_exc())
            coinstate = CoinState.zero()

    rewrite = False

    if os.path.isdir('chain'):
        # the code below is no longer needed by normal users, but some old testcases still rely on it:
        for filename in sorted(os.listdir('chain')):
            (height_str, hash_str) = filename.split("-")
            (height, hash) = (int(height_str), computer(hash_str))

            if hash not in coinstate.block_by_hash:

                if height % 1000 == 0:
                    print(filename)

                if os.path.getsize(f"chain/{filename}") == 0:
                    print("Stopping at empty block file: %s" % filename)
                    break

                with open(Path("chain") / filename, 'rb') as f:
                    try:
                        block = Block.stream_deserialize(f)
                    except Exception as e:
                        raise Exception("Corrupted block on disk: %s" %
                                        filename) from e
                    try:
                        coinstate = coinstate.add_block_no_validation(block)
                    except Exception:
                        print(
                            "Failed to add block at height=%d, previous_hash=%s"
                            %
                            (block.height,
                             human(block.header.summary.previous_block_hash)))
                        break

                rewrite = True

    if rewrite:
        DiskInterface().write_chain_cache_to_disk(coinstate)

    return coinstate
Beispiel #5
0
# [a] riling up the troops, i.e. convincing everyone that they're all in some kind of political project together.
# [b] proving that the blockchain is no older than a certain date (how else could you have produced the headline?).
#
# The second purpose is somewhat suspicious though, since:
#
# [1] given crypto's own set of assumptions, how long you spend calculating hashes shouldn't matter, all that matters is
#     the total (on average) of hashes being calculated.
# [2] the main threat against any crypto is not that the original creator spent some time mining coins, but that someone
#     else comes along with a _newer_ coin that's just as good or better than the old one. No newspaper headline can
#     guard against that.
#
# Thus, Skepticoin's genesis block only does [a].

just_believe_in_me = (b"You buy a piece of paradise\n"
                      b"You buy a piece of me\n"
                      b"I'll get you everything you wanted\n"
                      b"I'll get you everything you need\n"
                      b"Don't need to believe in hereafter\n"
                      b"Just believe in me")

genesis_block_data = computer(
    '00000000000000000000000000000000000000000000000000000000000000000000616c35621abdf928185b74d57985cea7ff2d66ef318c58'
    'ec7ea8dd01ed089028604e7f3101000000000000000000000000000000000000000000000000000000000000000000003aea13176dbcbf6210'
    '55bdb3d6a138be4b73229d8584cb380e1dd1bbe1cedd42820000000000000000000000000000000000000000000000000000000000000000e3'
    '8ee41a6b0f6584fe8b95bd8c8d7b4d6db961fa5c2a6fafe72ea1533dd2838b0100010000000000000000000000000000000000000000000000'
    '000000000000000000000000000100000000ab596f75206275792061207069656365206f662070617261646973650a596f7520627579206120'
    '7069656365206f66206d650a49276c6c2067657420796f752065766572797468696e6720796f752077616e7465640a49276c6c206765742079'
    '6f752065766572797468696e6720796f75206e6565640a446f6e2774206e65656420746f2062656c6965766520696e20686572656166746572'
    '0a4a7573742062656c6965766520696e206d6501000000003b9aca0002aac3faad6ddc26ec4674328741498fe74bdb0d8e49a22473a02370e5'
    '3d69b0079819d5ac3f0cd36f25578eb042ad2a7b59f84a0b5f622e41ac982f478e8cb259')