#!/usr/bin/env python3
# Copyright (c) 2016-2025 The Hush developers
# Distributed under the GPLv3 software license, see the accompanying
# file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html

from test_framework.test_framework import BitcoinTestFramework
from test_framework.authproxy import JSONRPCException
from test_framework.util import assert_equal, assert_greater_than, \
    initialize_chain_clean, initialize_chain, start_nodes, start_node, connect_nodes_bi, \
    stop_nodes, sync_blocks, sync_mempools, wait_bitcoinds, rpc_port, assert_raises, assert_true, \
    wait_and_assert_operationid_status

import time
from decimal import Decimal
from random import choice
from string import ascii_uppercase

def assert_success(result):
    assert_equal(result['result'], 'success')

def assert_error(result):
    assert_equal(result['result'], 'error')

class LockZinsTest (BitcoinTestFramework):
    def setup_chain(self):
        print("Initializing test directory "+self.options.tmpdir)
        self.num_nodes = 1
        self.options.nocleanup = 1 # do not delete datadir after test run
        initialize_chain_clean(self.options.tmpdir, self.num_nodes)

    def setup_network(self, split = False):
        print("Setting up network...")
        self.nodes   = start_nodes(self.num_nodes, self.options.tmpdir,
                    extra_args=[[
                    # always give -ac_name as first extra_arg and port as third
                    '-ac_name=ZZZ',
                    #'-ac_algo=randomx',
                    #'-testnode=1', # why does this make the test node hang before run_test() ?
                    '-conf='+self.options.tmpdir+'/node0/regtest/ZZZ.conf',
                    '-port=64367',
                    '-rpcport=64368',
                    '-ac_supply=1',
                    '-ac_reward=100000000',
                    '-ac_private=1',
                    '-allowlist=127.0.0.1',
                    #'-debug',
                    '-regtest',
                    '--daemon',
                    '-zrpc',
                    '-zdebug',
                    '-zrpcunsafe'
                    ]]
        )
        self.is_network_split = False
        self.rpc              = self.nodes[0]
        self.sync_all()
        print("Done setting up network")

    def run_test (self):
        print("Mining blocks...")
        rpc     = self.nodes[0]
        # mine initial ac_supply
        rpc.generate(1)
        self.sync_all()

        zaddr1 = rpc.z_getnewaddress()
        zaddr2 = rpc.z_getnewaddress()

        x = 0
        while x < 15:
            rpc.generate(1)
            self.sync_all()
            time.sleep(1)
            rpc.z_shieldcoinbase('*', zaddr1, 0, 1)
            x = x+1

        rpc.generate(11)
        self.sync_all()
        # rpc.z_listunspent()
        rpc.z_getbalances()

        recipients = []
        recipients.append({"address": zaddr2, "amount": Decimal('3')})

        # queue 4 ztxs, which will try to spend the same funds multiple times
        # without correct locking of zins
        opid1 = rpc.z_sendmany(zaddr1,recipients,1,0)
        rpc.z_listlockunspent()
        opid2 = rpc.z_sendmany(zaddr1,recipients,1,0)
        rpc.z_listlockunspent()
        opid3 = rpc.z_sendmany(zaddr1,recipients,1,0)
        rpc.z_listlockunspent()
        opid4 = rpc.z_sendmany(zaddr1,recipients,1,0)

        rpc.generate(1)
        self.sync_all()

        # now that we have fullyNotified, this may not be needed?
        #time.sleep(3)

        wait_and_assert_operationid_status(self.nodes[0], opid1)
        wait_and_assert_operationid_status(self.nodes[0], opid2)
        wait_and_assert_operationid_status(self.nodes[0], opid3)
        wait_and_assert_operationid_status(self.nodes[0], opid4)

        rpc.z_getoperationstatus()

if __name__ == '__main__':
    LockZinsTest ().main()
