diff --git a/apiserver/controllers/transaction.go b/apiserver/controllers/transaction.go index 45a59b2e..7b9e7980 100644 --- a/apiserver/controllers/transaction.go +++ b/apiserver/controllers/transaction.go @@ -70,21 +70,19 @@ func GetTransactionsByAddressHandler(address string, skip uint64, limit uint64) // GetUTXOsByAddressHandler searches for all UTXOs that belong to a certain address. func GetUTXOsByAddressHandler(address string) (interface{}, *utils.HandlerError) { - utxos := []*models.UTXO{} + var transactionOutputs []*models.TransactionOutput database.DB. - Joins("LEFT JOIN `transaction_outputs` ON `transaction_outputs`.`id` = `utxos`.`transaction_output_id`"). Joins("LEFT JOIN `addresses` ON `addresses`.`id` = `transaction_outputs`.`address_id`"). - Where("`addresses`.`address` = ?", address). - Preload("AcceptingBlock"). - Preload("TransactionOutput"). - Find(&utxos) - UTXOsResponses := make([]*transactionOutputResponse, len(utxos)) - for i, utxo := range utxos { + Where("`addresses`.`address` = ? AND `transaction_outputs`.`is_spent` = 0", address). + Preload("Transaction.AcceptingBlock"). + Find(&transactionOutputs) + UTXOsResponses := make([]*transactionOutputResponse, len(transactionOutputs)) + for i, transactionOutput := range transactionOutputs { UTXOsResponses[i] = &transactionOutputResponse{ - Value: utxo.TransactionOutput.Value, - ScriptPubKey: hex.EncodeToString(utxo.TransactionOutput.ScriptPubKey), - AcceptingBlockHash: utxo.AcceptingBlock.BlockHash, - AcceptingBlockBlueScore: utxo.AcceptingBlock.BlueScore, + Value: transactionOutput.Value, + ScriptPubKey: hex.EncodeToString(transactionOutput.ScriptPubKey), + AcceptingBlockHash: transactionOutput.Transaction.AcceptingBlock.BlockHash, + AcceptingBlockBlueScore: transactionOutput.Transaction.AcceptingBlock.BlueScore, } } return UTXOsResponses, nil diff --git a/apiserver/migrations/000008_create_transaction_outputs_table.up.sql b/apiserver/migrations/000008_create_transaction_outputs_table.up.sql index 5d2ce5f3..d0e06600 100644 --- a/apiserver/migrations/000008_create_transaction_outputs_table.up.sql +++ b/apiserver/migrations/000008_create_transaction_outputs_table.up.sql @@ -5,6 +5,7 @@ CREATE TABLE `transaction_outputs` `index` INT UNSIGNED NOT NULL, `value` BIGINT UNSIGNED NOT NULL, `script_pub_key` BLOB NOT NULL, + `is_spent` TINYINT NOT NULL, `address_id` BIGINT UNSIGNED NOT NULL, PRIMARY KEY (`id`), INDEX `idx_transaction_outputs_transaction_id` (`transaction_id`), diff --git a/apiserver/migrations/000010_create_utxos_table.down.sql b/apiserver/migrations/000010_create_utxos_table.down.sql deleted file mode 100644 index d967fe0c..00000000 --- a/apiserver/migrations/000010_create_utxos_table.down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE `utxos`; diff --git a/apiserver/migrations/000010_create_utxos_table.up.sql b/apiserver/migrations/000010_create_utxos_table.up.sql deleted file mode 100644 index 7e1bfac7..00000000 --- a/apiserver/migrations/000010_create_utxos_table.up.sql +++ /dev/null @@ -1,10 +0,0 @@ -CREATE TABLE `utxos` -( - `transaction_output_id` BIGINT UNSIGNED NOT NULL, - `accepting_block_id` INT UNSIGNED NULL, - PRIMARY KEY (`transaction_output_id`), - INDEX `idx_utxos_accepting_block_id` (`accepting_block_id`), - CONSTRAINT `fk_utxos_transaction_output_id` - FOREIGN KEY (`transaction_output_id`) - REFERENCES `transaction_outputs` (`id`) -); diff --git a/apiserver/models/models.go b/apiserver/models/models.go index 98df34d1..d25c5c69 100644 --- a/apiserver/models/models.go +++ b/apiserver/models/models.go @@ -86,6 +86,7 @@ type TransactionOutput struct { Index uint32 Value uint64 ScriptPubKey []byte + IsSpent bool AddressID uint64 Address Address } @@ -102,14 +103,6 @@ type TransactionInput struct { Sequence uint64 } -// UTXO is the gorm model for the 'utxos' table -type UTXO struct { - TransactionOutputID uint64 - TransactionOutput TransactionOutput - AcceptingBlockID uint64 - AcceptingBlock Block -} - // Address is the gorm model for the 'utxos' table type Address struct { ID uint64 `gorm:"primary_key"`