org.ethereum.util.RLP Java Examples

The following examples show how to use org.ethereum.util.RLP. 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: BlockHeader.java    From ethereumj with MIT License 6 votes vote down vote up
public byte[] getEncoded(boolean withNonce) {
       byte[] parentHash		= RLP.encodeElement(this.parentHash);
       byte[] unclesHash		= RLP.encodeElement(this.unclesHash);
       byte[] coinbase			= RLP.encodeElement(this.coinbase);
       byte[] stateRoot		= RLP.encodeElement(this.stateRoot);
       byte[] txTrieRoot		= RLP.encodeElement(this.txTrieRoot);
       byte[] difficulty		= RLP.encodeElement(this.difficulty);
       byte[] number			= RLP.encodeBigInteger(BigInteger.valueOf(this.number));
       byte[] minGasPrice		= RLP.encodeBigInteger(BigInteger.valueOf(this.minGasPrice));
       byte[] gasLimit			= RLP.encodeBigInteger(BigInteger.valueOf(this.gasLimit));
       byte[] gasUsed			= RLP.encodeBigInteger(BigInteger.valueOf(this.gasUsed));
       byte[] timestamp		= RLP.encodeBigInteger(BigInteger.valueOf(this.timestamp));
       byte[] extraData		= RLP.encodeElement(this.extraData);
       if(withNonce) {
       	byte[] nonce			= RLP.encodeElement(this.nonce);
       	return RLP.encodeList(parentHash, unclesHash, coinbase,
   				stateRoot, txTrieRoot, difficulty, number,
   				minGasPrice, gasLimit, gasUsed, timestamp, extraData, nonce);
       } else {
       	return RLP.encodeList(parentHash, unclesHash, coinbase,
   				stateRoot, txTrieRoot, difficulty, number,
   				minGasPrice, gasLimit, gasUsed, timestamp, extraData);
       }
}
 
Example #2
Source File: TrieImpl.java    From nuls with MIT License 6 votes vote down vote up
private void parse() {
    if (children != null) {
        return;
    }
    resolve();

    RLP.LList list = parsedRlp == null ? RLP.decodeLazyList(rlp) : parsedRlp;

    if (list.size() == 2) {
        children = new Object[2];
        TrieKey key = TrieKey.fromPacked(list.getBytes(0));
        children[0] = key;
        if (key.isTerminal()) {
            children[1] = list.getBytes(1);
        } else {
            children[1] = list.isList(1) ? new Node(list.getList(1)) : new Node(list.getBytes(1));
        }
    } else {
        children = new Object[17];
        parsedRlp = list;
    }
}
 
Example #3
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 #4
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 #5
Source File: TrieImpl.java    From nuls-v2 with MIT License 6 votes vote down vote up
private void parse() {
    if (children != null) {
        return;
    }
    resolve();

    RLP.LList list = parsedRlp == null ? RLP.decodeLazyList(rlp) : parsedRlp;

    if (list.size() == 2) {
        children = new Object[2];
        TrieKey key = TrieKey.fromPacked(list.getBytes(0));
        children[0] = key;
        if (key.isTerminal()) {
            children[1] = list.getBytes(1);
        } else {
            children[1] = list.isList(1) ? new Node(list.getList(1)) : new Node(list.getBytes(1));
        }
    } else {
        children = new Object[17];
        parsedRlp = list;
    }
}
 
Example #6
Source File: Transaction.java    From ethereumj with MIT License 6 votes vote down vote up
/**
   *  For signatures you have to keep also
   *  RLP of the transaction without any signature data
   */
  public byte[] getEncodedRaw() {

      if (!parsed) rlpParse();
      if (rlpRaw != null) return rlpRaw;

      // parse null as 0 for nonce
      byte[] nonce = null;
      if ( this.nonce == null || this.nonce.length == 1 && this.nonce[0] == 0){
          nonce 				= RLP.encodeElement(null);
      } else {
          nonce 				= RLP.encodeElement(this.nonce);
      }
      byte[] gasPrice 			= RLP.encodeElement(this.gasPrice);
      byte[] gasLimit 			= RLP.encodeElement(this.gasLimit);
      byte[] receiveAddress 		= RLP.encodeElement(this.receiveAddress);
      byte[] value 				= RLP.encodeElement(this.value);
      byte[] data 				= RLP.encodeElement(this.data);

this.rlpRaw = RLP.encodeList(nonce, gasPrice, gasLimit, receiveAddress,
		value, data);
      return rlpRaw;
  }
 
