Skip to content

Commit

Permalink
maintain sstxcommitment pseudo-type for api, more rpcutils cleanout
Browse files Browse the repository at this point in the history
  • Loading branch information
chappjc committed Dec 12, 2021
1 parent 29f3835 commit 7428895
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 166 deletions.
75 changes: 55 additions & 20 deletions api/types/apitypes.go
Expand Up @@ -12,6 +12,7 @@ import (
"time"

chainjson "github.com/decred/dcrd/rpc/jsonrpc/types/v3"
"github.com/decred/dcrd/txscript/v4/stdscript"
"github.com/decred/dcrdata/v7/db/dbtypes"
"github.com/decred/dcrdata/v7/txhelpers"
)
Expand Down Expand Up @@ -75,29 +76,24 @@ type BlockTransactions struct {
STx []string `json:"stx"`
}

// tx raw
// tx short (tx raw - extra context)
// txout
// scriptPubKey (hex -> decodescript -> result)
// vout
// vin

// Tx models TxShort with the number of confirmations and block info Block
type Tx struct {
TxShort
Confirmations int64 `json:"confirmations"`
Block *BlockID `json:"block,omitempty"`
}

type Vin = chainjson.Vin

// TxShort models info about transaction TxID
type TxShort struct {
TxID string `json:"txid"`
Size int32 `json:"size"`
Version int32 `json:"version"`
Locktime uint32 `json:"locktime"`
Expiry uint32 `json:"expiry"`
Vin []chainjson.Vin `json:"vin"`
Vout []Vout `json:"vout"`
TxID string `json:"txid"`
Size int32 `json:"size"`
Version int32 `json:"version"`
Locktime uint32 `json:"locktime"`
Expiry uint32 `json:"expiry"`
Vin []Vin `json:"vin"`
Vout []Vout `json:"vout"`
}

