Skip to content

Commit

Permalink
Add assets and issuance functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenroose committed Mar 20, 2019
1 parent 96af43b commit a687dd9
Show file tree
Hide file tree
Showing 4 changed files with 669 additions and 0 deletions.
70 changes: 70 additions & 0 deletions test/functional/feature_assetsdir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env python3
# Copyright (c) 2014-2016 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

#
# Test use of assetdir to locally label assets.
# Test listissuances returns a list of all issuances or specific issuances based on asset hex or asset label.
#

from decimal import Decimal
from test_framework.test_framework import BitcoinTestFramework
from test_framework.authproxy import JSONRPCException
from test_framework.util import assert_equal, assert_greater_than
from test_framework.util import *

class AssetdirTests(BitcoinTestFramework):
"""
Test use of assetdir to specify asset labels.
Test listissuances returns a list of all issuances or specific issuances based on asset hex or asset label.
"""

def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 1
[["-initialfreecoins=2100000000000000", "-anyonecanspendaremine=1", "-con_connect_coinbase=1", "-con_blocksubsidy=0"]]

def setup_network(self, split=False):
self.setup_nodes()

def run_test(self):
self.nodes[0].generate(101)

#Issue two assets that we will later label using the assetdir parameter
issuance1 = self.nodes[0].issueasset(100, 1, False)
asset1hex = issuance1["asset"]

issuance2 = self.nodes[0].issueasset(100, 1, False)
asset2hex = issuance2["asset"]

#Stop and restart the nodes, providing the assetdir parameter to locally label the assets
self.stop_nodes()
self.start_nodes([["-assetdir=" + asset1hex + ":asset1", "-assetdir=" + asset2hex + ":asset2"]])

#Check that listissuances return all issuances
issuances = self.nodes[0].listissuances()
assert_equal(len(issuances), 2)

#Check all asset labels have been set: 'asset1', 'asset2'
#We can not be sure they will always be returned in the same order so will loop each one
label = ""
for issue in issuances:
label += issue["assetlabel"]

assert_greater_than(label.find("asset1"), -1)
assert_greater_than(label.find("asset2"), -1)

#Check we can get a list of isuances for a given label
issuances = self.nodes[0].listissuances("asset1")
assert_equal(len(issuances), 1)
assert_equal(issuances[0]["assetlabel"], "asset1")

#Check we can get a list of issuances for a given hex
issuances = self.nodes[0].listissuances(asset2hex)
assert_equal(len(issuances), 1)
assert_equal(issuances[0]["assetlabel"], "asset2")

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

70 changes: 70 additions & 0 deletions test/functional/feature_default_asset_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env python3
# Copyright (c) 2014-2016 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

#
# Test chain initialisation when specifying default asset name.
#

from decimal import Decimal
from test_framework.test_framework import BitcoinTestFramework
from test_framework.authproxy import JSONRPCException
from test_framework.util import assert_equal, connect_nodes_bi

class NamedDefaultAssetTest(BitcoinTestFramework):
"""
Test creation of default asset is named according to defaultpeggedasset parameter
"""

def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 2

#Set default asset name
self.extra_args = [["-defaultpeggedassetname=testasset", "-initialfreecoins=2100000000000000", "-anyonecanspendaremine=1", "-con_connect_coinbase=1", "-con_blocksubsidy=0"]]*2

def setup_network(self, split=False):
self.setup_nodes()
connect_nodes_bi(self.nodes, 0, 1)
self.is_network_split = False
self.sync_all()

def run_test(self):
#Claim all anyone-can-spend coins and test that calling sendtoaddress without providing the assetlabel parameter results in the specified default pegged asset being sent.
self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 21000000, "", "", True)
self.nodes[0].generate(101)
self.sync_all()

#Check the default asset is named correctly
walletinfo1 = self.nodes[0].getwalletinfo()
assert_equal(walletinfo1["balance"]["testasset"], 21000000)

#Send some of the default asset to the second node
self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 1, "", "", False)
self.nodes[0].generate(101)
self.sync_all()