Example #7
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 #8
Source File: Transaction.java    From wkcwallet-java with Apache License 2.0 6 votes vote down vote up
public byte[] getEncodedRaw() {
    if(!this.parsed) {
        this.rlpParse();
    }

    if(this.rlpRaw != null) {
        return this.rlpRaw;
    } else {
        byte[] nonce = null;
        if(this.nonce != null && (this.nonce.length != 1 || this.nonce[0] != 0)) {
            nonce = RLP.encodeElement(this.nonce);
        } else {
            nonce = RLP.encodeElement((byte[])null);
        }

        byte[] gasPrice = RLP.encodeElement(this.gasPrice);
        byte[] gasLimit = RLP.encodeElement(this.gasLimit);
        byte[] receiveAddress = RLP.encodeElement(this.receiveAddress);
        byte[] value = RLP.encodeElement(this.value);
        byte[] data = RLP.encodeElement(this.data);
        this.rlpRaw = RLP.encodeList(new byte[][]{nonce, gasPrice, gasLimit, receiveAddress, value, data});
        return this.rlpRaw;
    }
}
 
Example #9
Source File: ContractDetails.java    From ethereumj with MIT License 6 votes vote down vote up
public byte[] getEncoded() {

		if (rlpEncoded == null) {

			int size = storageKeys == null ? 0 : storageKeys.size();

			byte[][] keys = new byte[size][];
			byte[][] values = new byte[size][];

			for (int i = 0; i < size; ++i) {
				DataWord key = storageKeys.get(i);
				keys[i] = RLP.encodeElement(key.getData());
			}
			for (int i = 0; i < size; ++i) {
				DataWord value = storageValues.get(i);
				values[i] = RLP.encodeElement(value.getNoLeadZeroesData());
			}

			byte[] rlpKeysList = RLP.encodeList(keys);
			byte[] rlpValuesList = RLP.encodeList(values);
			byte[] rlpCode = RLP.encodeElement(code);

			this.rlpEncoded = RLP.encodeList(rlpKeysList, rlpValuesList, rlpCode);
		}
		return rlpEncoded;
	}
 
Example #10
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 #11
Source File: Block.java    From ethereumj with MIT License 6 votes vote down vote up
private void addTxReceipt(int counter, TransactionReceipt txReceipt) {
    this.txReceiptList.add(txReceipt);
    this.txsState.update(RLP.encodeInt(counter), txReceipt.getEncoded());
    
    /* Figure out type of tx
     * 1. Contract creation
     * 		- perform code
     * 		- create state object
     * 		- add contract body to DB, 
     * 2. Contract call			
     * 		- perform code
     * 		- update state object
     * 3. Account to account	- 
     * 		- update state object
     */
}
 
Example #12
Source File: Serializers.java    From nuls with MIT License 5 votes vote down vote up
@Override
public DataWord deserialize(byte[] stream) {
    if (stream == null || stream.length == 0) {
        return null;
    }
    byte[] dataDecoded = RLP.decode2(stream).get(0).getRLPData();
    return DataWord.of(dataDecoded);
}
 
Example #13
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 #14
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 #15
Source File: BlocksMessage.java    From ethereumj with MIT License 5 votes vote down vote up
private void encode() {

        List<byte[]> encodedElements = new Vector<>();
        encodedElements.add(RLP.encodeByte(BLOCKS.asByte()));

        for (Block block : blocks){
            encodedElements.add(block.getEncoded());
        }

        byte[][] encodedElementArray = encodedElements
                .toArray(new byte[encodedElements.size()][]);

        this.encoded = RLP.encodeList(encodedElementArray);
    }
 
Example #16
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 #17
Source File: StatusMessage.java    From ethereumj with MIT License 5 votes vote down vote up
private void encode() {
    byte[] command			= RLP.encodeByte(STATUS.asByte());
    byte[] protocolVersion	= RLP.encodeByte(this.protocolVersion);
    byte[] networkId		= RLP.encodeByte(this.networkId);
    byte[] totalDifficulty	= RLP.encodeElement(this.totalDifficulty);
    byte[] bestHash			= RLP.encodeElement(this.bestHash);
    byte[] genesisHash		= RLP.encodeElement(this.genesisHash);

    this.encoded = RLP.encodeList(command, protocolVersion, networkId,
            totalDifficulty, bestHash, genesisHash);
}
 
