/
dagdb_tx.go
executable file
·138 lines (130 loc) · 4.06 KB
/
dagdb_tx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
*
* This file is part of go-palletone.
* go-palletone is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* go-palletone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with go-palletone. If not, see <http://www.gnu.org/licenses/>.
* /
*
* * @author PalletOne core developer <dev@pallet.one>
* * @date 2018-2019
*
*/
package storage
import (
"errors"
"fmt"
"github.com/ethereum/go-ethereum/rlp"
"github.com/palletone/go-palletone/common"
"github.com/palletone/go-palletone/common/log"
"github.com/palletone/go-palletone/dag/constants"
"github.com/palletone/go-palletone/dag/modules"
)
/**
key: [TRANSACTION_PREFIX][tx hash]
value: transaction struct rlp encoding bytes
*/
func (dagdb *DagDb) SaveTransaction(tx *modules.Transaction) error {
// save transaction
txHash := tx.Hash()
//Save tx to db
key := append(constants.TRANSACTION_PREFIX, txHash.Bytes()...)
err := StoreToRlpBytes(dagdb.db, key, tx)
if err != nil {
log.Errorf("Save tx[%s] error:%s", txHash.Str(), err.Error())
return err
}
log.DebugDynamic(func() string {
return fmt.Sprintf("Save tx[%s] to db", txHash.String())
})
//Save reqid
if tx.IsContractTx() {
if err := dagdb.saveReqIdByTx(tx); err != nil {
log.Error("SaveReqIdByTx is failed,", "error", err)
}
}
return nil
}
func (dagdb *DagDb) saveReqIdByTx(tx *modules.Transaction) error {
txhash := tx.Hash()
reqid := tx.RequestHash()
if txhash == reqid {
return nil
}
log.Debugf("Save RequestId[%s] map to TxId[%s]", reqid.String(), txhash.String())
key := append(constants.REQID_TXID_PREFIX, reqid.Bytes()...)
return dagdb.db.Put(key, txhash.Bytes())
}
func (dagdb *DagDb) GetAllTxs() ([]*modules.Transaction, error) {
kvs := getprefix(dagdb.db, constants.TRANSACTION_PREFIX)
result := make([]*modules.Transaction, 0, len(kvs))
for k, v := range kvs {
tx := new(modules.Transaction)
err := rlp.DecodeBytes(v, tx)
if err != nil {
log.Errorf("Cannot decode key[%s] rlp tx:%x", k, v)
return nil, err
}
result = append(result, tx)
}
return result, nil
}
// GetTransactionOnly can get a transaction by hash.
func (dagdb *DagDb) GetTransactionOnly(hash common.Hash) (*modules.Transaction, error) {
if hash == (common.Hash{}) {
return nil, errors.New("hash is not exist.")
}
tx := new(modules.Transaction)
key := append(constants.TRANSACTION_PREFIX, hash.Bytes()...)
err := RetrieveFromRlpBytes(dagdb.db, key, tx)
if err != nil {
log.Warn("get transaction failed.", "tx_hash", hash.String(), "error", err)
return nil, err
}
return tx, nil
}
func (dagdb *DagDb) IsTransactionExist(hash common.Hash) (bool, error) {
key := append(constants.TRANSACTION_PREFIX, hash.Bytes()...)
exist, err := dagdb.db.Has(key)
if err != nil {
log.Warnf("Check tx is exist throw error:%s", err.Error())
return false, err
}
return exist, nil
}
func (dagdb *DagDb) GetTxHashByReqId(reqid common.Hash) (common.Hash, error) {
key := append(constants.REQID_TXID_PREFIX, reqid.Bytes()...)
txid := common.Hash{}
val, err := dagdb.db.Get(key)
if err != nil {
return txid, err
}
txid.SetBytes(val)
return txid, err
}
func (dagdb *DagDb) ForEachAllTxDo(txAction func(key []byte, transaction *modules.Transaction) error) error {
iter := dagdb.db.NewIteratorWithPrefix(constants.TRANSACTION_PREFIX)
for iter.Next() {
key := iter.Key()
value := iter.Value()
tx := new(modules.Transaction)
err := rlp.DecodeBytes(value, tx)
if err != nil {
log.Errorf("Cannot decode key[%s] rlp tx:%x", key, value)
return err
}
err = txAction(key, tx)
if err != nil {
log.Errorf("tx[%s] action error:%s", tx.Hash().String(), err.Error())
return err
}
}
return nil
}