#Check balances are correct and asset is named correctly
walletinfo1 = self.nodes[0].getwalletinfo()
assert_equal(walletinfo1["balance"]["testasset"], 20999999)

walletinfo2 = self.nodes[1].getwalletinfo()
assert_equal(walletinfo2["balance"]["testasset"], 1)

#Check we send the default 'testasset' when calling 'sendmany' without needing to provide the relevant asset label
outputs = {self.nodes[1].getnewaddress(): 1.0, self.nodes[1].getnewaddress(): 3.0}
self.nodes[0].sendmany("", outputs)
self.nodes[0].generate(101)
self.sync_all()

#Check balances are correct and asset is named correctly
walletinfo1 = self.nodes[0].getwalletinfo()
assert_equal(walletinfo1["balance"]["testasset"], 20999995)

walletinfo2 = self.nodes[1].getwalletinfo()
assert_equal(walletinfo2["balance"]["testasset"], 5)

if __name__ == '__main__':
NamedDefaultAssetTest().main()
87 changes: 87 additions & 0 deletions test/functional/feature_initial_reissuance_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env python3
# Copyright (c) 2014-2016 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

#
# Test chain initialisation when specifying number of reissuance tokens to issue.
#

from decimal import Decimal
from test_framework.test_framework import BitcoinTestFramework
from test_framework.authproxy import JSONRPCException
from test_framework.util import connect_nodes_bi, assert_equal
from test_framework.util import *

class InitialReissuanceTokenTest(BitcoinTestFramework):
"""
Test creation of initial reissuance token for default asset on chain set up
"""

def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 2

#Set number of initial reissuance tokens and also set initial free coins less than max so we can reissue more later
self.extra_args = [["-initialreissuancetokens=200000000", "-initialfreecoins=2000000000000000", "-anyonecanspendaremine=1", "-con_connect_coinbase=1", "-con_blocksubsidy=0", "-blindedaddresses=1"]]*2

def setup_network(self, split=False):
self.setup_nodes()
connect_nodes_bi(self.nodes, 0, 1)
self.is_network_split = False
self.sync_all()

def run_test(self):
self.nodes[0].generate(101)
self.sync_all()

walletinfo = self.nodes[0].getwalletinfo()
balance = walletinfo['balance']
token = ""

#Get the hex of the reissuance token
for i in balance:
token = i
if token != "bitcoin":
break

# Claim all anyone-can-spend reissuance tokens, which also blinds the token output
# which is required for re-issuance: https://github.com/ElementsProject/elements/issues/259
self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 2, "", "", False, False, 6, "UNSET", token)
self.nodes[0].generate(101)
self.sync_all()

#Check balances
walletinfo1 = self.nodes[0].getwalletinfo()
assert_equal(walletinfo1["balance"]["bitcoin"], 20000000)
assert_equal(walletinfo1["balance"][token], 2)

#Reissue some of the default asset
self.nodes[0].reissueasset("bitcoin", 1234)
self.nodes[0].generate(101)
self.sync_all()

#Check the reissuance worked
walletinfo1 = self.nodes[0].getwalletinfo()
assert_equal(walletinfo1["balance"]["bitcoin"], 20001234)

#Send some 'bitcoin' to node 2 so they can fund a reissuance transaction
self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 1, "", "", False)

#Send a reissuance token to node 2 so they can reissue the default asset
self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 1, "", "", False, False, 6, "UNSET", token)
self.nodes[0].generate(101)
self.sync_all()

#Reissue some of the default asset
self.nodes[1].reissueasset("bitcoin", 1000)
self.nodes[1].generate(101)
self.sync_all()

#Check balance is the 1 'bitcoin' sent from node 1 plus the 1000 'bitcoin' reissued by node 2
walletinfo2 = self.nodes[1].getwalletinfo()
assert_equal(walletinfo2["balance"]["bitcoin"], 1001)

if __name__ == '__main__':
InitialReissuanceTokenTest().main()
Loading

0 comments on commit a687dd9

Please sign in to comment.