org.ethereum.util.RLPList Java Examples

The following examples show how to use org.ethereum.util.RLPList. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Block.java    From ethereumj with MIT License 6 votes vote down vote up
private void parseTxs(byte[] expectedRoot, RLPList txReceipts) {

        this.txsState = new TrieImpl(null);
        for (int i = 0; i < txReceipts.size(); i++) {
        	RLPElement rlpTxReceipt = txReceipts.get(i);
            RLPElement txData = ((RLPList)rlpTxReceipt).get(0);
            
            // YP 4.3.1
            RLPElement pstTxState = ((RLPList)rlpTxReceipt).get(1);
            RLPElement cummGas    = ((RLPList)rlpTxReceipt).get(2);

            Transaction tx = new Transaction(txData.getRLPData());
            this.transactionsList.add(tx);
            TransactionReceipt txReceipt =
                new TransactionReceipt(tx, pstTxState.getRLPData(), cummGas.getRLPData());
            this.addTxReceipt(i, txReceipt);
        }
        String calculatedRoot = Hex.toHexString(txsState.getRootHash());
        if(!calculatedRoot.equals(Hex.toHexString(expectedRoot)))
			logger.error("Added tx receipts don't match the given txsStateRoot");
    }
 
Example #2
Source File: Block.java    From ethereumj with MIT License 6 votes vote down vote up
private void parseRLP() {

        RLPList params = RLP.decode2(rlpEncoded);
        RLPList block = (RLPList) params.get(0);
        
        // Parse Header
        RLPList header = (RLPList) block.get(0);
        this.header = new BlockHeader(header);
        
        // Parse Transactions
        RLPList txReceipts = (RLPList) block.get(1);
        this.parseTxs(this.header.getTxTrieRoot(), txReceipts);

        // Parse Uncles
        RLPList uncleBlocks = (RLPList) block.get(2);
        for (RLPElement rawUncle : uncleBlocks) {

            RLPList uncleHeader = (RLPList) rawUncle;
            BlockHeader blockData = new BlockHeader(uncleHeader);
            this.uncleList.add(blockData);
        }
        this.parsed = true;
    }
 
Example #3
Source File: Transaction.java    From wkcwallet-java with Apache License 2.0 6 votes vote down vote up
public void rlpParse() {
    RLPList decodedTxList = RLP.decode2(this.rlpEncoded);
    RLPList transaction = (RLPList)decodedTxList.get(0);
    this.nonce = ((RLPElement)transaction.get(0)).getRLPData();
    this.gasPrice = ((RLPElement)transaction.get(1)).getRLPData();
    this.gasLimit = ((RLPElement)transaction.get(2)).getRLPData();
    this.receiveAddress = ((RLPElement)transaction.get(3)).getRLPData();
    this.value = ((RLPElement)transaction.get(4)).getRLPData();
    this.data = ((RLPElement)transaction.get(5)).getRLPData();
    if(((RLPElement)transaction.get(6)).getRLPData() != null) {
        byte v = ((RLPElement)transaction.get(6)).getRLPData()[0];
        byte[] r = ((RLPElement)transaction.get(7)).getRLPData();
        byte[] s = ((RLPElement)transaction.get(8)).getRLPData();
        this.signature = ECKey.ECDSASignature.fromComponents(r, s, v);
    } else {
    }

    this.parsed = true;
    this.hash = this.getHash();
}
 
Example #4
Source File: Transaction.java    From ethereumj with MIT License 6 votes vote down vote up
public void rlpParse() {

        RLPList decodedTxList = RLP.decode2(rlpEncoded);
        RLPList transaction =  (RLPList) decodedTxList.get(0);

        this.nonce =          ((RLPItem) transaction.get(0)).getRLPData();
        this.gasPrice =       ((RLPItem) transaction.get(1)).getRLPData();
        this.gasLimit =       ((RLPItem) transaction.get(2)).getRLPData();
        this.receiveAddress = ((RLPItem) transaction.get(3)).getRLPData();
        this.value =          ((RLPItem) transaction.get(4)).getRLPData();

        this.data =     ((RLPItem) transaction.get(5)).getRLPData();
        // only parse signature in case tx is signed
        if(((RLPItem) transaction.get(6)).getRLPData() != null) {
            byte v =		((RLPItem) transaction.get(6)).getRLPData()[0];
            byte[] r =		((RLPItem) transaction.get(7)).getRLPData();
            byte[] s =		((RLPItem) transaction.get(8)).getRLPData();
            this.signature = ECDSASignature.fromComponents(r, s, v);
        } else {
            logger.debug("RLP encoded tx is not signed!");
        }
        this.parsed = true;
        this.hash  = this.getHash();
    }
 
