Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
[NOD-639] Make it possible to have address-less txs (#15)
Browse files Browse the repository at this point in the history
* [NOD-639] Make Address and AddressID nullable.

* [NOD-639] Fix merge errors.

* [NOD-639] Make address_id nullable in the database schema.

* [NOD-639] Bring back the foreign key constraint over addresses.id.
  • Loading branch information
stasatdaglabs authored and someone235 committed Jan 12, 2020
1 parent 82b98a8 commit 5aaaec2
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CREATE TABLE `transaction_outputs`
`value` BIGINT UNSIGNED NOT NULL,
`script_pub_key` BLOB NOT NULL,
`is_spent` TINYINT NOT NULL,
`address_id` BIGINT UNSIGNED NOT NULL,
`address_id` BIGINT UNSIGNED NULL,
PRIMARY KEY (`id`),
INDEX `idx_transaction_outputs_transaction_id` (`transaction_id`),
CONSTRAINT `fk_transaction_outputs_transaction_id`
Expand Down
4 changes: 2 additions & 2 deletions dbmodels/dbmodels.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ type TransactionOutput struct {
Value uint64
ScriptPubKey []byte
IsSpent bool
AddressID uint64
Address Address
AddressID *uint64
Address *Address
}

// TransactionInput is the gorm model for the 'transaction_inputs' table
Expand Down
8 changes: 6 additions & 2 deletions kasparovd/controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,21 @@ func convertTxDBModelToTxResponse(tx *dbmodels.Transaction) *apimodels.Transacti
txRes.Outputs[i] = &apimodels.TransactionOutputResponse{
Value: txOut.Value,
ScriptPubKey: hex.EncodeToString(txOut.ScriptPubKey),
Address: txOut.Address.Address,
Index: txOut.Index,
}
if txOut.Address != nil {
txRes.Outputs[i].Address = txOut.Address.Address
}
}
for i, txIn := range tx.TransactionInputs {
txRes.Inputs[i] = &apimodels.TransactionInputResponse{
PreviousTransactionID: txIn.PreviousTransactionOutput.Transaction.TransactionID,
PreviousTransactionOutputIndex: txIn.PreviousTransactionOutput.Index,
SignatureScript: hex.EncodeToString(txIn.SignatureScript),
Sequence: txIn.Sequence,
Address: txIn.PreviousTransactionOutput.Address.Address,
}
if txIn.PreviousTransactionOutput.Address != nil {
txRes.Inputs[i].Address = txIn.PreviousTransactionOutput.Address.Address
}
}
return txRes
Expand Down
4 changes: 1 addition & 3 deletions kasparovsyncd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,11 +666,9 @@ func insertAddress(dbTx *gorm.DB, scriptPubKey []byte) (*dbmodels.Address, error
if err != nil {
return nil, err
}

if addr == nil {
return nil, nil
}

hexAddress := addr.EncodeAddress()

var dbAddress dbmodels.Address
Expand Down Expand Up @@ -714,7 +712,7 @@ func insertTransactionOutput(dbTx *gorm.DB, dbTransaction *dbmodels.Transaction,
ScriptPubKey: scriptPubKey,
}
if dbAddress != nil {
dbTransactionOutput.AddressID = dbAddress.ID
dbTransactionOutput.AddressID = &dbAddress.ID
}
dbResult := dbTx.Create(&dbTransactionOutput)
dbErrors := dbResult.GetErrors()
Expand Down

0 comments on commit 5aaaec2

Please sign in to comment.