Search in sources :

Example 21 with RLPElement

use of org.ethereum.util.RLPElement in project rskj by rsksmart.

the class HelloMessage method parse.

private void parse() {
    RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
    byte[] p2pVersionBytes = paramsList.get(0).getRLPData();
    this.p2pVersion = p2pVersionBytes != null ? p2pVersionBytes[0] : 0;
    byte[] clientIdBytes = paramsList.get(1).getRLPData();
    this.clientId = new String(clientIdBytes != null ? clientIdBytes : EMPTY_BYTE_ARRAY);
    RLPList capabilityList = (RLPList) paramsList.get(2);
    this.capabilities = new ArrayList<>();
    for (Object aCapabilityList : capabilityList) {
        RLPElement capId = ((RLPList) aCapabilityList).get(0);
        RLPElement capVersion = ((RLPList) aCapabilityList).get(1);
        String name = new String(capId.getRLPData());
        byte version = capVersion.getRLPData() == null ? 0 : capVersion.getRLPData()[0];
        Capability cap = new Capability(name, version);
        this.capabilities.add(cap);
    }
    byte[] peerPortBytes = paramsList.get(3).getRLPData();
    this.listenPort = ByteUtil.byteArrayToInt(peerPortBytes);
    byte[] peerIdBytes = paramsList.get(4).getRLPData();
    this.peerId = Hex.toHexString(peerIdBytes);
    this.parsed = true;
}
Also used : Capability(org.ethereum.net.client.Capability) RLPElement(org.ethereum.util.RLPElement) RLPList(org.ethereum.util.RLPList)

Example 22 with RLPElement

use of org.ethereum.util.RLPElement in project rskj by rsksmart.

the class BridgeEventLoggerImplTest method logCommitFederation.

@Test
public void logCommitFederation() {
    // Setup event logger
    BridgeConstants constantsMock = mock(BridgeConstants.class);
    when(constantsMock.getFederationActivationAge()).thenReturn(BridgeRegTestConstants.getInstance().getFederationActivationAge());
    List<LogInfo> eventLogs = new LinkedList<>();
    BridgeEventLogger eventLogger = new BridgeEventLoggerImpl(constantsMock, eventLogs);
    // Setup parameters for test method call
    Block executionBlock = mock(Block.class);
    when(executionBlock.getTimestamp()).thenReturn(15005L);
    when(executionBlock.getNumber()).thenReturn(15L);
    Federation oldFederation = new Federation(Arrays.asList(BtcECKey.fromPublicOnly(Hex.decode("036bb9eab797eadc8b697f0e82a01d01cabbfaaca37e5bafc06fdc6fdd38af894a")), BtcECKey.fromPublicOnly(Hex.decode("031da807c71c2f303b7f409dd2605b297ac494a563be3b9ca5f52d95a43d183cc5")), BtcECKey.fromPublicOnly(Hex.decode("025eefeeeed5cdc40822880c7db1d0a88b7b986945ed3fc05a0b45fe166fe85e12")), BtcECKey.fromPublicOnly(Hex.decode("03c67ad63527012fd4776ae892b5dc8c56f80f1be002dc65cd520a2efb64e37b49"))), Instant.ofEpochMilli(15005L), 15L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST));
    Federation newFederation = new Federation(Arrays.asList(BtcECKey.fromPublicOnly(Hex.decode("0346cb6b905e4dee49a862eeb2288217d06afcd4ace4b5ca77ebedfbc6afc1c19d")), BtcECKey.fromPublicOnly(Hex.decode("0269a0dbe7b8f84d1b399103c466fb20531a56b1ad3a7b44fe419e74aad8c46db7")), BtcECKey.fromPublicOnly(Hex.decode("026192d8ab41bd402eb0431457f6756a3f3ce15c955c534d2b87f1e0372d8ba338"))), Instant.ofEpochMilli(5005L), 0L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST));
    // Do method call
    eventLogger.logCommitFederation(executionBlock, oldFederation, newFederation);
    // Assert
    Assert.assertEquals(1, eventLogs.size());
    // Assert address that made the log
    LogInfo result = eventLogs.get(0);
    Assert.assertArrayEquals(PrecompiledContracts.BRIDGE_ADDR.getBytes(), result.getAddress());
    // Assert log topics
    Assert.assertEquals(1, result.getTopics().size());
    Assert.assertEquals(Bridge.COMMIT_FEDERATION_TOPIC, result.getTopics().get(0));
    // Assert log data
    Assert.assertNotNull(result.getData());
    List<RLPElement> rlpData = RLP.decode2(result.getData());
    Assert.assertEquals(1, rlpData.size());
    RLPList dataList = (RLPList) rlpData.get(0);
    Assert.assertEquals(3, dataList.size());
    // Assert old federation data
    RLPList oldFedData = (RLPList) dataList.get(0);
    Assert.assertEquals(2, oldFedData.size());
    Assert.assertArrayEquals(oldFederation.getAddress().getHash160(), oldFedData.get(0).getRLPData());
    RLPList oldFedPubKeys = (RLPList) oldFedData.get(1);
    Assert.assertEquals(4, oldFedPubKeys.size());
    for (int i = 0; i < 4; i++) {
        Assert.assertEquals(oldFederation.getPublicKeys().get(i), BtcECKey.fromPublicOnly(oldFedPubKeys.get(i).getRLPData()));
    }
    // Assert new federation data
    RLPList newFedData = (RLPList) dataList.get(1);
    Assert.assertEquals(2, newFedData.size());
    Assert.assertArrayEquals(newFederation.getAddress().getHash160(), newFedData.get(0).getRLPData());
    RLPList newFedPubKeys = (RLPList) newFedData.get(1);
    Assert.assertEquals(3, newFedPubKeys.size());
    for (int i = 0; i < 3; i++) {
        Assert.assertEquals(newFederation.getPublicKeys().get(i), BtcECKey.fromPublicOnly(newFedPubKeys.get(i).getRLPData()));
    }
    // Assert new federation activation block number
    Assert.assertEquals(15L + BridgeRegTestConstants.getInstance().getFederationActivationAge(), Long.valueOf(new String(dataList.get(2).getRLPData(), StandardCharsets.UTF_8)).longValue());
}
Also used : LogInfo(org.ethereum.vm.LogInfo) LinkedList(java.util.LinkedList) RLPList(org.ethereum.util.RLPList) Federation(co.rsk.peg.Federation) RLPElement(org.ethereum.util.RLPElement) Block(org.ethereum.core.Block) BridgeConstants(co.rsk.config.BridgeConstants) Test(org.junit.Test)

