Search in sources :

Example 16 with OneOffWhiteListEntry

use of co.rsk.peg.whitelist.OneOffWhiteListEntry in project rskj by rsksmart.

the class BridgeSerializationUtilsTest method serializeDeserializeOneOffLockWhitelistAndDisableBlockHeight.

@Test
public void serializeDeserializeOneOffLockWhitelistAndDisableBlockHeight() {
    NetworkParameters btcParams = NetworkParameters.fromID(NetworkParameters.ID_REGTEST);
    Map<Address, LockWhitelistEntry> whitelist = new HashMap<>();
    Address address = BtcECKey.fromPrivate(BigInteger.valueOf(100L)).toAddress(btcParams);
    whitelist.put(address, new OneOffWhiteListEntry(address, Coin.COIN));
    LockWhitelist originalLockWhitelist = new LockWhitelist(whitelist, 0);
    byte[] serializedLockWhitelist = BridgeSerializationUtils.serializeOneOffLockWhitelist(Pair.of(originalLockWhitelist.getAll(OneOffWhiteListEntry.class), originalLockWhitelist.getDisableBlockHeight()));
    Pair<HashMap<Address, OneOffWhiteListEntry>, Integer> deserializedLockWhitelist = BridgeSerializationUtils.deserializeOneOffLockWhitelistAndDisableBlockHeight(serializedLockWhitelist, btcParams);
    List<Address> originalAddresses = originalLockWhitelist.getAddresses();
    List<Address> deserializedAddresses = new ArrayList(deserializedLockWhitelist.getLeft().keySet());
    Assert.assertThat(originalAddresses, hasSize(1));
    Assert.assertThat(deserializedAddresses, hasSize(1));
    Assert.assertThat(originalAddresses, is(deserializedAddresses));
    Assert.assertThat(((OneOffWhiteListEntry) originalLockWhitelist.get(originalAddresses.get(0))).maxTransferValue(), is((deserializedLockWhitelist.getLeft().get(deserializedAddresses.get(0))).maxTransferValue()));
}
Also used : BigInteger(java.math.BigInteger) LockWhitelist(co.rsk.peg.whitelist.LockWhitelist) RskAddress(co.rsk.core.RskAddress) LockWhitelistEntry(co.rsk.peg.whitelist.LockWhitelistEntry) OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry) Test(org.junit.Test)

Example 17 with OneOffWhiteListEntry

use of co.rsk.peg.whitelist.OneOffWhiteListEntry in project rskj by rsksmart.

the class BridgeSerializationUtils method deserializeOneOffLockWhitelistAndDisableBlockHeight.

public static Pair<HashMap<Address, OneOffWhiteListEntry>, Integer> deserializeOneOffLockWhitelistAndDisableBlockHeight(byte[] data, NetworkParameters parameters) {
    if (data == null || data.length == 0) {
        return null;
    }
    RLPList rlpList = (RLPList) RLP.decode2(data).get(0);
    int serializedAddressesSize = rlpList.size() - 1;
    // serialized addresses size must be even - key, value pairs expected in sequence
    if (serializedAddressesSize % 2 != 0) {
        throw new RuntimeException("deserializeLockWhitelist: expected an even number of addresses, but odd given");
    }
    HashMap<Address, OneOffWhiteListEntry> entries = new HashMap<>(serializedAddressesSize / 2);
    for (int i = 0; i < serializedAddressesSize; i = i + 2) {
        byte[] hash160 = rlpList.get(i).getRLPData();
        byte[] maxTransferValueData = rlpList.get(i + 1).getRLPData();
        Address address = new Address(parameters, hash160);
        entries.put(address, new OneOffWhiteListEntry(address, Coin.valueOf(safeToBigInteger(maxTransferValueData).longValueExact())));
    }
    int disableBlockHeight = safeToBigInteger(rlpList.get(serializedAddressesSize).getRLPData()).intValueExact();
    return Pair.of(entries, disableBlockHeight);
}
Also used : RskAddress(co.rsk.core.RskAddress) OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry) RLPList(org.ethereum.util.RLPList)

Example 18 with OneOffWhiteListEntry

use of co.rsk.peg.whitelist.OneOffWhiteListEntry in project rskj by rsksmart.

the class BridgeTestPowerMock method getLockWhitelistEntryByAddressAfterRskip87Fork.

