Skip to content

Commit

Permalink
Merge pull request #91 from web3j/nicks/eip-tx-types
Browse files Browse the repository at this point in the history
EIP Transaction Types Added
  • Loading branch information
NickSneo authored Feb 15, 2024
2 parents 8e2d112 + 9588d03 commit 65fd079
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 3 deletions.
Binary file added docs/img/Transaction-Types/blobTx.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/Transaction-Types/blobTx_lifetime.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 54 additions & 0 deletions docs/transactions/EIP_transaction_types/eip1559_transaction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
## EIP-1559 Transaction

Ethereum historically priced transaction fees using a simple auction mechanism, where users send transactions with bids
("gasprices") and miners choose transactions with the highest bids, and transactions that get included pay the bid that
they specify. This leads to several large sources of inefficiency:

- Mismatch between volatility of transaction fee levels and social cost of transactions
- Needless delays for users
- Inefficiencies of first price auctions
- Instability of blockchains with no block reward

With EIP-1559, there will be a discrete “base fee” for transactions to be included in the next block. For users or
applications that want to prioritize their transaction, they can add a “tip,” which is called a “priority fee” to pay a
miner for faster inclusion.

### Spec
Introduce a new EIP-2718 transaction type, with value `2(0x02)` and `TransactionPayload` for this transaction is `rlp([chain_id,
nonce, max_priority_fee_per_gas, max_fee_per_gas, gas_limit, destination, amount, data, access_list, signature_y_parity,
signature_r, signature_s])`.

The `GASPRICE (0x3a)` opcode return the `effective_gas_price`.

### Example

```java

public void signedEip1559() throws SignatureException {
Credentials credentials = Credentials.create("<privateKey>");
Web3j web3j = Web3j.build(new HttpService("<nodeUrl>"));

final RawTransaction rawTransaction = createEip1559RawTransaction();

final byte[] signedMessage =
TransactionEncoder.signMessage(rawTransaction, credentials);
final String signedHexMessage = Numeric.toHexString(signedMessage);

EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(signedHexMessage).send();

System.out.println("Transaction hash: " + ethSendTransaction.getTransactionHash());
System.out.println("Tx Receipt = " + web3j.ethGetTransactionReceipt(ethSendTransaction.getTransactionHash()).send().getTransactionReceipt());

}

private static RawTransaction createEip1559RawTransaction() {
return RawTransaction.createEtherTransaction(
3L,
BigInteger.valueOf(0),
BigInteger.valueOf(30000),
"0x627306090abab3a6e1400e9345bc60c78a8bef57",
BigInteger.valueOf(123),
BigInteger.valueOf(5678),
BigInteger.valueOf(1100000));
}
```
55 changes: 55 additions & 0 deletions docs/transactions/EIP_transaction_types/eip2930_transaction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
## EIP 2930 Transaction

Adds a transaction type which contains an `accessList`, a list of addresses and storage keys that the transaction plans
to access: these addresses and storage keys are added into the `accessed_addresses` and `accessed_storage_keys` global sets.
Accesses outside the list are possible, but become more expensive.

### Spec
The new EIP-2718 `TransactionType = 1(0x01)` and TransactionPayload for this transaction is `rlp([chainId, nonce, gasPrice, gasLimit, to, value, data,
accessList, signatureYParity, signatureR, signatureS])`.

### Example

```java
public void signedEip2930() throws SignatureException {
Credentials credentials = Credentials.create("<privateKey>");
Web3j web3j = Web3j.build(new HttpService("<nodeUrl>"));

final RawTransaction rawTransaction = createEip2930RawTransaction();

final byte[] signedMessage =
TransactionEncoder.signMessage(rawTransaction, credentials);
final String signedHexMessage = Numeric.toHexString(signedMessage);

EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(signedHexMessage).send();

System.out.println("Transaction hash: " + ethSendTransaction.getTransactionHash());
System.out.println("Tx Receipt = " + web3j.ethGetTransactionReceipt(ethSendTransaction.getTransactionHash()).send().getTransactionReceipt());

}

private static RawTransaction createEip2930RawTransaction() {
// Test example from https://eips.ethereum.org/EIPS/eip-2930
List<AccessListObject> accessList = Stream.of(
new AccessListObject(
"0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae",
Stream.of(
"0x0000000000000000000000000000000000000000000000000000000000000003",
"0x0000000000000000000000000000000000000000000000000000000000000007")
.collect(toList())),
new AccessListObject(
"0xbb9bc244d798123fde783fcc1c72d3bb8c189413",
Collections.emptyList()))
.collect(toList());

return RawTransaction.createTransaction(
3L,
BigInteger.valueOf(0),
BigInteger.valueOf(30000),
BigInteger.valueOf(500000),
"0x627306090abab3a6e1400e9345bc60c78a8bef57",
BigInteger.valueOf(1000000),
"0x1000001111100000",
accessList);
}
```
109 changes: 109 additions & 0 deletions docs/transactions/EIP_transaction_types/eip4844_blob_transaction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
## EIP-4844 Blob Transaction

EIP-4844 introduces a new kind of transaction type to Ethereum “blob-carrying transactions” which contain a large amount of data
that cannot be accessed by EVM execution, but whose commitment can be accessed. These blobs to be persisted in the beacon
node for a short period of time.

Full data sharding will still take a considerable amount of time to finish implementing and deploying. This EIP provides
a stop-gap solution until that point by implementing the transaction format that would be used in sharding,
but not actually sharding those transactions. Instead, the data from this transaction format is simply part of the beacon
chain and is fully downloaded by all consensus nodes (but can be deleted after only a relatively short delay). Compared
to full data sharding, this EIP has a reduced cap on the number of these transactions that can be included, corresponding
to a target of ~0.375 MB per block and a limit of ~0.75 MB.