Example #18
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 #19
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 #20
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 #21
Source File: GetBlocksMessage.java    From ethereumj with MIT License 5 votes vote down vote up
private void encode() {
	List<byte[]> encodedElements = new ArrayList<>();
	encodedElements.add(RLP.encodeByte(GET_BLOCKS.asByte()));
	for (byte[] hash : blockHashes)
		encodedElements.add(RLP.encodeElement(hash));
	byte[][] encodedElementArray = encodedElements
			.toArray(new byte[encodedElements.size()][]);
	this.encoded = RLP.encodeList(encodedElementArray);
}
 
Example #22
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 #23
Source File: TransactionsMessage.java    From ethereumj with MIT License 5 votes vote down vote up
private void encode() {
  	List<byte[]> encodedElements = new ArrayList<>();
  	encodedElements.add(RLP.encodeByte(TRANSACTIONS.asByte()));
  	for (Transaction tx : transactions)
          encodedElements.add(tx.getEncoded());
byte[][] encodedElementArray = encodedElements
		.toArray(new byte[encodedElements.size()][]);
      this.encoded = RLP.encodeList(encodedElementArray);
  }
 
Example #24
Source File: PeersMessage.java    From ethereumj with MIT License 5 votes vote down vote up
private void encode() {
	byte[][] encodedByteArrays = new byte[this.peers.size() + 1][];
	encodedByteArrays[0] = RLP.encodeByte(this.getCommand().asByte());
	List<Peer> peerList = new ArrayList<>(this.peers);
	for (int i = 0; i < peerList.size(); i++) {
		encodedByteArrays[i + 1] = peerList.get(i).getEncoded();
	}
	this.encoded = RLP.encodeList(encodedByteArrays);
}
 
Example #25
Source File: ContractDetails.java    From ethereumj with MIT License 5 votes vote down vote up
public byte[] getStorageHash() {

    	storageTrie = new TrieImpl(null);
        // calc the trie for root hash
        for (int i = 0; i < storageKeys.size(); ++i){
			storageTrie.update(storageKeys.get(i).getData(), RLP
					.encodeElement(storageValues.get(i).getNoLeadZeroesData()));
        }
        return storageTrie.getRootHash();
    }
 
Example #26
Source File: HashUtil.java    From ethereumj with MIT License 5 votes vote down vote up
/**
 * The way to calculate new address inside ethereum
 *
 * @param addr  - creating addres
 * @param nonce - nonce of creating address
 * @return new address
 */
public static byte[] calcNewAddr(byte[] addr, byte[] nonce) {

    byte[] encSender = RLP.encodeElement(addr);
    byte[] encNonce = RLP.encodeBigInteger(new BigInteger(1, nonce));
    byte[] newAddress = sha3omit12(RLP.encodeList(encSender, encNonce));

    return newAddress;
}
 
Example #27
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 #28
Source File: AccountState.java    From nuls with MIT License 5 votes vote down vote up
public byte[] getEncoded() {
    if (rlpEncoded == null) {
        byte[] nonce = RLP.encodeBigInteger(this.nonce);
        byte[] balance = RLP.encodeBigInteger(this.balance);
        byte[] stateRoot = RLP.encodeElement(this.stateRoot);
        byte[] codeHash = RLP.encodeElement(this.codeHash);
        byte[] owner = RLP.encodeElement(this.owner);
        this.rlpEncoded = RLP.encodeList(nonce, balance, stateRoot, codeHash, owner);
    }
    return rlpEncoded;
}
 
Example #29
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 #30
Source File: Block.java    From nuls with MIT License 5 votes vote down vote up
public byte[] getEncoded() {
    if (rlpEncoded == null) {
        byte[] header = this.header.getEncoded();

        List<byte[]> block = getBodyElements();
        block.add(0, header);
        byte[][] elements = block.toArray(new byte[block.size()][]);

        this.rlpEncoded = RLP.encodeList(elements);
    }
    return rlpEncoded;
}