github.com/iotexproject/iotex-core@v1.14.1-rc1/tools/executiontester/executiontester.go (about) 1 // Copyright (c) 2019 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package main 7 8 import ( 9 "context" 10 "flag" 11 "math/big" 12 "time" 13 14 "github.com/iotexproject/go-pkgs/crypto" 15 "github.com/pkg/errors" 16 "go.uber.org/zap" 17 18 "github.com/iotexproject/iotex-core/config" 19 "github.com/iotexproject/iotex-core/pkg/log" 20 "github.com/iotexproject/iotex-core/pkg/probe" 21 "github.com/iotexproject/iotex-core/server/itx" 22 "github.com/iotexproject/iotex-core/tools/executiontester/assetcontract" 23 "github.com/iotexproject/iotex-core/tools/executiontester/blockchain" 24 ) 25 26 func main() { 27 // total indicates the total amount value of a fp token 28 var total int64 29 // risk indicates the risk amount value of a fp token 30 var risk int64 31 // transfer indicates the transfer amount value of a fp token 32 var transfer int64 33 34 flag.Int64Var(&total, "total", 10000, "total amount value of a fp token") 35 flag.Int64Var(&risk, "risk", 2000, "risk amount value of a fp token") 36 flag.Int64Var(&transfer, "transfer", 1000, "transfer amount value of a fp token") 37 flag.Parse() 38 39 if risk > total { 40 log.L().Fatal("risk amount cannot be greater than total amount") 41 } 42 43 ctx := context.Background() 44 // Start iotex-server 45 cfg := config.Default 46 cfg.Plugins[config.GatewayPlugin] = true 47 cfg.Chain.EnableAsyncIndexWrite = false 48 cfg.Genesis.BlockInterval = 2 * time.Second 49 cfg.ActPool.MinGasPriceStr = big.NewInt(0).String() 50 itxsvr, err := itx.NewServer(cfg) 51 if err != nil { 52 log.L().Fatal("Failed to start itxServer.", zap.Error(err)) 53 } 54 go itx.StartServer(ctx, itxsvr, probe.New(7799), cfg) 55 56 time.Sleep(2 * time.Second) 57 58 // Deploy contracts 59 ret, err := assetcontract.StartContracts(cfg) 60 if err != nil { 61 log.L().Fatal("Failed to deploy contracts.", zap.Error(err)) 62 } 63 64 // Create two accounts 65 _, debtorPriKey, debtorAddr, err := createAccount() 66 if err != nil { 67 log.L().Fatal("Failed to create account.", zap.Error(err)) 68 } 69 _, creditorPriKey, creditorAddr, err := createAccount() 70 if err != nil { 71 log.L().Fatal("Failed to create account.", zap.Error(err)) 72 } 73 74 // Create fp token 75 assetID := assetcontract.GenerateAssetID() 76 open := time.Now().Unix() 77 exp := open + 100000 78 79 if _, err := ret.FpToken.CreateToken(assetID, debtorAddr, creditorAddr, total, risk, open, exp); err != nil { 80 log.L().Fatal("Failed to create fp token", zap.Error(err)) 81 } 82 // create fpToken with assetID 83 contractAddr, err := ret.FpToken.TokenAddress(assetID) 84 if err != nil { 85 log.L().Fatal("Failed to get token contract address", zap.Error(err)) 86 } 87 tokenID := assetcontract.GenerateAssetID() 88 // create erc721 token with tokenID 89 if _, err := ret.Erc721Token.CreateToken(tokenID, creditorAddr); err != nil { 90 log.L().Fatal("Failed to create erc721 token", zap.Error(err)) 91 } 92 // Transfer fp token 93 if _, err := ret.FpToken.Transfer(contractAddr, debtorAddr, debtorPriKey, creditorAddr, total); err != nil { 94 log.L().Fatal("Failed to transfer total amount from debtor to creditor", zap.Error(err)) 95 } 96 // Transfer erc721 token 97 _, err = ret.Erc721Token.Transfer(ret.Erc721Token.Address(), creditorAddr, creditorPriKey, debtorAddr, tokenID) 98 if err != nil { 99 log.L().Fatal("Failed to transfer 1 token from creditor to debtor", zap.Error(err)) 100 } 101 102 if _, err := ret.FpToken.RiskLock(contractAddr, creditorAddr, creditorPriKey, risk); err != nil { 103 log.L().Fatal("Failed to transfer amount of risk from creditor to contract", zap.Error(err)) 104 } 105 106 debtorBalance, err := ret.FpToken.ReadValue(contractAddr, blockchain.BalanceOf, debtorAddr) 107 if err != nil { 108 log.L().Fatal("Failed to get debtor's asset balance.", zap.Error(err)) 109 } 110 log.L().Info("Debtor's asset balance: ", zap.Int64("balance", debtorBalance)) 111 112 creditorBalance, err := ret.FpToken.ReadValue(contractAddr, blockchain.BalanceOf, creditorAddr) 113 if err != nil { 114 log.L().Fatal("Failed to get creditor's asset balance.", zap.Error(err)) 115 } 116 log.L().Info("Creditor's asset balance: ", zap.Int64("balance", creditorBalance)) 117 118 if debtorBalance+creditorBalance != total-risk { 119 log.L().Fatal("Sum of balance is incorrect.") 120 } 121 122 log.L().Info("Fp token transfer test pass!") 123 // get debtor balance,should be 1 124 debtorBalance, err = ret.Erc721Token.ReadValue(ret.Erc721Token.Address(), blockchain.BalanceOf, debtorAddr) 125 if err != nil { 126 log.L().Fatal("Failed to get erc721 debtor's asset balance.", zap.Error(err)) 127 } 128 log.L().Info("erc721 Debtor's asset balance: ", zap.Int64("balance", debtorBalance)) 129 // get creditor balance,should be 0 130 creditorBalance, err = ret.Erc721Token.ReadValue(ret.Erc721Token.Address(), blockchain.BalanceOf, creditorAddr) 131 if err != nil { 132 log.L().Fatal("Failed to get erc721 creditor's asset balance.", zap.Error(err)) 133 } 134 log.L().Info("erc721 Creditor's asset balance: ", zap.Int64("balance", creditorBalance)) 135 if (debtorBalance) != 1 && (creditorBalance != 0) { 136 log.L().Fatal("erc721 balance is incorrect.") 137 } 138 log.L().Info("erc721 token transfer test pass!") 139 140 // test ArrayDeletePassing func 141 testArrayDeletePassing(ret.ArrDeletePassing) 142 // test ArrayString func 143 testArrayString(ret.ArrString) 144 } 145 func testArrayString(arr blockchain.ArrayString) { 146 ret, err := arr.GetString() 147 if err != nil { 148 log.L().Fatal("Failed to call array-of-strings Func", zap.Error(err)) 149 } 150 expected := "bye" 151 if ret != expected { 152 log.L().Fatal("one return is incorrect:", zap.String("string", ret)) 153 } 154 log.L().Info("array-of-strings test pass!") 155 } 156 func testArrayDeletePassing(arr blockchain.ArrayDeletePassing) { 157 ret, err := arr.GetArray() 158 if err != nil { 159 log.L().Fatal("Failed to call array-delete-passing GetArray Func", zap.Error(err)) 160 } 161 expected := []*big.Int{big.NewInt(100), big.NewInt(200), big.NewInt(0), big.NewInt(400), big.NewInt(500)} 162 163 for i, v := range ret { 164 if v.Cmp(expected[i]) != 0 { 165 log.L().Fatal("one return is incorrect:", zap.Int("array", i)) 166 } 167 } 168 169 retNum, err := arr.GetNum() 170 if err != nil { 171 log.L().Fatal("Failed to call array-delete-passing Func", zap.Error(err)) 172 } 173 expectedNum := int64(10) 174 if retNum != expectedNum { 175 log.L().Fatal("one return is incorrect:", zap.Int64("string", retNum)) 176 } 177 log.L().Info("array-delete-passing test pass!") 178 } 179 func createAccount() (string, string, string, error) { 180 priKey, err := crypto.GenerateKey() 181 if err != nil { 182 return "", "", "", err 183 } 184 pubKey := priKey.PublicKey() 185 addr := pubKey.Address() 186 if addr == nil { 187 return "", "", "", errors.New("failed to get address") 188 } 189 return pubKey.HexString(), priKey.HexString(), addr.String(), nil 190 }