0

I'm trying to create 100k ethereum wallets for test purposes. All should use same pass phrase, because it doesn't matter right now. I launched this code with 10 threads and it froze my macbook, I had to restart it. 3 thread somewhat work, but it's still very slow (generates like ~6 wallets a second). What's wrong? I'm using web3j dependency

    public EthWallets(String[] args)
    {
        File destinationDir = new File("ethwallets/");
        if(args[1].equalsIgnoreCase("create")) {
            try {
                int count = Integer.valueOf(args[2]);
                if(count > 10)
                {
                    final int threadedCount = count / 10;
                    for(int i = 0; i < 10; i++)
                    {
                        Thread t = new Thread()
                        {
                            @Override
                            public void run() {
                                for(int j = 0; j < threadedCount; j++)
                                {
                                    create("passphph", destinationDir);
                                }
                            }
                        };
                        t.start();
                    }
                }
                else
                    for(int i = 0; i < count; i++)
                        create("passphph", destinationDir);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void create(String passPhrase, File destinationDirectory)
    {
        try {
            String path = WalletUtils.generateFullNewWalletFile(passPhrase, destinationDirectory);
//            Credentials credentials = WalletUtils.loadCredentials(passPhrase, new File(path));
//            System.out.println("address: " + credentials.getAddress());
//            System.out.println("private: " + credentials.getEcKeyPair().getPrivateKey());
//            System.out.println("public: " + credentials.getEcKeyPair().getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Michal Borowiecki
  • 4,244
  • 1
  • 11
  • 18
Gintas_
  • 4,940
  • 12
  • 44
  • 87
  • Because you're running too many threads for your processor to handle? Use fewer threads or a better processor – konsolas Aug 29 '17 at 19:53
  • is it really that much calculation? It's only a few crypto functions to generate the wallet – Gintas_ Aug 29 '17 at 19:54
  • You stated that your problem was that your macbook froze. The individual calculations may not be intensive, but 100,000 of them right after each other are. I'd assume you have a laptop with a dual core processor/hyperthreading, so this task maxes out every single thread, leaving little room for the OS and UI. – konsolas Aug 29 '17 at 19:56
  • so you really think a CPU cannot generate more than ~6 wallets per second? Do you realise how little crypto functions are needed to be executed for single wallet? – Gintas_ Aug 29 '17 at 20:01
  • I can't answer that, but even if your wallet generation was faster, you'd still freeze your macbook if you started too many threads. Maybe a different library is the way to go because there aren't any problems with the java code you posted. – konsolas Aug 29 '17 at 20:19
  • 2
    I am pretty sure that the library uses `SecureRandom`, intensive usage of which may result in [entropy exhaustion](https://major.io/2007/07/01/check-available-entropy-in-linux/) – user3707125 Aug 29 '17 at 20:22
  • @user3707125 correct, replaced it with new Random()... – Gintas_ Aug 29 '17 at 21:15
  • There are things such as `FutureTask` that are more lightweight than threads. Basically you need something that runs irrespective of the amount of threads, so a higher level, lightweight component would be beneficial. Using Random is of course insecure and ugly, reusing the same `SecureRandom` instance would be a better idea. – Maarten Bodewes Aug 29 '17 at 21:20
  • Looking the code for `WalletUtils.generateFullNewWalletFile(...)`, it is not at all optimized for multiple calls, but that isn't the major cause of the poor performance. For security reasons it is intentionally slow, using scrypt to generate a key from the password for securing the wallet. Of course if you were going to use the same password for every wallet then you could essentially defeat this. If you instead call `WalletUtils.generateLightNewWalletFile()` the key generation will be 64 times faster. – President James K. Polk Aug 29 '17 at 22:36

1 Answers1

-1

I suggest you to read this reply from https://stackoverflow.com/a/1718522/5632150

As he said, the number of threads you can spawn depends on the fact that your threads do or do not any I/O operation. If so there are some ways to optimize this problem. If not I usually do MAX_THREADS = N_CORES + 1.

MrGoodKat
  • 41
  • 6