Example #5
Source File: PeersMessage.java    From ethereumj with MIT License 6 votes vote down vote up
private void parse() {
	RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);

	peers = new LinkedHashSet<>();
	for (int i = 1; i < paramsList.size(); ++i) {
		RLPList peerParams = (RLPList) paramsList.get(i);
		byte[] ipBytes = peerParams.get(0).getRLPData();
		byte[] portBytes = peerParams.get(1).getRLPData();
		byte[] peerIdRaw = peerParams.get(2).getRLPData();

		try {
			int peerPort = ByteUtil.byteArrayToInt(portBytes);
			InetAddress address = InetAddress.getByAddress(ipBytes);

               String peerId = peerIdRaw == null ? "" :  Hex.toHexString(peerIdRaw);
               Peer peer = new Peer(address, peerPort, peerId);
			peers.add(peer);
		} catch (UnknownHostException e) {
			throw new RuntimeException("Malformed ip", e);
		}
	}
	this.parsed = true;
}
 
Example #6
Source File: StatusMessage.java    From ethereumj with MIT License 5 votes vote down vote up
private void parse() {
    RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);

    this.protocolVersion	= ((RLPItem) paramsList.get(1)).getRLPData()[0];
    byte[] networkIdBytes	= ((RLPItem) paramsList.get(2)).getRLPData();
    this.networkId			= networkIdBytes == null ? 0 : networkIdBytes[0];
    this.totalDifficulty	= ((RLPItem) paramsList.get(3)).getRLPData();
    this.bestHash 			= ((RLPItem) paramsList.get(4)).getRLPData();
    this.genesisHash 		= ((RLPItem) paramsList.get(5)).getRLPData();

    parsed = true;
}
 
Example #7
Source File: BlockHeader.java    From ethereumj with MIT License 5 votes vote down vote up
public BlockHeader(RLPList rlpHeader) {
    this.parentHash     = ((RLPItem) rlpHeader.get(0)).getRLPData();
    this.unclesHash     = ((RLPItem) rlpHeader.get(1)).getRLPData();
    this.coinbase       = ((RLPItem) rlpHeader.get(2)).getRLPData();
    this.stateRoot      = ((RLPItem) rlpHeader.get(3)).getRLPData();
    
    this.txTrieRoot     = ((RLPItem) rlpHeader.get(4)).getRLPData();
    if(this.txTrieRoot == null)
    	this.txTrieRoot = EMPTY_BYTE_ARRAY;
    
    this.difficulty     = ((RLPItem) rlpHeader.get(5)).getRLPData();
 
    byte[] nrBytes      = ((RLPItem) rlpHeader.get(6)).getRLPData();
    byte[] gpBytes      = ((RLPItem) rlpHeader.get(7)).getRLPData();
    byte[] glBytes      = ((RLPItem) rlpHeader.get(8)).getRLPData();
    byte[] guBytes      = ((RLPItem) rlpHeader.get(9)).getRLPData();
    byte[] tsBytes      = ((RLPItem) rlpHeader.get(10)).getRLPData();
    
    this.number 		= nrBytes == null ? 0 : (new BigInteger(1, nrBytes)).longValue();
    this.minGasPrice 	= gpBytes == null ? 0 : (new BigInteger(1, gpBytes)).longValue();
    this.gasLimit 		= glBytes == null ? 0 : (new BigInteger(1, glBytes)).longValue();
    this.gasUsed 		= guBytes == null ? 0 : (new BigInteger(1, guBytes)).longValue();
    this.timestamp      = tsBytes == null ? 0 : (new BigInteger(1, tsBytes)).longValue();
    
    this.extraData       = ((RLPItem) rlpHeader.get(11)).getRLPData();
    this.nonce           = ((RLPItem) rlpHeader.get(12)).getRLPData();
}
 
