From 7d7df10493aa96b5164fd40cdb226b181a424bb7 Mon Sep 17 00:00:00 2001 From: Svarog Date: Wed, 13 Nov 2019 16:25:10 +0200 Subject: [PATCH] [NOD-418] Added IsSpendable field to /utxos/ queries in API server (#466) --- apiserver/apimodels/response_types.go | 1 + apiserver/controllers/transaction.go | 10 ++++++++-- apiserver/sync.go | 7 ++++--- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/apiserver/apimodels/response_types.go b/apiserver/apimodels/response_types.go index 5a9489ba..db81d0f4 100644 --- a/apiserver/apimodels/response_types.go +++ b/apiserver/apimodels/response_types.go @@ -26,6 +26,7 @@ type TransactionOutputResponse struct { AcceptingBlockBlueScore uint64 `json:"acceptingBlockBlueScore,omitempty"` Index uint32 `json:"index"` IsCoinbase *bool `json:"isCoinbase,omitempty"` + IsSpendable *bool `json:"isSpendable,omitempty"` Confirmations *uint64 `json:"confirmations,omitempty"` } diff --git a/apiserver/controllers/transaction.go b/apiserver/controllers/transaction.go index 0105b06b..f9bd6d4d 100644 --- a/apiserver/controllers/transaction.go +++ b/apiserver/controllers/transaction.go @@ -5,13 +5,15 @@ import ( "encoding/hex" "encoding/json" "fmt" + "net/http" + "github.com/daglabs/btcd/apiserver/apimodels" + "github.com/daglabs/btcd/apiserver/config" "github.com/daglabs/btcd/apiserver/dbmodels" "github.com/daglabs/btcd/blockdag" "github.com/daglabs/btcd/httpserverutils" "github.com/daglabs/btcd/util/subnetworkid" "github.com/pkg/errors" - "net/http" "github.com/daglabs/btcd/apiserver/database" "github.com/daglabs/btcd/apiserver/jsonrpc" @@ -187,6 +189,8 @@ func GetUTXOsByAddressHandler(address string) (interface{}, error) { } } + activeNetParams := config.ActiveConfig().NetParams() + UTXOsResponses := make([]*apimodels.TransactionOutputResponse, len(transactionOutputs)) for i, transactionOutput := range transactionOutputs { subnetworkID := &subnetworkid.SubnetworkID{} @@ -204,6 +208,7 @@ func GetUTXOsByAddressHandler(address string) (interface{}, error) { acceptingBlockBlueScore = transactionOutput.Transaction.AcceptingBlock.BlueScore confirmations = selectedTip.BlueScore - acceptingBlockBlueScore + 2 } + isCoinbase := subnetworkID.IsEqual(subnetworkid.SubnetworkIDCoinbase) UTXOsResponses[i] = &apimodels.TransactionOutputResponse{ TransactionID: transactionOutput.Transaction.TransactionID, Value: transactionOutput.Value, @@ -211,8 +216,9 @@ func GetUTXOsByAddressHandler(address string) (interface{}, error) { AcceptingBlockHash: acceptingBlockHash, AcceptingBlockBlueScore: acceptingBlockBlueScore, Index: transactionOutput.Index, - IsCoinbase: btcjson.Bool(subnetworkID.IsEqual(subnetworkid.SubnetworkIDCoinbase)), + IsCoinbase: btcjson.Bool(isCoinbase), Confirmations: btcjson.Uint64(confirmations), + IsSpendable: btcjson.Bool(!isCoinbase || confirmations >= activeNetParams.BlockCoinbaseMaturity), } } return UTXOsResponses, nil diff --git a/apiserver/sync.go b/apiserver/sync.go index f823d456..fa79b583 100644 --- a/apiserver/sync.go +++ b/apiserver/sync.go @@ -3,12 +3,15 @@ package main import ( "bytes" "encoding/hex" + "strconv" + "time" + + "github.com/daglabs/btcd/apiserver/config" "github.com/daglabs/btcd/apiserver/database" "github.com/daglabs/btcd/apiserver/dbmodels" "github.com/daglabs/btcd/apiserver/jsonrpc" "github.com/daglabs/btcd/blockdag" "github.com/daglabs/btcd/btcjson" - "github.com/daglabs/btcd/config" "github.com/daglabs/btcd/httpserverutils" "github.com/daglabs/btcd/txscript" "github.com/daglabs/btcd/util" @@ -17,8 +20,6 @@ import ( "github.com/daglabs/btcd/wire" "github.com/jinzhu/gorm" "github.com/pkg/errors" - "strconv" - "time" ) // startSync keeps the node and the API server in sync. On start, it downloads