github.com/codingfuture/orig-energi3@v0.8.4/energi/api/blacklist.go (about) 1 // Copyright 2019 The Energi Core Authors 2 // This file is part of the Energi Core library. 3 // 4 // The Energi Core library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The Energi Core library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>. 16 17 package api 18 19 import ( 20 "errors" 21 "math/big" 22 23 "github.com/ethereum/go-ethereum/accounts/abi/bind" 24 "github.com/ethereum/go-ethereum/common" 25 "github.com/ethereum/go-ethereum/common/hexutil" 26 "github.com/ethereum/go-ethereum/log" 27 28 energi_abi "energi.world/core/gen3/energi/abi" 29 energi_common "energi.world/core/gen3/energi/common" 30 energi_params "energi.world/core/gen3/energi/params" 31 ) 32 33 type BlacklistAPI struct { 34 backend Backend 35 infoCache *energi_common.CacheStorage 36 } 37 38 func NewBlacklistAPI(b Backend) *BlacklistAPI { 39 r := &BlacklistAPI{ 40 backend: b, 41 infoCache: energi_common.NewCacheStorage(), 42 } 43 b.OnSyncedHeadUpdates(func() { 44 r.BlacklistInfo() 45 }) 46 return r 47 } 48 49 const ( 50 blacklistCallGas uint64 = 3000000 51 ) 52 53 func (b *BlacklistAPI) registry( 54 password *string, 55 dst common.Address, 56 ) (session *energi_abi.IBlacklistRegistrySession, err error) { 57 contract, err := energi_abi.NewIBlacklistRegistry( 58 energi_params.Energi_BlacklistRegistry, b.backend.(bind.ContractBackend)) 59 if err != nil { 60 return nil, err 61 } 62 63 session = &energi_abi.IBlacklistRegistrySession{ 64 Contract: contract, 65 CallOpts: bind.CallOpts{ 66 From: dst, 67 GasLimit: energi_params.UnlimitedGas, 68 }, 69 TransactOpts: bind.TransactOpts{ 70 From: dst, 71 Signer: createSignerCallback(b.backend, password), 72 Value: common.Big0, 73 GasLimit: blacklistCallGas, 74 }, 75 } 76 return 77 } 78 79 type BLInfo struct { 80 Target common.Address 81 Enforce *ProposalInfo 82 Revoke *ProposalInfo 83 Drain *ProposalInfo 84 Blocked bool 85 } 86 87 func (b *BlacklistAPI) BlacklistInfo() (res []BLInfo, err error) { 88 data, err := b.infoCache.Get(b.backend, b.blacklistInfo) 89 if err != nil || data == nil { 90 log.Error("BlacklistInfo failed", "err", err) 91 return 92 } 93 94 res = data.([]BLInfo) 95 96 return 97 } 98 99 func (b *BlacklistAPI) blacklistInfo(num *big.Int) (interface{}, error) { 100 registry, err := energi_abi.NewIBlacklistRegistryCaller( 101 energi_params.Energi_BlacklistRegistry, b.backend.(bind.ContractCaller)) 102 if err != nil { 103 log.Error("Failed", "err", err) 104 return nil, err 105 } 106 107 call_opts := &bind.CallOpts{ 108 BlockNumber: num, 109 GasLimit: energi_params.UnlimitedGas, 110 } 111 addresses, err := registry.EnumerateAll(call_opts) 112 if err != nil { 113 log.Error("Failed", "err", err) 114 return nil, err 115 } 116 117 res := make([]BLInfo, 0, len(addresses)) 118 119 for _, addr := range addresses { 120 blocked, err := registry.IsBlacklisted(call_opts, addr) 121 if err != nil { 122 log.Warn("IsBlacklisted error", "addr", addr, "err", err) 123 continue 124 } 125 126 proposals, err := registry.Proposals(call_opts, addr) 127 if err != nil { 128 log.Warn("Proposals error", "addr", addr, "err", err) 129 continue 130 } 131 132 enforceInfo, err := proposalInfo(b.backend, proposals.Enforce) 133 if err != nil { 134 log.Warn("Enforce info error", "addr", addr, "err", err) 135 } 136 137 revokeInfo, err := proposalInfo(b.backend, proposals.Revoke) 138 if err != nil { 139 log.Warn("Revoke info error", "addr", addr, "err", err) 140 } 141 142 drainInfo, err := proposalInfo(b.backend, proposals.Drain) 143 if err != nil { 144 log.Warn("Drain info error", "addr", addr, "err", err) 145 } 146 147 res = append(res, BLInfo{ 148 Target: addr, 149 Enforce: enforceInfo, 150 Revoke: revokeInfo, 151 Drain: drainInfo, 152 Blocked: blocked, 153 }) 154 } 155 156 return res, nil 157 } 158 159 func (b *BlacklistAPI) BlacklistEnforce( 160 address common.Address, 161 fee *hexutil.Big, 162 payer common.Address, 163 password *string, 164 ) (txhash common.Hash, err error) { 165 registry, err := b.registry(password, payer) 166 if err != nil { 167 return 168 } 169 170 registry.TransactOpts.Value = fee.ToInt() 171 tx, err := registry.Propose(address) 172 173 if tx != nil { 174 txhash = tx.Hash() 175 log.Info("Note: please wait until the proposal TX gets into a block!", "tx", txhash.Hex()) 176 } 177 178 return 179 } 180 181 func (b *BlacklistAPI) BlacklistRevoke( 182 address common.Address, 183 fee *hexutil.Big, 184 payer common.Address, 185 password *string, 186 ) (txhash common.Hash, err error) { 187 registry, err := b.registry(password, payer) 188 if err != nil { 189 return 190 } 191 192 is_blacklisted, err := registry.IsBlacklisted(address) 193 if err != nil { 194 return 195 } 196 if !is_blacklisted { 197 err = errors.New("Not blocklisted") 198 return 199 } 200 201 registry.TransactOpts.Value = fee.ToInt() 202 tx, err := registry.ProposeRevoke(address) 203 204 if tx != nil { 205 txhash = tx.Hash() 206 log.Info("Note: please wait until the proposal TX gets into a block!", "tx", txhash.Hex()) 207 } 208 209 return 210 } 211 212 func (b *BlacklistAPI) BlacklistDrain( 213 address common.Address, 214 fee *hexutil.Big, 215 payer common.Address, 216 password *string, 217 ) (txhash common.Hash, err error) { 218 registry, err := b.registry(password, payer) 219 if err != nil { 220 return 221 } 222 223 is_blacklisted, err := registry.IsBlacklisted(address) 224 if err != nil { 225 return 226 } 227 if !is_blacklisted { 228 err = errors.New("Not blocklisted") 229 return 230 } 231 232 registry.TransactOpts.Value = fee.ToInt() 233 tx, err := registry.ProposeDrain(address) 234 235 if tx != nil { 236 txhash = tx.Hash() 237 log.Info("Note: please wait until the proposal TX gets into a block!", "tx", txhash.Hex()) 238 } 239 240 return 241 } 242 243 func (b *BlacklistAPI) BlacklistCollect( 244 target common.Address, 245 payer common.Address, 246 password *string, 247 ) (txhash common.Hash, err error) { 248 registry, err := b.registry(password, payer) 249 if err != nil { 250 return 251 } 252 253 tx, err := registry.Collect(target) 254 255 if tx != nil { 256 txhash = tx.Hash() 257 log.Info("Note: please wait until the collect TX gets into a block!", "tx", txhash.Hex()) 258 } 259 260 return 261 }