2

I'm testing Ethereum smart contracts with web3j and Geth. Geth is running with scripts like that:

PASSFILE=/var/folders/_l/kl9pdj2x50v7416j8htb6w740000gn/T/pwd6597172766860806720.tmp
DATADIR=/var/folders/_l/kl9pdj2x50v7416j8htb6w740000gn/T/geth808290308908087582
IPCPATH=/var/folders/_l/kl9pdj2x50v7416j8htb6w740000gn/T/geth808290308908087582/geth.ipc

geth --datadir $DATADIR account new --password $PASSFILE
geth --datadir $DATADIR \
    --ipcpath $IPCPATH \
    --dev \
    --rpc \
    --mine \
    --networkid 1999

(paths are generated in tests).

Smart contract wrappers are generated with web3j-maven-plugin.

The problem is that every send() invocation takes few seconds:

private TransactionReceipt deployFeature_() throws Exception {
    logger.info("Deploying feature ...");
    return contract.deployFeature(featureKey).send();
}

log (you can see 1 transaction took 16 seconds):

166955 [main] INFO FeatureTest - Deploying feature ...
182006 [main] INFO FeatureTest - Checking feature is listed ...

Web3j is connected over IPC:

web3 = Web3j.build(new UnixIpcService(ipcPath));

How can i speed-up invocations? If testing with TestRPC every invocations takes much less than a second! Since Geth is started with --dev it generates genesis file itself so i can't change difficulty. Also i can see blocks are mined very fast:

INFO [11-09|00:34:39] Commit new mining work                   number=9 txs=0 uncles=0 elapsed=2.000s
INFO [11-09|00:34:39] Successfully sealed new block            number=9 hash=b869ca…870644
INFO [11-09|00:34:39]  block reached canonical chain          number=4 hash=c758a0…a529af
INFO [11-09|00:34:39]  mined potential block                  number=9 hash=b869ca…870644
INFO [11-09|00:34:39] Commit new mining work                   number=10 txs=0 uncles=0 elapsed=84.12µs
INFO [11-09|00:34:40] Successfully sealed new block            number=10 hash=5e81a9…fc1364
INFO [11-09|00:34:40]  block reached canonical chain          number=5  hash=465c2b…78461a
INFO [11-09|00:34:40]  mined potential block                  number=10 hash=5e81a9…fc1364

For some reason invocations are not fast. What can i do?

4ntoine
  • 19,816
  • 21
  • 96
  • 220
  • 1
    try to start `geth --cache XXX` with proper cache value. By default it's 128MB – palik Nov 09 '17 at 09:34
  • 1
    i've just tried `--fast --cache=1024` but every transaction still lasts for ~ 15 seconds though blocks are mined ~ every 1 second – 4ntoine Nov 09 '17 at 10:18

2 Answers2

3

It's web3j feature/issue and you can change this behaviour: https://github.com/web3j/web3j/issues/231

4ntoine
  • 19,816
  • 21
  • 96
  • 220
1

Here is an example of how to change the TransactionManager in a Contract:

// Using RawTransactionManager
TransactionManager transactionManager = new org.web3j.tx.RawTransactionManager(web3j, <credentials>, 40, 500);

// Or using ClientTransactionManager
TransactionManager transactionManager = new org.web3j.tx.ClientTransactionManager(web3j, "0x<fromAddress>", 40, 500);

// Now we'll poll every 500 ms for a transaction response
YourContract contract = YourContract.deploy("0x<contracAddress>", web3j,
        transactionManager, ManagedTransaction.GAS_PRICE, Contract.GAS_LIMIT,
        <initialValue>, <constructorParam1>,...);

I've got this example from here.

pringi
  • 3,987
  • 5
  • 35
  • 45