def StartNodeAndImport(port, importport, title, dbproxyport, suffix="", host="localhost"): datadir = _lib.CreateTestFolder(suffix) # this will create config file to remember other node address configfile = "{\"Port\": " + str( port) + ",\"Nodes\":[{\"Host\": \"localhost\",\"Port\":" + str( importport) + "}]}" _lib.SaveConfigFile(datadir, configfile) address = initblockchain.ImportBockchain(datadir, "localhost", importport) _complex.AddMinterToConfig(datadir, address) if dbproxyport > 0: _complex.AddProxyToConfig(datadir, "localhost:" + str(dbproxyport)) _complex.AddInternalKeyToConfig(datadir, address) # init internal signing startnode.StartNode(datadir, address, port, title, host) #check nodes. must be minimum 1 and import port must be present nodes = managenodes.GetNodes(datadir) _lib.FatalAssert(len(nodes) > 0, "Should be minimum 1 nodes in output") return [datadir, address]
def test(testfilter): global datadir1 global datadir2 _lib.StartTestGroup("Import long blockchain") _lib.CleanTestFolders() _lib.StartTestGroup("Copy blockchain from dataset") datadir1 = _lib.CreateTestFolder() datadir2 = _lib.CreateTestFolder() _lib.CopyTestData(datadir1, "bigchain") balances = _transfers.GetGroupBalance(datadir1) address = balances.keys()[0] startnode.StartNode(datadir1, address, '30000', "Server 1") address2 = initblockchain.ImportBockchain(datadir2, "localhost", '30000') #managenodes.RemoveAllNodes(datadir1) startnode.StartNode(datadir2, address2, '30001', "Server 2") #managenodes.RemoveAllNodes(datadir2) #managenodes.AddNode(datadir1, "localhost",'30001') blocks1 = _blocks.GetBlocks(datadir1) state = _node.NodeState(datadir1) _node.WaitBlocksInState(datadir2, len(blocks1), 120) blocks2 = _blocks.WaitBlocks(datadir2, len(blocks1), 120) #print len(blocks1), len(blocks2) _lib.FatalAssert( len(blocks1) == len(blocks2), "Number of bocks must be same on both servers") startnode.StopNode(datadir1, "Server 1") startnode.StopNode(datadir2, "Server 2") datadir1 = "" datadir2 = "" _lib.EndTestGroupSuccess()
def StartNodeAndImport(port, importport, title, suffix=""): datadir = _lib.CreateTestFolder(suffix) address = initblockchain.ImportBockchain(datadir, "localhost", importport) # this will create config file to remember other node address configfile = "{\"MinterAddress\":\"" + address + "\",\"Port\": " + str( port) + ",\"Nodes\":[{\"Host\": \"localhost\",\"Port\":" + str( importport) + "}]}" _lib.SaveConfigFile(datadir, configfile) startnode.StartNode(datadir, address, port, title) #check nodes. must be minimum 1 and import port must be present nodes = managenodes.GetNodes(datadir) _lib.FatalAssert(len(nodes) > 0, "Should be minimum 1 nodes in output") return [datadir, address]
def test(testfilter): global datadir1 global datadir2 global datadir3 _lib.StartTestGroup("Manage nodes list") _lib.CleanTestFolders() datadir1 = _lib.CreateTestFolder('_1_') datadir2 = _lib.CreateTestFolder('_2_') _lib.StartTestGroup("Create blockchain and run node 1") r = blocksbasic.PrepareBlockchain(datadir1, '30000') address = r[0] startnode.StartNode(datadir1, address, '30000', "Server 1") address2 = initblockchain.ImportBockchain(datadir2, "localhost", '30000') #RemoveAllNodes(datadir1) nodes = GetNodes(datadir1) _lib.FatalAssert(len(nodes) == 0, "Should be 0 nodes in output") AddNode(datadir1, "localhost", '30001') nodes = GetNodes(datadir1) _lib.FatalAssert(len(nodes) == 1, "Should be 1 nodes in output") RemoveNode(datadir1, "localhost", '30001') nodes = GetNodes(datadir1) _lib.FatalAssert(len(nodes) == 0, "Should be 0 nodes in output") startnode.StartNode(datadir2, address2, '30001', "Server 2") #RemoveAllNodes(datadir2) #AddNode(datadir1, "localhost",'30001') nodes = GetNodes(datadir1) _lib.FatalAssert(len(nodes) == 1, "Should be 1 nodes in output") startnode.StopNode(datadir2, "Server 2") startnode.StartNode(datadir2, address2, '30001', "Server 2") nodes = GetNodes(datadir1) _lib.FatalAssert(len(nodes) == 1, "Should be 1 nodes in output") _lib.FatalAssert( nodes.keys()[0] == "localhost:30001" or nodes.keys()[0] == "127.0.0.1:30001", "Wrong node in the list") # check transactions work fine between nodes _transfers.Send(datadir1, address, address2, '3') txlist = transactions.GetUnapprovedTransactions(datadir1) _lib.FatalAssert(len(txlist) == 1, "Should be 1 unapproved transaction") blocks = _blocks.WaitBlocks(datadir1, 2) # send another 2 TX to make a block tx = _transfers.Send(datadir1, address, address2, '0.01') tx = _transfers.Send(datadir1, address, address2, '0.01') blocks = _blocks.WaitBlocks(datadir1, 3) time.sleep(2) tx = txid1 = _transfers.Send(datadir1, address, address2, '1') time.sleep(1) txlist = transactions.GetUnapprovedTransactions(datadir1) _lib.FatalAssert( len(txlist) == 1, "Should be 1 unapproved transaction. Got " + str(len(txlist))) time.sleep(3) # and now get transactions from second node txlist = _transfers.WaitUnapprovedTransactions(datadir2, 1) _lib.FatalAssert( len(txlist) == 1, "Should be 1 unapproved transaction on second node. Got " + str(len(txlist)) + " " + str(txlist)) #print txid1 #print txlist if txid1 not in txlist.keys(): _lib.Fatal( "Transaction 1 is not in the list of transactions on second node") # start one more node datadir3 = _lib.CreateTestFolder('_3_') address3 = initblockchain.ImportBockchain(datadir3, "localhost", '30000') startnode.StartNode(datadir3, address3, '30002', "Server 3") #RemoveAllNodes(datadir3) #AddNode(datadir3, "localhost",'30001') time.sleep(2) # wait while nodes exchange addresses nodes = GetNodes(datadir1) _lib.FatalAssert(len(nodes) == 2, "Should be 2 nodes in output of 1") nodes = GetNodes(datadir2) _lib.FatalAssert(len(nodes) == 2, "Should be 2 nodes in output of 2") nodes = GetNodes(datadir3) _lib.FatalAssert(len(nodes) == 2, "Should be 2 nodes in output of 3") txid1 = _transfers.Send(datadir1, address, address3, '4') # unapproved TX is pulled from node 1 on start txlist3 = transactions.GetUnapprovedTransactions(datadir3) _lib.FatalAssert( len(txlist3) == 1, "Should be 1 unapproved transactions on 3") time.sleep(3) # we need to give a chance to sync all txlist1 = transactions.GetUnapprovedTransactions(datadir1) txlist2 = transactions.GetUnapprovedTransactions(datadir2) _lib.FatalAssert( len(txlist1) == 2, "Should be 2 unapproved transactions on 1") _lib.FatalAssert( len(txlist2) == 2, "Should be 2 unapproved transactions on 2") if txid1 not in txlist1.keys(): _lib.Fatal( "Transaction 2 is not in the list of transactions on node 1") if txid1 not in txlist2.keys(): _lib.Fatal( "Transaction 2 is not in the list of transactions on node 2") # send one more TX. Block must be created txid3 = _transfers.Send(datadir1, address, address2, '1') blocks = _blocks.WaitBlocks(datadir3, 4) blocks = _blocks.WaitBlocks(datadir2, 4) blocks = _blocks.WaitBlocks(datadir1, 4) time.sleep(1) transactions.GetUnapprovedTransactionsEmpty(datadir1) transactions.GetUnapprovedTransactionsEmpty(datadir2) transactions.GetUnapprovedTransactionsEmpty(datadir3) # check if a block is present on all nodes. it must be 2 block on every node blockshashes = _blocks.GetBlocks(datadir1) _lib.FatalAssert( len(blockshashes) == 4, "Should be 4 blocks in blockchain on 1") blockshashes = _blocks.GetBlocks(datadir2) _lib.FatalAssert( len(blockshashes) == 4, "Should be 4 blocks in blockchain on 2") blockshashes = _blocks.GetBlocks(datadir3) _lib.FatalAssert( len(blockshashes) == 4, "Should be 4 blocks in blockchain on 3") startnode.StopNode(datadir1, "Server 1") startnode.StopNode(datadir2, "Server 2") startnode.StopNode(datadir3, "Server 3") #_lib.RemoveTestFolder(datadir1) #_lib.RemoveTestFolder(datadir2) datadir1 = "" datadir2 = "" datadir3 = "" _lib.EndTestGroupSuccess()
def test(testfilter): global datadir, datadir2 _lib.StartTestGroup("SQL basic") _lib.CleanTestFolders() datadir = _lib.CreateTestFolder('_1_') datadir2 = _lib.CreateTestFolder('_2_') startnode.StartNodeWithoutBlockchain(datadir) address = startnode.InitBockchain(datadir) startnode.StartNode(datadir, address, '30000') _lib.StartTestGroup("Do transactions") transactions.GetUnapprovedTransactionsEmpty(datadir) tx1 = _sql.ExecuteSQL( datadir, address, "CREATE TABLE test (a INT auto_increment PRIMARY KEY, b VARCHAR(20))") # check new table exists tables = _lib.DBGetRows(datadir, "SHOW TABLES") found = False for table in tables: if table[0] == "test": found = True break _lib.FatalAssert(found, "Table not found in the DB") blocks = _blocks.WaitBlocks(datadir, 2) tx2 = _sql.ExecuteSQL(datadir, address, "INSERT INTO test SET b='row1'") tx3 = _sql.ExecuteSQL(datadir, address, "INSERT INTO test SET a=2,b='row2'") time.sleep(1) tx4 = _sql.ExecuteSQL(datadir, address, "INSERT INTO test (b) VALUES ('row3')") rows = _lib.DBGetRows(datadir, "SELECT * FROM test") _lib.FatalAssert(len(rows) == 3, "Must be 3 rows in a table") blocks = _blocks.WaitBlocks(datadir, 3) time.sleep(1) # while all caches are cleaned txlist = transactions.GetUnapprovedTransactions(datadir) _lib.FatalAssert(len(txlist) == 1, "Should be 1 unapproved transaction") # update data _sql.ExecuteSQL(datadir, address, " update test SET b=\"row3 updated\" where a=3") _sql.ExecuteSQL(datadir, address, " update test SET b=\"row2 updated\" where a = '2'") blocks = _blocks.WaitBlocks(datadir, 4) time.sleep(1) # while all caches are cleaned rows = _lib.DBGetRows(datadir, "SELECT * FROM test") for row in rows: if row[0] == "1": _lib.FatalAssert(row[1] == "row1", "Row 1 value is wrong. Got: " + row[1]) if row[0] == "2": _lib.FatalAssert(row[1] == "row2 updated", "Row 2 value is wrong. Got: " + row[1]) if row[0] == "3": _lib.FatalAssert(row[1] == "row3 updated", "Row 3 value is wrong. Got: " + row[1]) error = _sql.ExecuteSQLFailure(datadir, address, "INSERT INTO test SET a=2,b='row2'") txid = _sql.ExecuteSQL(datadir, address, " DELETE from test where a=3") rows = _lib.DBGetRows(datadir, "SELECT * FROM test") _lib.FatalAssert(len(rows) == 2, "Must be 2 rows in a table") address2 = initblockchain.ImportBockchain(datadir2, "localhost", '30000') startnode.StartNode(datadir2, address2, '30001', "Server 2") blocks = _blocks.WaitBlocks(datadir2, 4) # must be 3 rows because delete transaction was not posted to that node rows = _lib.DBGetRows(datadir2, "SELECT * FROM test") _lib.FatalAssert(len(rows) == 3, "Must be 3 rows in a table") txid = _sql.ExecuteSQL(datadir, address, " DELETE from test where a=2") # should be 1 row on first node rows = _lib.DBGetRows(datadir, "SELECT * FROM test") _lib.FatalAssert(len(rows) == 1, "Must be 1 rows in a table") time.sleep(1) # give time to send transaction # and 2 row on second rows = _lib.DBGetRows(datadir2, "SELECT * FROM test") _lib.FatalAssert(len(rows) == 2, "Must be 2 rows in a table") startnode.StopNode(datadir) datadir = "" startnode.StopNode(datadir2) datadir2 = "" #_lib.RemoveTestFolder(datadir) _lib.EndTestGroupSuccess()
def test(testfilter): global datadir, datadir2 _lib.StartTestGroup("SQL Sync with Proxy. care offline") _lib.CleanTestFolders() datadir = _lib.CreateTestFolder('_1_') startnode.StartNodeWithoutBlockchain(datadir) address = startnode.InitBockchain(datadir) _complex.AddProxyToConfig(datadir, "localhost:40041") _complex.AddInternalKeyToConfig(datadir, address) # init internal signing startnode.StartNode(datadir, address, '30000') _lib.StartTestGroup("Do initial transactions") transactions.GetUnapprovedTransactionsEmpty(datadir) _sql.ExecuteSQLOnProxy( datadir, "CREATE TABLE test (a INT auto_increment PRIMARY KEY, b VARCHAR(20))") # check new table exists tables = _lib.DBGetRows(datadir, "SHOW TABLES", True) found = False for table in tables: if table[0] == "test": found = True break _lib.FatalAssert(found, "Table not found in the DB") blocks = _blocks.WaitBlocks(datadir, 2) time.sleep(1) _sql.ExecuteSQLOnProxy(datadir, "INSERT INTO test SET b='row1'") datadir2 = _lib.CreateTestFolder('_2_') address2 = initblockchain.ImportBockchain(datadir2, "localhost", '30000') _complex.AddProxyToConfig(datadir2, "localhost:40042") _complex.AddInternalKeyToConfig(datadir2, address2) # init internal signing startnode.StartNode(datadir2, address2, '30001', "Server 2") blocks = _blocks.WaitBlocks(datadir2, 2) _sql.ExecuteSQLOnProxy(datadir2, "INSERT INTO test SET b='row1.2'") # must be 1 row , because the first row from the first node is not yet in a block and is not synced rows = _lib.DBGetRows(datadir2, "SELECT * FROM test", True) _lib.FatalAssert(len(rows) == 1, "Must be 1 row in a table on node 2") _sql.ExecuteSQLOnProxy(datadir, "INSERT INTO test SET b='row2'") # should be 1 row on first node rows = _lib.DBGetRows(datadir, "SELECT * FROM test", True) _lib.FatalAssert(len(rows) == 2, "Must be 2 rows in a table on node 1") # wait 3-rd block blocks = _blocks.WaitBlocks(datadir, 3) time.sleep(1) # give time to send transaction # wait 3-rd block on a second node blocks = _blocks.WaitBlocks(datadir2, 3) time.sleep(1) # and 2 row on second rows2 = _lib.DBGetRows(datadir2, "SELECT * FROM test ORDER BY a", True) _lib.FatalAssert(len(rows2) == 2, "Must be 2 rows in a table") rows = _lib.DBGetRows(datadir, "SELECT * FROM test ORDER BY a", True) _lib.FatalAssert(len(rows) == 2, "Must be 2 rows in a table") _lib.FatalAssert( set(rows) == set(rows2), "COntents of tables on both nodes must be same") _lib.StartTestGroup("Check cancel of following transactions") # temporary stop second node startnode.StopNode(datadir2) _sql.ExecuteSQLOnProxy(datadir, "INSERT INTO test SET b='row3'") startnode.StartNode(datadir2, address2, '30001', "Server 2") _sql.ExecuteSQLOnProxy(datadir2, "INSERT INTO test SET b='row3.2'") _sql.ExecuteSQLOnProxy(datadir, "UPDATE test SET b='row3_updated' WHERE a=3") _sql.ExecuteSQLOnProxy(datadir2, "UPDATE test SET b='row3_updated_2' WHERE a=3") _sql.ExecuteSQLOnProxy(datadir, "UPDATE test SET b='row3_updated_again' WHERE a=3") blocks = _blocks.WaitBlocks(datadir, 4) time.sleep(1) # give time to send transaction blocks = _blocks.WaitBlocks(datadir2, 4) time.sleep(1) # give time to send transaction rows = _lib.DBGetRows(datadir, "SELECT * FROM test ORDER BY a", True) _lib.FatalAssert(len(rows) == 3, "Must be 3 rows in a table") rows2 = _lib.DBGetRows(datadir2, "SELECT * FROM test ORDER BY a", True) _lib.FatalAssert(len(rows2) == 3, "Must be 3 rows in a table") _lib.FatalAssert( set(rows) == set(rows2), "COntents of tables on both nodes must be same") startnode.StopNode(datadir) datadir = "" startnode.StopNode(datadir2) datadir2 = "" #_lib.RemoveTestFolder(datadir) _lib.EndTestGroupSuccess()
def test(testfilter): global datadir, datadir2 _lib.StartTestGroup("SQL Sync with Proxy") _lib.CleanTestFolders() datadir = _lib.CreateTestFolder('_1_') startnode.StartNodeWithoutBlockchain(datadir) address = startnode.InitBockchain(datadir) _complex.AddProxyToConfig(datadir, "localhost:40041") _complex.AddInternalKeyToConfig(datadir, address) # init internal signing startnode.StartNode(datadir, address, '30000') _lib.StartTestGroup("Do transactions") transactions.GetUnapprovedTransactionsEmpty(datadir) _sql.ExecuteSQLOnProxy( datadir, "CREATE TABLE test (a INT auto_increment PRIMARY KEY, b VARCHAR(20))") # check new table exists tables = _lib.DBGetRows(datadir, "SHOW TABLES", True) found = False for table in tables: if table[0] == "test": found = True break _lib.FatalAssert(found, "Table not found in the DB") blocks = _blocks.WaitBlocks(datadir, 2) time.sleep(2) _sql.ExecuteSQLOnProxy(datadir, "INSERT INTO test SET b='row1'") _sql.ExecuteSQLOnProxy(datadir, "INSERT INTO test SET a=2,b='row2'") time.sleep(1) _sql.ExecuteSQLOnProxy(datadir, "INSERT INTO test (b) VALUES ('row3')") rows = _lib.DBGetRows(datadir, "SELECT * FROM test", True) _lib.FatalAssert(len(rows) == 3, "Must be 3 rows in a table") blocks = _blocks.WaitBlocks(datadir, 3) time.sleep(2) # while all caches are cleaned txlist = transactions.GetUnapprovedTransactions(datadir) _lib.FatalAssert(len(txlist) == 1, "Should be 1 unapproved transaction") # update data _sql.ExecuteSQLOnProxy(datadir, " update test SET b=\"row3 updated\" where a=3") _sql.ExecuteSQLOnProxy( datadir, " update test SET b=\"row2 updated\" where a = '2'") blocks = _blocks.WaitBlocks(datadir, 4) time.sleep(1) # while all caches are cleaned rows = _lib.DBGetRows(datadir, "SELECT * FROM test", True) for row in rows: if row[0] == "1": _lib.FatalAssert(row[1] == "row1", "Row 1 value is wrong. Got: " + row[1]) if row[0] == "2": _lib.FatalAssert(row[1] == "row2 updated", "Row 2 value is wrong. Got: " + row[1]) if row[0] == "3": _lib.FatalAssert(row[1] == "row3 updated", "Row 3 value is wrong. Got: " + row[1]) _sql.ExecuteSQLOnProxyFail(datadir, "INSERT INTO test SET a=2,b='row2'") _sql.ExecuteSQLOnProxy(datadir, " DELETE from test where a=3") rows = _lib.DBGetRows(datadir, "SELECT * FROM test", True) _lib.FatalAssert(len(rows) == 2, "Must be 2 rows in a table") datadir2 = _lib.CreateTestFolder('_2_') address2 = initblockchain.ImportBockchain(datadir2, "localhost", '30000') _complex.AddProxyToConfig(datadir2, "localhost:40042") _complex.AddInternalKeyToConfig(datadir2, address2) # init internal signing startnode.StartNode(datadir2, address2, '30001', "Server 2") blocks = _blocks.WaitBlocks(datadir2, 4) # Send money to new node address txid1 = _transfers.Send(datadir, address, address2, 1) time.sleep(2) # must be 2 delete transaction should be imported rows = _lib.DBGetRows(datadir2, "SELECT * FROM test", True) _lib.FatalAssert(len(rows) == 2, "Must be 2 rows in a table") _sql.ExecuteSQLOnProxy(datadir, " DELETE from test where a=2") # send money again txid2 = _transfers.Send(datadir, address, address2, 1) # should be 1 row on first node rows = _lib.DBGetRows(datadir, "SELECT * FROM test", True) _lib.FatalAssert(len(rows) == 1, "Must be 1 rows in a table") time.sleep(1) # give time to send transaction # and 2 row on second rows = _lib.DBGetRows(datadir2, "SELECT * FROM test", True) _lib.FatalAssert(len(rows) == 1, "Must be 1 rows in a table") blocks = _blocks.WaitBlocks(datadir, 5) blocks = _blocks.WaitBlocks(datadir2, 5) time.sleep(2) # send money back txid3 = _transfers.Send(datadir2, address2, address, 2) time.sleep(1) # insert on second node. check on first _sql.ExecuteSQLOnProxy(datadir2, "INSERT INTO test SET a=2,b='row2'") time.sleep(3) # give time to send transaction rows = _lib.DBGetRows(datadir, "SELECT * FROM test", True) _lib.FatalAssert(len(rows) == 2, "Must be 2 rows in a table") # check balances bal1 = _transfers.GetBalance(datadir, address) bal2 = _transfers.GetBalance(datadir2, address2) _lib.FatalAssert(bal2[2] == -2, "Pending balance should be -2 for second address") _lib.FatalAssert(bal1[2] == 2, "Pending balance should be 2 for first address") startnode.StopNode(datadir) datadir = "" startnode.StopNode(datadir2) datadir2 = "" #_lib.RemoveTestFolder(datadir) _lib.EndTestGroupSuccess()