# Path: caesure/bitcoin.py
# def P (msg):
#     def goto_testnet (self):
# def dhash (s):
# def rhash (s):
# def bcrepr (n):
# def float_to_btc (f):
# def key_to_address (s, version=0):
# def address_to_key (s, version=0):
# def compute_rewards (n):
# def compute_reward (height):
#     def __init__ (self):
#     def end (self):
#     def __init__ (self):
#     def copy (self):
#     def get_hash (self):
#     def dump (self, fout=sys.stdout):
#     def render (self):
#     def verify (self, index, lock_script, block_timestamp):
#     def dump (self, fout=sys.stdout):
#     def __len__ (self):
#     def get_height (self):
#     def make_TX (self):
#     def check_bits (self):
#     def get_merkle_hash (self):
#     def check_rules (self):
# W = sys.stderr.write
#     BITCOIN_PORT = 8333
#     MAGIC = '\xf9\xbe\xb4\xd9'
#     ADMIN_PORT = 8380
#     BD_FILE = '/tmp/caesure.bd'
#         ADMIN_PORT = 9380
#         BD_FILE = '/tmp/caesure_testnet.bd'
# ZERO_NAME = Name ('\x00' * 32)
# OBJ_TX    = 1
# OBJ_BLOCK = 2
# MAX_BLOCK_SIZE = 1000000
# COIN           = 100000000
# MAX_MONEY      = 21000000 * COIN
# NULL_OUTPOINT = (ZERO_NAME, 4294967295)
#         D = fout.write
# class NetworkConfig:
# class BadAddress (Exception):
# class timer:
# class TX (caesure.proto.TX):
# class BadBlock (Exception):
# class BLOCK (caesure.proto.BLOCK):


import coro
import sys
import argparse
from caesure.bitcoin import KEY, TX
from caesure.script import *

# XXX consider piping via stdin
# $ lpython ../utils/find_multi.py 315000 316000 > multisig_tests.txt
# $ lpython test_txns.py multisig_tests.txt

def HD (s):
    return s.decode ('hex')

# XXX fuzz these guys up and force them to fail.
def test (lock_script, tx_raw, index, block_timestamp):
    tx = TX()
    tx.unpack (tx_raw)
    tx.verify (index, lock_script, block_timestamp)
    
class BadFile:
    def __init__ (self, path):
        self.bad_path = path + '.bad'
        self.file = None
    def write (self, line):
        if self.file is None:
            self.file = open (self.bad_path, 'wb')
        self.file.write (line)
        self.file.flush()
    def close (self):
        if self.file:
            self.file.close()

def test_all (args):

    if args.debug:

# Path: caesure/bitcoin.py
# def P (msg):
#     def goto_testnet (self):
# def dhash (s):
# def rhash (s):
# def bcrepr (n):
# def float_to_btc (f):
# def key_to_address (s, version=0):
# def address_to_key (s, version=0):
# def compute_rewards (n):
# def compute_reward (height):
#     def __init__ (self):
#     def end (self):
#     def __init__ (self):
#     def copy (self):
#     def get_hash (self):
#     def dump (self, fout=sys.stdout):
#     def render (self):
#     def verify (self, index, lock_script, block_timestamp):
#     def dump (self, fout=sys.stdout):
#     def __len__ (self):
#     def get_height (self):
#     def make_TX (self):
#     def check_bits (self):
#     def get_merkle_hash (self):
#     def check_rules (self):
# W = sys.stderr.write
#     BITCOIN_PORT = 8333
#     MAGIC = '\xf9\xbe\xb4\xd9'
#     ADMIN_PORT = 8380
#     BD_FILE = '/tmp/caesure.bd'
#         ADMIN_PORT = 9380
#         BD_FILE = '/tmp/caesure_testnet.bd'
# ZERO_NAME = Name ('\x00' * 32)
# OBJ_TX    = 1
# OBJ_BLOCK = 2
# MAX_BLOCK_SIZE = 1000000
# COIN           = 100000000
# MAX_MONEY      = 21000000 * COIN
# NULL_OUTPOINT = (ZERO_NAME, 4294967295)
#         D = fout.write
# class NetworkConfig:
# class BadAddress (Exception):
# class timer:
# class TX (caesure.proto.TX):
# class BadBlock (Exception):
# class BLOCK (caesure.proto.BLOCK):


# Path: test/test_txns.py
import coro
import sys
import argparse
from caesure.bitcoin import KEY, TX
from caesure.script import *


# XXX consider piping via stdin
# $ lpython ../utils/find_multi.py 315000 316000 > multisig_tests.txt
# $ lpython test_txns.py multisig_tests.txt

def HD (s):
    return s.decode ('hex')

# XXX fuzz these guys up and force them to fail.
def test (lock_script, tx_raw, index, block_timestamp):
    tx = TX()
    tx.unpack (tx_raw)
    tx.verify (index, lock_script, block_timestamp)
    
class BadFile:
    def __init__ (self, path):
        self.bad_path = path + '.bad'
        self.file = None
    def write (self, line):
        if self.file is None:
            self.file = open (self.bad_path, 'wb')
        self.file.write (line)
        self.file.flush()
    def close (self):
        if self.file:
            self.file.close()

def test_all (args):

    if args.debug:

        verifying_machine.debug = True