#!/usr/bin/env python3
# Copyright (c) 2020 The Bitcoin developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""
Tests for Bitcoin ABC mining RPCs
"""

from test_framework.messages import (
    COIN,
)
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
    assert_equal,
    assert_greater_than_or_equal,
)

from decimal import Decimal

AXION_ACTIVATION_TIME = 2000000600


class AbcMiningRPCTest(BitcoinTestFramework):
    def set_test_params(self):
        self.num_nodes = 2
        self.extra_args = [[
            '-enableminerfund',
            '-axionactivationtime={}'.format(AXION_ACTIVATION_TIME),
        ], []]

    def run_test(self):
        node = self.nodes[0]
        address = node.get_deterministic_priv_key().address

        # Move MTP forward to axion activation
        node.setmocktime(AXION_ACTIVATION_TIME)
        node.generatetoaddress(6, address)
        assert_equal(
            node.getblockchaininfo()['mediantime'],
            AXION_ACTIVATION_TIME)

        # Assert the results of getblocktemplate have expected values. Keys not
        # in 'expected' are not checked.
        def assert_getblocktemplate(expected):
            blockTemplate = node.getblocktemplate()
            for key, value in expected.items():
                assert_equal(blockTemplate[key], value)

        def get_best_coinbase():
            return node.getblock(node.getbestblockhash(), 2)['tx'][0]

        coinbase = get_best_coinbase()
        assert_equal(len(coinbase['vout']), 1)
        block_reward = coinbase['vout'][0]['value']

        # We don't need to test all fields in getblocktemplate since many of
        # them are covered in mining_basic.py
        assert_equal(node.getmempoolinfo()['size'], 0)
        assert_getblocktemplate({
            # Although the coinbase value need not necessarily be the same as
            # the last block due to halvings and fees, we know this to be true
            # since we are not crossing a halving boundary and there are no
            # transactions in the mempool.
            'coinbasevalue': block_reward * COIN,
        })

        # First block with the new rules
        node.generatetoaddress(1, address)

        # We expect the coinbase to have multiple outputs now
        coinbase = get_best_coinbase()
        assert_greater_than_or_equal(len(coinbase['vout']), 2)
        total = Decimal()
        for o in coinbase['vout']:
            total += o['value']

        assert_equal(total, block_reward)
        assert_getblocktemplate({
            # Again, we assume the coinbase value is the same as prior blocks.
            'coinbasevalue': block_reward * COIN,
        })


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

Add a test for ABC-specific getblocktemplate behavior
Add a test for ABC-specific getblocktemplate behavior

Summary:
First step in T1014

As we modify behavior in getblocktemplate, we really need better test coverage.
D7201 (f8360b5e981) is one such change. This patch tests that behavior specifically
but this test can be expanded to cover other changes that we may have made or will
make in the future.

Test Plan:
Before D7201, this test should fail
```
git checkout f8360b5e981^
git cherry-pick this-patch
test_runner.py abc_mining_basic
```
But the test passes today:
```
git checkout this-patch
test_runner.py abc_mining_basic
test_runner.py --with-axionactivation abc_mining_basic
```

Reviewers: #bitcoin_abc, Fabien, deadalnix

Reviewed By: #bitcoin_abc, Fabien, deadalnix

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D7230
