github.com/klaytn/klaytn@v1.12.1/node/sc/multi_bridge_test.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 "math/big" 21 "testing" 22 "time" 23 24 "github.com/klaytn/klaytn/accounts/abi/bind" 25 "github.com/klaytn/klaytn/accounts/abi/bind/backends" 26 "github.com/klaytn/klaytn/blockchain" 27 "github.com/klaytn/klaytn/common" 28 "github.com/klaytn/klaytn/contracts/bridge" 29 sc_token "github.com/klaytn/klaytn/contracts/sc_erc20" 30 sc_nft "github.com/klaytn/klaytn/contracts/sc_erc721" 31 "github.com/klaytn/klaytn/crypto" 32 "github.com/klaytn/klaytn/event" 33 "github.com/klaytn/klaytn/params" 34 "github.com/stretchr/testify/assert" 35 ) 36 37 type bridgeTestInfo struct { 38 acc *bind.TransactOpts 39 b *bridge.Bridge 40 sim *backends.SimulatedBackend 41 } 42 43 type multiBridgeTestInfo struct { 44 accounts []*bind.TransactOpts 45 b *bridge.Bridge 46 bAddr common.Address 47 sim *backends.SimulatedBackend 48 requestCh chan *bridge.BridgeRequestValueTransfer 49 requestSub event.Subscription 50 handleCh chan *bridge.BridgeHandleValueTransfer 51 handleSub event.Subscription 52 } 53 54 func prepareMultiBridgeTest(t *testing.T) *bridgeTestInfo { 55 accKey, _ := crypto.GenerateKey() 56 acc := bind.NewKeyedTransactor(accKey) 57 58 alloc := blockchain.GenesisAlloc{acc.From: {Balance: big.NewInt(params.KLAY)}} 59 sim := backends.NewSimulatedBackend(alloc) 60 61 chargeAmount := big.NewInt(10000000) 62 acc.Value = chargeAmount 63 _, tx, b, err := bridge.DeployBridge(acc, sim, false) 64 assert.NoError(t, err) 65 sim.Commit() 66 assert.Nil(t, bind.CheckWaitMined(sim, tx)) 67 return &bridgeTestInfo{acc, b, sim} 68 } 69 70 func prepareMultiBridgeEventTest(t *testing.T) *multiBridgeTestInfo { 71 const maxAccounts = 4 72 res := multiBridgeTestInfo{} 73 74 accountMap := make(map[common.Address]blockchain.GenesisAccount) 75 res.accounts = make([]*bind.TransactOpts, maxAccounts) 76 77 for i := 0; i < maxAccounts; i++ { 78 accKey, _ := crypto.GenerateKey() 79 res.accounts[i] = bind.NewKeyedTransactor(accKey) 80 accountMap[res.accounts[i].From] = blockchain.GenesisAccount{Balance: big.NewInt(params.KLAY)} 81 } 82 83 res.sim = backends.NewSimulatedBackend(accountMap) 84 85 chargeAmount := big.NewInt(10000000) 86 res.accounts[0].Value = chargeAmount 87 bAddr, tx, b, err := bridge.DeployBridge(res.accounts[0], res.sim, true) 88 res.b = b 89 res.bAddr = bAddr 90 assert.NoError(t, err) 91 res.sim.Commit() 92 assert.Nil(t, bind.CheckWaitMined(res.sim, tx)) 93 94 owner := res.accounts[0] 95 for i := 0; i < maxAccounts; i++ { 96 acc := res.accounts[i] 97 opts := &bind.TransactOpts{From: owner.From, Signer: owner.Signer, GasLimit: DefaultBridgeTxGasLimit} 98 _, _ = b.RegisterOperator(opts, acc.From) 99 res.sim.Commit() 100 } 101 102 res.requestCh = make(chan *bridge.BridgeRequestValueTransfer, 100) 103 res.handleCh = make(chan *bridge.BridgeHandleValueTransfer, 100) 104 res.requestSub, err = b.WatchRequestValueTransfer(nil, res.requestCh, nil, nil, nil) 105 assert.NoError(t, err) 106 res.handleSub, err = b.WatchHandleValueTransfer(nil, res.handleCh, nil, nil, nil) 107 assert.NoError(t, err) 108 109 return &res 110 } 111 112 // TestRegisterDeregisterOperator checks the following: 113 // - the specified operator is registered by the contract method RegisterOperator. 114 // - the specified operator is deregistered by the contract method DeregisterOperator. 115 func TestRegisterDeregisterOperator(t *testing.T) { 116 info := prepareMultiBridgeTest(t) 117 defer info.sim.Close() 118 119 testAddrs := []common.Address{{10}, {20}} 120 121 opts := &bind.TransactOpts{From: info.acc.From, Signer: info.acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 122 123 // info.acc.From is duplicated because it is an owner. ignored. 124 operatorList, err := info.b.GetOperatorList(nil) 125 assert.NoError(t, err) 126 assert.Equal(t, 1, len(operatorList)) 127 128 tx, err := info.b.RegisterOperator(opts, testAddrs[0]) 129 assert.NoError(t, err) 130 info.sim.Commit() 131 assert.Nil(t, bind.CheckWaitMined(info.sim, tx)) 132 133 tx, err = info.b.RegisterOperator(opts, testAddrs[1]) 134 assert.NoError(t, err) 135 info.sim.Commit() 136 assert.Nil(t, bind.CheckWaitMined(info.sim, tx)) 137 138 // Check operators 139 isOperator, err := info.b.Operators(nil, info.acc.From) 140 assert.NoError(t, err) 141 assert.Equal(t, true, isOperator) 142 143 isOperator, err = info.b.Operators(nil, testAddrs[0]) 144 assert.NoError(t, err) 145 assert.Equal(t, true, isOperator) 146 147 isOperator, err = info.b.Operators(nil, testAddrs[1]) 148 assert.NoError(t, err) 149 assert.Equal(t, true, isOperator) 150 151 operatorList, err = info.b.GetOperatorList(nil) 152 assert.NoError(t, err) 153 assert.Equal(t, 3, len(operatorList)) 154 155 // Deregister an operator 156 opts = &bind.TransactOpts{From: info.acc.From, Signer: info.acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 157 tx, err = info.b.DeregisterOperator(opts, testAddrs[0]) 158 assert.NoError(t, err) 159 info.sim.Commit() 160 assert.Nil(t, bind.CheckWaitMined(info.sim, tx)) 161 162 // Check operators 163 isOperator, err = info.b.Operators(nil, testAddrs[0]) 164 assert.NoError(t, err) 165 assert.Equal(t, false, isOperator) 166 167 operatorList, err = info.b.GetOperatorList(nil) 168 assert.NoError(t, err) 169 assert.Equal(t, 2, len(operatorList)) 170 assert.Equal(t, info.acc.From, operatorList[0]) 171 assert.Equal(t, testAddrs[1], operatorList[1]) 172 } 173 174 // TestStartStop checks the following: 175 // - the bridge contract method Start. 176 // - the bridge contract method Stop. 177 func TestStartStop(t *testing.T) { 178 info := prepareMultiBridgeTest(t) 179 defer info.sim.Close() 180 181 opts := &bind.TransactOpts{From: info.acc.From, Signer: info.acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 182 tx, err := info.b.Start(opts, true) 183 assert.NoError(t, err) 184 info.sim.Commit() 185 assert.Nil(t, bind.CheckWaitMined(info.sim, tx)) 186 187 isRunning, err := info.b.IsRunning(nil) 188 assert.NoError(t, err) 189 assert.Equal(t, true, isRunning) 190 191 opts = &bind.TransactOpts{From: info.acc.From, Signer: info.acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 192 tx, err = info.b.Start(opts, false) 193 assert.NoError(t, err) 194 info.sim.Commit() 195 assert.Nil(t, bind.CheckWaitMined(info.sim, tx)) 196 197 isRunning, err = info.b.IsRunning(nil) 198 assert.NoError(t, err) 199 assert.Equal(t, false, isRunning) 200 } 201 202 // TestSetCounterPartBridge checks the following: 203 // - the bridge contract method TestSetCounterPartBridge. 204 func TestSetCounterPartBridge(t *testing.T) { 205 info := prepareMultiBridgeTest(t) 206 defer info.sim.Close() 207 208 dummy := common.Address{10} 209 210 opts := &bind.TransactOpts{From: info.acc.From, Signer: info.acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 211 tx, err := info.b.SetCounterPartBridge(opts, dummy) 212 assert.NoError(t, err) 213 info.sim.Commit() 214 assert.Nil(t, bind.CheckWaitMined(info.sim, tx)) 215 216 cBridge, err := info.b.CounterpartBridge(nil) 217 assert.NoError(t, err) 218 assert.Equal(t, dummy, cBridge) 219 } 220 221 // TestRegisterDeregisterToken checks the following: 222 // - the bridge contract method RegisterToken and DeregisterToken. 223 func TestRegisterDeregisterToken(t *testing.T) { 224 info := prepareMultiBridgeTest(t) 225 defer info.sim.Close() 226 227 dummy1 := common.Address{10} 228 dummy2 := common.Address{20} 229 230 opts := &bind.TransactOpts{From: info.acc.From, Signer: info.acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 231 tx, err := info.b.RegisterToken(opts, dummy1, dummy2) 232 assert.NoError(t, err) 233 info.sim.Commit() 234 assert.Nil(t, bind.CheckWaitMined(info.sim, tx)) 235 236 // Try duplicated insertion. 237 tx, err = info.b.RegisterToken(opts, dummy1, dummy2) 238 assert.NoError(t, err) 239 info.sim.Commit() 240 assert.Error(t, bind.CheckWaitMined(info.sim, tx)) 241 242 res, err := info.b.RegisteredTokens(nil, dummy1) 243 assert.NoError(t, err) 244 assert.Equal(t, dummy2, res) 245 246 allowedTokenList, err := info.b.GetRegisteredTokenList(nil) 247 assert.NoError(t, err) 248 assert.Equal(t, 1, len(allowedTokenList)) 249 assert.Equal(t, dummy1, allowedTokenList[0]) 250 251 opts = &bind.TransactOpts{From: info.acc.From, Signer: info.acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 252 tx, err = info.b.DeregisterToken(opts, dummy1) 253 assert.NoError(t, err) 254 info.sim.Commit() 255 assert.Nil(t, bind.CheckWaitMined(info.sim, tx)) 256 257 res, err = info.b.RegisteredTokens(nil, dummy1) 258 assert.NoError(t, err) 259 assert.Equal(t, common.Address{0}, res) 260 261 allowedTokenList, err = info.b.GetRegisteredTokenList(nil) 262 assert.NoError(t, err) 263 assert.Equal(t, 0, len(allowedTokenList)) 264 } 265 266 // TestMultiBridgeKLAYTransfer1 checks the following: 267 // - successful value transfer with proper transaction counts. 268 func TestMultiBridgeKLAYTransfer1(t *testing.T) { 269 info := prepareMultiBridgeEventTest(t) 270 defer info.sim.Close() 271 272 acc := info.accounts[0] 273 to := common.Address{100} 274 275 opts := &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 276 tx, err := info.b.SetOperatorThreshold(opts, voteTypeValueTransfer, 2) 277 assert.NoError(t, err) 278 info.sim.Commit() 279 assert.Nil(t, bind.CheckWaitMined(info.sim, tx)) 280 281 nonceOffset := uint64(17) 282 sentNonce := nonceOffset 283 transferAmount := uint64(100) 284 sentBlockNumber := uint64(100000) 285 286 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 287 info.sim.Commit() 288 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 289 290 acc = info.accounts[1] 291 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 292 info.sim.Commit() 293 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 294 295 for { 296 select { 297 case ev := <-info.handleCh: 298 assert.Equal(t, nonceOffset, ev.HandleNonce) 299 balance, err := info.sim.BalanceAt(nil, to, nil) 300 assert.NoError(t, err) 301 assert.Equal(t, big.NewInt(int64(transferAmount)).String(), balance.String()) 302 return 303 case err := <-info.handleSub.Err(): 304 t.Fatal("Contract Event Loop Running Stop by sub.Err()", "err", err) 305 case <-time.After(timeOut): 306 t.Fatal("Contract Event Loop Running Stop by timeout") 307 } 308 } 309 } 310 311 // TestMultiBridgeKLAYTransfer2 checks the following: 312 // - failed value transfer without proper transaction counts. 313 // - timeout is expected since operator threshold will not be satisfied. 314 func TestMultiBridgeKLAYTransfer2(t *testing.T) { 315 info := prepareMultiBridgeEventTest(t) 316 defer info.sim.Close() 317 318 acc := info.accounts[0] 319 to := common.Address{100} 320 321 opts := &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 322 tx, err := info.b.SetOperatorThreshold(opts, voteTypeValueTransfer, 2) 323 assert.NoError(t, err) 324 info.sim.Commit() 325 assert.Nil(t, bind.CheckWaitMined(info.sim, tx)) 326 327 nonceOffset := uint64(17) 328 sentNonce := nonceOffset 329 transferAmount := uint64(100) 330 sentBlockNumber := uint64(100000) 331 332 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 333 info.sim.Commit() 334 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 335 336 for { 337 select { 338 case _ = <-info.handleCh: 339 t.Fatal("unexpected handling of value transfer") 340 case err := <-info.handleSub.Err(): 341 t.Fatal("Contract Event Loop Running Stop by sub.Err()", "err", err) 342 case <-time.After(timeOut): 343 // expected timeout 344 return 345 } 346 } 347 } 348 349 // TestMultiBridgeKLAYTransfer3 checks the following: 350 // - tx is actually handled when operator threshold is satisfied. 351 // - no double spending is made due to additional tx. 352 func TestMultiBridgeKLAYTransfer3(t *testing.T) { 353 info := prepareMultiBridgeEventTest(t) 354 defer info.sim.Close() 355 356 acc := info.accounts[0] 357 to := common.Address{100} 358 359 opts := &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 360 tx, err := info.b.SetOperatorThreshold(opts, voteTypeValueTransfer, 2) 361 assert.NoError(t, err) 362 info.sim.Commit() 363 assert.Nil(t, bind.CheckWaitMined(info.sim, tx)) 364 365 nonceOffset := uint64(17) 366 sentNonce := nonceOffset 367 transferAmount := uint64(100) 368 sentBlockNumber := uint64(100000) 369 370 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 371 info.sim.Commit() 372 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 373 374 acc = info.accounts[1] 375 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 376 info.sim.Commit() 377 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 378 379 for { 380 select { 381 case _ = <-info.handleCh: 382 acc = info.accounts[2] 383 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 384 info.sim.Commit() 385 assert.Error(t, bind.CheckWaitMined(info.sim, tx)) 386 return 387 case err := <-info.handleSub.Err(): 388 t.Fatal("Contract Event Loop Running Stop by sub.Err()", "err", err) 389 case <-time.After(timeOut): 390 t.Fatal("Contract Event Loop Running Stop by timeout") 391 } 392 } 393 } 394 395 // TestMultiBridgeERC20Transfer checks the following: 396 // - successful value transfer with proper transaction counts. 397 func TestMultiBridgeERC20Transfer(t *testing.T) { 398 info := prepareMultiBridgeEventTest(t) 399 defer info.sim.Close() 400 401 acc := info.accounts[0] 402 to := common.Address{100} 403 404 opts := &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 405 tx, err := info.b.SetOperatorThreshold(opts, voteTypeValueTransfer, 2) 406 assert.NoError(t, err) 407 info.sim.Commit() 408 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 409 410 // Deploy token 411 opts = &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 412 erc20Addr, tx, erc20, err := sc_token.DeployServiceChainToken(opts, info.sim, info.bAddr) 413 assert.NoError(t, err) 414 info.sim.Commit() 415 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 416 417 // Register token 418 opts = &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 419 tx, err = info.b.RegisterToken(opts, erc20Addr, erc20Addr) 420 assert.NoError(t, err) 421 info.sim.Commit() 422 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 423 424 // Give minter role to bridge contract 425 opts = &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 426 tx, err = erc20.AddMinter(opts, info.bAddr) 427 assert.NoError(t, err) 428 info.sim.Commit() 429 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 430 431 nonceOffset := uint64(17) 432 sentNonce := nonceOffset 433 amount := int64(100) 434 sentBlockNumber := uint64(100000) 435 436 opts = &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 437 tx, err = info.b.HandleERC20Transfer(opts, common.Hash{10}, to, to, erc20Addr, big.NewInt(amount), sentNonce, sentBlockNumber, nil) 438 assert.NoError(t, err) 439 info.sim.Commit() 440 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 441 442 acc = info.accounts[1] 443 opts = &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 444 tx, err = info.b.HandleERC20Transfer(opts, common.Hash{10}, to, to, erc20Addr, big.NewInt(amount), sentNonce, sentBlockNumber, nil) 445 assert.NoError(t, err) 446 info.sim.Commit() 447 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 448 449 for { 450 select { 451 case ev := <-info.handleCh: 452 assert.Equal(t, nonceOffset, ev.HandleNonce) 453 balance, err := erc20.BalanceOf(nil, to) 454 assert.NoError(t, err) 455 assert.Equal(t, big.NewInt(amount).String(), balance.String()) 456 return 457 case err := <-info.handleSub.Err(): 458 t.Fatal("Contract Event Loop Running Stop by sub.Err()", "err", err) 459 case <-time.After(timeOut): 460 t.Fatal("Contract Event Loop Running Stop by timeout") 461 } 462 } 463 } 464 465 // TestMultiBridgeERC721Transfer checks the following: 466 // - successful value transfer with proper transaction counts. 467 func TestMultiBridgeERC721Transfer(t *testing.T) { 468 info := prepareMultiBridgeEventTest(t) 469 defer info.sim.Close() 470 471 acc := info.accounts[0] 472 to := common.Address{100} 473 474 opts := &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 475 tx, err := info.b.SetOperatorThreshold(opts, voteTypeValueTransfer, 1) 476 assert.NoError(t, err) 477 info.sim.Commit() 478 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 479 480 // Deploy token 481 opts = &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 482 erc721Addr, tx, erc721, err := sc_nft.DeployServiceChainNFT(opts, info.sim, info.bAddr) 483 assert.NoError(t, err) 484 info.sim.Commit() 485 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 486 487 // Register token 488 opts = &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 489 tx, err = info.b.RegisterToken(opts, erc721Addr, erc721Addr) 490 assert.NoError(t, err) 491 info.sim.Commit() 492 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 493 494 // Give minter role to bridge contract 495 opts = &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 496 tx, err = erc721.AddMinter(opts, info.bAddr) 497 assert.NoError(t, err) 498 info.sim.Commit() 499 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 500 501 nonceOffset := uint64(17) 502 sentNonce := nonceOffset 503 amount := int64(100) 504 sentBlockNumber := uint64(100000) 505 506 opts = &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 507 tx, err = info.b.HandleERC721Transfer(opts, common.Hash{10}, to, to, erc721Addr, big.NewInt(amount), sentNonce, sentBlockNumber, "", nil) 508 assert.NoError(t, err) 509 info.sim.Commit() 510 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 511 512 for { 513 select { 514 case ev := <-info.handleCh: 515 assert.Equal(t, nonceOffset, ev.HandleNonce) 516 owner, err := erc721.OwnerOf(nil, big.NewInt(amount)) 517 assert.NoError(t, err) 518 assert.Equal(t, to, owner) 519 return 520 case err := <-info.handleSub.Err(): 521 t.Fatal("Contract Event Loop Running Stop by sub.Err()", "err", err) 522 case <-time.After(timeOut): 523 t.Fatal("Contract Event Loop Running Stop by timeout") 524 } 525 } 526 } 527 528 // TestMultiBridgeSetKLAYFee checks the following: 529 // - successfully setting KLAY fee 530 func TestMultiBridgeSetKLAYFee(t *testing.T) { 531 info := prepareMultiBridgeEventTest(t) 532 defer info.sim.Close() 533 534 acc := info.accounts[0] 535 const confThreshold = uint8(2) 536 const fee = 1000 537 requestNonce := uint64(0) 538 539 opts := &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 540 tx, err := info.b.SetOperatorThreshold(opts, voteTypeConfiguration, confThreshold) 541 assert.NoError(t, err) 542 info.sim.Commit() 543 assert.Nil(t, bind.CheckWaitMined(info.sim, tx)) 544 545 opts = &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 546 tx, err = info.b.SetKLAYFee(opts, big.NewInt(fee), requestNonce) 547 assert.NoError(t, err) 548 info.sim.Commit() 549 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 550 551 ret, err := info.b.FeeOfKLAY(nil) 552 assert.NoError(t, err) 553 assert.Equal(t, big.NewInt(0).String(), ret.String()) 554 555 acc = info.accounts[1] 556 opts = &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 557 tx, err = info.b.SetKLAYFee(opts, big.NewInt(fee), requestNonce) 558 assert.NoError(t, err) 559 info.sim.Commit() 560 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 561 562 ret, err = info.b.FeeOfKLAY(nil) 563 assert.NoError(t, err) 564 assert.Equal(t, big.NewInt(fee).String(), ret.String()) 565 } 566 567 // TestMultiBridgeSetERC20Fee checks the following: 568 // - successfully setting ERC20 fee 569 func TestMultiBridgeSetERC20Fee(t *testing.T) { 570 info := prepareMultiBridgeEventTest(t) 571 defer info.sim.Close() 572 573 acc := info.accounts[0] 574 const confThreshold = uint8(2) 575 const fee = 1000 576 tokenAddr := common.Address{10} 577 requestNonce := uint64(0) 578 579 opts := &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 580 tx, err := info.b.SetOperatorThreshold(opts, voteTypeConfiguration, confThreshold) 581 assert.NoError(t, err) 582 info.sim.Commit() 583 assert.Nil(t, bind.CheckWaitMined(info.sim, tx)) 584 585 opts = &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 586 tx, err = info.b.SetERC20Fee(opts, tokenAddr, big.NewInt(fee), requestNonce) 587 assert.NoError(t, err) 588 info.sim.Commit() 589 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 590 591 ret, err := info.b.FeeOfERC20(nil, tokenAddr) 592 assert.NoError(t, err) 593 assert.Equal(t, big.NewInt(0).String(), ret.String()) 594 595 acc = info.accounts[1] 596 opts = &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 597 tx, err = info.b.SetERC20Fee(opts, tokenAddr, big.NewInt(fee), requestNonce) 598 assert.NoError(t, err) 599 info.sim.Commit() 600 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 601 602 ret, err = info.b.FeeOfERC20(nil, tokenAddr) 603 assert.NoError(t, err) 604 assert.Equal(t, big.NewInt(fee).String(), ret.String()) 605 } 606 607 // TestSetFeeReceiver checks the following: 608 // - the bridge contract method SetFeeReceiver. 609 func TestSetFeeReceiver(t *testing.T) { 610 info := prepareMultiBridgeTest(t) 611 defer info.sim.Close() 612 613 dummy := common.Address{10} 614 615 opts := &bind.TransactOpts{From: info.acc.From, Signer: info.acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 616 tx, err := info.b.SetFeeReceiver(opts, dummy) 617 assert.NoError(t, err) 618 info.sim.Commit() 619 assert.Nil(t, bind.CheckWaitMined(info.sim, tx)) 620 621 cBridge, err := info.b.FeeReceiver(nil) 622 assert.NoError(t, err) 623 assert.Equal(t, dummy, cBridge) 624 } 625 626 // TestMultiBridgeErrNotOperator1 checks the following: 627 // - set threshold to 1. 628 // - non-operator failed to handle value transfer. 629 func TestMultiBridgeErrNotOperator1(t *testing.T) { 630 info := prepareMultiBridgeEventTest(t) 631 defer info.sim.Close() 632 633 to := common.Address{100} 634 nonceOffset := uint64(17) 635 sentNonce := nonceOffset 636 transferAmount := uint64(100) 637 sentBlockNumber := uint64(100000) 638 639 accKey, _ := crypto.GenerateKey() 640 acc := bind.NewKeyedTransactor(accKey) 641 642 tx := SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 643 info.sim.Commit() 644 assert.Error(t, bind.CheckWaitMined(info.sim, tx)) 645 } 646 647 // TestMultiBridgeErrNotOperator2 checks the following: 648 // - set threshold to 2. 649 // - operator succeeds to handle value transfer. 650 // - non-operator fails to handle value transfer. 651 func TestMultiBridgeErrNotOperator2(t *testing.T) { 652 info := prepareMultiBridgeEventTest(t) 653 defer info.sim.Close() 654 655 to := common.Address{100} 656 acc := info.accounts[0] 657 658 opts := &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 659 tx, err := info.b.SetOperatorThreshold(opts, voteTypeValueTransfer, 2) 660 assert.NoError(t, err) 661 info.sim.Commit() 662 assert.Nil(t, bind.CheckWaitMined(info.sim, tx)) 663 664 nonceOffset := uint64(17) 665 sentNonce := nonceOffset 666 transferAmount := uint64(100) 667 sentBlockNumber := uint64(100000) 668 669 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 670 info.sim.Commit() 671 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 672 673 accKey, _ := crypto.GenerateKey() 674 acc = bind.NewKeyedTransactor(accKey) 675 676 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 677 info.sim.Commit() 678 assert.Error(t, bind.CheckWaitMined(info.sim, tx)) 679 } 680 681 // TestMultiBridgeErrInvalTx checks the following: 682 // - set threshold to 2. 683 // - first operator succeeds to handle value transfer. 684 // - second operator fails to handle value transfer with invalid tx. 685 func TestMultiBridgeErrInvalTx(t *testing.T) { 686 info := prepareMultiBridgeEventTest(t) 687 defer info.sim.Close() 688 689 to := common.Address{100} 690 acc := info.accounts[0] 691 692 opts := &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 693 tx, err := info.b.SetOperatorThreshold(opts, voteTypeValueTransfer, 2) 694 assert.NoError(t, err) 695 info.sim.Commit() 696 assert.Nil(t, bind.CheckWaitMined(info.sim, tx)) 697 698 nonceOffset := uint64(17) 699 sentNonce := nonceOffset 700 transferAmount := uint64(100) 701 sentBlockNumber := uint64(100000) 702 703 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 704 info.sim.Commit() 705 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 706 707 acc = info.accounts[1] 708 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce+1, sentBlockNumber, t) 709 info.sim.Commit() 710 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 711 712 for { 713 select { 714 case _ = <-info.handleCh: 715 t.Fatal("Failure is expected") 716 case err := <-info.handleSub.Err(): 717 t.Fatal("Contract Event Loop Running Stop by sub.Err()", "err", err) 718 case <-time.After(timeOut): 719 return 720 } 721 } 722 } 723 724 // TestMultiBridgeErrOverSign checks the following: 725 // - set threshold to 2. 726 // - two operators succeed to handle value transfer. 727 // - the last operator fails to handle value transfer because of vote closing. 728 func TestMultiBridgeErrOverSign(t *testing.T) { 729 info := prepareMultiBridgeEventTest(t) 730 defer info.sim.Close() 731 732 to := common.Address{100} 733 acc := info.accounts[0] 734 735 opts := &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 736 tx, err := info.b.SetOperatorThreshold(opts, voteTypeValueTransfer, 2) 737 assert.NoError(t, err) 738 info.sim.Commit() 739 assert.Nil(t, bind.CheckWaitMined(info.sim, tx)) 740 741 nonceOffset := uint64(17) 742 sentNonce := nonceOffset 743 transferAmount := uint64(100) 744 sentBlockNumber := uint64(100000) 745 746 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 747 info.sim.Commit() 748 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 749 750 acc = info.accounts[1] 751 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 752 info.sim.Commit() 753 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 754 755 acc = info.accounts[2] 756 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 757 info.sim.Commit() 758 assert.Error(t, bind.CheckWaitMined(info.sim, tx)) 759 } 760 761 // TestMultiOperatorKLAYTransferDup checks the following: 762 // - set threshold to 1. 763 // - an operator succeed to handle value transfer. 764 // - the operator (same auth) fails to handle value transfer because of vote closing (duplicated). 765 // - another operator (different auth) fails to handle value transfer because of vote closing (duplicated). 766 func TestMultiOperatorKLAYTransferDup(t *testing.T) { 767 info := prepareMultiBridgeEventTest(t) 768 defer info.sim.Close() 769 770 to := common.Address{100} 771 acc := info.accounts[0] 772 773 opts := &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 774 tx, err := info.b.SetOperatorThreshold(opts, voteTypeValueTransfer, 1) 775 assert.NoError(t, err) 776 info.sim.Commit() 777 assert.Nil(t, bind.CheckWaitMined(info.sim, tx)) 778 779 nonceOffset := uint64(17) 780 sentNonce := nonceOffset 781 transferAmount := uint64(100) 782 sentBlockNumber := uint64(100000) 783 784 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 785 info.sim.Commit() 786 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 787 788 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 789 info.sim.Commit() 790 assert.Error(t, bind.CheckWaitMined(info.sim, tx)) 791 792 acc = info.accounts[1] 793 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 794 info.sim.Commit() 795 assert.Error(t, bind.CheckWaitMined(info.sim, tx)) 796 } 797 798 // TestMultiBridgeSetKLAYFeeErrNonce checks the following: 799 // - failed to set KLAY fee because of the wrong nonce. 800 func TestMultiBridgeSetKLAYFeeErrNonce(t *testing.T) { 801 info := prepareMultiBridgeEventTest(t) 802 defer info.sim.Close() 803 804 acc := info.accounts[0] 805 const confThreshold = uint8(2) 806 const fee = 1000 807 requestNonce := uint64(0) 808 809 opts := &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 810 tx, err := info.b.SetOperatorThreshold(opts, voteTypeConfiguration, confThreshold) 811 assert.NoError(t, err) 812 info.sim.Commit() 813 assert.Nil(t, bind.CheckWaitMined(info.sim, tx)) 814 815 opts = &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 816 tx, err = info.b.SetKLAYFee(opts, big.NewInt(fee), requestNonce) 817 assert.NoError(t, err) 818 info.sim.Commit() 819 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 820 821 ret, err := info.b.FeeOfKLAY(nil) 822 assert.NoError(t, err) 823 assert.Equal(t, big.NewInt(0).String(), ret.String()) 824 825 acc = info.accounts[1] 826 opts = &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 827 tx, err = info.b.SetKLAYFee(opts, big.NewInt(fee), requestNonce+1) 828 assert.NoError(t, err) 829 info.sim.Commit() 830 assert.Error(t, bind.CheckWaitMined(info.sim, tx)) 831 832 acc = info.accounts[1] 833 opts = &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 834 tx, err = info.b.SetKLAYFee(opts, big.NewInt(fee), requestNonce-1) 835 assert.NoError(t, err) 836 info.sim.Commit() 837 assert.Error(t, bind.CheckWaitMined(info.sim, tx)) 838 } 839 840 // TestMultiBridgeKLAYTransferNonceJump checks the following: 841 // - set threshold to 2. 842 // - first request is not executed yet since one operator does not vote. 843 // - jump 100 nonce and successfully handle value transfer. 844 func TestMultiBridgeKLAYTransferNonceJump(t *testing.T) { 845 info := prepareMultiBridgeEventTest(t) 846 defer info.sim.Close() 847 848 acc := info.accounts[0] 849 to := common.Address{100} 850 851 opts := &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 852 tx, err := info.b.SetOperatorThreshold(opts, voteTypeValueTransfer, 2) 853 assert.NoError(t, err) 854 info.sim.Commit() 855 assert.Nil(t, bind.CheckWaitMined(info.sim, tx)) 856 857 nonceOffset := uint64(17) 858 sentNonce := nonceOffset 859 transferAmount := uint64(100) 860 sentBlockNumber := uint64(100000) 861 862 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 863 info.sim.Commit() 864 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 865 866 sentNonce = sentNonce + 100 867 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 868 info.sim.Commit() 869 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 870 871 acc = info.accounts[1] 872 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 873 info.sim.Commit() 874 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 875 876 for { 877 select { 878 case ev := <-info.handleCh: 879 assert.Equal(t, sentNonce, ev.HandleNonce) 880 return 881 case err := <-info.handleSub.Err(): 882 t.Fatal("Contract Event Loop Running Stop by sub.Err()", "err", err) 883 case <-time.After(timeOut): 884 t.Fatal("Contract Event Loop Running Stop by timeout") 885 } 886 } 887 } 888 889 // TestMultiBridgeKLAYTransferParallel checks the following: 890 // - set threshold to 2. 891 // - two different value transfers succeed to handle the value transfer. 892 func TestMultiBridgeKLAYTransferParallel(t *testing.T) { 893 info := prepareMultiBridgeEventTest(t) 894 defer info.sim.Close() 895 896 to := common.Address{100} 897 acc := info.accounts[0] 898 899 opts := &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 900 tx, err := info.b.SetOperatorThreshold(opts, voteTypeValueTransfer, 2) 901 assert.NoError(t, err) 902 info.sim.Commit() 903 assert.Nil(t, bind.CheckWaitMined(info.sim, tx)) 904 905 nonceOffset := uint64(17) 906 sentNonce := nonceOffset 907 transferAmount := uint64(100) 908 sentBlockNumber := uint64(100000) 909 910 go func() { 911 acc := info.accounts[0] 912 tx := SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 913 info.sim.Commit() 914 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 915 916 acc = info.accounts[1] 917 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 918 info.sim.Commit() 919 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 920 }() 921 922 go func() { 923 acc := info.accounts[2] 924 sentNonce := sentNonce + 20 925 tx := SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 926 info.sim.Commit() 927 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 928 929 acc = info.accounts[3] 930 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 931 info.sim.Commit() 932 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 933 }() 934 935 handleCounter := 0 936 for { 937 select { 938 case _ = <-info.handleCh: 939 handleCounter++ 940 if handleCounter == 2 { 941 return 942 } 943 case err := <-info.handleSub.Err(): 944 t.Fatal("Contract Event Loop Running Stop by sub.Err()", "err", err) 945 case <-time.After(timeOut): 946 t.Fatal("Contract Event Loop Running Stop by timeout") 947 } 948 } 949 } 950 951 // TestMultiBridgeKLAYTransferMixConfig1 checks the following: 952 // - set threshold to 2. 953 // - the first tx is done. 954 // - set threshold to 1. 955 // - the second operator successfully handles the value transfer. 956 func TestMultiBridgeKLAYTransferMixConfig1(t *testing.T) { 957 info := prepareMultiBridgeEventTest(t) 958 defer info.sim.Close() 959 960 acc := info.accounts[0] 961 to := common.Address{100} 962 963 opts := &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 964 tx, err := info.b.SetOperatorThreshold(opts, voteTypeValueTransfer, 2) 965 assert.NoError(t, err) 966 info.sim.Commit() 967 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 968 969 nonceOffset := uint64(17) 970 sentNonce := nonceOffset 971 transferAmount := uint64(100) 972 sentBlockNumber := uint64(100000) 973 974 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 975 info.sim.Commit() 976 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 977 978 opts = &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 979 tx, err = info.b.SetOperatorThreshold(opts, voteTypeValueTransfer, 1) 980 assert.NoError(t, err) 981 info.sim.Commit() 982 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 983 984 acc = info.accounts[1] 985 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 986 info.sim.Commit() 987 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 988 989 for { 990 select { 991 case ev := <-info.handleCh: 992 assert.Equal(t, nonceOffset, ev.HandleNonce) 993 return 994 case err := <-info.handleSub.Err(): 995 t.Fatal("Contract Event Loop Running Stop by sub.Err()", "err", err) 996 case <-time.After(timeOut): 997 t.Fatal("Contract Event Loop Running Stop by timeout") 998 } 999 } 1000 } 1001 1002 // TestMultiBridgeKLAYTransferMixConfig2 checks the following: 1003 // - set threshold to 2. 1004 // - The first tx is done. 1005 // - set threshold to 3. 1006 // - remain operators successfully handle the value transfer. 1007 func TestMultiBridgeKLAYTransferMixConfig2(t *testing.T) { 1008 info := prepareMultiBridgeEventTest(t) 1009 defer info.sim.Close() 1010 1011 acc := info.accounts[0] 1012 to := common.Address{100} 1013 1014 opts := &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 1015 tx, err := info.b.SetOperatorThreshold(opts, voteTypeValueTransfer, 2) 1016 assert.NoError(t, err) 1017 info.sim.Commit() 1018 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 1019 1020 nonceOffset := uint64(17) 1021 sentNonce := nonceOffset 1022 transferAmount := uint64(100) 1023 sentBlockNumber := uint64(100000) 1024 1025 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 1026 info.sim.Commit() 1027 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 1028 1029 opts = &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 1030 tx, err = info.b.SetOperatorThreshold(opts, voteTypeValueTransfer, 3) 1031 assert.NoError(t, err) 1032 info.sim.Commit() 1033 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 1034 1035 acc = info.accounts[1] 1036 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 1037 info.sim.Commit() 1038 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 1039 1040 acc = info.accounts[2] 1041 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 1042 info.sim.Commit() 1043 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 1044 1045 for { 1046 select { 1047 case ev := <-info.handleCh: 1048 assert.Equal(t, nonceOffset, ev.HandleNonce) 1049 return 1050 case err := <-info.handleSub.Err(): 1051 t.Fatal("Contract Event Loop Running Stop by sub.Err()", "err", err) 1052 case <-time.After(timeOut): 1053 t.Fatal("Contract Event Loop Running Stop by timeout") 1054 } 1055 } 1056 } 1057 1058 // TestMultiBridgeKLAYTransferMixConfig1 checks the following: 1059 // - set threshold to 2. 1060 // - the first tx is done. 1061 // - set threshold to 1. 1062 // - the first operator successfully handles the value transfer if retry. 1063 func TestMultiBridgeKLAYTransferMixConfig3(t *testing.T) { 1064 info := prepareMultiBridgeEventTest(t) 1065 defer info.sim.Close() 1066 1067 acc := info.accounts[0] 1068 to := common.Address{100} 1069 1070 opts := &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 1071 tx, err := info.b.SetOperatorThreshold(opts, voteTypeValueTransfer, 2) 1072 assert.NoError(t, err) 1073 info.sim.Commit() 1074 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 1075 1076 nonceOffset := uint64(17) 1077 sentNonce := nonceOffset 1078 transferAmount := uint64(100) 1079 sentBlockNumber := uint64(100000) 1080 1081 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 1082 info.sim.Commit() 1083 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 1084 1085 opts = &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 1086 tx, err = info.b.SetOperatorThreshold(opts, voteTypeValueTransfer, 1) 1087 assert.NoError(t, err) 1088 info.sim.Commit() 1089 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 1090 1091 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 1092 info.sim.Commit() 1093 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 1094 1095 for { 1096 select { 1097 case ev := <-info.handleCh: 1098 assert.Equal(t, nonceOffset, ev.HandleNonce) 1099 return 1100 case err := <-info.handleSub.Err(): 1101 t.Fatal("Contract Event Loop Running Stop by sub.Err()", "err", err) 1102 case <-time.After(timeOut): 1103 t.Fatal("Contract Event Loop Running Stop by timeout") 1104 } 1105 } 1106 } 1107 1108 // TestNoncesAndBlockNumber checks the following: 1109 // - set threshold to 2. 1110 // - the first transfer is done and check nonces and block number (req 0, blk 100000) 1111 // - the second transfer is done and check nonces and block number (req 1, blk 100100) 1112 func TestNoncesAndBlockNumber(t *testing.T) { 1113 info := prepareMultiBridgeEventTest(t) 1114 defer info.sim.Close() 1115 1116 acc := info.accounts[0] 1117 to := common.Address{100} 1118 1119 opts := &bind.TransactOpts{From: acc.From, Signer: acc.Signer, GasLimit: DefaultBridgeTxGasLimit} 1120 tx, err := info.b.SetOperatorThreshold(opts, voteTypeValueTransfer, 2) 1121 assert.NoError(t, err) 1122 info.sim.Commit() 1123 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 1124 1125 // First value transfer. 1126 sentNonce := uint64(0) 1127 transferAmount := uint64(100) 1128 sentBlockNumber := uint64(100000) 1129 1130 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 1131 info.sim.Commit() 1132 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 1133 1134 acc = info.accounts[1] 1135 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 1136 info.sim.Commit() 1137 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 1138 1139 hMaxNonce, err := info.b.UpperHandleNonce(nil) 1140 assert.NoError(t, err) 1141 assert.Equal(t, sentNonce, hMaxNonce) 1142 1143 lowerHandleNonce, err := info.b.LowerHandleNonce(nil) 1144 assert.NoError(t, err) 1145 assert.Equal(t, sentNonce+1, lowerHandleNonce) 1146 1147 hBlkNum, err := info.b.RecoveryBlockNumber(nil) 1148 assert.NoError(t, err) 1149 assert.Equal(t, sentBlockNumber, hBlkNum) 1150 1151 // Second value transfer. 1152 sentNonce++ 1153 sentBlockNumber = sentBlockNumber + 100 1154 acc = info.accounts[0] 1155 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 1156 info.sim.Commit() 1157 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 1158 1159 acc = info.accounts[1] 1160 tx = SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 1161 info.sim.Commit() 1162 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 1163 1164 hMaxNonce, err = info.b.UpperHandleNonce(nil) 1165 assert.NoError(t, err) 1166 assert.Equal(t, sentNonce, hMaxNonce) 1167 1168 lowerHandleNonce, err = info.b.LowerHandleNonce(nil) 1169 assert.NoError(t, err) 1170 assert.Equal(t, sentNonce+1, lowerHandleNonce) 1171 1172 hBlkNum, err = info.b.RecoveryBlockNumber(nil) 1173 assert.NoError(t, err) 1174 assert.Equal(t, sentBlockNumber, hBlkNum) 1175 } 1176 1177 // TestNoncesAndBlockNumberUnordered checks the following: 1178 // - default threshold (1). 1179 // - check if reversed request nonce from 2 to 0 success. 1180 // - check if reversed request nonce from 2 to 0 fail. 1181 func TestNoncesAndBlockNumberUnordered(t *testing.T) { 1182 info := prepareMultiBridgeEventTest(t) 1183 defer info.sim.Close() 1184 1185 acc := info.accounts[0] 1186 to := common.Address{100} 1187 1188 transferAmount := uint64(100) 1189 1190 type testParams struct { 1191 // test input 1192 requestNonce uint64 1193 requestBlkNum uint64 1194 // expected result 1195 maxHandledRequestedNonce uint64 1196 minUnhandledRequestNonce uint64 1197 recoveryBlockNumber uint64 1198 } 1199 1200 testCases := []testParams{ 1201 {2, 300, 2, 0, 1}, 1202 {1, 200, 2, 0, 1}, 1203 {0, 100, 2, 3, 300}, 1204 {3, 400, 3, 4, 400}, 1205 } 1206 1207 for i := 0; i < len(testCases); i++ { 1208 sentNonce := testCases[i].requestNonce 1209 sentBlockNumber := testCases[i].requestBlkNum 1210 t.Log("test round", "i", i, "nonce", sentNonce, "blk", sentBlockNumber) 1211 tx := SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 1212 info.sim.Commit() 1213 assert.NoError(t, bind.CheckWaitMined(info.sim, tx)) 1214 1215 hMaxNonce, err := info.b.UpperHandleNonce(nil) 1216 assert.NoError(t, err) 1217 assert.Equal(t, testCases[i].maxHandledRequestedNonce, hMaxNonce) 1218 1219 minUnhandledNonce, err := info.b.LowerHandleNonce(nil) 1220 assert.NoError(t, err) 1221 assert.Equal(t, testCases[i].minUnhandledRequestNonce, minUnhandledNonce) 1222 1223 blkNum, err := info.b.RecoveryBlockNumber(nil) 1224 assert.NoError(t, err) 1225 assert.Equal(t, testCases[i].recoveryBlockNumber, blkNum) 1226 } 1227 1228 lowerHandleNonce, _ := info.b.LowerHandleNonce(nil) 1229 assert.Equal(t, uint64(4), lowerHandleNonce) 1230 1231 for i := 0; i < len(testCases); i++ { 1232 sentNonce := testCases[i].requestNonce 1233 sentBlockNumber := testCases[i].requestBlkNum 1234 t.Log("test round", "i", i, "nonce", sentNonce, "blk", sentBlockNumber) 1235 tx := SendHandleKLAYTransfer(info.b, acc, to, transferAmount, sentNonce, sentBlockNumber, t) 1236 info.sim.Commit() 1237 assert.Error(t, bind.CheckWaitMined(info.sim, tx)) 1238 } 1239 }