-
Notifications
You must be signed in to change notification settings - Fork 199
/
txInterceptorProcessor.go
84 lines (71 loc) · 2.69 KB
/
txInterceptorProcessor.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
package processor
import (
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go/process"
)
var _ process.InterceptorProcessor = (*TxInterceptorProcessor)(nil)
// TxInterceptorProcessor is the processor used when intercepting transactions
// (smart contract results, receipts, transaction) structs which satisfy TransactionHandler interface.
type TxInterceptorProcessor struct {
shardedPool ShardedPool
txValidator process.TxValidator
}
// NewTxInterceptorProcessor creates a new TxInterceptorProcessor instance
func NewTxInterceptorProcessor(argument *ArgTxInterceptorProcessor) (*TxInterceptorProcessor, error) {
if argument == nil {
return nil, process.ErrNilArgumentStruct
}
if check.IfNil(argument.ShardedDataCache) {
return nil, process.ErrNilDataPoolHolder
}
if check.IfNil(argument.TxValidator) {
return nil, process.ErrNilTxValidator
}
return &TxInterceptorProcessor{
shardedPool: argument.ShardedDataCache,
txValidator: argument.TxValidator,
}, nil
}
// Validate checks if the intercepted data can be processed
func (txip *TxInterceptorProcessor) Validate(data process.InterceptedData, _ core.PeerID) error {
interceptedTx, ok := data.(InterceptedTransactionHandler)
if !ok {
return process.ErrWrongTypeAssertion
}
return txip.txValidator.CheckTxValidity(interceptedTx)
}
// Save will save the received data into the cacher
func (txip *TxInterceptorProcessor) Save(data process.InterceptedData, _ core.PeerID, _ string) error {
interceptedTx, ok := data.(InterceptedTransactionHandler)
if !ok {
return process.ErrWrongTypeAssertion
}
err := txip.txValidator.CheckTxWhiteList(data)
if err != nil {
log.Trace(
"TxInterceptorProcessor.Save: not whitelisted cross transactions will not be added in pool",
"nonce", interceptedTx.Nonce(),
"sender address", interceptedTx.SenderAddress(),
"sender shard", interceptedTx.SenderShardId(),
"receiver shard", interceptedTx.ReceiverShardId(),
)
return nil
}
cacherIdentifier := process.ShardCacherIdentifier(interceptedTx.SenderShardId(), interceptedTx.ReceiverShardId())
txip.shardedPool.AddData(
data.Hash(),
interceptedTx.Transaction(),
interceptedTx.Transaction().Size(),
cacherIdentifier,
)
return nil
}
// RegisterHandler registers a callback function to be notified of incoming transactions
func (txip *TxInterceptorProcessor) RegisterHandler(_ func(topic string, hash []byte, data interface{})) {
log.Error("txInterceptorProcessor.RegisterHandler", "error", "not implemented")
}
// IsInterfaceNil returns true if there is no value under the interface
func (txip *TxInterceptorProcessor) IsInterfaceNil() bool {
return txip == nil
}