p2p_network_test.py 7.1 KB
Newer Older
1 2 3 4
#!/usr/bin/env python3

import testUtils
import p2p_test_peers
5
import impaired_network
6 7 8 9 10 11 12 13
import lossy_network
import p2p_stress

import copy
import decimal
import argparse
import random
import re
14
import time
15 16 17 18 19 20 21 22 23 24 25

Remote = p2p_test_peers.P2PTestPeers

hosts=Remote.hosts
ports=Remote.ports

DEFAULT_HOST=hosts[0]
DEFAULT_PORT=ports[0]

parser = argparse.ArgumentParser(add_help=False)
Print=testUtils.Utils.Print
26
cmdError=Utils.cmdError
27
errorExit=Utils.errorExit
28 29 30 31

# Override default help argument so that only --help (and not -h) can call help
parser.add_argument('-?', action='help', default=argparse.SUPPRESS,
                    help=argparse._('show this help message and exit'))
32 33 34 35
parser.add_argument("--defproducera_prvt_key", type=str, help="defproducera private key.",
                    default=testUtils.Cluster.defproduceraAccount.ownerPrivateKey)
parser.add_argument("--defproducerb_prvt_key", type=str, help="defproducerb private key.",
                    default=testUtils.Cluster.defproducerbAccount.ownerPrivateKey)
36 37
parser.add_argument("--wallet_host", type=str, help="wallet host", default="localhost")
parser.add_argument("--wallet_port", type=int, help="wallet port", default=8899)
38 39 40
parser.add_argument("--impaired_network", help="test impaired network", action='store_true')
parser.add_argument("--lossy_network", help="test lossy network", action='store_true')
parser.add_argument("--stress_network", help="test load/stress network", action='store_true')
41
parser.add_argument("--not_kill_wallet", help="not killing walletd", action='store_true')
42 43 44

args = parser.parse_args()
enableMongo=False
45 46
defproduceraPrvtKey=args.defproducera_prvt_key
defproducerbPrvtKey=args.defproducerb_prvt_key
47

48 49 50 51 52
walletMgr=testUtils.WalletMgr(True, port=args.wallet_port, host=args.wallet_host)

if args.impaired_network:
    module = impaired_network.ImpairedNetwork()
elif args.lossy_network:
53
    module = lossy_network.LossyNetwork()
54
elif args.stress_network:
55 56
    module = p2p_stress.StressNetwork()
else:
57
    errorExit("one of impaired_network, lossy_network or stress_network must be set. Please also check peer configs in p2p_test_peers.py.")
58

59
cluster=testUtils.Cluster(walletd=True, enableMongo=enableMongo, defproduceraPrvtKey=defproduceraPrvtKey, defproducerbPrvtKey=defproducerbPrvtKey, walletHost=args.wallet_host, walletPort=args.wallet_port)
60 61 62

print("BEGIN")

63
print("number of hosts: %d, list of hosts:" % (len(hosts)))
64 65 66 67 68 69 70 71 72 73
for i in range(len(hosts)):
    Print("%s:%d" % (hosts[i], ports[i]))

init_str='{"nodes":['
for i in range(len(hosts)):
    if i > 0:
        init_str=init_str+','
    init_str=init_str+'{"host":"' + hosts[i] + '", "port":'+str(ports[i])+'}'
init_str=init_str+']}'

74
#Print('init nodes with json str %s',(init_str))
75 76
cluster.initializeNodesFromJson(init_str);

77 78 79 80
if args.not_kill_wallet == False:
    print('killing all wallets')
    walletMgr.killall()

81 82 83 84 85 86 87
walletMgr.cleanup()

print('creating account keys')
accounts=testUtils.Cluster.createAccountKeys(3)
if accounts is None:
    errorExit("FAILURE - create keys")
testeraAccount=accounts[0]
88
testeraAccount.name="testera11111"
89
currencyAccount=accounts[1]
90
currencyAccount.name="currency1111"
91
exchangeAccount=accounts[2]
92
exchangeAccount.name="exchange1111"
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

PRV_KEY1=testeraAccount.ownerPrivateKey
PUB_KEY1=testeraAccount.ownerPublicKey
PRV_KEY2=currencyAccount.ownerPrivateKey
PUB_KEY2=currencyAccount.ownerPublicKey
PRV_KEY3=exchangeAccount.activePrivateKey
PUB_KEY3=exchangeAccount.activePublicKey

