Exemplo n.º 1
0
def build_pkb2(coinstate: CoinState) -> immutables.Map[PublicKey, PKBalance2]:
    public_key_balances_2: immutables.Map[PublicKey,
                                          PKBalance2] = immutables.Map()

    for height in range(coinstate.head().height + 1):
        block = coinstate.at_head.block_by_height[height]
        public_key_balances_2 = build_pkb2_block(coinstate, block,
                                                 public_key_balances_2)

    return public_key_balances_2
Exemplo n.º 2
0
def build_explorer(coinstate: CoinState) -> None:
    explorer_dir = Path(os.environ["EXPLORER_DIR"])
    public_key_balances_2: immutables.Map[PublicKey,
                                          PKBalance2] = immutables.Map()

    for height in range(coinstate.head().height + 1):
        print("Block", height)
        block = coinstate.at_head.block_by_height[height]
        potential_message = block.transactions[0].inputs[0].signature.signature

        if all([(32 <= b < 127) or (b == 10) for b in potential_message]):
            msg = "```\n" + str(potential_message, encoding="ascii") + "\n```"
        else:
            msg = ""

        unspent_transaction_outs = get_unspent_transaction_outs_before_block(
            coinstate, block)
        public_key_balances_2 = build_pkb2_block(coinstate, block,
                                                 public_key_balances_2)

        with open(explorer_dir / (human(block.hash()) + '.md'),
                  'w') as block_f:

            block_f.write(f"""## Block {human(block.hash())}

Attribute | Value
--- | ---
Height | {block.height}
Hash | {human(block.hash())}
Timestamp | {datetime.fromtimestamp(block.timestamp, tz=timezone.utc).isoformat()}
Target | {human(block.target)}
Merke root | {human(block.merkle_root_hash)}
Nonce | {block.nonce}

{msg}

### Transactions

Hash | Amount
--- | ---
""")

            for transaction in block.transactions:
                h = human(transaction.hash())
                v = show_coin(sum(o.value for o in transaction.outputs))
                block_f.write(f"""[{h}]({h}.md) | {v} \n""")

                with open(explorer_dir / (human(transaction.hash()) + ".md"),
                          'w') as transaction_f:
                    transaction_f.write(
                        f"""## Transaction {human(transaction.hash())}

In block [{human(block.hash())}]({human(block.hash())}.md)

### Inputs

Transaction | Output Index | Value | Address
--- | --- | --- | ---
""")
                    for input in transaction.inputs:
                        output_reference = input.output_reference
                        if output_reference.hash != 32 * b'\x00':
                            output: Output = unspent_transaction_outs[
                                output_reference]
                            h = human(output_reference.hash)
                            v = show_coin(output.value)
                            a = "SKE" + human(
                                output.public_key.public_key) + "PTI"

                            transaction_f.write(
                                f"""[{h}]({h}.md) | {output_reference.index} | """
                                f"""{v} | [{a}]({a}.md)\n""")
                        else:
                            h = human(output_reference.hash)
                            v = ""
                            a = "Thin Air"

                            transaction_f.write(
                                f"""{h} | {output_reference.index} | """
                                f"""{v} | {a}\n""")

                    transaction_f.write("""### Outputs

Value | Address
--- | ---
""")
                    for output in transaction.outputs:
                        v = show_coin(output.value)
                        a = "SKE" + human(output.public_key.public_key) + "PTI"

                        transaction_f.write(f"""{v} | [{a}]({a}.md)\n""")

    for pk, pkb2 in public_key_balances_2.items():
        v = show_coin(pkb2.value)
        address = "SKE" + human(pk.public_key) + "PTI"
        with open(explorer_dir / (address + ".md"), 'w') as address_f:
            address_f.write(f"""## {address}

Current balance: {v}
(as of block {coinstate.head().height})

## Received in

Transaction | Output Index
--- | ---
""")
            for output_reference in pkb2.all_output_references:
                h = human(output_reference.hash)
                address_f.write(
                    f"""[{h}]({h}.md) | {output_reference.index}\n""")

            if len(pkb2.spent_in_transactions) > 0:
                address_f.write("""
## Spent in

Transaction | ...
--- | ---
""")

                for transaction in pkb2.spent_in_transactions:
                    address_f.write(f"""{human(transaction.hash())} | ...\n""")

            else:
                address_f.write("""
## Spent in

-- not spent --
""")