MCPcopy Create free account
hub / github.com/CPChain/chain / TestRefund

Function TestRefund

contracts/dpor/rnode/rnode_test.go:376–433  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

374}
375
376func TestRefund(t *testing.T) {
377 // deploy contract
378 contractBackend := backends.NewDporSimulatedBackend(core.GenesisAlloc{
379 ownerAddr: {Balance: big.NewInt(1000000000000)},
380 candidateAddr: {Balance: new(big.Int).Mul(big.NewInt(1000000), big.NewInt(configs.Cpc))},
381 candidate2Addr: {Balance: new(big.Int).Mul(big.NewInt(1000000), big.NewInt(configs.Cpc))}})
382 _, instance := deploy(ownerKey, contractBackend)
383
384 // get balance
385 state, err := contractBackend.Blockchain().State()
386 checkError(t, "get state", err)
387
388 amount1 := state.GetBalance(candidateAddr)
389
390 // join rnode
391 candidateTransactOpts := bind.NewKeyedTransactor(candidateKey)
392 candidateInvest := new(big.Int).Mul(big.NewInt(200000), big.NewInt(1e+18))
393 candidateTransactOpts.GasLimit = uint64(50000000)
394 candidateTransactOpts.Value = candidateInvest
395 _, err = instance.JoinRnode(candidateTransactOpts, initVersion)
396 checkError(t, "join rnode", err)
397
398 contractBackend.Commit()
399
400 state, err = contractBackend.Blockchain().State()
401
402 amount2 := state.GetBalance(candidateAddr)
403
404 if amount2.Cmp(amount1) >= 0 || new(big.Int).Add(amount2, candidateInvest).Cmp(amount1) >= 0 {
405 t.Fatalf("balance error amount1: %v, amount2 + invest: %v", amount1, new(big.Int).Add(amount2, candidateInvest))
406 }
407
408 checkIsRNode(t, instance, candidateAddr, true)
409
410 checkRNodeNum(t, instance, 1)
411
412 // refund
413 ownerTransactOpts := bind.NewKeyedTransactor(ownerKey)
414 ownerInvest := new(big.Int).Mul(big.NewInt(0), big.NewInt(1e+18))
415 ownerTransactOpts.GasLimit = uint64(50000000)
416 ownerTransactOpts.Value = ownerInvest
417
418 _, err = instance.Refund(ownerTransactOpts, candidateAddr)
419 checkError(t, "refund", err)
420
421 contractBackend.Commit()
422
423 checkIsRNode(t, instance, candidateAddr, false)
424
425 checkRNodeNum(t, instance, 0)
426
427 state, err = contractBackend.Blockchain().State()
428
429 amount3 := state.GetBalance(candidateAddr)
430 if amount3.Cmp(amount2) <= 0 {
431 t.Fatalf("amount3 %v <= amount2 %v", amount3, amount2)
432 }
433}

Callers

nothing calls this directly

Calls 12

checkIsRNodeFunction · 0.85
checkRNodeNumFunction · 0.85
BlockchainMethod · 0.80
CmpMethod · 0.80
deployFunction · 0.70
checkErrorFunction · 0.70
StateMethod · 0.65
GetBalanceMethod · 0.65
JoinRnodeMethod · 0.65
CommitMethod · 0.65
AddMethod · 0.65
RefundMethod · 0.45

Tested by

no test coverage detected