![Lifetime of a blob TX](../../img/Transaction-Types/blobTx_lifetime.png)

### Spec

The new type of EIP-2718 transaction, “blob transaction”, where the TransactionType is `BLOB_TX_TYPE` = `Bytes1(0x03)`.
The fields `chain_id`, `nonce`, `max_priority_fee_per_gas`, `max_fee_per_gas`, `gas_limit`, `value`, `data`, and `access_list`
follow the same semantics as EIP-1559.

The field `max_fee_per_blob_gas` is a `uint256` and the field `blob_versioned_hashes` represents a list of hash outputs
from` kzg_to_versioned_hash`.

![Blob Transaction](../../img/Transaction-Types/blobTx.png)

### Networking

We can send a signed EIP-4844 transaction to `eth_sendRawTransaction` and the raw form must be the network form.
This means it includes the tx_payload_body, blobs, KZG commitments, and KZG proofs.

Each of these elements are defined as follows:

- <b>tx_payload_body</b> - is the TransactionPayloadBody of standard EIP-2718 blob transaction
- <b>blobs</b> - list of Blob items
- <b>commitments</b> - list of KZGCommitment of the corresponding blobs
- <b>proofs</b> - list of KZGProof of the corresponding blobs and commitments


### Example

#### Prerequisites
Before proceeding with the following code example, please ensure that the network you are working with has EIP-4844 support
enabled.

```java
public class Web3App {

public static void main(String[] args) throws Exception {

Credentials credentials = Credentials.create("<privateKey>");
Web3j web3j = Web3j.build(new HttpService("<nodeUrl>"));

EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
credentials.getAddress(), DefaultBlockParameterName.LATEST).send();
BigInteger nonce = ethGetTransactionCount.getTransactionCount();
System.out.println("nonce = " + nonce);

RawTransaction rawTransaction = createEip4844RawTransaction(nonce);
// Sign the transaction
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
String hexValue = Numeric.toHexString(signedMessage);
// Send the transaction
EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).send();

System.out.println("Transaction hash: " + ethSendTransaction.getTransactionHash());
System.out.println("Tx Receipt = " + web3j.ethGetTransactionReceipt(ethSendTransaction.getTransactionHash()).send().getTransactionReceipt());

// Tx Receipt = Optional[TransactionReceipt{transactionHash='0x79cfe2c65f1a039b372436cabcd964df1038c45e5d3ebf542af1afca3356a2bc', transactionIndex='0x19',
// blockHash='0x118be795f782d2d72b38217254efea4817a1a544cd847537049cb5028f87d183', blockNumber='0x4fd448', cumulativeGasUsed='0x142da9', gasUsed='0x5208',
// contractAddress='null', root='null', status='0x1', from='0x1ba43becc3ea96c37343b7cb18de7386ba29445b', to='0xff00000000000000000000000000000011155421',
// logs=[], logsBloom='0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
// 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
// 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
// 000000000000000000000000000000000000000000000000000000000000000000000000000000000000', revertReason='null', type='0x3', effectiveGasPrice='0x14e2779259'}]

web3j.shutdown();
}

private static RawTransaction createEip4844RawTransaction(BigInteger nonce) {
List<Blob> blobs = new ArrayList<>();

blobs.add(new Blob("<blobData_in_Bytes>"));
return RawTransaction.createTransaction(
blobs,
11155111L,
nonce,
BigInteger.valueOf(10_000_000_000L),
BigInteger.valueOf(50_000_000_000L),
BigInteger.valueOf(3_00_000L),
"<toAddress>",
BigInteger.valueOf(0),
"",
BigInteger.valueOf(25_689_969_947L));
}
}
```

If we just want to calculate KZG commitment and KZG proofs from a blob, we can do that using BlobUtils Class functions.

```java
Blob blob = new Blob(
Numeric.hexStringToByteArray(
loadResourceAsString("blob_data.txt")));
Bytes commitment = BlobUtils.getCommitment(blob);
Bytes proofs = BlobUtils.getProof(blob, commitment);
Bytes versionedHashes = BlobUtils.kzgToVersionedHash(commitment);
BlobUtils.checkProofValidity(blob, commitment, proofs)
```
2 changes: 1 addition & 1 deletion docs/versions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[
{"version": "4.10.0", "title": "", "aliases": ["latest"]}
{"version": "4.11.0", "title": "", "aliases": ["latest"]}
]
8 changes: 6 additions & 2 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repo_name: "web3j/web3j"
repo_url: "https://github.com/web3j/web3j"

# Copyright
copyright: "Copyright &copy; 2017-2023 Web3 Labs Ltd"
copyright: "Copyright &copy; 2017-2024 Web3 Labs Ltd"


nav:
Expand Down Expand Up @@ -38,6 +38,10 @@ nav:
- Transaction Mechanisms: transactions/transaction_mechanisms.md
- Transaction Nonce: transactions/transaction_nonce.md
- Transaction Types: transactions/transaction_types.md
- EIP Transaction Types:
- EIP-2930 Transaction: transactions/EIP_transaction_types/eip2930_transaction.md
- EIP-1559 Transaction: transactions/EIP_transaction_types/eip1559_transaction.md
- EIP-4844 Blob Transaction: transactions/EIP_transaction_types/eip4844_blob_transaction.md
- Transactions and Smart Contracts: transactions/transactions_and_smart_contracts.md
- Wallet Files: transactions/wallet_files.md
- Web3j Eth2 Client: web3j_eth2_client.md
Expand Down Expand Up @@ -88,7 +92,7 @@ theme:
# Customization
extra:
web3j:
version: 4.10.0
version: 4.11.0
android:
version: 4.6.0
sokt:
Expand Down

0 comments on commit 65fd079

Please sign in to comment.