[NOD-418] Added IsSpendable field to /utxos/ queries in API server (#466)

This commit is contained in:
Svarog 2019-11-13 16:25:10 +02:00 committed by Dan Aharoni
parent 8179862e0b
commit 7d7df10493
3 changed files with 13 additions and 5 deletions

View File

@ -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"`
}

View File

@ -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

View File

@ -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