Example 23 with RLPElement

use of org.ethereum.util.RLPElement in project rskj by rsksmart.

the class RemascFeesPayerTest method payMiningFees.

@Test
public void payMiningFees() {
    // Setup objects
    Repository repositoryMock = Mockito.mock(Repository.class);
    RemascFeesPayer feesPayer = new RemascFeesPayer(repositoryMock, PrecompiledContracts.REMASC_ADDR);
    byte[] blockHash = { 0x1, 0x2 };
    Coin value = Coin.valueOf(7L);
    RskAddress toAddress = new RskAddress("6c386a4b26f73c802f34673f7248bb118f97424a");
    List<LogInfo> logs = new ArrayList<>();
    // Do call
    feesPayer.payMiningFees(blockHash, value, toAddress, logs);
    Assert.assertEquals(1, logs.size());
    // Assert address that made the log
    LogInfo result = logs.get(0);
    Assert.assertArrayEquals(PrecompiledContracts.REMASC_ADDR.getBytes(), result.getAddress());
    // Assert log topics
    Assert.assertEquals(2, result.getTopics().size());
    Assert.assertEquals("000000000000000000000000000000006d696e696e675f6665655f746f706963", result.getTopics().get(0).toString());
    Assert.assertEquals("0000000000000000000000006c386a4b26f73c802f34673f7248bb118f97424a", result.getTopics().get(1).toString());
    // Assert log data
    Assert.assertNotNull(result.getData());
    List<RLPElement> rlpData = RLP.decode2(result.getData());
    Assert.assertEquals(1, rlpData.size());
    RLPList dataList = (RLPList) rlpData.get(0);
    Assert.assertEquals(2, dataList.size());
    Assert.assertArrayEquals(blockHash, dataList.get(0).getRLPData());
    Assert.assertEquals(value, RLP.parseCoin(dataList.get(1).getRLPData()));
    // Assert repository calls are made right
    verify(repositoryMock, times(1)).addBalance(PrecompiledContracts.REMASC_ADDR, value.negate());
    verify(repositoryMock, times(1)).addBalance(toAddress, value);
}
Also used : Coin(co.rsk.core.Coin) Repository(org.ethereum.core.Repository) LogInfo(org.ethereum.vm.LogInfo) RLPElement(org.ethereum.util.RLPElement) RskAddress(co.rsk.core.RskAddress) ArrayList(java.util.ArrayList) RLPList(org.ethereum.util.RLPList) Test(org.junit.Test)

