Commit 2a9c40e7 authored by zeroleak's avatar zeroleak
Browse files

new setting "server.rpc-client.protocol = mock"

Read blockchain data from mock instead of retrieving it from a real bitcoin node.
parent 1549228a
......@@ -19,7 +19,9 @@ java -jar target/whirlpool-server-0.0.1-SNAPSHOT.jar --spring.config.location=./
## Configuration
### RPC client
```
server.rpc-client.protocol = http
server.rpc-client.host = CONFIGURE-ME
server.rpc-client.port = CONFIGURE-ME
server.rpc-client.user = CONFIGURE-ME
server.rpc-client.password = CONFIGURE-ME
```
......@@ -54,9 +56,15 @@ After *server.round.liquidity-timeout* or when current *anonymity-set-target* is
```
server.rpc-client.mock-tx-broadcast = false
```
For testing purpose, *server.rpc-client.mock-tx-broadcast* can be enabled to mock txs instead of broadcasting it.<br/>
For testing purpose, *server.rpc-client.mock-tx-broadcast* can be enabled to mock txs instead of broadcasting it.
When enabled, server will keep whirlpool txs in memory until server restart and act as if these txs are confirmed in blockchain.
```
server.rpc-client.protocol = mock
```
For testing purpose, *server.rpc-client.protocol* can be set to *mock* to read blockchain data from mock instead of retrieving it from a real bitcoin node.
### Building
- Use *build.sh* on your local developer machine:
```
......
......@@ -17,12 +17,12 @@ import org.springframework.core.task.TaskExecutor;
import wf.bitcoin.javabitcoindrpcclient.BitcoinJSONRPCClient;
import java.lang.invoke.MethodHandles;
import java.net.MalformedURLException;
import java.net.URL;
@Configuration
public class ServicesConfig {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static final String RPC_CLIENT_PROTOCOL_MOCK = "mock";
@Autowired
private WhirlpoolServerConfig whirlpoolServerConfig;
......@@ -33,7 +33,12 @@ public class ServicesConfig {
}
@Bean
BitcoinJSONRPCClient bitcoinJSONRPCClient() throws Exception {
BitcoinJSONRPCClient bitcoinJSONRPCClient(WhirlpoolServerConfig whirlpoolServerConfig) throws Exception {
boolean mockRpc = isMockRpc(whirlpoolServerConfig);
if (mockRpc) {
log.warn("server.rpc-client.protocol=mock, no real blockchain data will be used");
return null;
}
URL rpcClientUrl = computeRpcClientUrl();
BitcoinJSONRPCClient rpcClient = new BitcoinJSONRPCClient(rpcClientUrl);
return rpcClient;
......@@ -74,11 +79,30 @@ public class ServicesConfig {
}
@Bean
public BlockchainDataService blockchainDataService(BitcoinJSONRPCClient rpcClient, CryptoService cryptoService, Bech32Util bech32Util, WhirlpoolServerConfig whirlpoolServerConfig) {
if (whirlpoolServerConfig.getRpcClient().isMockTxBroadcast()) {
log.warn("server.rpc-client.mock-tx-broadcast=TRUE, tx WON'T be broadcasted by server");
return new MockBlockchainDataService(rpcClient, cryptoService, bech32Util, whirlpoolServerConfig);
public BlockchainDataService blockchainDataService(BitcoinJSONRPCClient rpcClient, WhirlpoolServerConfig whirlpoolServerConfig, MockBlockchainDataService mockBlockchainDataService) {
if (mockBlockchainDataService != null) {
log.info("Loading BlockchainDataService... mocked");
return mockBlockchainDataService;
}
log.info("Loading BlockchainDataService... production");
return new BlockchainDataService(rpcClient, whirlpoolServerConfig);
}
@Bean
public MockBlockchainDataService mockBlockchainDataService(BitcoinJSONRPCClient rpcClient, CryptoService cryptoService, Bech32Util bech32Util, WhirlpoolServerConfig whirlpoolServerConfig) {
boolean mockRpc = isMockRpc(whirlpoolServerConfig);
if (whirlpoolServerConfig.getRpcClient().isMockTxBroadcast() || mockRpc) {
log.warn("server.rpc-client.mock-tx-broadcast=TRUE, tx WON'T be broadcasted by server");
return new MockBlockchainDataService(rpcClient, cryptoService, bech32Util, whirlpoolServerConfig, mockRpc);
}
// mock disabled
return null;
}
private boolean isMockRpc(WhirlpoolServerConfig whirlpoolServerConfig) {
return RPC_CLIENT_PROTOCOL_MOCK.equals(whirlpoolServerConfig.getRpcClient().getProtocol().toLowerCase());
}
}
......@@ -21,24 +21,41 @@ public class MockBlockchainDataService extends BlockchainDataService {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private CryptoService cryptoService;
private Bech32Util bech32Util;
private boolean mockRpc;
private Map<String,RpcTransaction> mockTransactions;
private static final int MOCK_TX_CONFIRMATIONS = 99;
public MockBlockchainDataService(BitcoinJSONRPCClient rpcClient, CryptoService cryptoService, Bech32Util bech32Util, WhirlpoolServerConfig whirlpoolServerConfig) {
public MockBlockchainDataService(BitcoinJSONRPCClient rpcClient, CryptoService cryptoService, Bech32Util bech32Util, WhirlpoolServerConfig whirlpoolServerConfig, boolean mockRpc) {
super(rpcClient, whirlpoolServerConfig);
this.cryptoService = cryptoService;
this.bech32Util = bech32Util;
this.mockRpc = mockRpc;
this.mockTransactions = new HashMap<>();
}
@Override
public boolean testConnectivity() {
if (mockRpc) {
log.info("Connecting to bitcoin node... MOCK");
return true;
}
return super.testConnectivity();
}
@Override
public RpcTransaction getRpcTransaction(String hash) {
RpcTransaction rpcTransaction = mockTransactions.get(hash);
if (rpcTransaction != null) {
return rpcTransaction;
}
return super.getRpcTransaction(hash);
if (mockRpc) {
log.error("mocked tx not found: " + hash);
return null;
}
else {
return super.getRpcTransaction(hash);
}
}
@Override
......
# mock tx broadcast for tests
server.rpc-client.protocol = mock
server.rpc-client.mock-tx-broadcast = true
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment