-
Notifications
You must be signed in to change notification settings - Fork 33
/
transaction.go
64 lines (53 loc) · 1.91 KB
/
transaction.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
package utility
import (
"encoding/hex"
"errors"
"github.com/dgraph-io/badger/v3"
"github.com/pokt-network/pocket/shared/codec"
coreTypes "github.com/pokt-network/pocket/shared/core/types"
"github.com/pokt-network/pocket/shared/crypto"
)
// HandleTransaction implements the exposed functionality of the shared utilityModule interface.
func (u *utilityModule) HandleTransaction(txProtoBytes []byte) error {
txHash := crypto.SHA3Hash(txProtoBytes)
// Is the tx already in the mempool (in memory)?
if u.mempool.Contains(hex.EncodeToString(txHash)) {
return coreTypes.ErrDuplicateTransaction()
}
// Is the tx already committed & indexed (on disk)?
txExists, err := u.GetBus().GetPersistenceModule().TransactionExists(txHash, txProtoBytes)
if err != nil {
return err
} else if txExists {
return coreTypes.ErrTransactionAlreadyCommitted()
}
// Can the tx be decoded?
tx := &coreTypes.Transaction{}
if err := codec.GetCodec().Unmarshal(txProtoBytes, tx); err != nil {
return coreTypes.ErrProtoUnmarshal(err)
}
// Does the tx pass basic validation?
if err := tx.ValidateBasic(); err != nil {
return err
}
// Store the tx in the mempool
return u.mempool.AddTx(txProtoBytes)
}
// GetIndexedTransaction implements the exposed functionality of the shared utilityModule interface.
func (u *utilityModule) GetIndexedTransaction(txProtoBytes []byte) (*coreTypes.IndexedTransaction, error) {
txHash := coreTypes.TxHash(txProtoBytes)
// TECHDEBT: Note the inconsistency between referencing tx hash as a string vs. byte slice in different places. Need to pick
// one and consolidate throughout the codebase
hash, err := hex.DecodeString(txHash)
if err != nil {
return nil, err
}
idTx, err := u.GetBus().GetPersistenceModule().GetTxIndexer().GetByHash(hash)
if err != nil {
if errors.Is(err, badger.ErrKeyNotFound) {
return nil, coreTypes.ErrTransactionNotCommitted()
}
return nil, err
}
return idTx, nil
}