Skip to content

Commit

Permalink
Merge pull request ethereum#87 from mdehoog/blob-tx-hash2
Browse files Browse the repository at this point in the history
update blob tx hash computations to use serialize instead of hash_tree_root
  • Loading branch information
roberto-bayardo committed Jan 6, 2023
2 parents 2bd3a3c + b4d3e40 commit ed01dbb
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 72 deletions.
57 changes: 0 additions & 57 deletions core/types/data_blob_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/holiman/uint256"
"github.com/protolambda/ztyp/codec"
"github.com/protolambda/ztyp/conv"
"github.com/protolambda/ztyp/tree"
. "github.com/protolambda/ztyp/view"
)

Expand Down Expand Up @@ -58,12 +57,6 @@ func (*AddressSSZ) FixedLength() uint64 {
return 20
}

func (addr *AddressSSZ) HashTreeRoot(hFn tree.HashFn) tree.Root {
var out tree.Root
copy(out[0:20], addr[:])
return out
}

// AddressOptionalSSZ implements Union[None, Address]
type AddressOptionalSSZ struct {
Address *AddressSSZ
Expand Down Expand Up @@ -112,14 +105,6 @@ func (*AddressOptionalSSZ) FixedLength() uint64 {
return 0
}

func (ao *AddressOptionalSSZ) HashTreeRoot(hFn tree.HashFn) tree.Root {
if ao.Address == nil {
return hFn(tree.Root{}, tree.Root{0: 0})
} else {
return hFn(ao.Address.HashTreeRoot(hFn), tree.Root{0: 1})
}
}

type TxDataView []byte

func (tdv *TxDataView) Deserialize(dr *codec.DecodingReader) error {
Expand All @@ -138,10 +123,6 @@ func (tdv *TxDataView) FixedLength() uint64 {
return 0
}

func (tdv TxDataView) HashTreeRoot(hFn tree.HashFn) tree.Root {
return hFn.ByteListHTR(tdv, MAX_CALLDATA_SIZE)
}

func (tdv TxDataView) MarshalText() ([]byte, error) {
return conv.BytesMarshalText(tdv[:])
}
Expand Down Expand Up @@ -214,16 +195,6 @@ func (vhv *VersionedHashesView) FixedLength() uint64 {
return 0 // it's a list, no fixed length
}

func (vhv VersionedHashesView) HashTreeRoot(hFn tree.HashFn) tree.Root {
length := uint64(len(vhv))
return hFn.ComplexListHTR(func(i uint64) tree.HTR {
if i < length {
return (*tree.Root)(&vhv[i])
}
return nil
}, length, MAX_VERSIONED_HASHES_LIST_SIZE)
}

type StorageKeysView []common.Hash

func (skv *StorageKeysView) Deserialize(dr *codec.DecodingReader) error {
Expand All @@ -242,16 +213,6 @@ func (skv *StorageKeysView) FixedLength() uint64 {
return 0 // it's a list, no fixed length
}

func (skv StorageKeysView) HashTreeRoot(hFn tree.HashFn) tree.Root {
length := uint64(len(skv))
return hFn.ComplexListHTR(func(i uint64) tree.HTR {
if i < length {
return (*tree.Root)(&skv[i])
}
return nil
}, length, MAX_ACCESS_LIST_STORAGE_KEYS)
}

type AccessTupleView AccessTuple

func (atv *AccessTupleView) Deserialize(dr *codec.DecodingReader) error {
Expand All @@ -270,10 +231,6 @@ func (atv *AccessTupleView) FixedLength() uint64 {
return 0
}

func (atv *AccessTupleView) HashTreeRoot(hFn tree.HashFn) tree.Root {
return hFn.HashTreeRoot((*AddressSSZ)(&atv.Address), (*StorageKeysView)(&atv.StorageKeys))
}

type AccessListView AccessList

func (alv *AccessListView) Deserialize(dr *codec.DecodingReader) error {
Expand Down Expand Up @@ -301,16 +258,6 @@ func (alv *AccessListView) FixedLength() uint64 {
return 0
}

func (alv AccessListView) HashTreeRoot(hFn tree.HashFn) tree.Root {
length := uint64(len(alv))
return hFn.ComplexListHTR(func(i uint64) tree.HTR {
if i < length {
return (*AccessTupleView)(&alv[i])
}
return nil
}, length, MAX_ACCESS_LIST_SIZE)
}

type BlobTxMessage struct {
ChainID Uint256View
Nonce Uint64View
Expand Down Expand Up @@ -350,10 +297,6 @@ func (stx *SignedBlobTx) FixedLength() uint64 {
return 0
}

func (tx *BlobTxMessage) HashTreeRoot(hFn tree.HashFn) tree.Root {
return hFn.HashTreeRoot(&tx.ChainID, &tx.Nonce, &tx.GasTipCap, &tx.GasFeeCap, &tx.Gas, &tx.To, &tx.Value, &tx.Data, &tx.AccessList, &tx.MaxFeePerDataGas, &tx.BlobVersionedHashes)
}

// copy creates a deep copy of the transaction data and initializes all fields.
func (tx *BlobTxMessage) copy() *BlobTxMessage {
cpy := &BlobTxMessage{
Expand Down
11 changes: 5 additions & 6 deletions core/types/hashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
"github.com/protolambda/ztyp/tree"
"github.com/protolambda/ztyp/codec"
"golang.org/x/crypto/sha3"
)

Expand Down Expand Up @@ -59,15 +59,14 @@ func prefixedRlpHash(prefix byte, x interface{}) (h common.Hash) {
return h
}

// prefixedSSZHash writes the prefix into the hasher before SSZ hash-tree-root-ing x.
// It's used for typed transactions.
func prefixedSSZHash(prefix byte, x tree.HTR) (h common.Hash) {
// prefixedSSZHash writes the prefix into the hasher before SSZ encoding x. It's used for
// computing the tx id & signing hashes of signed blob transactions.
func prefixedSSZHash(prefix byte, obj codec.Serializable) (h common.Hash) {
sha := hasherPool.Get().(crypto.KeccakState)
defer hasherPool.Put(sha)
sha.Reset()
sha.Write([]byte{prefix})
htr := x.HashTreeRoot(tree.GetHashFn())
sha.Write(htr[:])
EncodeSSZ(sha, obj)
sha.Read(h[:])
return h
}
Expand Down
2 changes: 1 addition & 1 deletion core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ func (tx *Transaction) Hash() common.Hash {
case BlobTxType:
// TODO(eip-4844): We should remove this ugly switch by making hash()
// a part of the TxData interface
h = prefixedSSZHash(tx.Type(), &tx.inner.(*SignedBlobTx).Message)
h = prefixedSSZHash(tx.Type(), tx.inner.(*SignedBlobTx))
default:
h = prefixedRlpHash(tx.Type(), tx.inner)
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ require (
github.com/gorilla/websocket v1.4.2
github.com/graph-gophers/graphql-go v1.3.0
github.com/hashicorp/go-bexpr v0.1.10
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d
github.com/herumi/bls-eth-go-binary v1.28.1 // indirect
github.com/holiman/big v0.0.0-20221017200358-a027dc42d04e
github.com/holiman/bloomfilter/v2 v2.0.3
Expand Down
7 changes: 0 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,6 @@ github.com/hashicorp/go-bexpr v0.1.10 h1:9kuI5PFotCboP3dkDYFr/wi0gg0QVbSNz5oFRpx
github.com/hashicorp/go-bexpr v0.1.10/go.mod h1:oxlubA2vC/gFVfX1A6JGp7ls7uCDlfJn732ehYYg+g0=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d h1:dg1dEPuWpEqDnvIw251EVy4zlP8gWbsGj4BsUKCRpYs=
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/herumi/bls-eth-go-binary v1.28.1 h1:fcIZ48y5EE9973k05XjE8+P3YiQgjZz4JI/YabAm8KA=
github.com/herumi/bls-eth-go-binary v1.28.1/go.mod h1:luAnRm3OsMQeokhGzpYmc0ZKwawY7o87PUEP11Z7r7U=
github.com/holiman/big v0.0.0-20221017200358-a027dc42d04e h1:pIYdhNkDh+YENVNi3gto8n9hAmRxKxoar0iE6BLucjw=
Expand Down Expand Up @@ -366,10 +364,6 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/tsdb v0.7.1 h1:YZcsG11NqnK4czYLrWd9mpEuAJIHVQLwdrleYfszMAA=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/protolambda/go-kzg v0.0.0-20221121235515-3f3e1ef6beb7 h1:yzqsT6UIT17bmesiG9iB6+3cQkmMAtFjUffx5KrXgkk=
github.com/protolambda/go-kzg v0.0.0-20221121235515-3f3e1ef6beb7/go.mod h1:7EhkBJFo/qJ9sToiW5baPqbyPo/TadVHn4iNdpwEW/w=
github.com/protolambda/go-kzg v0.0.0-20221122014024-bb3fa3695412 h1:MQBDul/k5XDDYF5fVn18lNwkWKxpK93FbYeaum1b1UE=
github.com/protolambda/go-kzg v0.0.0-20221122014024-bb3fa3695412/go.mod h1:7EhkBJFo/qJ9sToiW5baPqbyPo/TadVHn4iNdpwEW/w=
github.com/protolambda/go-kzg v0.0.0-20221129234330-612948a21fb0 h1:DDaoou46n4krrbtDkymmqFAx2iqxatt2Sk+B1ZOM45A=
github.com/protolambda/go-kzg v0.0.0-20221129234330-612948a21fb0/go.mod h1:7EhkBJFo/qJ9sToiW5baPqbyPo/TadVHn4iNdpwEW/w=
github.com/protolambda/ztyp v0.2.1 h1:+rfw75/Zh8EopNlG652TGDXlLgJflj6XWxJ9yCVpJws=
Expand Down Expand Up @@ -556,7 +550,6 @@ golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210420205809-ac73e9fd8988/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20211020174200-9d6173849985/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down

0 comments on commit ed01dbb

Please sign in to comment.