如何加速私有区块链中的 Geth 交易?
How to speed-up Geth transactions in private blockchain?
我正在用 web3j
和 Geth
测试以太坊智能合约。
Geth 是 运行 这样的脚本:
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
(路径在测试中生成)。
智能合约包装器由 web3j-maven-plugin
.
生成
问题是每次 send()
调用需要几秒钟:
private TransactionReceipt deployFeature_() throws Exception {
logger.info("Deploying feature ...");
return contract.deployFeature(featureKey).send();
}
日志(可以看到1笔交易用了16秒):
166955 [main] INFO FeatureTest - Deploying feature ...
182006 [main] INFO FeatureTest - Checking feature is listed ...
Web3j 通过 IPC 连接:
web3 = Web3j.build(new UnixIpcService(ipcPath));
我怎样才能加快调用速度?如果使用 TestRPC
进行测试,则每次调用都需要不到一秒钟的时间!由于 Geth
是从 --dev
开始的,它会自己生成创世文件,所以我无法更改难度。我还可以看到块的开采速度非常快:
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
由于某些原因调用速度不快。我能做什么?
它是 web3j
feature/issue,您可以更改此行为:
https://github.com/web3j/web3j/issues/231
下面是一个如何在合约中更改 TransactionManager 的示例:
// 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>,...);
我从 here 那里得到了这个例子。
我正在用 web3j
和 Geth
测试以太坊智能合约。
Geth 是 运行 这样的脚本:
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
(路径在测试中生成)。
智能合约包装器由 web3j-maven-plugin
.
问题是每次 send()
调用需要几秒钟:
private TransactionReceipt deployFeature_() throws Exception {
logger.info("Deploying feature ...");
return contract.deployFeature(featureKey).send();
}
日志(可以看到1笔交易用了16秒):
166955 [main] INFO FeatureTest - Deploying feature ...
182006 [main] INFO FeatureTest - Checking feature is listed ...
Web3j 通过 IPC 连接:
web3 = Web3j.build(new UnixIpcService(ipcPath));
我怎样才能加快调用速度?如果使用 TestRPC
进行测试,则每次调用都需要不到一秒钟的时间!由于 Geth
是从 --dev
开始的,它会自己生成创世文件,所以我无法更改难度。我还可以看到块的开采速度非常快:
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
由于某些原因调用速度不快。我能做什么?
它是 web3j
feature/issue,您可以更改此行为:
https://github.com/web3j/web3j/issues/231
下面是一个如何在合约中更改 TransactionManager 的示例:
// 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>,...);
我从 here 那里得到了这个例子。