Example #8
Source File: AccountState.java    From ethereumj with MIT License 5 votes vote down vote up
public AccountState(byte[] rlpData) {
      this.rlpEncoded = rlpData;

RLPList items 	= (RLPList) RLP.decode2(rlpEncoded).get(0);
this.nonce 		= new BigInteger(1, ((items.get(0).getRLPData()) == null ? new byte[]{0} :
                                                                                 items.get(0).getRLPData()));
this.balance 	= new BigInteger(1, ((items.get(1).getRLPData()) == null ? new byte[]{0} : 
																			items.get(1).getRLPData()));
this.stateRoot 	= items.get(2).getRLPData();
this.codeHash 	= items.get(3).getRLPData();
  }
 
Example #9
Source File: DisconnectMessage.java    From ethereumj with MIT License 5 votes vote down vote up
private void parse() {
	RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);

	byte[] reasonBytes = ((RLPItem) paramsList.get(1)).getRLPData();
	if (reasonBytes == null)
		this.reason = REQUESTED;
	else
		this.reason = ReasonCode.fromInt(reasonBytes[0]);

	parsed = true;
}
 
Example #10
Source File: TransactionsMessage.java    From ethereumj with MIT License 5 votes vote down vote up
private void parse() {
RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);

transactions = new HashSet<>();
      for (int i = 1; i < paramsList.size(); ++i) {
          RLPList rlpTxData = (RLPList) paramsList.get(i);
          Transaction tx = new Transaction(rlpTxData.getRLPData());
          transactions.add(tx);
      }
      parsed = true;
  }
 
Example #11
Source File: NewBlockMessage.java    From ethereumj with MIT License 5 votes vote down vote up
private void parse() {
	RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);

       RLPList blockRLP = ((RLPList) paramsList.get(1));
       block = new Block(blockRLP.getRLPData());
       difficulty =  paramsList.get(2).getRLPData();

       parsed = true;
}
 
Example #12
Source File: GetBlockHashesMessage.java    From ethereumj with MIT License 5 votes vote down vote up
private void parse() {
	RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);

	this.bestHash = paramsList.get(1).getRLPData();
	byte[] maxBlocksBytes = paramsList.get(2).getRLPData();
	this.maxBlocks = ByteUtil.byteArrayToInt(maxBlocksBytes);

	parsed = true;
}
 
Example #13
Source File: BlocksMessage.java    From ethereumj with MIT License 5 votes vote down vote up
private void parse() {
	RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);

	blocks = new ArrayList<>();
	for (int i = 1; i < paramsList.size(); ++i) {
		RLPList rlpData = ((RLPList) paramsList.get(i));
		Block blockData = new Block(rlpData.getRLPData());
		blocks.add(blockData);
	}
	parsed = true;
}
 
Example #14
Source File: BlockHashesMessage.java    From ethereumj with MIT License 5 votes vote down vote up
private void parse() {
	RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);

	blockHashes = new ArrayList<>();
	for (int i = 1; i < paramsList.size(); ++i) {
		RLPItem rlpData = ((RLPItem) paramsList.get(i));
		blockHashes.add(rlpData.getRLPData());
	}
	parsed = true;
}
 
Example #15
Source File: JournalSource.java    From nuls-v2 with MIT License 5 votes vote down vote up
private void parse(byte[] encoded) {
    RLPList l = (RLPList) RLP.decode2(encoded).get(0);
    updateHash = l.get(0).getRLPData();

    for (RLPElement aRInserted : (RLPList) l.get(1)) {
        insertedKeys.add(aRInserted.getRLPData());
    }
    for (RLPElement aRDeleted : (RLPList) l.get(2)) {
        deletedKeys.add(aRDeleted.getRLPData());
    }
}
 
Example #16
Source File: GetBlocksMessage.java    From ethereumj with MIT License 5 votes vote down vote up
private void parse() {
	RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);

	blockHashes = new ArrayList<>();
	for (int i = 1; i < paramsList.size(); ++i) {
		blockHashes.add(((RLPItem) paramsList.get(i)).getRLPData());
	}
	parsed = true;
}
 
Example #17
Source File: AccountState.java    From nuls with MIT License 5 votes vote down vote up
public AccountState(byte[] rlpData) {
    this.rlpEncoded = rlpData;

    RLPList items = (RLPList) RLP.decode2(rlpEncoded).get(0);
    this.nonce = ByteUtil.bytesToBigInteger(items.get(0).getRLPData());
    this.balance = ByteUtil.bytesToBigInteger(items.get(1).getRLPData());
    this.stateRoot = items.get(2).getRLPData();
    this.codeHash = items.get(3).getRLPData();
    this.owner = items.get(4).getRLPData();
}
 
Example #18
Source File: Block.java    From nuls with MIT License 5 votes vote down vote up
private synchronized void parseRLP() {
    if (parsed) {
        return;
    }

    RLPList params = RLP.decode2(rlpEncoded);
    RLPList block = (RLPList) params.get(0);

    // Parse Header
    RLPList header = (RLPList) block.get(0);
    this.header = new BlockHeader(header);
    this.parsed = true;
}
 
Example #19
Source File: JournalSource.java    From nuls with MIT License 5 votes vote down vote up
private void parse(byte[] encoded) {
    RLPList l = (RLPList) RLP.decode2(encoded).get(0);
    updateHash = l.get(0).getRLPData();

    for (RLPElement aRInserted : (RLPList) l.get(1)) {
        insertedKeys.add(aRInserted.getRLPData());
    }
    for (RLPElement aRDeleted : (RLPList) l.get(2)) {
        deletedKeys.add(aRDeleted.getRLPData());
    }
}
 
Example #20
Source File: AccountState.java    From nuls-v2 with MIT License 5 votes vote down vote up
public AccountState(byte[] rlpData) {
    this.rlpEncoded = rlpData;

    RLPList items = (RLPList) RLP.decode2(rlpEncoded).get(0);
    this.nonce = ByteUtil.bytesToBigInteger(items.get(0).getRLPData());
    this.balance = ByteUtil.bytesToBigInteger(items.get(1).getRLPData());
    this.stateRoot = items.get(2).getRLPData();
    this.codeHash = items.get(3).getRLPData();
    this.owner = items.get(4).getRLPData();
}
 
Example #21
Source File: Block.java    From nuls-v2 with MIT License 5 votes vote down vote up
private synchronized void parseRLP() {
    if (parsed) {
        return;
    }

    RLPList params = RLP.decode2(rlpEncoded);
    RLPList block = (RLPList) params.get(0);

    // Parse Header
    RLPList header = (RLPList) block.get(0);
    this.header = new BlockHeader(header);
    this.parsed = true;
}
 
Example #22
Source File: BlockHeader.java    From nuls with MIT License 4 votes vote down vote up
public BlockHeader(RLPList rlpHeader) {
    this.parentHash = rlpHeader.get(0).getRLPData();
    this.hash = rlpHeader.get(1).getRLPData();
    byte[] nrBytes = rlpHeader.get(2).getRLPData();
    this.number = ByteUtil.byteArrayToLong(nrBytes);
}
 
Example #23
Source File: BlockHeader.java    From nuls with MIT License 4 votes vote down vote up
public BlockHeader(byte[] encoded) {
    this((RLPList) RLP.decode2(encoded).get(0));
}
 
Example #24
Source File: BlockHeader.java    From nuls-v2 with MIT License 4 votes vote down vote up
public BlockHeader(RLPList rlpHeader) {
    this.parentHash = rlpHeader.get(0).getRLPData();
    this.hash = rlpHeader.get(1).getRLPData();
    byte[] nrBytes = rlpHeader.get(2).getRLPData();
    this.number = ByteUtil.byteArrayToLong(nrBytes);
}
 
Example #25
Source File: BlockHeader.java    From nuls-v2 with MIT License 4 votes vote down vote up
public BlockHeader(byte[] encoded) {
    this((RLPList) RLP.decode2(encoded).get(0));
}
 
Example #26
Source File: RLP.java    From ethereumj with MIT License 2 votes vote down vote up
/**
 * Parse wire byte[] message into RLP elements
 *
 * @param msgData
 *            - raw RLP data
 * @return rlpList
 *            - outcome of recursive RLP structure
 */
public static RLPList decode2(byte[] msgData) {
	RLPList rlpList = new RLPList();
    fullTraverse(msgData, 0, 0, msgData.length, 1, rlpList);
    return rlpList;
}