9 log "github.com/sirupsen/logrus"
11 "github.com/vapor/blockchain/query"
12 "github.com/vapor/crypto/sha3pool"
13 "github.com/vapor/protocol"
14 "github.com/vapor/protocol/bc/types"
18 //UnconfirmedTxPrefix is txpool unconfirmed transactions prefix
19 UnconfirmedTxPrefix = "UTXS:"
20 UnconfirmedTxCheckPeriod = 30 * time.Minute
21 MaxUnconfirmedTxDuration = 24 * time.Hour
24 // SortByTimestamp implements sort.Interface for AnnotatedTx slices
25 type SortByTimestamp []*query.AnnotatedTx
27 func (a SortByTimestamp) Len() int { return len(a) }
28 func (a SortByTimestamp) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
29 func (a SortByTimestamp) Less(i, j int) bool { return a[i].Timestamp > a[j].Timestamp }
31 // AddUnconfirmedTx handle wallet status update when tx add into txpool
32 func (w *Wallet) AddUnconfirmedTx(txD *protocol.TxDesc) {
33 if err := w.saveUnconfirmedTx(txD.Tx); err != nil {
34 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("wallet fail on saveUnconfirmedTx")
37 utxos := txOutToUtxos(txD.Tx, txD.StatusFail, 0)
38 utxos = w.filterAccountUtxo(utxos)
39 w.AccountMgr.AddUnconfirmedUtxo(utxos)
42 // GetUnconfirmedTxs get account unconfirmed transactions, filter transactions by accountID when accountID is not empty
43 func (w *Wallet) GetUnconfirmedTxs(accountID string) ([]*query.AnnotatedTx, error) {
44 annotatedTxs := []*query.AnnotatedTx{}
45 annotatedTxs, err := w.store.GetUnconfirmedTransactions()
50 newAnnotatedTxs := []*query.AnnotatedTx{}
51 for _, annotatedTx := range annotatedTxs {
52 if accountID == "" || findTransactionsByAccount(annotatedTx, accountID) {
53 annotateTxsAsset(w, []*query.AnnotatedTx{annotatedTx})
54 newAnnotatedTxs = append([]*query.AnnotatedTx{annotatedTx}, newAnnotatedTxs...)
58 sort.Sort(SortByTimestamp(newAnnotatedTxs))
59 return newAnnotatedTxs, nil
62 // GetUnconfirmedTxByTxID get unconfirmed transaction by txID
63 func (w *Wallet) GetUnconfirmedTxByTxID(txID string) (*query.AnnotatedTx, error) {
64 annotatedTx := &query.AnnotatedTx{}
65 txInfo := w.store.GetUnconfirmedTransaction(txID)
67 return nil, fmt.Errorf("No transaction(tx_id=%s) from txpool", txID)
70 if err := json.Unmarshal(txInfo, annotatedTx); err != nil {
74 annotateTxsAsset(w, []*query.AnnotatedTx{annotatedTx})
75 return annotatedTx, nil
78 // RemoveUnconfirmedTx handle wallet status update when tx removed from txpool
79 func (w *Wallet) RemoveUnconfirmedTx(txD *protocol.TxDesc) {
80 if !w.checkRelatedTransaction(txD.Tx) {
83 w.store.DeleteUnconfirmedTransaction(txD.Tx.ID.String())
84 w.AccountMgr.RemoveUnconfirmedUtxo(txD.Tx.ResultIds)
87 func (w *Wallet) buildAnnotatedUnconfirmedTx(tx *types.Tx) *query.AnnotatedTx {
88 annotatedTx := &query.AnnotatedTx{
90 Timestamp: uint64(time.Now().UnixNano() / int64(time.Millisecond)),
91 Inputs: make([]*query.AnnotatedInput, 0, len(tx.Inputs)),
92 Outputs: make([]*query.AnnotatedOutput, 0, len(tx.Outputs)),
93 Size: tx.SerializedSize,
96 for i := range tx.Inputs {
97 annotatedTx.Inputs = append(annotatedTx.Inputs, w.BuildAnnotatedInput(tx, uint32(i)))
99 for i := range tx.Outputs {
100 annotatedTx.Outputs = append(annotatedTx.Outputs, w.BuildAnnotatedOutput(tx, i))
105 // checkRelatedTransaction check related unconfirmed transaction.
106 func (w *Wallet) checkRelatedTransaction(tx *types.Tx) bool {
107 for _, v := range tx.Outputs {
109 sha3pool.Sum256(hash[:], v.ControlProgram())
110 cp, err := w.store.GetControlProgram(hash)
112 log.WithFields(log.Fields{"module": logModule, "err": err, "hash": string(hash[:])}).Error("checkRelatedTransaction fail.")
120 for _, v := range tx.Inputs {
121 outid, err := v.SpentOutputID()
125 if bytes := w.store.GetStandardUTXO(outid); bytes != nil {
132 // SaveUnconfirmedTx save unconfirmed annotated transaction to the database
133 func (w *Wallet) saveUnconfirmedTx(tx *types.Tx) error {
134 if !w.checkRelatedTransaction(tx) {
138 // annotate account and asset
139 annotatedTx := w.buildAnnotatedUnconfirmedTx(tx)
140 annotatedTxs := []*query.AnnotatedTx{}
141 annotatedTxs = append(annotatedTxs, annotatedTx)
142 annotateTxsAccount(annotatedTxs, w.store)
144 rawTx, err := json.Marshal(annotatedTxs[0])
149 w.store.SetUnconfirmedTransaction(tx.ID.String(), rawTx)
153 func (w *Wallet) delExpiredTxs() error {
154 AnnotatedTx, err := w.GetUnconfirmedTxs("")
158 for _, tx := range AnnotatedTx {
159 if time.Now().After(time.Unix(int64(tx.Timestamp), 0).Add(MaxUnconfirmedTxDuration)) {
160 w.store.DeleteUnconfirmedTransaction(tx.ID.String())
166 //delUnconfirmedTx periodically delete locally stored timeout did not confirm txs
167 func (w *Wallet) delUnconfirmedTx() {
168 if err := w.delExpiredTxs(); err != nil {
169 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("wallet fail on delUnconfirmedTx")
172 ticker := time.NewTicker(UnconfirmedTxCheckPeriod)
176 if err := w.delExpiredTxs(); err != nil {
177 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("wallet fail on delUnconfirmedTx")