diff --git a/sql/openmonero.sql b/sql/openmonero.sql index 4abb853..65f8b3d 100755 --- a/sql/openmonero.sql +++ b/sql/openmonero.sql @@ -1,13 +1,16 @@ -- phpMyAdmin SQL Dump --- version 4.6.5.2 +-- version 4.8.2 -- https://www.phpmyadmin.net/ -- -- Host: localhost --- Generation Time: Mar 12, 2017 at 08:38 AM --- Server version: 10.1.21-MariaDB --- PHP Version: 7.1.2 +-- Generation Time: Jun 29, 2018 at 03:46 AM +-- Server version: 10.1.34-MariaDB +-- PHP Version: 7.2.7 +SET FOREIGN_KEY_CHECKS=0; SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; +SET AUTOCOMMIT = 0; +START TRANSACTION; SET time_zone = "+00:00"; @@ -17,10 +20,8 @@ SET time_zone = "+00:00"; /*!40101 SET NAMES utf8mb4 */; -- --- Database: `openmonero` +-- Database: `openmonero_test` -- -CREATE DATABASE IF NOT EXISTS `openmonero` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; -USE `openmonero`; -- -------------------------------------------------------- @@ -29,15 +30,17 @@ USE `openmonero`; -- DROP TABLE IF EXISTS `Accounts`; -CREATE TABLE `Accounts` ( - `id` bigint(20) UNSIGNED NOT NULL, +CREATE TABLE IF NOT EXISTS `Accounts` ( + `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, `address` varchar(95) NOT NULL, `viewkey_hash` char(64) NOT NULL, `scanned_block_height` int(10) UNSIGNED NOT NULL DEFAULT '0', - `scanned_block_timestamp` timestamp NOT NULL DEFAULT 0, + `scanned_block_timestamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', `start_height` int(10) UNSIGNED NOT NULL DEFAULT '0', `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP + `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `address` (`address`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -------------------------------------------------------- @@ -47,14 +50,18 @@ CREATE TABLE `Accounts` ( -- DROP TABLE IF EXISTS `Inputs`; -CREATE TABLE `Inputs` ( - `id` bigint(20) UNSIGNED NOT NULL, +CREATE TABLE IF NOT EXISTS `Inputs` ( + `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, `account_id` bigint(20) UNSIGNED NOT NULL, `tx_id` bigint(20) UNSIGNED NOT NULL, `output_id` bigint(20) UNSIGNED NOT NULL, `key_image` varchar(64) NOT NULL DEFAULT '', `amount` bigint(20) UNSIGNED ZEROFILL NOT NULL DEFAULT '00000000000000000000', - `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP + `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + KEY `account_id2` (`account_id`), + KEY `tx_id2` (`tx_id`), + KEY `output_id2` (`output_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -------------------------------------------------------- @@ -64,8 +71,8 @@ CREATE TABLE `Inputs` ( -- DROP TABLE IF EXISTS `Outputs`; -CREATE TABLE `Outputs` ( - `id` bigint(20) UNSIGNED NOT NULL, +CREATE TABLE IF NOT EXISTS `Outputs` ( + `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, `account_id` bigint(20) UNSIGNED NOT NULL, `tx_id` bigint(20) UNSIGNED NOT NULL, `out_pub_key` varchar(64) NOT NULL, @@ -77,7 +84,11 @@ CREATE TABLE `Outputs` ( `global_index` bigint(20) UNSIGNED NOT NULL, `out_index` bigint(20) UNSIGNED NOT NULL DEFAULT '0', `mixin` bigint(20) UNSIGNED NOT NULL DEFAULT '0', - `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP + `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `out_pub_key` (`out_pub_key`), + KEY `tx_id` (`tx_id`), + KEY `account_id` (`account_id`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -------------------------------------------------------- @@ -87,8 +98,8 @@ CREATE TABLE `Outputs` ( -- DROP TABLE IF EXISTS `Payments`; -CREATE TABLE `Payments` ( - `id` bigint(20) UNSIGNED NOT NULL, +CREATE TABLE IF NOT EXISTS `Payments` ( + `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, `address` varchar(95) NOT NULL, `payment_id` varchar(64) NOT NULL, `tx_hash` varchar(64) NOT NULL DEFAULT '', @@ -96,7 +107,9 @@ CREATE TABLE `Payments` ( `payment_address` varchar(95) NOT NULL, `import_fee` bigint(20) NOT NULL, `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP + `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `payment_id` (`payment_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -------------------------------------------------------- @@ -106,8 +119,8 @@ CREATE TABLE `Payments` ( -- DROP TABLE IF EXISTS `Transactions`; -CREATE TABLE `Transactions` ( - `id` bigint(20) UNSIGNED NOT NULL, +CREATE TABLE IF NOT EXISTS `Transactions` ( + `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, `hash` varchar(64) NOT NULL, `prefix_hash` varchar(64) NOT NULL DEFAULT '', `tx_pub_key` varchar(64) NOT NULL DEFAULT '', @@ -123,82 +136,12 @@ CREATE TABLE `Transactions` ( `rct_type` int(4) NOT NULL DEFAULT '-1', `payment_id` varchar(64) NOT NULL DEFAULT '', `mixin` bigint(20) UNSIGNED NOT NULL DEFAULT '0', - `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP + `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `hash` (`hash`,`account_id`), + KEY `account_id_2` (`account_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; --- --- Indexes for dumped tables --- - --- --- Indexes for table `Accounts` --- -ALTER TABLE `Accounts` - ADD PRIMARY KEY (`id`), - ADD UNIQUE KEY `address` (`address`); - --- --- Indexes for table `Inputs` --- -ALTER TABLE `Inputs` - ADD PRIMARY KEY (`id`), - ADD KEY `account_id2` (`account_id`), - ADD KEY `tx_id2` (`tx_id`), - ADD KEY `output_id2` (`output_id`); - --- --- Indexes for table `Outputs` --- -ALTER TABLE `Outputs` - ADD PRIMARY KEY (`id`), - ADD UNIQUE KEY `account_id_2` (`account_id`,`out_pub_key`), - ADD KEY `account_id` (`account_id`), - ADD KEY `tx_id` (`tx_id`); - --- --- Indexes for table `Payments` --- -ALTER TABLE `Payments` - ADD PRIMARY KEY (`id`), - ADD UNIQUE KEY `payment_id` (`payment_id`); - --- --- Indexes for table `Transactions` --- -ALTER TABLE `Transactions` - ADD PRIMARY KEY (`id`), - ADD UNIQUE KEY `hash` (`hash`,`account_id`), - ADD KEY `account_id_2` (`account_id`); - --- --- AUTO_INCREMENT for dumped tables --- - --- --- AUTO_INCREMENT for table `Accounts` --- -ALTER TABLE `Accounts` - MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=55; --- --- AUTO_INCREMENT for table `Inputs` --- -ALTER TABLE `Inputs` - MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1738; --- --- AUTO_INCREMENT for table `Outputs` --- -ALTER TABLE `Outputs` - MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4373; --- --- AUTO_INCREMENT for table `Payments` --- -ALTER TABLE `Payments` - MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=11; --- --- AUTO_INCREMENT for table `Transactions` --- -ALTER TABLE `Transactions` - MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3702; -- -- Constraints for dumped tables -- @@ -223,6 +166,8 @@ ALTER TABLE `Outputs` -- ALTER TABLE `Transactions` ADD CONSTRAINT `account_id_FK` FOREIGN KEY (`account_id`) REFERENCES `Accounts` (`id`) ON DELETE CASCADE; +SET FOREIGN_KEY_CHECKS=1; +COMMIT; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; diff --git a/sql/openmonero_test.sql b/sql/openmonero_test.sql index 6730520..789645d 100644 --- a/sql/openmonero_test.sql +++ b/sql/openmonero_test.sql @@ -3,7 +3,7 @@ -- https://www.phpmyadmin.net/ -- -- Host: localhost --- Generation Time: Jun 26, 2018 at 04:26 AM +-- Generation Time: Jun 29, 2018 at 03:45 AM -- Server version: 10.1.34-MariaDB -- PHP Version: 7.2.7 @@ -20,7 +20,7 @@ SET time_zone = "+00:00"; /*!40101 SET NAMES utf8mb4 */; -- --- Database: `openmonero` +-- Database: `openmonero_test` -- -- -------------------------------------------------------- @@ -43,6 +43,11 @@ CREATE TABLE IF NOT EXISTS `Accounts` ( UNIQUE KEY `address` (`address`) ) ENGINE=InnoDB AUTO_INCREMENT=135 DEFAULT CHARSET=utf8; +-- +-- Truncate table before insert `Accounts` +-- + +TRUNCATE TABLE `Accounts`; -- -- Dumping data for table `Accounts` -- @@ -76,6 +81,11 @@ CREATE TABLE IF NOT EXISTS `Inputs` ( KEY `output_id2` (`output_id`) ) ENGINE=InnoDB AUTO_INCREMENT=60809 DEFAULT CHARSET=utf8; +-- +-- Truncate table before insert `Inputs` +-- + +TRUNCATE TABLE `Inputs`; -- -- Dumping data for table `Inputs` -- @@ -175,10 +185,16 @@ CREATE TABLE IF NOT EXISTS `Outputs` ( `mixin` bigint(20) UNSIGNED NOT NULL DEFAULT '0', `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), - KEY `account_id` (`account_id`), - KEY `tx_id` (`tx_id`) + UNIQUE KEY `out_pub_key` (`out_pub_key`), + KEY `tx_id` (`tx_id`), + KEY `account_id` (`account_id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=140460833003782 DEFAULT CHARSET=utf8; +-- +-- Truncate table before insert `Outputs` +-- + +TRUNCATE TABLE `Outputs`; -- -- Dumping data for table `Outputs` -- @@ -261,6 +277,11 @@ CREATE TABLE IF NOT EXISTS `Payments` ( UNIQUE KEY `payment_id` (`payment_id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; +-- +-- Truncate table before insert `Payments` +-- + +TRUNCATE TABLE `Payments`; -- -- Dumping data for table `Payments` -- @@ -298,6 +319,11 @@ CREATE TABLE IF NOT EXISTS `Transactions` ( KEY `account_id_2` (`account_id`) ) ENGINE=InnoDB AUTO_INCREMENT=106092 DEFAULT CHARSET=utf8; +-- +-- Truncate table before insert `Transactions` +-- + +TRUNCATE TABLE `Transactions`; -- -- Dumping data for table `Transactions` -- diff --git a/src/MySqlAccounts.cpp b/src/MySqlAccounts.cpp index 577b8a4..17500cd 100755 --- a/src/MySqlAccounts.cpp +++ b/src/MySqlAccounts.cpp @@ -298,13 +298,26 @@ MysqlOutpus::insert(const XmrOutput& out_data) Query query = conn->query(XmrOutput::INSERT_STMT); query.parse(); - // cout << query << endl; + //Query query = conn->query(); try { - query.insert(out_data); - - SimpleResult sr = query.execute(); +// query.insert(out_data); +// +// SimpleResult sr = query.execute(); + + SimpleResult sr = query.execute(out_data.account_id, + out_data.tx_id, + out_data.out_pub_key, + out_data.tx_pub_key, + out_data.rct_outpk, + out_data.rct_mask, + out_data.rct_amount, + out_data.amount, + out_data.global_index, + out_data.out_index, + out_data.mixin, + out_data.timestamp); if (sr.rows() == 1) return sr.insert_id(); diff --git a/tests/mysql_tests.cpp b/tests/mysql_tests.cpp index 830a576..b5b4ecc 100644 --- a/tests/mysql_tests.cpp +++ b/tests/mysql_tests.cpp @@ -425,6 +425,79 @@ INSTANTIATE_TEST_CASE_P( )); +xmreg::XmrOutput +make_mock_output_data() +{ + xmreg::XmrOutput mock_output_data ; + + // mock_output_data.account_id = acc.id; need to be set when used + mock_output_data.tx_id = 106086; // some tx id for this output + mock_output_data.out_pub_key = "18c6a80311d6f455ac1e5abdce7e86828d1ecf911f78da12a56ce8fdd5c716f4"; + mock_output_data.tx_pub_key = "38ae1d790bce890c3750b20ba8d35b8edee439fc8fb4218d50cec39a0cb7844a"; + mock_output_data.amount = 999916984840000ull; + mock_output_data.out_index = 1; + mock_output_data.rct_outpk = "e17cdc23fac1d92f2de196b567c8dd55ecd4cac52d6fef4eb446b6de4edb1d01"; + mock_output_data.rct_mask = "03cea1ffc18193639f7432287432c058a70551ceebed0db2c9d18088b423a255"; + mock_output_data.rct_amount = "f02e6d9dd504e6b428170d37b79344cad5538a4ad32f3f7dcebd5b96ac522e07"; + mock_output_data.global_index = 64916; + mock_output_data.mixin = 7; + mock_output_data.timestamp = mysqlpp::DateTime(static_cast(44434554));; + + return mock_output_data; +} + + +TEST_F(MYSQL_TEST, InsertOneOutput) +{ + ACC_FROM_HEX(owner_addr_5Ajfk); + + xmreg::XmrOutput mock_output_data = make_mock_output_data(); + + mock_output_data.account_id = acc.id; + + uint64_t expected_primary_id = xmr_accounts->get_next_primary_id(mock_output_data); + uint64_t inserted_output_id = xmr_accounts->insert_output(mock_output_data); + + EXPECT_EQ(inserted_output_id, expected_primary_id); + + // now we fetch the inserted output and compare its values + + xmreg::XmrOutput out_data2; + + EXPECT_TRUE(xmr_accounts->select_output_with_id(inserted_output_id, out_data2)); + + EXPECT_EQ(out_data2.tx_id, mock_output_data.tx_id); + EXPECT_EQ(out_data2.out_pub_key, mock_output_data.out_pub_key); + EXPECT_EQ(out_data2.tx_pub_key, mock_output_data.tx_pub_key); + EXPECT_EQ(out_data2.amount, mock_output_data.amount); + EXPECT_EQ(out_data2.out_index, mock_output_data.out_index); + EXPECT_EQ(out_data2.rct_outpk, mock_output_data.rct_outpk); + EXPECT_EQ(out_data2.rct_mask, mock_output_data.rct_mask); + EXPECT_EQ(out_data2.rct_amount, mock_output_data.rct_amount); + EXPECT_EQ(out_data2.global_index, mock_output_data.global_index); + EXPECT_EQ(out_data2.mixin, mock_output_data.mixin); + EXPECT_EQ(out_data2.timestamp, mock_output_data.timestamp); +} + +TEST_F(MYSQL_TEST, TryToInsertSameOutputTwice) +{ + ACC_FROM_HEX(owner_addr_5Ajfk); + + xmreg::XmrOutput mock_output_data = make_mock_output_data(); + + mock_output_data.account_id = acc.id; + + // first time insert should be fine + uint64_t inserted_output_id = xmr_accounts->insert_output(mock_output_data); + + EXPECT_GT(inserted_output_id, 0); + + // second insert should fail and result in 0 + inserted_output_id = xmr_accounts->insert_output(mock_output_data); + + EXPECT_EQ(inserted_output_id, 0); +} + //TEST(TEST_CHAIN, GenerateTestChain) //{ // uint64_t ts_start = 1338224400;