Skip to content

Commit

Permalink
[NOD-326] Replaced the UTXOs table with TransactionOutputs.isSpent (#…
Browse files Browse the repository at this point in the history
…404)

* [NOD-326] Replaced UTXO table with TransactionOutput.IsSpent.

* [NOD-326] Fixed merge errors.
  • Loading branch information
stasatdaglabs authored and svarogg committed Sep 15, 2019
1 parent a789680 commit 369031f
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 31 deletions.
22 changes: 10 additions & 12 deletions apiserver/controllers/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`),
Expand Down
1 change: 0 additions & 1 deletion apiserver/migrations/000010_create_utxos_table.down.sql

This file was deleted.

10 changes: 0 additions & 10 deletions apiserver/migrations/000010_create_utxos_table.up.sql

This file was deleted.

9 changes: 1 addition & 8 deletions apiserver/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type TransactionOutput struct {
Index uint32
Value uint64
ScriptPubKey []byte
IsSpent bool
AddressID uint64
Address Address
}
Expand All @@ -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"`
Expand Down

0 comments on commit 369031f

Please sign in to comment.