Skip to content

Commit

Permalink
Fix eip4844 state transition functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hwwhww committed Jul 1, 2022
1 parent b8cbfe5 commit 499226c
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 14 deletions.
5 changes: 4 additions & 1 deletion specs/eip4844/beacon-chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ def tx_peek_blob_versioned_hashes(opaque_tx: Transaction) -> Sequence[VersionedH
message_offset = 1 + uint32.decode_bytes(opaque_tx[1:5])
# field offset: 32 + 8 + 32 + 32 + 8 + 4 + 32 + 4 + 4 = 156
blob_versioned_hashes_offset = uint32.decode_bytes(opaque_tx[(message_offset + 156):(message_offset + 160)])
return [VersionedHash(opaque_tx[x:(x + 32)]) for x in range(blob_versioned_hashes_offset, len(opaque_tx), 32)]
return [
VersionedHash(opaque_tx[x:(x + 32)])
for x in range(message_offset + blob_versioned_hashes_offset, len(opaque_tx), 32)
]
```

#### `verify_kzgs_against_transactions`
Expand Down
2 changes: 1 addition & 1 deletion specs/eip4844/polynomial-commitments.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def lincomb(points: Sequence[KZGCommitment], scalars: Sequence[BLSFieldElement])
r = bls.Z1
for x, a in zip(points, scalars):
r = bls.add(r, bls.multiply(x, a))
return r
return KZGCommitment(kzg.G1_to_bytes48(r))
```

#### `matrix_lincomb`
Expand Down
2 changes: 1 addition & 1 deletion specs/eip4844/validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ blobs_sidecar = BlobsSidecar(
And then signed:

```
domain = get_domain(state, DOMAIN_BLOBS_SIDECAR, blobs_sidecar.beacon_block_slot / SLOTS_PER_EPOCH)
domain = get_domain(state, DOMAIN_BLOBS_SIDECAR, blobs_sidecar.beacon_block_slot // SLOTS_PER_EPOCH)
signing_root = compute_signing_root(blobs_sidecar, domain)
signature = bls.Sign(privkey, signing_root)
signed_blobs_sidecar = SignedBlobsSidecar(message=blobs_sidecar, signature=signature)
Expand Down
Empty file.
28 changes: 28 additions & 0 deletions tests/core/pyspec/eth2spec/test/eip4844/sanity/test_blocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from eth2spec.test.helpers.state import (
state_transition_and_sign_block
)
from eth2spec.test.helpers.block import (
build_empty_block_for_next_slot
)
from eth2spec.test.context import (
spec_state_test,
with_eip4844_and_later,
)
from eth2spec.test.helpers.sharding import (
get_sample_opaque_tx,
)


@with_eip4844_and_later
@spec_state_test
def test_simple_blobs(spec, state):
yield 'pre', state

block = build_empty_block_for_next_slot(spec, state)
opaque_tx, _, blob_kzgs = get_sample_opaque_tx(spec)
block.body.blob_kzgs = blob_kzgs
block.body.execution_payload.transactions = [opaque_tx]
signed_block = state_transition_and_sign_block(spec, state, block)

yield 'blocks', [signed_block]
yield 'post', state
14 changes: 4 additions & 10 deletions tests/core/pyspec/eth2spec/test/eip4844/unittests/test_kzg.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@

import random
from eth2spec.test.helpers.constants import (
EIP4844,
MINIMAL,
)
from eth2spec.test.helpers.sharding import (
create_blob,
)
from eth2spec.test.context import (
with_phases,
spec_state_test,
with_presets,
)


def _create_blob(spec):
rng = random.Random(5566)
return spec.Blob([
rng.randint(0, spec.BLS_MODULUS - 1)
for _ in range(spec.FIELD_ELEMENTS_PER_BLOB)
])


@with_phases([EIP4844])
@spec_state_test
@with_presets([MINIMAL])
def test_blob_to_kzg(spec, state):
blob = _create_blob(spec)
blob = create_blob(spec)
spec.blob_to_kzg(blob)
81 changes: 81 additions & 0 deletions tests/core/pyspec/eth2spec/test/helpers/sharding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import random
from eth2spec.utils.ssz.ssz_typing import (
Container,
Bytes20, Bytes32,
ByteList,
List,
Union,
boolean,
uint256, uint64,
)
from eth2spec.utils.ssz.ssz_impl import serialize


#
# Containers from EIP-4844
#
MAX_CALLDATA_SIZE = 2**24
MAX_VERSIONED_HASHES_LIST_SIZE = 2**24
MAX_ACCESS_LIST_STORAGE_KEYS = 2**24
MAX_ACCESS_LIST_SIZE = 2**24


class AccessTuple(Container):
address: Bytes20 # Address = Bytes20
storage_keys: List[Bytes32, MAX_ACCESS_LIST_STORAGE_KEYS]


class ECDSASignature(Container):
y_parity: boolean
r: uint256
s: uint256


class BlobTransaction(Container):
chain_id: uint256
nonce: uint64
priority_fee_per_gas: uint256
max_basefee_per_gas: uint256
gas: uint64
to: Union[None, Bytes20] # Address = Bytes20
value: uint256
data: ByteList[MAX_CALLDATA_SIZE]
access_list: List[AccessTuple, MAX_ACCESS_LIST_SIZE]
blob_versioned_hashes: List[Bytes32, MAX_VERSIONED_HASHES_LIST_SIZE]


class SignedBlobTransaction(Container):
message: BlobTransaction
signature: ECDSASignature


def create_blob(spec, rng=None):
if rng is None:
rng = random.Random(5566)

return spec.Blob([
rng.randint(0, spec.BLS_MODULUS - 1)
for _ in range(spec.FIELD_ELEMENTS_PER_BLOB)
])


def get_sample_opaque_tx(spec, blob_count=1, rng=None):
blobs = []
blob_kzgs = []
blob_versioned_hashes = []
for _ in range(blob_count):
blob = create_blob(spec, rng)
blob_kzg = spec.KZGCommitment(spec.blob_to_kzg(blob))
blob_versioned_hash = spec.kzg_to_versioned_hash(blob_kzg)
blobs.append(blob)
blob_kzgs.append(blob_kzg)
blob_versioned_hashes.append(blob_versioned_hash)

signed_blob_tx = SignedBlobTransaction(
message=BlobTransaction(
blob_versioned_hashes=blob_versioned_hashes,
)
)
serialized_tx = serialize(signed_blob_tx)
opaque_tx = spec.uint_to_bytes(spec.BLOB_TX_TYPE) + serialized_tx
return opaque_tx, blobs, blob_kzgs
4 changes: 3 additions & 1 deletion tests/core/pyspec/eth2spec/utils/kzg.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Ref: https://github.com/ethereum/research/blob/8f084630528ba33d92b2bc05edf5338dd193c6f1/trusted_setup/trusted_setup.py

from py_ecc.optimized_bls12_381 import ( # noqa: F401
G1,
G2,
Expand All @@ -11,6 +10,9 @@
multiply,
neg,
)
from py_ecc.bls.g2_primitives import ( # noqa: F401
G1_to_pubkey as G1_to_bytes48,
)


PRIMITIVE_ROOT_OF_UNITY = 7
Expand Down

0 comments on commit 499226c

Please sign in to comment.