Example 24 with RLPElement

use of org.ethereum.util.RLPElement in project rskj by rsksmart.

the class RLPTest method encodeDecodeSingleBytesWithHighValueUsingEncode.

@Test
public void encodeDecodeSingleBytesWithHighValueUsingEncode() {
    for (int k = 128; k < 256; k++) {
        byte[] bytes = new byte[1];
        bytes[0] = (byte) k;
        byte[] encoded = RLP.encode(bytes);
        Assert.assertNotNull(encoded);
        Assert.assertEquals(2, encoded.length);
        Assert.assertEquals((byte) 129, encoded[0]);
        RLPElement element = RLP.decode2OneItem(encoded, 0);
        Assert.assertNotNull(element);
        byte[] decoded = element.getRLPData();
        Assert.assertNotNull(decoded);
        Assert.assertArrayEquals(bytes, decoded);
    }
}
Also used : RLPElement(org.ethereum.util.RLPElement) Test(org.junit.Test)

Example 25 with RLPElement

use of org.ethereum.util.RLPElement in project rskj by rsksmart.

the class RLPTest method encodeDecodeShortListWithTwoByteArraysWithElementsLength55.

@Test
public void encodeDecodeShortListWithTwoByteArraysWithElementsLength55() {
    byte[] value1 = new byte[25];
    byte[] value2 = new byte[28];
    byte[] element1 = RLP.encodeElement(value1);
    byte[] element2 = RLP.encodeElement(value2);
    byte[] encoded = RLP.encodeList(element1, element2);
    Assert.assertNotNull(encoded);
    Assert.assertEquals(1 + 1 + 25 + 1 + 28, encoded.length);
    Assert.assertEquals((byte) (192 + 55), encoded[0]);
    ArrayList<RLPElement> list = RLP.decode2(encoded);
    Assert.assertNotNull(list);
    Assert.assertEquals(1, list.size());
    RLPList list2 = (RLPList) list.get(0);
    Assert.assertNotNull(list2);
    Assert.assertEquals(2, list2.size());
    Assert.assertArrayEquals(value1, list2.get(0).getRLPData());
    Assert.assertArrayEquals(value2, list2.get(1).getRLPData());
}
Also used : RLPElement(org.ethereum.util.RLPElement) RLPList(org.ethereum.util.RLPList) Test(org.junit.Test)

Aggregations

RLPElement (org.ethereum.util.RLPElement)38 Test (org.junit.Test)29 RLPList (org.ethereum.util.RLPList)19 ArrayList (java.util.ArrayList)4 LogInfo (org.ethereum.vm.LogInfo)3 Coin (co.rsk.core.Coin)2 RskAddress (co.rsk.core.RskAddress)2 Keccak256 (co.rsk.crypto.Keccak256)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 Script (co.rsk.bitcoinj.script.Script)1 ScriptChunk (co.rsk.bitcoinj.script.ScriptChunk)1 BridgeConstants (co.rsk.config.BridgeConstants)1 RepositoryImpl (co.rsk.db.RepositoryImpl)1 Federation (co.rsk.peg.Federation)1 BridgeEventLogger (co.rsk.peg.utils.BridgeEventLogger)1 BridgeEventLoggerImpl (co.rsk.peg.utils.BridgeEventLoggerImpl)1 RemascTransaction (co.rsk.remasc.RemascTransaction)1 LinkedList (java.util.LinkedList)1 Block (org.ethereum.core.Block)1 ImmutableTransaction (org.ethereum.core.ImmutableTransaction)1