Skip to content

Commit

Permalink
Add tokenization hard fork unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ShengguangXiao committed Sep 18, 2020
1 parent 2b4c86d commit 20ae168
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
144 changes: 144 additions & 0 deletions test/functional/feature_token_fork.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#!/usr/bin/env python3
# Copyright (c) 2014-2020 The Bitcoin Core developers
# Copyright (c) DeFi Blockchain Developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test token's RPC.
- verify before fork, not allow create token, after fork can create token
"""

from test_framework.test_framework import DefiTestFramework

from test_framework.authproxy import JSONRPCException
from test_framework.util import assert_equal

class TokensForkTest (DefiTestFramework):
def set_test_params(self):
self.num_nodes = 2
self.setup_clean_chain = True
self.extra_args = [['-txnotokens=0', '-amkheight=120'], ['-txnotokens=0', '-amkheight=120']]

def run_test(self):
assert_equal(len(self.nodes[0].listtokens()), 1) # only one token == DFI

self.nodes[0].generate(100)
self.sync_blocks()

self.nodes[0].generate(2) # for 2 matured utxos

# Try to create token before AMK fork height but will fail:
#========================
collateralGold = self.nodes[0].getnewaddress("", "legacy")
collateralSilver = self.nodes[0].getnewaddress("", "legacy")
try:
self.nodes[0].createtoken([], {
"symbol": "GOLD",
"name": "shiny gold",
"collateralAddress": collateralGold
})
self.nodes[0].createtoken([], {
"symbol": "SILVER",
"name": "just silver",
"collateralAddress": collateralSilver
})
except JSONRPCException as e:
errorString = e.error['message']
assert("No tokenization transaction before block height" in errorString)

self.nodes[0].generate(1)
# Before fork, create should fail, so now only have default token
tokens = self.nodes[0].listtokens()
assert_equal(len(tokens), 1)

# Try to destroy token before AMK fork height but will fail:
#========================
try:
self.nodes[0].destroytoken([], "GOLD")
except JSONRPCException as e:
errorString = e.error['message']
assert("No tokenization transaction before block height" in errorString)

# Try to mint token before AMK fork height but will fail:
#========================
try:
self.nodes[0].minttokens([], "300@GOLD")
except JSONRPCException as e:
errorString = e.error['message']
assert("No tokenization transaction before block height" in errorString)

self.nodes[0].generate(17)
self.sync_blocks()

# Now at AMK height 120
# Now create again, it should pass
self.nodes[0].createtoken([], {
"symbol": "GOLD",
"name": "shiny gold",
"collateralAddress": collateralGold
})
self.nodes[0].createtoken([], {
"symbol": "SILVER",
"name": "just silver",
"collateralAddress": collateralSilver
})

self.nodes[0].generate(1)
# After fork, create should pass, so now only have 3 kind of tokens
tokens = self.nodes[0].listtokens()
assert_equal(len(tokens), 3)

list_tokens = self.nodes[0].listtokens()
for idx, token in list_tokens.items():
if (token["symbol"] == "GOLD"):
idGold = idx
if (token["symbol"] == "SILVER"):
idSilver = idx

symbolGold = "GOLD#" + idGold
symbolSilver = "SILVER#" + idSilver

self.sync_blocks()

# MINT:
#========================
# Funding auth addresses

self.nodes[0].sendmany("", { collateralGold : 1, collateralSilver : 1 })
self.nodes[0].generate(1)
self.nodes[0].sendmany("", { collateralGold : 1, collateralSilver : 1 })
self.nodes[0].generate(1)

# print(self.nodes[0].listunspent())

alienMintAddr = self.nodes[1].getnewaddress("", "legacy")
self.nodes[0].minttokens([], "300@" + symbolGold)
self.nodes[0].minttokens([], "3000@" + symbolSilver)
self.nodes[0].generate(1)
self.sync_blocks()

self.nodes[0].accounttoutxos([], collateralGold, { self.nodes[0].getnewaddress("", "legacy"): "100@" + symbolGold, alienMintAddr: "200@" + symbolGold})
self.nodes[0].accounttoutxos([], collateralSilver, { self.nodes[0].getnewaddress("", "legacy"): "1000@" + symbolSilver, alienMintAddr: "2000@" + symbolSilver})
self.nodes[0].generate(1)
self.sync_blocks()

assert_equal(self.nodes[0].getbalances(True)['mine']['trusted'][str(idGold)], 100)
assert_equal(self.nodes[1].getbalances(True)['mine']['trusted'][str(idGold)], 200)
assert_equal(self.nodes[0].getbalances(True)['mine']['trusted'][str(idSilver)], 1000)
assert_equal(self.nodes[1].getbalances(True)['mine']['trusted'][str(idSilver)], 2000)

print ("Check 'sendmany' for tokens")
alienSendAddr = self.nodes[1].getnewaddress("", "legacy")
# check sending of different tokens on same address
self.nodes[0].sendmany("", { alienSendAddr : [ str(10) + "@" + symbolGold, str(20) + "@" + symbolSilver] })
self.nodes[0].generate(1)
self.sync_blocks()

assert_equal(self.nodes[0].getbalances(True)['mine']['trusted'][str(idGold)], 90)
assert_equal(self.nodes[0].getbalances(True)['mine']['trusted'][str(idSilver)], 980)
assert_equal(self.nodes[1].getbalances(True)['mine']['trusted'][str(idGold)], 210)
assert_equal(self.nodes[1].getbalances(True)['mine']['trusted'][str(idSilver)], 2020)


if __name__ == '__main__':
TokensForkTest ().main ()
1 change: 1 addition & 0 deletions test/functional/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
'feature_tokens_minting.py',
'feature_tokens_dat.py',
'feature_accounts_n_utxos.py',
'feature_token_fork.py',
'interface_rest.py',
'mempool_spend_coinbase.py',
'wallet_avoidreuse.py',
Expand Down

0 comments on commit 20ae168

Please sign in to comment.