github.com/klaytn/klaytn@v1.12.1/node/sc/api_bridge.go (about) 1 // Copyright 2019 The klaytn Authors 2 // This file is part of the klaytn library. 3 // 4 // The klaytn 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 klaytn 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 klaytn library. If not, see <http://www.gnu.org/licenses/>. 16 17 package sc 18 19 import ( 20 "context" 21 "fmt" 22 "math/big" 23 "strings" 24 25 "github.com/klaytn/klaytn/blockchain/types" 26 "github.com/klaytn/klaytn/common" 27 "github.com/klaytn/klaytn/contracts/bridge" 28 "github.com/klaytn/klaytn/networks/p2p" 29 "github.com/klaytn/klaytn/networks/p2p/discover" 30 "github.com/klaytn/klaytn/node" 31 "github.com/klaytn/klaytn/params" 32 "github.com/pkg/errors" 33 ) 34 35 var ( 36 ErrInvalidBridgePair = errors.New("Invalid bridge pair") 37 ErrBridgeContractVersionMismatch = errors.New("Bridge contract version mismatch") 38 ) 39 40 func parseBridgeAddrWithAlias(sb *SubBridge, cBridgeAddrOrAlias, pBridgeAddrOrFirstParam string, args ...interface{}) (common.Address, common.Address, []interface{}, error) { 41 if !strings.HasPrefix(cBridgeAddrOrAlias, "0x") { 42 // Takes pBridgeAddr as the first API argument and append residual arguments. 43 var newArgs []interface{} 44 if len(args) > 0 { 45 newArgs = append([]interface{}{pBridgeAddrOrFirstParam}, args[:len(args)-1]...) 46 } 47 cBridgeAddr, pBridgeAddr, err := sb.bridgeManager.getAddrByAlias(cBridgeAddrOrAlias) 48 return cBridgeAddr, pBridgeAddr, newArgs, err 49 } else { 50 return common.HexToAddress(cBridgeAddrOrAlias), common.HexToAddress(pBridgeAddrOrFirstParam), args, nil 51 } 52 } 53 54 func stringDeref(str *string) string { 55 if str == nil { 56 return "" 57 } 58 return *str 59 } 60 61 // MainBridgeAPI Implementation for main-bridge node 62 type MainBridgeAPI struct { 63 mainBridge *MainBridge 64 } 65 66 func (mb *MainBridgeAPI) GetChildChainIndexingEnabled() bool { 67 return mb.mainBridge.eventhandler.GetChildChainIndexingEnabled() 68 } 69 70 func (mb *MainBridgeAPI) ConvertChildChainBlockHashToParentChainTxHash(scBlockHash common.Hash) common.Hash { 71 return mb.mainBridge.eventhandler.ConvertChildChainBlockHashToParentChainTxHash(scBlockHash) 72 } 73 74 // Peers retrieves all the information we know about each individual peer at the 75 // protocol granularity. 76 func (mb *MainBridgeAPI) Peers() ([]*p2p.PeerInfo, error) { 77 server := mb.mainBridge.bridgeServer 78 if server == nil { 79 return nil, node.ErrNodeStopped 80 } 81 return server.PeersInfo(), nil 82 } 83 84 // NodeInfo retrieves all the information we know about the host node at the 85 // protocol granularity. 86 func (mb *MainBridgeAPI) NodeInfo() (*p2p.NodeInfo, error) { 87 server := mb.mainBridge.bridgeServer 88 if server == nil { 89 return nil, node.ErrNodeStopped 90 } 91 return server.NodeInfo(), nil 92 } 93 94 // SubBridgeAPI Implementation for sub-bridge node 95 type SubBridgeAPI struct { 96 subBridge *SubBridge 97 } 98 99 func (sb *SubBridgeAPI) ConvertChildChainBlockHashToParentChainTxHash(cBlockHash common.Hash) common.Hash { 100 return sb.subBridge.eventhandler.ConvertChildChainBlockHashToParentChainTxHash(cBlockHash) 101 } 102 103 func (sb *SubBridgeAPI) GetLatestAnchoredBlockNumber() uint64 { 104 return sb.subBridge.handler.GetLatestAnchoredBlockNumber() 105 } 106 107 func (sb *SubBridgeAPI) GetReceiptFromParentChain(blockHash common.Hash) *types.Receipt { 108 return sb.subBridge.handler.GetReceiptFromParentChain(blockHash) 109 } 110 111 func (sb *SubBridgeAPI) GetAnchoringTxHashByBlockNumber(bn uint64) common.Hash { 112 block := sb.subBridge.blockchain.GetBlockByNumber(bn) 113 if block == nil { 114 return common.Hash{} 115 } 116 receipt := sb.subBridge.handler.GetReceiptFromParentChain(block.Hash()) 117 if receipt == nil { 118 return common.Hash{} 119 } 120 return receipt.TxHash 121 } 122 123 func (sb *SubBridgeAPI) RegisterOperator(bridgeAddr, operatorAddr common.Address) (common.Hash, error) { 124 return sb.subBridge.bridgeManager.RegisterOperator(bridgeAddr, operatorAddr) 125 } 126 127 func (sb *SubBridgeAPI) GetRegisteredOperators(bridgeAddr common.Address) ([]common.Address, error) { 128 return sb.subBridge.bridgeManager.GetOperators(bridgeAddr) 129 } 130 131 func (sb *SubBridgeAPI) SetValueTransferOperatorThreshold(bridgeAddr common.Address, threshold uint8) (common.Hash, error) { 132 return sb.subBridge.bridgeManager.SetValueTransferOperatorThreshold(bridgeAddr, threshold) 133 } 134 135 func (sb *SubBridgeAPI) GetValueTransferOperatorThreshold(bridgeAddr common.Address) (uint8, error) { 136 return sb.subBridge.bridgeManager.GetValueTransferOperatorThreshold(bridgeAddr) 137 } 138 139 func (sb *SubBridgeAPI) DeployBridge() ([]common.Address, error) { 140 cAcc := sb.subBridge.bridgeAccounts.cAccount 141 pAcc := sb.subBridge.bridgeAccounts.pAccount 142 143 cBridge, cBridgeAddr, err := sb.subBridge.bridgeManager.DeployBridge(cAcc.GenerateTransactOpts(), sb.subBridge.localBackend, true) 144 if err != nil { 145 logger.Error("Failed to deploy child bridge.", "err", err) 146 return nil, err 147 } 148 pBridge, pBridgeAddr, err := sb.subBridge.bridgeManager.DeployBridge(pAcc.GenerateTransactOpts(), sb.subBridge.remoteBackend, false) 149 if err != nil { 150 logger.Error("Failed to deploy parent bridge.", "err", err) 151 return nil, err 152 } 153 154 err = sb.subBridge.bridgeManager.SetBridgeInfo(cBridgeAddr, cBridge, pBridgeAddr, pBridge, cAcc, true, false) 155 if err != nil { 156 return nil, err 157 } 158 159 err = sb.subBridge.bridgeManager.SetBridgeInfo(pBridgeAddr, pBridge, cBridgeAddr, cBridge, pAcc, false, false) 160 if err != nil { 161 return nil, err 162 } 163 164 err = sb.subBridge.bridgeManager.SetJournal("", cBridgeAddr, pBridgeAddr) 165 if err != nil { 166 return nil, err 167 } 168 169 return []common.Address{cBridgeAddr, pBridgeAddr}, nil 170 } 171 172 // doSubscribeBridge enables the given child/parent chain bridges to subscribe the events. 173 func (sb *SubBridgeAPI) doSubscribeBridge(cBridgeAddr, pBridgeAddr common.Address) error { 174 if !sb.subBridge.bridgeManager.IsValidBridgePair(cBridgeAddr, pBridgeAddr) { 175 return ErrInvalidBridgePair 176 } 177 178 err := sb.subBridge.bridgeManager.SubscribeEvent(cBridgeAddr) 179 if err != nil { 180 logger.Error("Failed to SubscribeEvent child bridge", "addr", cBridgeAddr, "err", err) 181 return err 182 } 183 184 err = sb.subBridge.bridgeManager.SubscribeEvent(pBridgeAddr) 185 if err != nil { 186 logger.Error("Failed to SubscribeEvent parent bridge", "addr", pBridgeAddr, "err", err) 187 sb.subBridge.bridgeManager.UnsubscribeEvent(cBridgeAddr) 188 return err 189 } 190 191 sb.subBridge.bridgeManager.journal.cacheMu.Lock() 192 sb.subBridge.bridgeManager.journal.cache[cBridgeAddr].Subscribed = true 193 sb.subBridge.bridgeManager.journal.cacheMu.Unlock() 194 195 // Update the journal's subscribed flag. 196 sb.subBridge.bridgeManager.journal.rotate(sb.subBridge.bridgeManager.GetAllBridge()) 197 198 err = sb.subBridge.bridgeManager.AddRecovery(cBridgeAddr, pBridgeAddr) 199 if err != nil { 200 sb.subBridge.bridgeManager.UnsubscribeEvent(cBridgeAddr) 201 sb.subBridge.bridgeManager.UnsubscribeEvent(pBridgeAddr) 202 return err 203 } 204 return nil 205 } 206 207 func (sb *SubBridgeAPI) SubscribeBridge(cBridgeAddrOrAlias, pBridgeAddrOrEmpty *string) error { 208 cBridgeAddrOrAliasStr, pBridgeAddrOrEmptyStr := stringDeref(cBridgeAddrOrAlias), stringDeref(pBridgeAddrOrEmpty) 209 cBridgeAddr, pBridgeAddr, _, err := parseBridgeAddrWithAlias(sb.subBridge, cBridgeAddrOrAliasStr, pBridgeAddrOrEmptyStr) 210 if err != nil { 211 return err 212 } 213 return sb.doSubscribeBridge(cBridgeAddr, pBridgeAddr) 214 } 215 216 // doUnsubscribeBridge disables the event subscription of the given child/parent chain bridges. 217 func (sb *SubBridgeAPI) doUnsubscribeBridge(cBridgeAddr, pBridgeAddr common.Address) error { 218 if !sb.subBridge.bridgeManager.IsValidBridgePair(cBridgeAddr, pBridgeAddr) { 219 return ErrInvalidBridgePair 220 } 221 222 sb.subBridge.bridgeManager.UnsubscribeEvent(cBridgeAddr) 223 sb.subBridge.bridgeManager.UnsubscribeEvent(pBridgeAddr) 224 225 sb.subBridge.bridgeManager.journal.cacheMu.Lock() 226 sb.subBridge.bridgeManager.journal.cache[cBridgeAddr].Subscribed = false 227 sb.subBridge.bridgeManager.journal.cacheMu.Unlock() 228 229 sb.subBridge.bridgeManager.journal.rotate(sb.subBridge.bridgeManager.GetAllBridge()) 230 return nil 231 } 232 233 func (sb *SubBridgeAPI) UnsubscribeBridge(cBridgeAddrOrAlias, pBridgeAddrOrEmpty *string) error { 234 cBridgeAddrOrAliasStr, pBridgeAddrOrEmptyStr := stringDeref(cBridgeAddrOrAlias), stringDeref(pBridgeAddrOrEmpty) 235 cBridgeAddr, pBridgeAddr, _, err := parseBridgeAddrWithAlias(sb.subBridge, cBridgeAddrOrAliasStr, pBridgeAddrOrEmptyStr) 236 if err != nil { 237 return err 238 } 239 return sb.doUnsubscribeBridge(cBridgeAddr, pBridgeAddr) 240 } 241 242 func (sb *SubBridgeAPI) ConvertRequestTxHashToHandleTxHash(hash common.Hash) common.Hash { 243 return sb.subBridge.chainDB.ReadHandleTxHashFromRequestTxHash(hash) 244 } 245 246 func (sb *SubBridgeAPI) TxPendingCount() int { 247 return sb.subBridge.GetBridgeTxPool().Stats() 248 } 249 250 func (sb *SubBridgeAPI) TxPending() map[common.Address]types.Transactions { 251 return sb.subBridge.GetBridgeTxPool().Pending() 252 } 253 254 func (sb *SubBridgeAPI) ListBridge() []*BridgeJournal { 255 return sb.subBridge.bridgeManager.GetAllBridge() 256 } 257 258 func (sb *SubBridgeAPI) GetBridgePairByAlias(bridgeAlias string) *BridgeJournal { 259 return sb.subBridge.bridgeManager.GetBridge(bridgeAlias) 260 } 261 262 func (sb *SubBridgeAPI) ChangeBridgeAlias(oldAlias, newAlias string) error { 263 bm := sb.subBridge.bridgeManager 264 if err := bm.journal.ChangeBridgeAlias(oldAlias, newAlias); err != nil { 265 return err 266 } 267 return bm.journal.rotate(bm.GetAllBridge()) 268 } 269 270 func (sb *SubBridgeAPI) GetBridgeInformation(bridgeAddr common.Address) (map[string]interface{}, error) { 271 if ctBridge := sb.subBridge.bridgeManager.GetCounterPartBridgeAddr(bridgeAddr); ctBridge == (common.Address{}) { 272 return nil, ErrInvalidBridgePair 273 } 274 275 bi, ok := sb.subBridge.bridgeManager.GetBridgeInfo(bridgeAddr) 276 if !ok { 277 return nil, ErrNoBridgeInfo 278 } 279 280 bi.UpdateInfo() 281 282 return map[string]interface{}{ 283 "isRunning": bi.isRunning, 284 "requestNonce": bi.requestNonceFromCounterPart, 285 "handleNonce": bi.handleNonce, 286 "lowerHandleNonce": bi.lowerHandleNonce, 287 "counterPart": bi.counterpartAddress, 288 "onServiceChain": bi.onChildChain, 289 "isSubscribed": bi.subscribed, 290 "pendingEventSize": bi.pendingRequestEvent.Len(), 291 }, nil 292 } 293 294 func (sb *SubBridgeAPI) KASAnchor(blkNum uint64) error { 295 block := sb.subBridge.blockchain.GetBlockByNumber(blkNum) 296 if block != nil { 297 if err := sb.subBridge.kasAnchor.AnchorBlock(block); err != nil { 298 logger.Error("Failed to anchor a block via KAS", "blkNum", block.NumberU64(), "err", err) 299 return err 300 } 301 return nil 302 } 303 return ErrInvalidBlock 304 } 305 306 func (sb *SubBridgeAPI) Anchoring(flag bool) bool { 307 return sb.subBridge.SetAnchoringTx(flag) 308 } 309 310 func (sb *SubBridgeAPI) GetAnchoring() bool { 311 return sb.subBridge.GetAnchoringTx() 312 } 313 314 func (sb *SubBridgeAPI) doRegisterBridge(cBridgeAddr common.Address, pBridgeAddr common.Address) error { 315 cBridge, err := bridge.NewBridge(cBridgeAddr, sb.subBridge.localBackend) 316 if err != nil { 317 return err 318 } 319 pBridge, err := bridge.NewBridge(pBridgeAddr, sb.subBridge.remoteBackend) 320 if err != nil { 321 return err 322 } 323 324 bm := sb.subBridge.bridgeManager 325 err = bm.SetBridgeInfo(cBridgeAddr, cBridge, pBridgeAddr, pBridge, sb.subBridge.bridgeAccounts.cAccount, true, false) 326 if err != nil { 327 return err 328 } 329 err = bm.SetBridgeInfo(pBridgeAddr, pBridge, cBridgeAddr, cBridge, sb.subBridge.bridgeAccounts.pAccount, false, false) 330 if err != nil { 331 bm.DeleteBridgeInfo(cBridgeAddr) 332 return err 333 } 334 return nil 335 } 336 337 func (sb *SubBridgeAPI) RegisterBridge(cBridgeAddr, pBridgeAddr common.Address, bridgeAliasP *string) error { 338 bridgeAlias := stringDeref(bridgeAliasP) 339 if err := sb.subBridge.bridgeManager.SetJournal(bridgeAlias, cBridgeAddr, pBridgeAddr); err != nil { 340 return err 341 } 342 return sb.doRegisterBridge(cBridgeAddr, pBridgeAddr) 343 } 344 345 func (sb *SubBridgeAPI) doDeregisterBridge(cBridgeAddr common.Address, pBridgeAddr common.Address) error { 346 if !sb.subBridge.bridgeManager.IsValidBridgePair(cBridgeAddr, pBridgeAddr) { 347 return ErrInvalidBridgePair 348 } 349 350 bm := sb.subBridge.bridgeManager 351 bm.journal.cacheMu.Lock() 352 journal := bm.journal.cache[cBridgeAddr] 353 354 if journal.Subscribed { 355 bm.UnsubscribeEvent(journal.ChildAddress) 356 bm.UnsubscribeEvent(journal.ParentAddress) 357 358 bm.DeleteRecovery(cBridgeAddr, pBridgeAddr) 359 } 360 361 delete(bm.journal.cache, cBridgeAddr) 362 bm.journal.cacheMu.Unlock() 363 364 if err := bm.journal.rotate(bm.GetAllBridge()); err != nil { 365 logger.Warn("failed to rotate bridge journal", "err", err, "cBridge", cBridgeAddr.String(), "pBridge", pBridgeAddr.String()) 366 } 367 368 if err := bm.DeleteBridgeInfo(cBridgeAddr); err != nil { 369 logger.Warn("failed to Delete child chain bridge info", "err", err, "bridge", cBridgeAddr.String()) 370 } 371 372 if err := bm.DeleteBridgeInfo(pBridgeAddr); err != nil { 373 logger.Warn("failed to Delete parent chain bridge info", "err", err, "bridge", pBridgeAddr.String()) 374 } 375 return nil 376 } 377 378 func (sb *SubBridgeAPI) DeregisterBridge(cBridgeAddrOrAlias, pBridgeAddrOrEmpty *string) error { 379 cBridgeAddrOrAliasStr, pBridgeAddrOrEmptyStr := stringDeref(cBridgeAddrOrAlias), stringDeref(pBridgeAddrOrEmpty) 380 cBridgeAddr, pBridgeAddr, _, err := parseBridgeAddrWithAlias(sb.subBridge, cBridgeAddrOrAliasStr, pBridgeAddrOrEmptyStr) 381 if err != nil { 382 return err 383 } 384 sb.subBridge.bridgeManager.journal.cacheMu.Lock() 385 delete(sb.subBridge.bridgeManager.journal.aliasCache, cBridgeAddrOrAliasStr) 386 sb.subBridge.bridgeManager.journal.cacheMu.Unlock() 387 return sb.doDeregisterBridge(cBridgeAddr, pBridgeAddr) 388 } 389 390 func (sb *SubBridgeAPI) doRegisterToken(cBridgeAddr, pBridgeAddr, cTokenAddr, pTokenAddr common.Address) error { 391 if !sb.subBridge.bridgeManager.IsValidBridgePair(cBridgeAddr, pBridgeAddr) { 392 return ErrInvalidBridgePair 393 } 394 395 cBi, cExist := sb.subBridge.bridgeManager.GetBridgeInfo(cBridgeAddr) 396 pBi, pExist := sb.subBridge.bridgeManager.GetBridgeInfo(pBridgeAddr) 397 398 if !cExist || !pExist { 399 return ErrNoBridgeInfo 400 } 401 402 err := cBi.RegisterToken(cTokenAddr, pTokenAddr) 403 if err != nil { 404 return err 405 } 406 407 err = pBi.RegisterToken(pTokenAddr, cTokenAddr) 408 if err != nil { 409 return err 410 } 411 412 cBi.account.Lock() 413 tx, err := cBi.bridge.RegisterToken(cBi.account.GenerateTransactOpts(), cTokenAddr, pTokenAddr) 414 if err != nil { 415 cBi.DeregisterToken(cTokenAddr, pTokenAddr) 416 cBi.account.UnLock() 417 return err 418 } 419 cBi.account.IncNonce() 420 cBi.account.UnLock() 421 logger.Debug("cBridge registered token", "txHash", tx.Hash().String(), "cToken", cTokenAddr.String(), "pToken", pTokenAddr.String()) 422 423 pBi.account.Lock() 424 tx, err = pBi.bridge.RegisterToken(pBi.account.GenerateTransactOpts(), pTokenAddr, cTokenAddr) 425 if err != nil { 426 pBi.DeregisterToken(pTokenAddr, cTokenAddr) 427 pBi.account.UnLock() 428 return err 429 } 430 pBi.account.IncNonce() 431 pBi.account.UnLock() 432 logger.Debug("pBridge registered token", "txHash", tx.Hash().String(), "cToken", cTokenAddr.String(), "pToken", pTokenAddr.String()) 433 434 logger.Info("Register token", "cToken", cTokenAddr.String(), "pToken", pTokenAddr.String()) 435 return nil 436 } 437 438 func (sb *SubBridgeAPI) RegisterToken(cBridgeAddrOrAlias, pBridgeOrChildToken, cTokenAddrOrPtokenAddr, pTokenAddrOrEmpty *string) error { 439 cBridgeAddrOrAliasStr, pBridgeAddrOrChildTokenStr, cTokenAddrOrPtokenAddrStr, pTokenAddrOrEmptyStr := stringDeref(cBridgeAddrOrAlias), stringDeref(pBridgeOrChildToken), stringDeref(cTokenAddrOrPtokenAddr), stringDeref(pTokenAddrOrEmpty) 440 cBridgeAddr, pBridgeAddr, args, err := parseBridgeAddrWithAlias(sb.subBridge, cBridgeAddrOrAliasStr, pBridgeAddrOrChildTokenStr, cTokenAddrOrPtokenAddrStr, pTokenAddrOrEmptyStr) 441 if err != nil { 442 return err 443 } 444 cTokenAddr, pTokenAddr := common.HexToAddress(args[0].(string)), common.HexToAddress(args[1].(string)) 445 return sb.doRegisterToken(cBridgeAddr, pBridgeAddr, cTokenAddr, pTokenAddr) 446 } 447 448 func (sb *SubBridgeAPI) GetParentTransactionReceipt(txHash common.Hash) (map[string]interface{}, error) { 449 ctx := context.Background() 450 return sb.subBridge.remoteBackend.(RemoteBackendInterface).TransactionReceiptRpcOutput(ctx, txHash) 451 } 452 453 func (sb *SubBridgeAPI) SetERC20Fee(bridgeAddr, tokenAddr common.Address, fee *big.Int) (common.Hash, error) { 454 return sb.subBridge.bridgeManager.SetERC20Fee(bridgeAddr, tokenAddr, fee) 455 } 456 457 func (sb *SubBridgeAPI) SetKLAYFee(bridgeAddr common.Address, fee *big.Int) (common.Hash, error) { 458 return sb.subBridge.bridgeManager.SetKLAYFee(bridgeAddr, fee) 459 } 460 461 func (sb *SubBridgeAPI) SetFeeReceiver(bridgeAddr, receiver common.Address) (common.Hash, error) { 462 return sb.subBridge.bridgeManager.SetFeeReceiver(bridgeAddr, receiver) 463 } 464 465 func (sb *SubBridgeAPI) GetERC20Fee(bridgeAddr, tokenAddr common.Address) (*big.Int, error) { 466 return sb.subBridge.bridgeManager.GetERC20Fee(bridgeAddr, tokenAddr) 467 } 468 469 func (sb *SubBridgeAPI) GetKLAYFee(bridgeAddr common.Address) (*big.Int, error) { 470 return sb.subBridge.bridgeManager.GetKLAYFee(bridgeAddr) 471 } 472 473 func (sb *SubBridgeAPI) GetFeeReceiver(bridgeAddr common.Address) (common.Address, error) { 474 return sb.subBridge.bridgeManager.GetFeeReceiver(bridgeAddr) 475 } 476 477 func (sb *SubBridgeAPI) doDeregisterToken(cBridgeAddr, pBridgeAddr, cTokenAddr, pTokenAddr common.Address) error { 478 if !sb.subBridge.bridgeManager.IsValidBridgePair(cBridgeAddr, pBridgeAddr) { 479 return ErrInvalidBridgePair 480 } 481 482 cBi, cExist := sb.subBridge.bridgeManager.GetBridgeInfo(cBridgeAddr) 483 pBi, pExist := sb.subBridge.bridgeManager.GetBridgeInfo(pBridgeAddr) 484 485 if !cExist || !pExist { 486 return ErrNoBridgeInfo 487 } 488 489 pTokenAddrCheck := cBi.GetCounterPartToken(cTokenAddr) 490 cTokenAddrCheck := pBi.GetCounterPartToken(pTokenAddr) 491 492 if pTokenAddr != pTokenAddrCheck || cTokenAddr != cTokenAddrCheck { 493 return errors.New("invalid toke pair") 494 } 495 496 cBi.DeregisterToken(cTokenAddr, pTokenAddr) 497 pBi.DeregisterToken(pTokenAddr, cTokenAddr) 498 499 cBi.account.Lock() 500 defer cBi.account.UnLock() 501 tx, err := cBi.bridge.DeregisterToken(cBi.account.GenerateTransactOpts(), cTokenAddr) 502 if err != nil { 503 return err 504 } 505 cBi.account.IncNonce() 506 logger.Debug("cBridge deregistered token", "txHash", tx.Hash().String(), "cToken", cTokenAddr.String(), "pToken", pTokenAddr.String()) 507 508 pBi.account.Lock() 509 defer pBi.account.UnLock() 510 tx, err = pBi.bridge.DeregisterToken(pBi.account.GenerateTransactOpts(), pTokenAddr) 511 if err != nil { 512 return err 513 } 514 pBi.account.IncNonce() 515 logger.Debug("pBridge deregistered token", "txHash", tx.Hash().String(), "cToken", cTokenAddr.String(), "pToken", pTokenAddr.String()) 516 return err 517 } 518 519 func (sb *SubBridgeAPI) DeregisterToken(cBridgeAddrOrAlias, pBridgeOrChildToken, cTokenAddrOrPtokenAddr, pTokenAddrOrEmpty *string) error { 520 cBridgeAddrOrAliasStr, pBridgeAddrOrChildTokenStr, cTokenAddrOrPtokenAddrStr, pTokenAddrOrEmptyStr := stringDeref(cBridgeAddrOrAlias), stringDeref(pBridgeOrChildToken), stringDeref(cTokenAddrOrPtokenAddr), stringDeref(pTokenAddrOrEmpty) 521 cBridgeAddr, pBridgeAddr, args, err := parseBridgeAddrWithAlias(sb.subBridge, cBridgeAddrOrAliasStr, pBridgeAddrOrChildTokenStr, cTokenAddrOrPtokenAddrStr, pTokenAddrOrEmptyStr) 522 if err != nil { 523 return err 524 } 525 cTokenAddr, pTokenAddr := common.HexToAddress(args[0].(string)), common.HexToAddress(args[1].(string)) 526 return sb.doDeregisterToken(cBridgeAddr, pBridgeAddr, cTokenAddr, pTokenAddr) 527 } 528 529 // AddPeer requests connecting to a remote node, and also maintaining the new 530 // connection at all times, even reconnecting if it is lost. 531 func (sb *SubBridgeAPI) AddPeer(url string) (bool, error) { 532 // Make sure the server is running, fail otherwise 533 server := sb.subBridge.bridgeServer 534 if server == nil { 535 return false, node.ErrNodeStopped 536 } 537 // TODO-Klaytn Refactoring this to check whether the url is valid or not by dialing and return it. 538 if _, err := addPeerInternal(server, url); err != nil { 539 return false, err 540 } else { 541 return true, nil 542 } 543 } 544 545 // addPeerInternal does common part for AddPeer. 546 func addPeerInternal(server p2p.Server, url string) (*discover.Node, error) { 547 // Try to add the url as a static peer and return 548 node, err := discover.ParseNode(url) 549 if err != nil { 550 return nil, fmt.Errorf("invalid kni: %v", err) 551 } 552 server.AddPeer(node) 553 return node, nil 554 } 555 556 // RemovePeer disconnects from a a remote node if the connection exists 557 func (sb *SubBridgeAPI) RemovePeer(url string) (bool, error) { 558 // Make sure the server is running, fail otherwise 559 server := sb.subBridge.bridgeServer 560 if server == nil { 561 return false, node.ErrNodeStopped 562 } 563 // Try to remove the url as a static peer and return 564 node, err := discover.ParseNode(url) 565 if err != nil { 566 return false, fmt.Errorf("invalid kni: %v", err) 567 } 568 server.RemovePeer(node) 569 return true, nil 570 } 571 572 // Peers retrieves all the information we know about each individual peer at the 573 // protocol granularity. 574 func (sb *SubBridgeAPI) Peers() ([]*p2p.PeerInfo, error) { 575 server := sb.subBridge.bridgeServer 576 if server == nil { 577 return nil, node.ErrNodeStopped 578 } 579 return server.PeersInfo(), nil 580 } 581 582 // NodeInfo retrieves all the information we know about the host node at the 583 // protocol granularity. 584 func (sb *SubBridgeAPI) NodeInfo() (*p2p.NodeInfo, error) { 585 server := sb.subBridge.bridgeServer 586 if server == nil { 587 return nil, node.ErrNodeStopped 588 } 589 return server.NodeInfo(), nil 590 } 591 592 func (sb *SubBridgeAPI) GetParentOperatorAddr() common.Address { 593 return sb.subBridge.bridgeAccounts.pAccount.address 594 } 595 596 func (sb *SubBridgeAPI) GetChildOperatorAddr() common.Address { 597 return sb.subBridge.bridgeAccounts.cAccount.address 598 } 599 600 func (sb *SubBridgeAPI) GetParentOperatorNonce() uint64 { 601 return sb.subBridge.handler.getParentOperatorNonce() 602 } 603 604 func (sb *SubBridgeAPI) GetChildOperatorNonce() uint64 { 605 return sb.subBridge.handler.getChildOperatorNonce() 606 } 607 608 func (sb *SubBridgeAPI) GetParentOperatorBalance() (*big.Int, error) { 609 return sb.subBridge.handler.getParentOperatorBalance() 610 } 611 612 // GetParentBridgeContractBalance returns the balance of the bridge contract in the parent chain. 613 func (sb *SubBridgeAPI) GetParentBridgeContractBalance(addr common.Address) (*big.Int, error) { 614 return sb.subBridge.handler.getParentBridgeContractBalance(addr) 615 } 616 617 func (sb *SubBridgeAPI) GetChildOperatorBalance() (*big.Int, error) { 618 return sb.subBridge.handler.getChildOperatorBalance() 619 } 620 621 // GetChildBridgeContractBalance returns the balance of the bridge contract in the child chain. 622 func (sb *SubBridgeAPI) GetChildBridgeContractBalance(addr common.Address) (*big.Int, error) { 623 return sb.subBridge.handler.getChildBridgeContractBalance(addr) 624 } 625 626 // GetOperators returns the information of bridge operators. 627 func (sb *SubBridgeAPI) GetOperators() map[string]interface{} { 628 return sb.subBridge.bridgeAccounts.GetBridgeOperators() 629 } 630 631 // LockParentOperator can lock the parent bridge operator. 632 func (sb *SubBridgeAPI) LockParentOperator() error { 633 return sb.subBridge.bridgeAccounts.pAccount.LockAccount() 634 } 635 636 // LockChildOperator can lock the child bridge operator. 637 func (sb *SubBridgeAPI) LockChildOperator() error { 638 return sb.subBridge.bridgeAccounts.cAccount.LockAccount() 639 } 640 641 // UnlockParentOperator can unlock the parent bridge operator. 642 func (sb *SubBridgeAPI) UnlockParentOperator(passphrase string, duration *uint64) error { 643 return sb.subBridge.bridgeAccounts.pAccount.UnLockAccount(passphrase, duration) 644 } 645 646 // UnlockChildOperator can unlock the child bridge operator. 647 func (sb *SubBridgeAPI) UnlockChildOperator(passphrase string, duration *uint64) error { 648 return sb.subBridge.bridgeAccounts.cAccount.UnLockAccount(passphrase, duration) 649 } 650 651 func (sb *SubBridgeAPI) GetAnchoringPeriod() uint64 { 652 return sb.subBridge.config.AnchoringPeriod 653 } 654 655 func (sb *SubBridgeAPI) GetSentChainTxsLimit() uint64 { 656 return sb.subBridge.config.SentChainTxsLimit 657 } 658 659 // SetParentOperatorFeePayer can set the parent bridge operator's fee payer. 660 func (sb *SubBridgeAPI) SetParentOperatorFeePayer(feePayer common.Address) error { 661 return sb.subBridge.bridgeAccounts.SetParentOperatorFeePayer(feePayer) 662 } 663 664 // SetChildOperatorFeePayer can set the child bridge operator's fee payer. 665 func (sb *SubBridgeAPI) SetChildOperatorFeePayer(feePayer common.Address) error { 666 return sb.subBridge.bridgeAccounts.SetChildOperatorFeePayer(feePayer) 667 } 668 669 // SetParentBridgeOperatorGasLimit changes value of bridge parent operator's gaslimit. 670 func (sb *SubBridgeAPI) SetParentBridgeOperatorGasLimit(fee uint64) { 671 sb.subBridge.bridgeAccounts.SetParentBridgeOperatorGasLimit(fee) 672 } 673 674 // SetChildBridgeOperatorGasLimit changes value of bridge child operator's gaslimit. 675 func (sb *SubBridgeAPI) SetChildBridgeOperatorGasLimit(fee uint64) { 676 sb.subBridge.bridgeAccounts.SetChildBridgeOperatorGasLimit(fee) 677 } 678 679 // GetParentOperatorFeePayer can return the parent bridge operator's fee payer. 680 func (sb *SubBridgeAPI) GetParentOperatorFeePayer() common.Address { 681 return sb.subBridge.bridgeAccounts.GetParentOperatorFeePayer() 682 } 683 684 // GetChildOperatorFeePayer can return the child bridge operator's fee payer. 685 func (sb *SubBridgeAPI) GetChildOperatorFeePayer() common.Address { 686 return sb.subBridge.bridgeAccounts.GetChildOperatorFeePayer() 687 } 688 689 // GetParentBridgeOperatorGasLimit gets value of bridge parent operator's gaslimit. 690 func (sb *SubBridgeAPI) GetParentBridgeOperatorGasLimit() uint64 { 691 return sb.subBridge.bridgeAccounts.GetParentBridgeOperatorGasLimit() 692 } 693 694 // GetChildBridgeOperatorGasLimit gets value of bridge child operator's gaslimit. 695 func (sb *SubBridgeAPI) GetChildBridgeOperatorGasLimit() uint64 { 696 return sb.subBridge.bridgeAccounts.GetChildBridgeOperatorGasLimit() 697 } 698 699 // getParentGasPrice returns the recently synced parent chain's gas price 700 func (sb *SubBridgeAPI) GetParentGasPrice() uint64 { 701 return sb.subBridge.bridgeAccounts.GetParentGasPrice() 702 } 703 704 // GetParentKIP71Config returns the recently synced parent chain's Magma config values 705 func (sb *SubBridgeAPI) GetParentKIP71Config() params.KIP71Config { 706 return sb.subBridge.bridgeAccounts.GetParentKIP71Config() 707 } 708 709 // RequestParentSync request to synchronize the parent chain values 710 func (sb *SubBridgeAPI) RequestParentSync() { 711 sb.subBridge.handler.SyncNonceAndGasPrice() 712 }