github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/evm/watcher/tx_watcher.go (about)

     1  package watcher
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/ethereum/go-ethereum/common"
     7  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     8  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth"
     9  )
    10  
    11  // TxWatcher cache watch data when run tx
    12  // Use Enabled() to check if watcher is enable when call methods of TxWatcher
    13  type TxWatcher struct {
    14  	enable     bool
    15  	staleBatch []WatchMessage
    16  	batch      []WatchMessage
    17  }
    18  
    19  var watcherPool = sync.Pool{
    20  	New: func() interface{} {
    21  		return &TxWatcher{
    22  			enable: IsWatcherEnabled(),
    23  		}
    24  	},
    25  }
    26  
    27  func NewTxWatcher() *TxWatcher {
    28  	return watcherPool.Get().(*TxWatcher)
    29  }
    30  
    31  func (w *TxWatcher) Enabled() bool {
    32  	return w.enable
    33  }
    34  
    35  func (w *TxWatcher) SaveContractCode(addr common.Address, code []byte, height uint64) {
    36  	wMsg := NewMsgCode(addr, code, height)
    37  	if wMsg != nil {
    38  		w.staleBatch = append(w.staleBatch, wMsg)
    39  	}
    40  }
    41  
    42  func (w *TxWatcher) SaveContractCodeByHash(hash []byte, code []byte) {
    43  	wMsg := NewMsgCodeByHash(hash, code)
    44  	w.staleBatch = append(w.staleBatch, wMsg)
    45  }
    46  
    47  func (w *TxWatcher) SaveAccount(account interface{}) {
    48  	acc, ok := account.(auth.Account)
    49  	if !ok {
    50  		return
    51  	}
    52  	wMsg := NewMsgAccount(acc)
    53  	if wMsg != nil {
    54  		w.staleBatch = append(w.staleBatch, wMsg)
    55  	}
    56  }
    57  
    58  func (w *TxWatcher) DeleteAccount(account interface{}) {
    59  	acc, ok := account.(auth.Account)
    60  	if !ok {
    61  		return
    62  	}
    63  	wMsg := NewDelAccMsg(acc)
    64  	w.batch = append(w.batch, wMsg)
    65  
    66  }
    67  
    68  func (w *TxWatcher) SaveState(addr common.Address, key, value []byte) {
    69  	wMsg := NewMsgState(addr, key, value)
    70  	w.staleBatch = append(w.staleBatch, wMsg)
    71  }
    72  
    73  func (w *TxWatcher) SaveContractBlockedListItem(addr interface{}) {
    74  	realAddr, ok := addr.(sdk.AccAddress)
    75  	if !ok {
    76  		return
    77  	}
    78  	wMsg := NewMsgContractBlockedListItem(realAddr)
    79  	w.batch = append(w.batch, wMsg)
    80  }
    81  
    82  func (w *TxWatcher) SaveContractMethodBlockedListItem(addr interface{}, methods []byte) {
    83  	realAddr, ok := addr.(sdk.AccAddress)
    84  	if !ok {
    85  		return
    86  	}
    87  	wMsg := NewMsgContractMethodBlockedListItem(realAddr, methods)
    88  	w.batch = append(w.batch, wMsg)
    89  }
    90  
    91  func (w *TxWatcher) SaveContractDeploymentWhitelistItem(addr interface{}) {
    92  	realAddr, ok := addr.(sdk.AccAddress)
    93  	if !ok {
    94  		return
    95  	}
    96  	wMsg := NewMsgContractDeploymentWhitelistItem(realAddr)
    97  	w.batch = append(w.batch, wMsg)
    98  }
    99  
   100  func (w *TxWatcher) DeleteContractBlockedList(addr interface{}) {
   101  	realAddr, ok := addr.(sdk.AccAddress)
   102  	if !ok {
   103  		return
   104  	}
   105  	wMsg := NewMsgDelContractBlockedListItem(realAddr)
   106  	w.batch = append(w.batch, wMsg)
   107  }
   108  
   109  func (w *TxWatcher) DeleteContractDeploymentWhitelist(addr interface{}) {
   110  	realAddr, ok := addr.(sdk.AccAddress)
   111  	if !ok {
   112  		return
   113  	}
   114  	wMsg := NewMsgDelContractDeploymentWhitelistItem(realAddr)
   115  	w.batch = append(w.batch, wMsg)
   116  }
   117  
   118  func (w *TxWatcher) Finalize() {
   119  	if !w.Enabled() {
   120  		return
   121  	}
   122  	w.batch = append(w.batch, w.staleBatch...)
   123  	w.staleBatch = []WatchMessage{}
   124  }
   125  
   126  func (w *TxWatcher) Destruct() []WatchMessage {
   127  	batch := w.batch
   128  	w.staleBatch = []WatchMessage{}
   129  	w.batch = []WatchMessage{}
   130  	watcherPool.Put(w)
   131  	return batch
   132  }