// AgendasInfo holds the high level details about an agenda.
Expand All @@ -117,12 +113,12 @@ type AgendaAPIResponse struct {

// TrimmedTx models data to resemble to result of the decoderawtransaction RPC.
type TrimmedTx struct {
TxID string `json:"txid"`
Version int32 `json:"version"`
Locktime uint32 `json:"locktime"`
Expiry uint32 `json:"expiry"`
Vin []chainjson.Vin `json:"vin"`
Vout []Vout `json:"vout"`
TxID string `json:"txid"`
Version int32 `json:"version"`
Locktime uint32 `json:"locktime"`
Expiry uint32 `json:"expiry"`
Vin []Vin `json:"vin"`
Vout []Vout `json:"vout"`
}

// Txns models the multi transaction post data structure
Expand Down Expand Up @@ -223,6 +219,7 @@ const (
ScriptClassStakeGen // Stake generation
ScriptClassStakeRevocation // Stake revocation.
ScriptClassStakeSubChange // Change for stake submission tx.
ScriptClassStakeSubCommit // Pseudo-class, actually nulldata odd outputs of stake submission (tickets)
ScriptClassTreasuryAdd // Treasury Add (e.g. treasury add tx types, or 0th output of treasury base tx)
ScriptClassTreasuryGen // Treasury Generation (e.g. >0th outputs of treasury spend)
ScriptClassInvalid
Expand All @@ -241,6 +238,7 @@ var scriptClassToName = map[ScriptClass]string{
ScriptClassStakeGen: "stakegen",
ScriptClassStakeRevocation: "stakerevoke",
ScriptClassStakeSubChange: "sstxchange",
ScriptClassStakeSubCommit: "sstxcommitment",
ScriptClassTreasuryAdd: "treasuryadd",
ScriptClassTreasuryGen: "treasurygen",
ScriptClassInvalid: "invalid",
Expand All @@ -259,12 +257,49 @@ var scriptNameToClass = map[string]ScriptClass{
"stakegen": ScriptClassStakeGen,
"stakerevoke": ScriptClassStakeRevocation,
"sstxchange": ScriptClassStakeSubChange,
"sstxcommitment": ScriptClassStakeSubCommit,
"treasuryadd": ScriptClassTreasuryAdd,
"treasurygen": ScriptClassTreasuryGen,

// No "invalid" mapping!
}

// NewScriptClass converts a stdscript.ScriptType to the ScriptClass type, which
// is less fine-grained with respect to the stake subtypes.
func NewScriptClass(sc stdscript.ScriptType) ScriptClass {
switch sc {
case stdscript.STNonStandard:
return ScriptClassNonStandard
case stdscript.STPubKeyEcdsaSecp256k1:
return ScriptClassPubKey
case stdscript.STPubKeyHashEcdsaSecp256k1:
return ScriptClassPubKeyHash
case stdscript.STScriptHash:
return ScriptClassScriptHash
case stdscript.STMultiSig:
return ScriptClassMultiSig
case stdscript.STNullData:
return ScriptClassNullData // maybe ScriptClassStakeSubCommit!
case stdscript.STStakeSubmissionPubKeyHash, stdscript.STStakeSubmissionScriptHash:
return ScriptClassStakeSubmission
case stdscript.STStakeGenPubKeyHash, stdscript.STStakeGenScriptHash:
return ScriptClassStakeGen
case stdscript.STStakeRevocationPubKeyHash, stdscript.STStakeRevocationScriptHash:
return ScriptClassStakeRevocation
case stdscript.STStakeChangePubKeyHash, stdscript.STStakeChangeScriptHash:
return ScriptClassStakeSubChange
case stdscript.STPubKeyEd25519, stdscript.STPubKeySchnorrSecp256k1:
return ScriptClassPubkeyAlt
case stdscript.STPubKeyHashEd25519, stdscript.STPubKeyHashSchnorrSecp256k1:
return ScriptClassPubkeyHashAlt
case stdscript.STTreasuryGenPubKeyHash, stdscript.STTreasuryGenScriptHash:
return ScriptClassTreasuryGen
case stdscript.STTreasuryAdd:
return ScriptClassTreasuryAdd
}
return ScriptClassInvalid
}

// ScriptClassFromName attempts to identify the ScriptClass for the given script
// class/type name. An unknown script name will return ScriptClassInvalid. This
// may be used to map the Type field of the ScriptPubKey data type to a known
Expand Down
6 changes: 3 additions & 3 deletions cmd/dcrdata/api/apiroutes.go
Expand Up @@ -83,7 +83,7 @@ type DataSource interface {
GetHeader(idx int) *chainjson.GetBlockHeaderVerboseResult
GetBlockHeaderByHash(hash string) (*wire.BlockHeader, error)
GetBlockVerboseByHash(hash string, verboseTx bool) *chainjson.GetBlockVerboseResult
GetRawAPITransaction(txid *chainhash.Hash) *apitypes.Tx
GetAPITransaction(txid *chainhash.Hash) *apitypes.Tx
GetTransactionHex(txid *chainhash.Hash) string
GetTrimmedTransaction(txid *chainhash.Hash) *apitypes.TrimmedTx
GetVoteInfo(txid *chainhash.Hash) (*apitypes.VoteInfo, error)
Expand Down Expand Up @@ -642,7 +642,7 @@ func (c *appContext) getTransaction(w http.ResponseWriter, r *http.Request) {
return
}

tx := c.DataSource.GetRawAPITransaction(txid)
tx := c.DataSource.GetAPITransaction(txid)
if tx == nil {
apiLog.Errorf("Unable to get transaction %s", txid)
http.Error(w, http.StatusText(422), 422)
Expand Down Expand Up @@ -831,7 +831,7 @@ func (c *appContext) getTransactions(w http.ResponseWriter, r *http.Request) {

txns := make([]*apitypes.Tx, 0, len(txids))
for i := range txids {
tx := c.DataSource.GetRawAPITransaction(txids[i])
tx := c.DataSource.GetAPITransaction(txids[i])
if tx == nil {
apiLog.Errorf("Unable to get transaction %s", txids[i])
http.Error(w, http.StatusText(422), 422)
Expand Down
4 changes: 3 additions & 1 deletion db/dbtypes/types.go
Expand Up @@ -47,6 +47,7 @@ const (
SCStakeGen // Stake generation
SCStakeRevocation // Stake revocation.
SCStakeSubChange // Change for stake submission tx.
SCStakeSubCommit // Pseudo-class for stake submission commitments, odd outputs
SCPubkeyAlt // Alternative signature pubkey.
SCPubkeyHashAlt // Alternative signature pubkey hash.
SCTreasuryAdd // Add value to treasury
Expand All @@ -69,6 +70,7 @@ var scriptClassToName = [...]string{
SCStakeGen: "stakegen",
SCStakeRevocation: "stakerevoke",
SCStakeSubChange: "sstxchange",
SCStakeSubCommit: "sstxcommitment",
SCTreasuryAdd: "treasuryadd",
SCTreasuryGen: "treasurygen",
}
Expand All @@ -88,7 +90,7 @@ func NewScriptClass(sc stdscript.ScriptType) ScriptClass {
case stdscript.STMultiSig:
return SCMultiSig
case stdscript.STNullData:
return SCNullData
return SCNullData // could be a SCStakeSubCommit
case stdscript.STStakeSubmissionPubKeyHash, stdscript.STStakeSubmissionScriptHash:
return SCStakeSubmission
case stdscript.STStakeGenPubKeyHash, stdscript.STStakeGenScriptHash:
Expand Down

0 comments on commit 7428895

Please sign in to comment.