Search in sources :

Example 51 with RLPList

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

the class GetBlockHeadersMessage method parse.

private void parse() {
    RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
    byte[] blockBytes = paramsList.get(0).getRLPData();
    // it might be either a hash or number
    if (blockBytes == null) {
        this.blockNumber = 0;
    } else if (blockBytes.length == DEFAULT_SIZE_BYTES) {
        this.blockHash = blockBytes;
    } else {
        this.blockNumber = byteArrayToLong(blockBytes);
    }
    byte[] maxHeaders = paramsList.get(1).getRLPData();
    this.maxHeaders = byteArrayToInt(maxHeaders);
    byte[] skipBlocks = paramsList.get(2).getRLPData();
    this.skipBlocks = byteArrayToInt(skipBlocks);
    byte[] reverse = paramsList.get(3).getRLPData();
    this.reverse = byteArrayToInt(reverse) == 1;
    parsed = true;
}
Also used : RLPList(org.ethereum.util.RLPList)

Example 52 with RLPList

use of org.ethereum.util.RLPList 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 53 with RLPList

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

the class AuthResponseMessageV4 method decode.

static AuthResponseMessageV4 decode(byte[] wire) {
    AuthResponseMessageV4 message = new AuthResponseMessageV4();
    RLPList params = (RLPList) RLP.decode2OneItem(wire, 0);
    byte[] pubKeyBytes = params.get(0).getRLPData();
    byte[] bytes = new byte[65];
    System.arraycopy(pubKeyBytes, 0, bytes, 1, 64);
    // uncompressed
    bytes[0] = 0x04;
    message.ephemeralPublicKey = ECKey.CURVE.getCurve().decodePoint(bytes);
    message.nonce = params.get(1).getRLPData();
    byte[] versionBytes = params.get(2).getRLPData();
    message.version = ByteUtil.byteArrayToInt(versionBytes);
    return message;
}
Also used : RLPList(org.ethereum.util.RLPList)

Example 54 with RLPList

use of org.ethereum.util.RLPList 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 55 with RLPList

use of org.ethereum.util.RLPList 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)

Aggregations

RLPList (org.ethereum.util.RLPList)60 RLPElement (org.ethereum.util.RLPElement)19 Test (org.junit.Test)13 Keccak256 (co.rsk.crypto.Keccak256)8 RskAddress (co.rsk.core.RskAddress)7 BigInteger (java.math.BigInteger)5 RLPItem (org.ethereum.util.RLPItem)5 LogInfo (org.ethereum.vm.LogInfo)5 Script (co.rsk.bitcoinj.script.Script)3 ScriptChunk (co.rsk.bitcoinj.script.ScriptChunk)3 Coin (co.rsk.core.Coin)3 RepositoryImpl (co.rsk.db.RepositoryImpl)3 BridgeEventLogger (co.rsk.peg.utils.BridgeEventLogger)3 BridgeEventLoggerImpl (co.rsk.peg.utils.BridgeEventLoggerImpl)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2