Skip to content

Commit

Permalink
[NOD-418] Added IsSpendable field to /utxos/ queries in API server (#…
Browse files Browse the repository at this point in the history
…466)
  • Loading branch information
svarogg authored and Dan Aharoni committed Nov 13, 2019
1 parent 8179862 commit 7d7df10
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions apiserver/apimodels/response_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
10 changes: 8 additions & 2 deletions apiserver/controllers/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{}
Expand All @@ -204,15 +208,17 @@ 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,
ScriptPubKey: hex.EncodeToString(transactionOutput.ScriptPubKey),
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
Expand Down
7 changes: 4 additions & 3 deletions apiserver/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down

0 comments on commit 7d7df10

Please sign in to comment.