Skip to content

Commit

Permalink
Add merge tests and enhance tests framework with helper functions (#1442
Browse files Browse the repository at this point in the history
)

* Add rollback_to function to test framework
  • Loading branch information
dcorral authored Sep 9, 2022
1 parent 21bfbff commit fd0312b
Show file tree
Hide file tree
Showing 11 changed files with 846 additions and 75 deletions.
20 changes: 4 additions & 16 deletions test/functional/feature_poolswap.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from test_framework.authproxy import JSONRPCException
from test_framework.util import (
assert_equal,
connect_nodes_bi,
disconnect_nodes,
assert_raises_rpc_error,
)
Expand All @@ -23,24 +22,17 @@

class PoolPairTest (DefiTestFramework):
def set_test_params(self):
self.num_nodes = 4
self.num_nodes = 3
# node0: main (Foundation)
# node3: revert create (all)
# node2: Non Foundation
self.setup_clean_chain = True
self.extra_args = [
['-txnotokens=0', '-amkheight=50', '-bayfrontheight=50', '-bayfrontgardensheight=0', '-dakotaheight=160', '-fortcanningheight=163', '-fortcanninghillheight=170', '-fortcanningroadheight=177', '-acindex=1', '-dexstats'],
['-txnotokens=0', '-amkheight=50', '-bayfrontheight=50', '-bayfrontgardensheight=0', '-dakotaheight=160', '-fortcanningheight=163', '-fortcanninghillheight=170', '-fortcanningroadheight=177', '-acindex=1', '-dexstats'],
['-txnotokens=0', '-amkheight=50', '-bayfrontheight=50', '-bayfrontgardensheight=0', '-dakotaheight=160', '-fortcanningheight=163', '-fortcanninghillheight=170', '-fortcanningroadheight=177', '-dexstats'],
['-txnotokens=0', '-amkheight=50', '-bayfrontheight=50', '-bayfrontgardensheight=0', '-dakotaheight=160', '-fortcanningheight=163', '-fortcanninghillheight=170', '-fortcanningroadheight=177', '-dexstats']]

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

self.setup_tokens()
# Stop node #3 for future revert
self.stop_node(3)

self.symbolGOLD = "GOLD#" + self.get_id_token("GOLD")
self.symbolSILVER = "SILVER#" + self.get_id_token("SILVER")
self.idGold = list(self.nodes[0].gettoken(self.symbolGOLD).keys())[0]
Expand Down Expand Up @@ -537,14 +529,10 @@ def update_comission_and_fee_to_1pct_pool2(self):
assert_equal(attributes['v0/live/economy/dex/%s/fee_burn_a'%(self.idBL)], Decimal(str(round(dexoutfee, 8))))

def revert_to_initial_state(self):
self.start_node(3)
self.nodes[3].generate(30)

connect_nodes_bi(self.nodes, 0, 3)
connect_nodes_bi(self.nodes, 1, 3)
connect_nodes_bi(self.nodes, 2, 3)
self.sync_blocks()
self.rollback_to(block=0, nodes=[0, 1, 2])
assert_equal(len(self.nodes[0].listpoolpairs()), 0)
assert_equal(len(self.nodes[1].listpoolpairs()), 0)
assert_equal(len(self.nodes[2].listpoolpairs()), 0)


def run_test(self):
Expand Down
11 changes: 2 additions & 9 deletions test/functional/feature_restore_utxo.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ def set_test_params(self):
self.extra_args = [['-txnotokens=0', '-amkheight=1', '-bayfrontheight=1'],
['-txnotokens=0', '-amkheight=1', '-bayfrontheight=1']]

def rollback(self, count):
block = self.nodes[1].getblockhash(count)
self.nodes[1].invalidateblock(block)
self.nodes[1].clearmempool()
assert_equal(len(self.nodes[1].getrawmempool()), 0)
assert_equal(self.nodes[1].getblockcount(), count - 1)

def run_test(self):
self.nodes[0].generate(101)
self.sync_blocks()
Expand Down Expand Up @@ -69,7 +62,7 @@ def run_test(self):

# Set up for rollback tests
disconnect_nodes(self.nodes[0], 1)
block = self.nodes[1].getblockcount() + 1
block = self.nodes[1].getblockcount()
node1_utxos = len(self.nodes[1].listunspent())

# Test rollbacks
Expand All @@ -79,7 +72,7 @@ def run_test(self):
self.nodes[1].generate(1)
self.nodes[1].accounttoaccount(node1_source, {node1_source: "1@BTC"})
self.nodes[1].generate(1)
self.rollback(block)
self.rollback_to(block, nodes=[1])
assert_equal(len(self.nodes[1].listunspent()), node1_utxos)