@Test
public void getLockWhitelistEntryByAddressAfterRskip87Fork() throws Exception {
    byte[] result;
    Transaction mockedTransaction;
    doReturn(true).when(activationConfig).isActive(eq(RSKIP87), anyLong());
    doReturn(false).when(activationConfig).isActive(eq(RSKIP88), anyLong());
    Repository repository = createRepository();
    Repository track = repository.startTracking();
    Address mockedAddressForUnlimited = new BtcECKey().toAddress(networkParameters);
    Address mockedAddressForOneOff = new BtcECKey().toAddress(networkParameters);
    BridgeSupport bridgeSupportMock = mock(BridgeSupport.class);
    when(bridgeSupportMock.getLockWhitelistEntryByAddress(mockedAddressForUnlimited.toBase58())).then((InvocationOnMock invocation) -> new UnlimitedWhiteListEntry(mockedAddressForUnlimited));
    when(bridgeSupportMock.getLockWhitelistEntryByAddress(mockedAddressForOneOff.toBase58())).then((InvocationOnMock invocation) -> new OneOffWhiteListEntry(mockedAddressForOneOff, Coin.COIN));
    mockedTransaction = mock(Transaction.class);
    when(mockedTransaction.isLocalCallTransaction()).thenReturn(true);
    BridgeSupportFactory bridgeSupportFactoryMock = mock(BridgeSupportFactory.class);
    when(bridgeSupportFactoryMock.newInstance(any(), any(), any(), any())).thenReturn(bridgeSupportMock);
    Bridge bridge = new Bridge(PrecompiledContracts.BRIDGE_ADDR, constants, activationConfig, bridgeSupportFactoryMock);
    bridge.init(mockedTransaction, getGenesisBlock(), track, null, null, null);
    // Get the unlimited whitelist address
    result = bridge.execute(Bridge.GET_LOCK_WHITELIST_ENTRY_BY_ADDRESS.encode(new Object[] { mockedAddressForUnlimited.toBase58() }));
    BigInteger decodedResult = (BigInteger) BridgeMethods.GET_LOCK_WHITELIST_ENTRY_BY_ADDRESS.getFunction().decodeResult(result)[0];
    Assert.assertEquals(0, decodedResult.longValue());
    // Get the one-off whitelist address
    result = bridge.execute(Bridge.GET_LOCK_WHITELIST_ENTRY_BY_ADDRESS.encode(new Object[] { mockedAddressForOneOff.toBase58() }));
    decodedResult = (BigInteger) BridgeMethods.GET_LOCK_WHITELIST_ENTRY_BY_ADDRESS.getFunction().decodeResult(result)[0];
    Assert.assertEquals(Coin.COIN.value, decodedResult.longValue());
    // Try fetch an unexisting address
    result = bridge.execute(Bridge.GET_LOCK_WHITELIST_ENTRY_BY_ADDRESS.encode(new Object[] { (new BtcECKey().toAddress(networkParameters)).toBase58() }));
    decodedResult = (BigInteger) BridgeMethods.GET_LOCK_WHITELIST_ENTRY_BY_ADDRESS.getFunction().decodeResult(result)[0];
    Assert.assertEquals(-1, decodedResult.longValue());
}
Also used : MutableRepository(org.ethereum.db.MutableRepository) UnlimitedWhiteListEntry(co.rsk.peg.whitelist.UnlimitedWhiteListEntry) SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) RskAddress(co.rsk.core.RskAddress) InvocationOnMock(org.mockito.invocation.InvocationOnMock) BigInteger(java.math.BigInteger) OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 19 with OneOffWhiteListEntry

use of co.rsk.peg.whitelist.OneOffWhiteListEntry in project rskj by rsksmart.

the class BridgeStorageProvider method getLockWhitelist.

