Java源码示例:org.ethereum.util.RLP

示例1
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;
    }
}
 
示例2
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();
}
 
示例3
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;
    }
}
 
示例4
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;
    }
}
 
示例5
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;
	}
 
示例6
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;
}
 
示例7
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();
    }
 
示例8
/**
   *  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;
  }
 
示例9
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;
    }
 
示例10
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
     */
}
 
示例11
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);
       }
}
 
示例12
public byte[] serialize() {
    byte[][] insertedBytes = new byte[insertedKeys.size()][];
    for (int i = 0; i < insertedBytes.length; i++) {
        insertedBytes[i] = RLP.encodeElement(insertedKeys.get(i));
    }
    byte[][] deletedBytes = new byte[deletedKeys.size()][];
    for (int i = 0; i < deletedBytes.length; i++) {
        deletedBytes[i] = RLP.encodeElement(deletedKeys.get(i));
    }
    return RLP.encodeList(RLP.encodeElement(updateHash),
            RLP.encodeList(insertedBytes), RLP.encodeList(deletedBytes));
}
 
示例13
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());
    }
}
 
示例14
@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);
}
 
示例15
/**
 * The way to calculate new address inside ethereum
 *
 * @param addr  - creating address
 * @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));

    return sha3omit12(RLP.encodeList(encSender, encNonce));
}
 
示例16
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;
}
 
示例17
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;
}
 
示例18
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();
}
 
示例19
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;
}
 
示例20
/**
 * 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));

    return sha3omit12(RLP.encodeList(encSender, encNonce));
}
 
示例21
public byte[] getEncoded() {
    if(this.rlpEncoded != null) {
        return this.rlpEncoded;
    } 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);
        byte[] v;
        byte[] r;
        byte[] s;
        if(this.signature != null) {
            v = RLP.encodeByte(this.signature.v);
            r = RLP.encodeElement(BigIntegers.asUnsignedByteArray(this.signature.r));
            s = RLP.encodeElement(BigIntegers.asUnsignedByteArray(this.signature.s));
        } else {
            v = RLP.encodeElement(ByteUtil.EMPTY_BYTE_ARRAY);
            r = RLP.encodeElement(ByteUtil.EMPTY_BYTE_ARRAY);
            s = RLP.encodeElement(ByteUtil.EMPTY_BYTE_ARRAY);
        }

        this.rlpEncoded = RLP.encodeList(new byte[][]{nonce, gasPrice, gasLimit, receiveAddress, value, data, v, r, s});
        this.hash = this.getHash();
        return this.rlpEncoded;
    }
}
 
示例22
public byte[] serialize() {
    byte[][] insertedBytes = new byte[insertedKeys.size()][];
    for (int i = 0; i < insertedBytes.length; i++) {
        insertedBytes[i] = RLP.encodeElement(insertedKeys.get(i));
    }
    byte[][] deletedBytes = new byte[deletedKeys.size()][];
    for (int i = 0; i < deletedBytes.length; i++) {
        deletedBytes[i] = RLP.encodeElement(deletedKeys.get(i));
    }
    return RLP.encodeList(RLP.encodeElement(updateHash),
            RLP.encodeList(insertedBytes), RLP.encodeList(deletedBytes));
}
 
示例23
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());
    }
}
 
示例24
@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);
}
 
示例25
/**
 * The way to calculate new address inside ethereum
 *
 * @param addr  - creating address
 * @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));

    return sha3omit12(RLP.encodeList(encSender, encNonce));
}
 
示例26
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;
}
 
示例27
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;
}
 
示例28
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();
}
 
示例29
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;
}
 
示例30
/**
 * 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;
}