if __name__ == '__main__':
Expand Down
25 changes: 10 additions & 15 deletions test/functional/feature_smart_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ def set_test_params(self):
self.setup_clean_chain = True
self.extra_args = [['-txnotokens=0', '-amkheight=1', '-bayfrontheight=1', '-eunosheight=1', '-fortcanningheight=1', '-fortcanninghillheight=1010', '-subsidytest=1', '-txindex=1', '-jellyfish_regtest=1']]

def rollback(self, count):
block = self.nodes[0].getblockhash(count)
self.nodes[0].invalidateblock(block)
self.nodes[0].clearmempool()

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

Expand Down Expand Up @@ -147,7 +142,7 @@ def run_test(self):
assert_equal(balance + staker_reward + community_reward - Decimal('18336.225') + fee, self.nodes[0].getbalance())

# Test swap for more than in community fund by 1 Sat
block = self.nodes[0].getblockcount() + 1
block = self.nodes[0].getblockcount()
self.nodes[0].setgov({"ATTRIBUTES":{'v0/params/dfip2201/premium':'0.00000000'}})
self.nodes[0].generate(1)
assert_raises_rpc_error(-32600, 'amount 18336.22500000 is less than 18336.22500001', self.nodes[0].executesmartcontract, dfip, '18336.22500001@2', address)
Expand All @@ -159,66 +154,66 @@ def run_test(self):
assert('0' not in self.nodes[0].listsmartcontracts())

# Set "real world" prices
self.rollback(block)
self.rollback_to(block)
prices = [{'currency': 'USD', 'tokenAmount': '2@DFI'},
{'currency': 'USD', 'tokenAmount': '40000@BTC'}]
self.nodes[0].setoracledata(oracle, int(time.time()), prices)
self.nodes[0].generate(10)

# Test default 2.5% premium
block = self.nodes[0].getblockcount() + 1
block = self.nodes[0].getblockcount()
self.nodes[0].executesmartcontract(dfip, '0.09999999@2', address)
self.nodes[0].generate(1)
assert_equal(2049.999795, float(self.nodes[0].getaccount(address)[0].split('@')[0]))

# Test 5% premium
self.rollback(block)
self.rollback_to(block)
self.nodes[0].setgov({"ATTRIBUTES":{'v0/params/dfip2201/premium':'0.05'}})
self.nodes[0].generate(1)
self.nodes[0].executesmartcontract(dfip, '0.09999999@2', address)
self.nodes[0].generate(1)
assert_equal(2099.99979, float(self.nodes[0].getaccount(address)[0].split('@')[0]))

# Test 0.1% premium
self.rollback(block)
self.rollback_to(block)
self.nodes[0].setgov({"ATTRIBUTES":{'v0/params/dfip2201/premium':'0.001'}})
self.nodes[0].generate(1)
self.nodes[0].executesmartcontract(dfip, '0.09999999@2', address)
self.nodes[0].generate(1)
assert_equal(2001.9997998, float(self.nodes[0].getaccount(address)[0].split('@')[0]))

# Test 0.000001% premium
self.rollback(block)
self.rollback_to(block)
self.nodes[0].setgov({"ATTRIBUTES":{'v0/params/dfip2201/premium':'0.00000001'}})
self.nodes[0].generate(1)
self.nodes[0].executesmartcontract(dfip, '0.1@2', address)
self.nodes[0].generate(1)
assert_equal(2000.00002, float(self.nodes[0].getaccount(address)[0].split('@')[0]))

# Test 0% premium
self.rollback(block)
self.rollback_to(block)
self.nodes[0].setgov({"ATTRIBUTES":{'v0/params/dfip2201/premium':'0.00000000'}})
self.nodes[0].generate(1)
self.nodes[0].executesmartcontract(dfip, '0.1@2', address)
self.nodes[0].generate(1)
assert_equal(2000, float(self.nodes[0].getaccount(address)[0].split('@')[0]))

# Swap min amount
self.rollback(block)
self.rollback_to(block)
self.nodes[0].executesmartcontract(dfip, '0.00001@2', address)
self.nodes[0].generate(1)
assert_equal(0.205, float(self.nodes[0].getaccount(address)[0].split('@')[0]))

# Test smallest min amount
self.rollback(block)
self.rollback_to(block)
self.nodes[0].setgov({"ATTRIBUTES":{'v0/params/dfip2201/minswap':'0.00000001'}})
self.nodes[0].generate(1)
self.nodes[0].executesmartcontract(dfip, '0.00000001@2', address)
self.nodes[0].generate(1)
assert_equal(0.000205, float(self.nodes[0].getaccount(address)[0].split('@')[0]))

# Test no smallest min amount
self.rollback(block)
self.rollback_to(block)
self.nodes[0].setgov({"ATTRIBUTES":{'v0/params/dfip2201/minswap':'0.00000001'}})
self.nodes[0].generate(1)
self.nodes[0].executesmartcontract(dfip, '0.00000001@2', address)
Expand Down
Loading

0 comments on commit fd0312b

Please sign in to comment.