public LockWhitelist getLockWhitelist() {
    if (lockWhitelist != null) {
        return lockWhitelist;
    }
    Pair<HashMap<Address, OneOffWhiteListEntry>, Integer> oneOffWhitelistAndDisableBlockHeightData = safeGetFromRepository(LOCK_ONE_OFF_WHITELIST_KEY, data -> BridgeSerializationUtils.deserializeOneOffLockWhitelistAndDisableBlockHeight(data, networkParameters));
    if (oneOffWhitelistAndDisableBlockHeightData == null) {
        lockWhitelist = new LockWhitelist(new HashMap<>());
        return lockWhitelist;
    }
    Map<Address, LockWhitelistEntry> whitelistedAddresses = new HashMap<>();
    whitelistedAddresses.putAll(oneOffWhitelistAndDisableBlockHeightData.getLeft());
    if (activations.isActive(RSKIP87)) {
        whitelistedAddresses.putAll(safeGetFromRepository(LOCK_UNLIMITED_WHITELIST_KEY, data -> BridgeSerializationUtils.deserializeUnlimitedLockWhitelistEntries(data, networkParameters)));
    }
    lockWhitelist = new LockWhitelist(whitelistedAddresses, oneOffWhitelistAndDisableBlockHeightData.getRight());
    return lockWhitelist;
}
Also used : OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry) FastBridgeFederationInformation(co.rsk.peg.fastbridge.FastBridgeFederationInformation) java.util(java.util) LockWhitelistEntry(co.rsk.peg.whitelist.LockWhitelistEntry) CoinbaseInformation(co.rsk.peg.bitcoin.CoinbaseInformation) Hex(org.spongycastle.util.encoders.Hex) DataWord(org.ethereum.vm.DataWord) RskAddress(co.rsk.core.RskAddress) IOException(java.io.IOException) LockWhitelist(co.rsk.peg.whitelist.LockWhitelist) Keccak256(co.rsk.crypto.Keccak256) Repository(org.ethereum.core.Repository) UnlimitedWhiteListEntry(co.rsk.peg.whitelist.UnlimitedWhiteListEntry) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) Script(co.rsk.bitcoinj.script.Script) Pair(org.apache.commons.lang3.tuple.Pair) BridgeConstants(co.rsk.config.BridgeConstants) co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) ConsensusRule(org.ethereum.config.blockchain.upgrades.ConsensusRule) LockWhitelist(co.rsk.peg.whitelist.LockWhitelist) RskAddress(co.rsk.core.RskAddress) LockWhitelistEntry(co.rsk.peg.whitelist.LockWhitelistEntry)

Example 20 with OneOffWhiteListEntry

use of co.rsk.peg.whitelist.OneOffWhiteListEntry in project rskj by rsksmart.

the class Bridge method getLockWhitelistEntryByAddress.

public long getLockWhitelistEntryByAddress(Object[] args) {
    logger.trace("getLockWhitelistEntryByAddress");
    String addressBase58;
    try {
        addressBase58 = (String) args[0];
    } catch (Exception e) {
        logger.warn("Exception in getLockWhitelistEntryByAddress", e);
        return LOCK_WHITELIST_INVALID_ADDRESS_FORMAT_ERROR_CODE;
    }
    LockWhitelistEntry entry = bridgeSupport.getLockWhitelistEntryByAddress(addressBase58);
    if (entry == null) {
        // Empty string is returned when address is not found
        return LOCK_WHITELIST_ENTRY_NOT_FOUND_CODE;
    }
    return entry.getClass() == OneOffWhiteListEntry.class ? ((OneOffWhiteListEntry) entry).maxTransferValue().getValue() : LOCK_WHITELIST_UNLIMITED_MODE_CODE;
}
Also used : LockWhitelistEntry(co.rsk.peg.whitelist.LockWhitelistEntry) OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry) VMException(org.ethereum.vm.exception.VMException) IOException(java.io.IOException) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException)

Aggregations

OneOffWhiteListEntry (co.rsk.peg.whitelist.OneOffWhiteListEntry)20 RskAddress (co.rsk.core.RskAddress)17 Test (org.junit.Test)14 LockWhitelist (co.rsk.peg.whitelist.LockWhitelist)13 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)12 MutableRepository (org.ethereum.db.MutableRepository)12 Repository (org.ethereum.core.Repository)11 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)9 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)9 BigInteger (java.math.BigInteger)8 InternalTransaction (org.ethereum.vm.program.InternalTransaction)8 PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation (co.rsk.peg.PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation)7 PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation (co.rsk.peg.PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation)7 PeginInstructionsProvider (co.rsk.peg.pegininstructions.PeginInstructionsProvider)7 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)7 Block (org.ethereum.core.Block)7 Transaction (org.ethereum.core.Transaction)7 BtcLockSenderProvider (co.rsk.peg.btcLockSender.BtcLockSenderProvider)6 ECKey (org.ethereum.crypto.ECKey)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6