-
Notifications
You must be signed in to change notification settings - Fork 199
/
process.go
157 lines (128 loc) · 4.03 KB
/
process.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package transactionLog
import (
"encoding/hex"
"sync"
logger "github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/data"
"github.com/ElrondNetwork/elrond-go/data/transaction"
"github.com/ElrondNetwork/elrond-go/marshal"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/storage"
vmcommon "github.com/ElrondNetwork/elrond-vm-common"
)
var _ process.TransactionLogProcessor = (*txLogProcessor)(nil)
var log = logger.GetOrCreate("process/transactionLog")
// ArgTxLogProcessor defines the arguments needed for transaction logs processor
type ArgTxLogProcessor struct {
Storer storage.Storer
Marshalizer marshal.Marshalizer
}
type txLogProcessor struct {
storeLogsInCache bool
logs map[string]*transaction.Log
mut sync.RWMutex
storer storage.Storer
marshalizer marshal.Marshalizer
}
// NewTxLogProcessor creates a transaction log processor capable of parsing logs from the VM
// and saving them into the injected storage
func NewTxLogProcessor(args ArgTxLogProcessor) (*txLogProcessor, error) {
if check.IfNil(args.Storer) {
return nil, process.ErrNilStore
}
if check.IfNil(args.Marshalizer) {
return nil, process.ErrNilMarshalizer
}
return &txLogProcessor{
storer: args.Storer,
marshalizer: args.Marshalizer,
logs: make(map[string]*transaction.Log),
storeLogsInCache: false,
mut: sync.RWMutex{},
}, nil
}
// GetLog retrieves a log generated by a transaction
func (tlp *txLogProcessor) GetLog(txHash []byte) (data.LogHandler, error) {
txLogBuff, err := tlp.storer.Get(txHash)
if err != nil {
return nil, process.ErrLogNotFound
}
var txLog data.LogHandler
err = tlp.marshalizer.Unmarshal(txLog, txLogBuff)
if err != nil {
return nil, err
}
return txLog, nil
}
// GetLogFromCache retrieves a log generated by a transaction from ram
func (tlp *txLogProcessor) GetLogFromCache(txHash []byte) (data.LogHandler, bool) {
tlp.mut.RLock()
defer tlp.mut.RUnlock()
if txLog, ok := tlp.logs[string(txHash)]; ok {
return txLog, true
}
return nil, false
}
// EnableLogToBeSavedInCache will set a flag with true and txLogProcessor will start saving transactions logs also in RAM
func (tlp *txLogProcessor) EnableLogToBeSavedInCache() {
tlp.storeLogsInCache = true
}
// Clean will remove all transaction logs from RAM memory
func (tlp *txLogProcessor) Clean() {
tlp.mut.Lock()
tlp.logs = make(map[string]*transaction.Log)
tlp.mut.Unlock()
}
// SaveLog takes the VM logs and saves them into the correct format in storage
func (tlp *txLogProcessor) SaveLog(txHash []byte, tx data.TransactionHandler, logEntries []*vmcommon.LogEntry) error {
if len(txHash) == 0 {
return process.ErrNilTxHash
}
if check.IfNil(tx) {
return process.ErrNilTransaction
}
if len(logEntries) == 0 {
log.Trace("txLogProcessor.SaveLog()",
"txHash", hex.EncodeToString(txHash),
"message", "no logEntries provided",
)
return nil
}
txLog := &transaction.Log{
Address: getLogAddressByTx(tx),
}
for _, logEntry := range logEntries {
txLog.Events = append(txLog.Events, &transaction.Event{
Identifier: logEntry.Identifier,
Address: logEntry.Address,
Topics: logEntry.Topics,
Data: logEntry.Data,
})
}
tlp.saveLogToCache(txHash, txLog)
buff, err := tlp.marshalizer.Marshal(txLog)
if err != nil {
return err
}
return tlp.storer.Put(txHash, buff)
}
func (tlp *txLogProcessor) saveLogToCache(txHash []byte, log *transaction.Log) {
if tlp.storeLogsInCache {
tlp.mut.Lock()
tlp.logs[string(txHash)] = log
tlp.mut.Unlock()
}
}
// For SC deployment transactions, we use the sender address
func getLogAddressByTx(tx data.TransactionHandler) []byte {
if core.IsEmptyAddress(tx.GetRcvAddr()) {
return tx.GetSndAddr()
}
return tx.GetRcvAddr()
}
// IsInterfaceNil returns true if there is no value under the interface
func (tlp *txLogProcessor) IsInterfaceNil() bool {
return tlp == nil
}