Skip to content

Commit

Permalink
multi: return additional info about transaction
Browse files Browse the repository at this point in the history
return transaction type. If transaction is ticketpurchase also return
ticket status
  • Loading branch information
kLkA committed Oct 27, 2018
1 parent e980707 commit d74138a
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ dcrwallet
vendor
*~
.vscode
.idea
2 changes: 2 additions & 0 deletions internal/rpchelp/helpdescs_en_US.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ var helpDescsEnUS = map[string]string{
"gettransactionresult-timereceived": "The earliest Unix time this transaction was known to exist",
"gettransactionresult-details": "Additional details for each recorded wallet credit and debit",
"gettransactionresult-hex": "The transaction encoded as a hexadecimal string",
"gettransactionresult-type": "The transaction type",
"gettransactionresult-ticketstatus": "If transaction type is 'ticketpurchase' this field will return ticket status",

// GetTransactionDetailsResult help.
"gettransactiondetailsresult-account": "DEPRECATED -- Unset",
Expand Down
20 changes: 20 additions & 0 deletions rpc/legacyrpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -1477,17 +1477,37 @@ func getTransaction(s *Server, icmd interface{}) (interface{}, error) {
return nil, err
}

txType := wallet.TxTransactionType(&txd.MsgTx)
// TODO: Add a "generated" field to this result type. "generated":true
// is only added if the transaction is a coinbase.
ret := dcrjson.GetTransactionResult{
TxID: cmd.Txid,
Hex: b.String(),
Time: txd.Received.Unix(),
TimeReceived: txd.Received.Unix(),
Type: txType.String(),
WalletConflicts: []string{}, // Not saved
//Generated: blockchain.IsCoinBaseTx(&details.MsgTx),
}

var ticketInfo *wallet.TicketSummary
if txType == wallet.TransactionTypeTicketPurchase {
var chainClient *rpcclient.Client
if n, ok := w.NetworkBackend(); ok {
client, err := chain.RPCClientFromBackend(n)
if err == nil {
chainClient = client
}
}

ticketInfo, err = w.GetTicketSummary(chainClient, txd)
if err != nil {
return nil, err
}

ret.TicketStatus = ticketInfo.Status.String()
}

if txd.Block.Height != -1 {
ret.BlockHash = txd.Block.Hash.String()
ret.BlockTime = txd.Block.Time.Unix()
Expand Down
2 changes: 1 addition & 1 deletion spv/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require (
github.com/decred/dcrd/blockchain/stake v1.0.1
github.com/decred/dcrd/chaincfg/chainhash v1.0.1
github.com/decred/dcrd/dcrutil v1.1.1
github.com/decred/dcrd/gcs v1.0.1
github.com/decred/dcrd/gcs v1.0.2
github.com/decred/dcrd/txscript v1.0.1
github.com/decred/dcrd/wire v1.1.0
github.com/decred/dcrwallet/errors v1.0.0
Expand Down
3 changes: 3 additions & 0 deletions spv/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dchest/blake256 v1.0.0 h1:6gUgI5MHdz9g0TdrgKqXsoDX+Zjxmm1Sc6OsoGru50I=
github.com/dchest/blake256 v1.0.0/go.mod h1:xXNWCE1jsAP8DAjP+rKw2MbeqLczjI3TRx2VK+9OEYY=
github.com/dchest/siphash v1.2.0/go.mod h1:q+IRvb2gOSrUnYoPqHiyHXS0FOBBOdl6tONBlVnOnt4=
github.com/decred/base58 v1.0.0 h1:BVi1FQCThIjZ0ehG+I99NJ51o0xcc9A/fDKhmJxY6+w=
github.com/decred/base58 v1.0.0/go.mod h1:LLY1p5e3g91byL/UO1eiZaYd+uRoVRarybgcoymu9Ks=
github.com/decred/dcrd v1.2.1-0.20180801202239-0761de129164 h1:wYjHNfbzWlLpFuqlVVNtXRmqrEETEDv7KWEf7psUWnc=
Expand All @@ -28,6 +29,7 @@ github.com/decred/dcrd/addrmgr v1.0.2/go.mod h1:gNnmTuf/Xkg8ZX3j5GXbajzPrSdf5bA7
github.com/decred/dcrd/blockchain v1.0.0/go.mod h1:nNMgOz12wlasmEJDCuSuMWYSnjDdmB4l38GKuQ/Yd+8=
github.com/decred/dcrd/blockchain v1.0.1 h1:7cviDS26sZ9ZyTFka3aC9C/EChXBslmAvse+4nF5d60=
github.com/decred/dcrd/blockchain v1.0.1/go.mod h1:R/4XnwNOTj5IP8jQIUzrJ8zhr/7EOk09IMODwBamZoI=
github.com/decred/dcrd/blockchain v1.0.2/go.mod h1:R/4XnwNOTj5IP8jQIUzrJ8zhr/7EOk09IMODwBamZoI=
github.com/decred/dcrd/blockchain/stake v1.0.0/go.mod h1:opuzF8UouYyQyRJVF00Rdd7OgWb1WKyy1pyU0QYaxz0=
github.com/decred/dcrd/blockchain/stake v1.0.1 h1:IYGsNZRyMUsoFtVAUjd7XIccrIQ4YIqDeNzQJCjyS8A=
github.com/decred/dcrd/blockchain/stake v1.0.1/go.mod h1:hgoGmWMIu2LLApBbcguVpzCEEfX7M2YhuMrQdpohJzc=
Expand Down Expand Up @@ -70,6 +72,7 @@ github.com/decred/dcrd/dcrutil v1.1.1/go.mod h1:Jsttr0pEvzPAw+qay1kS1/PsbZYPyhlu
github.com/decred/dcrd/gcs v1.0.0/go.mod h1:5uHIPAzn4SdGP2/FhVBK2YdAoKmufds3ZI8yNzojUCM=
github.com/decred/dcrd/gcs v1.0.1 h1:MpJXLskT41+JDaD3RLdlSlF2vlu1sxPpZgiRI7FVTWw=
github.com/decred/dcrd/gcs v1.0.1/go.mod h1:YwutGzusSdJM79CJtxCo9t7WRCvnkLtWSD19TPo1i9g=
github.com/decred/dcrd/gcs v1.0.2/go.mod h1:eLCvrzUsWro48TlTyrmFcZAZqnllYFz0vEv5VZtufF4=
github.com/decred/dcrd/hdkeychain v1.0.0 h1:YZPNvvFYME29nnjLl1c4v+Wusrx4BzUCYdRYmUdRnPI=
github.com/decred/dcrd/hdkeychain v1.0.0/go.mod h1:ZLzIMYN4rLNQPdy2I5FYtdYevIKwmUVo1Sz9x909kYc=
github.com/decred/dcrd/hdkeychain v1.1.0 h1:6bFdL672dCmtg/JEzb3Jw0dTRO2jLxcA7BK2J+JaoUM=
Expand Down
2 changes: 1 addition & 1 deletion ticketbuyer/go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/decred/dcrwallet/ticketbuyer

require (
github.com/decred/dcrd/blockchain v1.0.1
github.com/decred/dcrd/blockchain v1.0.2
github.com/decred/dcrd/chaincfg v1.1.1
github.com/decred/dcrd/dcrjson v1.0.0
github.com/decred/dcrd/dcrutil v1.1.1
Expand Down
3 changes: 3 additions & 0 deletions ticketbuyer/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dchest/blake256 v1.0.0 h1:6gUgI5MHdz9g0TdrgKqXsoDX+Zjxmm1Sc6OsoGru50I=
github.com/dchest/blake256 v1.0.0/go.mod h1:xXNWCE1jsAP8DAjP+rKw2MbeqLczjI3TRx2VK+9OEYY=
github.com/dchest/siphash v1.2.0/go.mod h1:q+IRvb2gOSrUnYoPqHiyHXS0FOBBOdl6tONBlVnOnt4=
github.com/decred/base58 v1.0.0 h1:BVi1FQCThIjZ0ehG+I99NJ51o0xcc9A/fDKhmJxY6+w=
github.com/decred/base58 v1.0.0/go.mod h1:LLY1p5e3g91byL/UO1eiZaYd+uRoVRarybgcoymu9Ks=
github.com/decred/dcrd/blockchain v1.0.0/go.mod h1:nNMgOz12wlasmEJDCuSuMWYSnjDdmB4l38GKuQ/Yd+8=
github.com/decred/dcrd/blockchain v1.0.1 h1:7cviDS26sZ9ZyTFka3aC9C/EChXBslmAvse+4nF5d60=
github.com/decred/dcrd/blockchain v1.0.1/go.mod h1:R/4XnwNOTj5IP8jQIUzrJ8zhr/7EOk09IMODwBamZoI=
github.com/decred/dcrd/blockchain v1.0.2/go.mod h1:R/4XnwNOTj5IP8jQIUzrJ8zhr/7EOk09IMODwBamZoI=
github.com/decred/dcrd/blockchain/stake v1.0.0/go.mod h1:opuzF8UouYyQyRJVF00Rdd7OgWb1WKyy1pyU0QYaxz0=
github.com/decred/dcrd/blockchain/stake v1.0.1 h1:IYGsNZRyMUsoFtVAUjd7XIccrIQ4YIqDeNzQJCjyS8A=
github.com/decred/dcrd/blockchain/stake v1.0.1/go.mod h1:hgoGmWMIu2LLApBbcguVpzCEEfX7M2YhuMrQdpohJzc=
Expand Down Expand Up @@ -55,6 +57,7 @@ github.com/decred/dcrd/dcrutil v1.1.1/go.mod h1:Jsttr0pEvzPAw+qay1kS1/PsbZYPyhlu
github.com/decred/dcrd/gcs v1.0.0/go.mod h1:5uHIPAzn4SdGP2/FhVBK2YdAoKmufds3ZI8yNzojUCM=
github.com/decred/dcrd/gcs v1.0.1 h1:MpJXLskT41+JDaD3RLdlSlF2vlu1sxPpZgiRI7FVTWw=
github.com/decred/dcrd/gcs v1.0.1/go.mod h1:YwutGzusSdJM79CJtxCo9t7WRCvnkLtWSD19TPo1i9g=
github.com/decred/dcrd/gcs v1.0.2/go.mod h1:eLCvrzUsWro48TlTyrmFcZAZqnllYFz0vEv5VZtufF4=
github.com/decred/dcrd/hdkeychain v1.1.0 h1:6bFdL672dCmtg/JEzb3Jw0dTRO2jLxcA7BK2J+JaoUM=
github.com/decred/dcrd/hdkeychain v1.1.0/go.mod h1:zyUZtZ3PdnTPHt2XUr1x76b8ZuiM+9aVkP8Rq8Scp1k=
github.com/decred/dcrd/mempool v1.0.1 h1:xZOfwGmVbWfKDNztVgsVO/GkMmiZn3MMfDDVE07ZKKs=
Expand Down
3 changes: 3 additions & 0 deletions ticketbuyer/v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dchest/blake256 v1.0.0 h1:6gUgI5MHdz9g0TdrgKqXsoDX+Zjxmm1Sc6OsoGru50I=
github.com/dchest/blake256 v1.0.0/go.mod h1:xXNWCE1jsAP8DAjP+rKw2MbeqLczjI3TRx2VK+9OEYY=
github.com/dchest/siphash v1.2.0/go.mod h1:q+IRvb2gOSrUnYoPqHiyHXS0FOBBOdl6tONBlVnOnt4=
github.com/decred/base58 v1.0.0 h1:BVi1FQCThIjZ0ehG+I99NJ51o0xcc9A/fDKhmJxY6+w=
github.com/decred/base58 v1.0.0/go.mod h1:LLY1p5e3g91byL/UO1eiZaYd+uRoVRarybgcoymu9Ks=
github.com/decred/dcrd/blockchain v1.0.0/go.mod h1:nNMgOz12wlasmEJDCuSuMWYSnjDdmB4l38GKuQ/Yd+8=
github.com/decred/dcrd/blockchain v1.0.1 h1:7cviDS26sZ9ZyTFka3aC9C/EChXBslmAvse+4nF5d60=
github.com/decred/dcrd/blockchain v1.0.1/go.mod h1:R/4XnwNOTj5IP8jQIUzrJ8zhr/7EOk09IMODwBamZoI=
github.com/decred/dcrd/blockchain v1.0.2/go.mod h1:R/4XnwNOTj5IP8jQIUzrJ8zhr/7EOk09IMODwBamZoI=
github.com/decred/dcrd/blockchain/stake v1.0.0/go.mod h1:opuzF8UouYyQyRJVF00Rdd7OgWb1WKyy1pyU0QYaxz0=
github.com/decred/dcrd/blockchain/stake v1.0.1 h1:IYGsNZRyMUsoFtVAUjd7XIccrIQ4YIqDeNzQJCjyS8A=
github.com/decred/dcrd/blockchain/stake v1.0.1/go.mod h1:hgoGmWMIu2LLApBbcguVpzCEEfX7M2YhuMrQdpohJzc=
Expand Down Expand Up @@ -55,6 +57,7 @@ github.com/decred/dcrd/dcrutil v1.1.1/go.mod h1:Jsttr0pEvzPAw+qay1kS1/PsbZYPyhlu
github.com/decred/dcrd/gcs v1.0.0/go.mod h1:5uHIPAzn4SdGP2/FhVBK2YdAoKmufds3ZI8yNzojUCM=
github.com/decred/dcrd/gcs v1.0.1 h1:MpJXLskT41+JDaD3RLdlSlF2vlu1sxPpZgiRI7FVTWw=
github.com/decred/dcrd/gcs v1.0.1/go.mod h1:YwutGzusSdJM79CJtxCo9t7WRCvnkLtWSD19TPo1i9g=
github.com/decred/dcrd/gcs v1.0.2/go.mod h1:eLCvrzUsWro48TlTyrmFcZAZqnllYFz0vEv5VZtufF4=
github.com/decred/dcrd/hdkeychain v1.1.0 h1:6bFdL672dCmtg/JEzb3Jw0dTRO2jLxcA7BK2J+JaoUM=
github.com/decred/dcrd/hdkeychain v1.1.0/go.mod h1:zyUZtZ3PdnTPHt2XUr1x76b8ZuiM+9aVkP8Rq8Scp1k=
github.com/decred/dcrd/mempool v1.0.1 h1:xZOfwGmVbWfKDNztVgsVO/GkMmiZn3MMfDDVE07ZKKs=
Expand Down
56 changes: 56 additions & 0 deletions wallet/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ func makeTicketSummary(chainClient *dcrrpcclient.Client, dbtx walletdb.ReadTx, w
// Check if ticket age is over TicketExpiry limit and therefore expired
} else if ticketExpired(w.chainParams, details.Ticket.Height(), tipHeight) {
ticketStatus = TicketStatusExpired
if chainClient != nil {
ticketStatus = TicketStatusExpired
} else {
ticketStatus = TicketStatusUnspentExpired
}
} else if chainClient == nil {
// Mark ticket as unspent. Possible states are missed, live or expired
ticketStatus = TicketStatusUnspent
}
}
return &TicketSummary{
Expand Down Expand Up @@ -455,8 +463,39 @@ const (
TicketStatusMissed
// TicketStatusExpired any ticket that has yet to be revoked, and was expired.
TicketStatusExpired
// TicketStatusUnspent any ticket that missed, live or expired
TicketStatusUnspent
// TicketStatusUnspentExpired any ticket that missed or expired
TicketStatusUnspentExpired
)

func (t TicketStatus) String() string {
switch t {
case TicketStatusUnknown:
return "UNKNOWN"
case TicketStatusUnmined:
return "UNMINED"
case TicketStatusImmature:
return "IMMATURE"
case TicketStatusLive:
return "LIVE"
case TicketStatusVoted:
return "VOTED"
case TicketStatusRevoked:
return "REVOKED"
case TicketStatusMissed:
return "MISSED"
case TicketStatusExpired:
return "EXPIRED"
case TicketStatusUnspent:
return "UNSPENT"
case TicketStatusUnspentExpired:
return "UNSPENTEXPIRED"
default:
return ""
}
}

// TransactionSummary contains a transaction relevant to the wallet and marks
// which inputs and outputs were relevant.
type TransactionSummary struct {
Expand Down Expand Up @@ -510,6 +549,23 @@ func TxTransactionType(tx *wire.MsgTx) TransactionType {
}
}

func (t TransactionType) String() string {
switch t {
case TransactionTypeRegular:
return "REGULAR"
case TransactionTypeCoinbase:
return "COINBASE"
case TransactionTypeTicketPurchase:
return "TICKETPURCHASE"
case TransactionTypeVote:
return "VOTE"
case TransactionTypeRevocation:
return "REVOCATION"
default:
return ""
}
}

// TransactionSummaryInput describes a transaction input that is relevant to the
// wallet. The Index field marks the transaction input index of the transaction
// (not included here). The PreviousAccount and PreviousAmount fields describe
Expand Down
26 changes: 26 additions & 0 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2635,6 +2635,32 @@ func (w *Wallet) GetTickets(f func([]*TicketSummary, *wire.BlockHeader) (bool, e
return nil
}

// GetTicketSummary
func (w *Wallet) GetTicketSummary(chainClient *dcrrpcclient.Client, txDetails *udb.TxDetails) (*TicketSummary, error) {
const op errors.Op = "wallet.GetTicketSummary"

var ticketSummary *TicketSummary
err := walletdb.View(w.db, func(dbtx walletdb.ReadTx) error {
txmgrNs := dbtx.ReadBucket(wtxmgrNamespaceKey)

ticketInfo, err := w.TxStore.TicketDetails(txmgrNs, txDetails)
if err != nil {
return errors.Errorf("%v while trying to get ticket details for txhash: %v", err, txDetails.Hash)
}
// Continue if not a ticket
if ticketInfo == nil {
return errors.New("tx is not a ticket")
}
ticketSummary = makeTicketSummary(chainClient, dbtx, w, ticketInfo)
return nil
})
if err != nil {
return nil, errors.E(op, err)
}

return ticketSummary, nil
}

// GetTransactionsResult is the result of the wallet's GetTransactions method.
// See GetTransactions for more details.
type GetTransactionsResult struct {
Expand Down

0 comments on commit d74138a

Please sign in to comment.