|
14 | 14 | */
|
15 | 15 | package org.hyperledger.besu.ethereum.core.encoding;
|
16 | 16 |
|
17 |
| -import org.hyperledger.besu.ethereum.core.DepositContract; |
18 | 17 | import org.hyperledger.besu.evm.log.Log;
|
19 | 18 |
|
20 | 19 | import org.apache.tuweni.bytes.Bytes;
|
21 |
| -import org.web3j.tx.Contract; |
22 | 20 |
|
| 21 | +/** |
| 22 | + * Decodes a deposit log into its constituent parts. |
| 23 | + * |
| 24 | + * <p>Deposit logs are emitted by the Ethereum 2.0 deposit contract when a validator deposits funds |
| 25 | + * into the contract. |
| 26 | + * |
| 27 | + * <p>The data of the deposit log is organized in 32 bytes chunks. The data layout is: |
| 28 | + * |
| 29 | + * <ol> |
| 30 | + * <li>POSITION_PUB_KEY |
| 31 | + * <li>POSITION_WITHDRAWAL_CRED |
| 32 | + * <li>POSITION_AMOUNT |
| 33 | + * <li>POSITION_SIGNATURE |
| 34 | + * <li>POSITION_INDEX |
| 35 | + * <li>LENGTH_PUB_KEY |
| 36 | + * <li>PUB_KEY_DATA_1 |
| 37 | + * <li>PUB_KEY_DATA_2 |
| 38 | + * <li>LENGTH_WITHDRAWAL_CRED |
| 39 | + * <li>WITHDRAWAL_CRED_DATA |
| 40 | + * <li>LENGTH_AMOUNT |
| 41 | + * <li>AMOUNT_DATA |
| 42 | + * <li>LENGTH_SIGNATURE |
| 43 | + * <li>SIGNATURE_DATA_1 |
| 44 | + * <li>SIGNATURE_DATA_2 |
| 45 | + * <li>LENGTH_INDEX |
| 46 | + * <li>INDEX_DATA |
| 47 | + * </ol> |
| 48 | + * |
| 49 | + * We are only interested in the data fields and will skip over the position and length fields. |
| 50 | + */ |
23 | 51 | public class DepositLogDecoder {
|
24 | 52 |
|
| 53 | + private static final int DEPOSIT_LOG_LENGTH = 576; |
| 54 | + private static final int LENGTH_FIELD_SIZE = 32; |
| 55 | + |
| 56 | + private static final int PUB_KEY_LENGTH = 48; |
| 57 | + private static final int WITHDRAWAL_CRED_LENGTH = 32; |
| 58 | + private static final int AMOUNT_LENGTH = 8; |
| 59 | + private static final int SIGNATURE_LENGTH = 96; |
| 60 | + private static final int INDEX_LENGTH = 8; |
| 61 | + |
| 62 | + // PublicKey is the first element. |
| 63 | + private static final int PUB_KEY_OFFSET = 0; |
| 64 | + // ABI encoding pads values to 32 bytes, so despite BLS public keys being length 48, the value |
| 65 | + // length here is 64. Then skip over the next length value. |
| 66 | + private static final int WITHDRAWAL_CRED_OFFSET = |
| 67 | + PUB_KEY_OFFSET + PUB_KEY_LENGTH + 16 + LENGTH_FIELD_SIZE; |
| 68 | + // WithdrawalCredentials is 32 bytes. Read that value then skip over next length. |
| 69 | + private static final int AMOUNT_OFFSET = |
| 70 | + WITHDRAWAL_CRED_OFFSET + WITHDRAWAL_CRED_LENGTH + LENGTH_FIELD_SIZE; |
| 71 | + // amount is only 8 bytes long but is stored in a 32 byte slot. The remaining 24 bytes need to be |
| 72 | + // added to the offset. |
| 73 | + private static final int SIGNATURE_OFFSET = |
| 74 | + AMOUNT_OFFSET + AMOUNT_LENGTH + 24 + LENGTH_FIELD_SIZE; |
| 75 | + // Signature is 96 bytes. Skip over it and the next length. |
| 76 | + private static final int INDEX_OFFSET = SIGNATURE_OFFSET + SIGNATURE_LENGTH + LENGTH_FIELD_SIZE; |
| 77 | + |
| 78 | + // The ABI encodes the position of dynamic elements first. Since there are 5 |
| 79 | + // elements, skip over the positional data. The first 32 bytes of dynamic |
| 80 | + // elements also encode their actual length. Skip over that value too. |
| 81 | + private static final int DATA_START_POSITION = 32 * 5 + LENGTH_FIELD_SIZE; |
| 82 | + |
25 | 83 | public static Bytes decodeFromLog(final Log log) {
|
26 |
| - Contract.EventValuesWithLog eventValues = DepositContract.staticExtractDepositEventWithLog(log); |
27 |
| - final Bytes rawPublicKey = |
28 |
| - Bytes.wrap((byte[]) eventValues.getNonIndexedValues().get(0).getValue()); |
29 |
| - final Bytes rawWithdrawalCredential = |
30 |
| - Bytes.wrap((byte[]) eventValues.getNonIndexedValues().get(1).getValue()); |
31 |
| - final Bytes rawAmount = |
32 |
| - Bytes.wrap((byte[]) eventValues.getNonIndexedValues().get(2).getValue()); |
33 |
| - final Bytes rawSignature = |
34 |
| - Bytes.wrap((byte[]) eventValues.getNonIndexedValues().get(3).getValue()); |
35 |
| - final Bytes rawIndex = Bytes.wrap((byte[]) eventValues.getNonIndexedValues().get(4).getValue()); |
36 |
| - |
37 |
| - return Bytes.concatenate( |
38 |
| - rawPublicKey, rawWithdrawalCredential, rawAmount, rawSignature, rawIndex); |
| 84 | + final Bytes data = log.getData(); |
| 85 | + |
| 86 | + if (data.size() != DEPOSIT_LOG_LENGTH) { |
| 87 | + throw new IllegalArgumentException( |
| 88 | + "Invalid deposit log length. Must be " |
| 89 | + + DEPOSIT_LOG_LENGTH |
| 90 | + + " bytes, but is " |
| 91 | + + data.size() |
| 92 | + + " bytes"); |
| 93 | + } |
| 94 | + |
| 95 | + final Bytes pubKey = data.slice(DATA_START_POSITION + PUB_KEY_OFFSET, PUB_KEY_LENGTH); |
| 96 | + final Bytes withdrawalCred = |
| 97 | + data.slice(DATA_START_POSITION + WITHDRAWAL_CRED_OFFSET, WITHDRAWAL_CRED_LENGTH); |
| 98 | + final Bytes amount = data.slice(DATA_START_POSITION + AMOUNT_OFFSET, AMOUNT_LENGTH); |
| 99 | + final Bytes signature = data.slice(DATA_START_POSITION + SIGNATURE_OFFSET, SIGNATURE_LENGTH); |
| 100 | + final Bytes index = data.slice(DATA_START_POSITION + INDEX_OFFSET, INDEX_LENGTH); |
| 101 | + |
| 102 | + return Bytes.concatenate(pubKey, withdrawalCred, amount, signature, index); |
39 | 103 | }
|
40 | 104 | }
|
0 commit comments