-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx_watcher.go
132 lines (113 loc) · 2.94 KB
/
tx_watcher.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
package watcher
import (
"sync"
"github.com/ethereum/go-ethereum/common"
sdk "github.com/evoblockchain/evochain/libs/cosmos-sdk/types"
"github.com/evoblockchain/evochain/libs/cosmos-sdk/x/auth"
)
// TxWatcher cache watch data when run tx
// Use Enabled() to check if watcher is enable when call methods of TxWatcher
type TxWatcher struct {
enable bool
staleBatch []WatchMessage
batch []WatchMessage
}
var watcherPool = sync.Pool{
New: func() interface{} {
return &TxWatcher{
enable: IsWatcherEnabled(),
}
},
}
func NewTxWatcher() *TxWatcher {
return watcherPool.Get().(*TxWatcher)
}
func (w *TxWatcher) Enabled() bool {
return w.enable
}
func (w *TxWatcher) SaveContractCode(addr common.Address, code []byte, height uint64) {
wMsg := NewMsgCode(addr, code, height)
if wMsg != nil {
w.staleBatch = append(w.staleBatch, wMsg)
}
}
func (w *TxWatcher) SaveContractCodeByHash(hash []byte, code []byte) {
wMsg := NewMsgCodeByHash(hash, code)
w.staleBatch = append(w.staleBatch, wMsg)
}
func (w *TxWatcher) SaveAccount(account interface{}) {
acc, ok := account.(auth.Account)
if !ok {
return
}
wMsg := NewMsgAccount(acc)
if wMsg != nil {
w.staleBatch = append(w.staleBatch, wMsg)
}
}
func (w *TxWatcher) DeleteAccount(account interface{}) {
acc, ok := account.(auth.Account)
if !ok {
return
}
wMsg := NewDelAccMsg(acc)
w.batch = append(w.batch, wMsg)
}
func (w *TxWatcher) SaveState(addr common.Address, key, value []byte) {
wMsg := NewMsgState(addr, key, value)
w.staleBatch = append(w.staleBatch, wMsg)
}
func (w *TxWatcher) SaveContractBlockedListItem(addr interface{}) {
realAddr, ok := addr.(sdk.AccAddress)
if !ok {
return
}
wMsg := NewMsgContractBlockedListItem(realAddr)
w.batch = append(w.batch, wMsg)
}
func (w *TxWatcher) SaveContractMethodBlockedListItem(addr interface{}, methods []byte) {
realAddr, ok := addr.(sdk.AccAddress)
if !ok {
return
}
wMsg := NewMsgContractMethodBlockedListItem(realAddr, methods)
w.batch = append(w.batch, wMsg)
}
func (w *TxWatcher) SaveContractDeploymentWhitelistItem(addr interface{}) {
realAddr, ok := addr.(sdk.AccAddress)
if !ok {
return
}
wMsg := NewMsgContractDeploymentWhitelistItem(realAddr)
w.batch = append(w.batch, wMsg)
}
func (w *TxWatcher) DeleteContractBlockedList(addr interface{}) {
realAddr, ok := addr.(sdk.AccAddress)
if !ok {
return
}
wMsg := NewMsgDelContractBlockedListItem(realAddr)
w.batch = append(w.batch, wMsg)
}
func (w *TxWatcher) DeleteContractDeploymentWhitelist(addr interface{}) {
realAddr, ok := addr.(sdk.AccAddress)
if !ok {
return
}
wMsg := NewMsgDelContractDeploymentWhitelistItem(realAddr)
w.batch = append(w.batch, wMsg)
}
func (w *TxWatcher) Finalize() {
if !w.Enabled() {
return
}
w.batch = append(w.batch, w.staleBatch...)
w.staleBatch = []WatchMessage{}
}
func (w *TxWatcher) Destruct() []WatchMessage {
batch := w.batch
w.staleBatch = []WatchMessage{}
w.batch = []WatchMessage{}
watcherPool.Put(w)
return batch
}