use of org.ethereum.core.Repository in project rskj by rsksmart.
the class BridgeStorageProviderTest method setFeePerKb_savedAndRecreated.
@Test
public void setFeePerKb_savedAndRecreated() {
Repository repository = new RepositoryImpl(config);
Repository track = repository.startTracking();
BridgeStorageProvider provider0 = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
Coin expectedCoin = Coin.valueOf(5325);
provider0.setFeePerKb(expectedCoin);
provider0.saveFeePerKb();
track.commit();
track = repository.startTracking();
BridgeStorageProvider provider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
assertThat(provider.getFeePerKb(), is(expectedCoin));
}
use of org.ethereum.core.Repository in project rskj by rsksmart.
the class BridgeStorageProviderTest method createSaveAndRecreateInstanceWithUTXOS.
@Test
public void createSaveAndRecreateInstanceWithUTXOS() throws IOException {
Sha256Hash hash1 = PegTestUtils.createHash();
Sha256Hash hash2 = PegTestUtils.createHash();
Repository repository = new RepositoryImpl(config);
Repository track = repository.startTracking();
BridgeConstants bridgeConstants = config.getBlockchainConfig().getCommonConstants().getBridgeConstants();
// Federation is the genesis federation ATM
Federation federation = bridgeConstants.getGenesisFederation();
BridgeStorageProvider provider0 = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
provider0.getNewFederationBtcUTXOs().add(new UTXO(hash1, 1, Coin.COIN, 0, false, ScriptBuilder.createOutputScript(federation.getAddress())));
provider0.getNewFederationBtcUTXOs().add(new UTXO(hash2, 2, Coin.FIFTY_COINS, 0, false, ScriptBuilder.createOutputScript(federation.getAddress())));
provider0.save();
track.commit();
track = repository.startTracking();
BridgeStorageProvider provider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
List<UTXO> utxos = provider.getNewFederationBtcUTXOs();
Assert.assertTrue(utxos.get(0).getHash().equals(hash1));
Assert.assertTrue(utxos.get(1).getHash().equals(hash2));
}
use of org.ethereum.core.Repository in project rskj by rsksmart.
the class BridgeStorageProviderTest method getFederationElection_nullBytes.
@Test
public void getFederationElection_nullBytes() throws IOException {
List<Integer> calls = new ArrayList<>();
AddressBasedAuthorizer authorizerMock = mock(AddressBasedAuthorizer.class);
ABICallElection electionMock = mock(ABICallElection.class);
PowerMockito.mockStatic(BridgeSerializationUtils.class);
Repository repositoryMock = mock(Repository.class);
BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
when(repositoryMock.getStorageBytes(any(RskAddress.class), any(DataWord.class))).then((InvocationOnMock invocation) -> {
calls.add(0);
RskAddress contractAddress = invocation.getArgumentAt(0, RskAddress.class);
DataWord address = invocation.getArgumentAt(1, DataWord.class);
// Make sure the bytes are got from the correct address in the repo
Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd }, contractAddress.getBytes()));
Assert.assertEquals(new DataWord("federationElection".getBytes(StandardCharsets.UTF_8)), address);
return null;
});
PowerMockito.when(BridgeSerializationUtils.deserializeElection(any(byte[].class), any(AddressBasedAuthorizer.class))).then((InvocationOnMock invocation) -> {
calls.add(0);
return null;
});
ABICallElection result = storageProvider.getFederationElection(authorizerMock);
Assert.assertSame(authorizerMock, Whitebox.getInternalState(result, "authorizer"));
Assert.assertEquals(0, result.getVotes().size());
// getStorageBytes is the only one called (can't be the other way around)
Assert.assertEquals(1, calls.size());
}
use of org.ethereum.core.Repository in project rskj by rsksmart.
the class BridgeStorageProviderTest method getLockWhitelist_nonNullBytes.
@Test
public void getLockWhitelist_nonNullBytes() throws IOException {
List<Integer> calls = new ArrayList<>();
LockWhitelist whitelistMock = mock(LockWhitelist.class);
PowerMockito.mockStatic(BridgeSerializationUtils.class);
Repository repositoryMock = mock(Repository.class);
BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
Context contextMock = mock(Context.class);
when(contextMock.getParams()).thenReturn(NetworkParameters.fromID(NetworkParameters.ID_REGTEST));
Whitebox.setInternalState(storageProvider, "btcContext", contextMock);
when(repositoryMock.getStorageBytes(any(RskAddress.class), any(DataWord.class))).then((InvocationOnMock invocation) -> {
calls.add(0);
RskAddress contractAddress = invocation.getArgumentAt(0, RskAddress.class);
DataWord address = invocation.getArgumentAt(1, DataWord.class);
// Make sure the bytes are got from the correct address in the repo
Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd }, contractAddress.getBytes()));
Assert.assertEquals(new DataWord("lockWhitelist".getBytes(StandardCharsets.UTF_8)), address);
return new byte[] { (byte) 0xaa };
});
PowerMockito.when(BridgeSerializationUtils.deserializeLockWhitelist(any(byte[].class), any(NetworkParameters.class))).then((InvocationOnMock invocation) -> {
calls.add(0);
byte[] data = invocation.getArgumentAt(0, byte[].class);
NetworkParameters parameters = invocation.getArgumentAt(1, NetworkParameters.class);
Assert.assertEquals(NetworkParameters.fromID(NetworkParameters.ID_REGTEST), parameters);
// Make sure we're deserializing what just came from the repo with the correct AddressBasedAuthorizer
Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa }, data));
return whitelistMock;
});
Assert.assertSame(whitelistMock, storageProvider.getLockWhitelist());
// 1 for each call to deserializeFederation & getStorageBytes
Assert.assertEquals(2, calls.size());
}
use of org.ethereum.core.Repository in project rskj by rsksmart.
the class RepositoryBlockStoreTest method test.
@Test
public void test() throws Exception {
// This Is how I produced RepositoryBlockStore_data.ser. I had a bitcoind in regtest with 613 blocks + genesis block
// NetworkParameters params = RegTestParams.get();
// Context context = new Context(params);
// Wallet wallet = new Wallet(context);
// BlockStore store = new SPVBlockStore(params, new File("spvBlockstore"));
// AbstractBlockChain chain = new BlockChain(context, wallet, store);
// PeerGroup peerGroup = new PeerGroup(context, chain);
// peerGroup.start();
// final DownloadProgressTracker listener = new DownloadProgressTracker();
// peerGroup.startBlockChainDownload(listener);
// listener.await();
// peerGroup.stop();
// StoredBlock storedBlock = chain.getChainHead();
// FileOutputStream fos = new FileOutputStream("RepositoryBlockStore_data.ser");
// ObjectOutputStream oos = new ObjectOutputStream(fos);
// for (int i = 0; i < 614; i++) {
// Triple<byte[], BigInteger , Integer> tripleStoredBlock = new ImmutableTriple<>(storedBlock.getHeader().bitcoinSerialize(), storedBlock.getChainWork(), storedBlock.getHeight());
// oos.writeObject(tripleStoredBlock);
// storedBlock = store.get(storedBlock.getHeader().getPrevBlockHash());
// }
// oos.close();
// Read original store
InputStream fileInputStream = ClassLoader.getSystemResourceAsStream("peg/RepositoryBlockStore_data.ser");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
Repository repository = new RepositoryImplForTesting();
RskSystemProperties config = new RskSystemProperties();
RepositoryBlockStore store = new RepositoryBlockStore(config, repository, PrecompiledContracts.BRIDGE_ADDR);
for (int i = 0; i < 614; i++) {
Triple<byte[], BigInteger, Integer> tripleStoredBlock = (Triple<byte[], BigInteger, Integer>) objectInputStream.readObject();
BtcBlock header = RegTestParams.get().getDefaultSerializer().makeBlock(tripleStoredBlock.getLeft());
StoredBlock storedBlock = new StoredBlock(header, tripleStoredBlock.getMiddle(), tripleStoredBlock.getRight());
if (i == 0) {
store.setChainHead(storedBlock);
}
store.put(storedBlock);
}
// Create a new instance of the store
RepositoryBlockStore store2 = new RepositoryBlockStore(config, repository, PrecompiledContracts.BRIDGE_ADDR);
// Check a specific block that used to fail when we had a bug
assertEquals(store.get(Sha256Hash.wrap("373941fe83961cf70e181e468abc5f9f7cc440c711c3d06948fa66f3912ed27a")), store2.get(Sha256Hash.wrap("373941fe83961cf70e181e468abc5f9f7cc440c711c3d06948fa66f3912ed27a")));
// Check new instance content is identical to the original one
StoredBlock storedBlock = store.getChainHead();
StoredBlock storedBlock2 = store2.getChainHead();
int headHeight = storedBlock.getHeight();
for (int i = 0; i < headHeight; i++) {
assertNotNull(storedBlock);
assertEquals(storedBlock, storedBlock2);
Sha256Hash prevBlockHash = storedBlock.getHeader().getPrevBlockHash();
storedBlock = store.get(prevBlockHash);
storedBlock2 = store2.get(prevBlockHash);
}
}
Aggregations