testeraAccount.activePrivateKey=currencyAccount.activePrivateKey=PRV_KEY3
testeraAccount.activePublicKey=currencyAccount.activePublicKey=PUB_KEY3

exchangeAccount.ownerPrivateKey=PRV_KEY2
exchangeAccount.ownerPublicKey=PUB_KEY2

print("Stand up walletd")
if walletMgr.launch() is False:
    cmdError("%s" % (WalletdName))
    errorExit("Failed to stand up eos walletd.")

testWalletName="test"
Print("Creating wallet \"%s\"." % (testWalletName))
testWallet=walletMgr.create(testWalletName)

for account in accounts:
    Print("Importing keys for account %s into wallet %s." % (account.name, testWallet.name))
    if not walletMgr.importKey(account, testWallet):
        cmdError("%s wallet import" % (ClientName))
        errorExit("Failed to import key for account %s" % (account.name))

122 123 124
defproduceraWalletName="defproducera"
Print("Creating wallet \"%s\"." % (defproduceraWalletName))
defproduceraWallet=walletMgr.create(defproduceraWalletName)
125

126 127
defproduceraAccount=testUtils.Cluster.defproduceraAccount
# defproducerbAccount=testUtils.Cluster.defproducerbAccount
128

129 130
Print("Importing keys for account %s into wallet %s." % (defproduceraAccount.name, defproduceraWallet.name))
if not walletMgr.importKey(defproduceraAccount, defproduceraWallet):
131
     cmdError("%s wallet import" % (ClientName))
132
     errorExit("Failed to import key for account %s" % (defproduceraAccount.name))
133 134 135

node0=cluster.getNode(0)

136 137
# eosio should have the same key as defproducera
eosio = copy.copy(defproduceraAccount)
138 139
eosio.name = "eosio"

140 141
Print("Info of each node:")
for i in range(len(hosts)):
142
    node = node0
143 144 145 146 147
    cmd="%s %s get info" % (testUtils.Utils.EosClientPath, node.endpointArgs)
    trans = node.runCmdReturnJson(cmd)
    Print("host %s: %s" % (hosts[i], trans))


148
wasmFile="eosio.system.wasm"
149
abiFile="eosio.system.abi"
150 151
Print("\nPush system contract %s %s" % (wasmFile, abiFile))
trans=node0.publishContract(eosio.name, wasmFile, abiFile, waitForTransBlock=True)
152 153 154 155 156
if trans is None:
    Utils.errorExit("Failed to publish eosio.system.")
else:
    Print("transaction id %s" % (node0.getTransId(trans)))

157 158 159
try:
    maxIndex = module.maxIndex()
    for cmdInd in range(maxIndex):
K
Kayan 已提交
160
        (transIdList, checkacct, expBal, errmsg) = module.execute(cmdInd, node0, testeraAccount, eosio)
161 162

        if len(transIdList) == 0 and len(checkacct) == 0:
K
Kayan 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
            errorExit("failed to execute command in host %s:%s" % (hosts[0], errmsg))

        successhosts = []
        attempts = 2
        while attempts > 0 and len(successhosts) < len(hosts):
            attempts = attempts - 1
            for i in range(len(hosts)):
                host = hosts[i]
                if host in successhosts:
                    continue
                if len(checkacct) > 0:
                    actBal = cluster.getNode(i).getAccountBalance(checkacct)
                    if expBal == actBal:
                        Print("acct balance verified in host %s" % (host))
                    else:
                        Print("acct balance check failed in host %s, expect %d actual %d" % (host, expBal, actBal))
                okcount = 0
                failedcount = 0
                for j in range(len(transIdList)):
                    if cluster.getNode(i).getTransaction(transIdList[j]) is None:
                        failedcount = failedcount + 1
                    else:
                        okcount = okcount + 1
                Print("%d transaction(s) verified in host %s, %d transaction(s) failed" % (okcount, host, failedcount))
                if failedcount == 0:
                    successhosts.append(host)
        Print("%d host(s) passed, %d host(s) failed" % (len(successhosts), len(hosts) - len(successhosts)))
190 191 192 193
finally:
    Print("\nfinally: restore everything")
    module.on_exit()
exit(0)