resolve merge conflict

This commit is contained in:
Sina Mahmoodi 2024-01-19 17:07:12 +03:30
commit 04556db2b5
10 changed files with 40 additions and 44 deletions

View file

@ -25,6 +25,7 @@ import (
cmath "github.com/ethereum/go-ethereum/common/math" cmath "github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
) )
@ -324,9 +325,8 @@ func (st *StateTransition) preCheck() error {
return ErrMissingBlobHashes return ErrMissingBlobHashes
} }
for i, hash := range msg.BlobHashes { for i, hash := range msg.BlobHashes {
if hash[0] != params.BlobTxHashVersion { if !kzg4844.IsValidVersionedHash(hash[:]) {
return fmt.Errorf("blob %d hash version mismatch (have %d, supported %d)", return fmt.Errorf("blob %d has invalid hash version", i)
i, hash[0], params.BlobTxHashVersion)
} }
} }
} }

View file

@ -51,21 +51,9 @@ var (
emptyBlob = kzg4844.Blob{} emptyBlob = kzg4844.Blob{}
emptyBlobCommit, _ = kzg4844.BlobToCommitment(emptyBlob) emptyBlobCommit, _ = kzg4844.BlobToCommitment(emptyBlob)
emptyBlobProof, _ = kzg4844.ComputeBlobProof(emptyBlob, emptyBlobCommit) emptyBlobProof, _ = kzg4844.ComputeBlobProof(emptyBlob, emptyBlobCommit)
emptyBlobVHash = blobHash(emptyBlobCommit) emptyBlobVHash = kzg4844.CalcBlobHashV1(sha256.New(), &emptyBlobCommit)
) )
func blobHash(commit kzg4844.Commitment) common.Hash {
hasher := sha256.New()
hasher.Write(commit[:])
hash := hasher.Sum(nil)
var vhash common.Hash
vhash[0] = params.BlobTxHashVersion
copy(vhash[1:], hash[1:])
return vhash
}
// Chain configuration with Cancun enabled. // Chain configuration with Cancun enabled.
// //
// TODO(karalabe): replace with params.MainnetChainConfig after Cancun. // TODO(karalabe): replace with params.MainnetChainConfig after Cancun.

View file

@ -143,17 +143,10 @@ func validateBlobSidecar(hashes []common.Hash, sidecar *types.BlobTxSidecar) err
// Blob quantities match up, validate that the provers match with the // Blob quantities match up, validate that the provers match with the
// transaction hash before getting to the cryptography // transaction hash before getting to the cryptography
hasher := sha256.New() hasher := sha256.New()
for i, want := range hashes { for i, vhash := range hashes {
hasher.Write(sidecar.Commitments[i][:]) computed := kzg4844.CalcBlobHashV1(hasher, &sidecar.Commitments[i])
hash := hasher.Sum(nil) if vhash != computed {
hasher.Reset() return fmt.Errorf("blob %d: computed hash %#x mismatches transaction one %#x", i, computed, vhash)
var vhash common.Hash
vhash[0] = params.BlobTxHashVersion
copy(vhash[1:], hash[1:])
if vhash != want {
return fmt.Errorf("blob %d: computed hash %#x mismatches transaction one %#x", i, vhash, want)
} }
} }
// Blob commitments match with the hashes in the transaction, verify the // Blob commitments match with the hashes in the transaction, verify the

View file

@ -61,9 +61,10 @@ type BlobTxSidecar struct {
// BlobHashes computes the blob hashes of the given blobs. // BlobHashes computes the blob hashes of the given blobs.
func (sc *BlobTxSidecar) BlobHashes() []common.Hash { func (sc *BlobTxSidecar) BlobHashes() []common.Hash {
hasher := sha256.New()
h := make([]common.Hash, len(sc.Commitments)) h := make([]common.Hash, len(sc.Commitments))
for i := range sc.Blobs { for i := range sc.Blobs {
h[i] = BlobHash(&sc.Commitments[i]) h[i] = kzg4844.CalcBlobHashV1(hasher, &sc.Commitments[i])
} }
return h return h
} }
@ -241,12 +242,3 @@ func (tx *BlobTx) decode(input []byte) error {
} }
return nil return nil
} }
func BlobHash(commit *kzg4844.Commitment) common.Hash {
hasher := sha256.New()
hasher.Write(commit[:])
var vhash common.Hash
hasher.Sum(vhash[:0])
vhash[0] = params.BlobTxHashVersion
return vhash
}

View file

@ -20,6 +20,7 @@ package kzg4844
import ( import (
"embed" "embed"
"errors" "errors"
"hash"
"reflect" "reflect"
"sync/atomic" "sync/atomic"
@ -147,3 +148,21 @@ func VerifyBlobProof(blob Blob, commitment Commitment, proof Proof) error {
} }
return gokzgVerifyBlobProof(blob, commitment, proof) return gokzgVerifyBlobProof(blob, commitment, proof)
} }
// CalcBlobHashV1 calculates the 'versioned blob hash' of a commitment.
// The given hasher must be a sha256 hash instance, otherwise the result will be invalid!
func CalcBlobHashV1(hasher hash.Hash, commit *Commitment) (vh [32]byte) {
if hasher.Size() != 32 {
panic("wrong hash size")
}
hasher.Reset()
hasher.Write(commit[:])
hasher.Sum(vh[:0])
vh[0] = 0x01 // version
return vh
}
// IsValidVersionedHash checks that h is a structurally-valid versioned blob hash.
func IsValidVersionedHash(h []byte) bool {
return len(h) == 32 && h[0] == 0x01
}

View file

@ -29,6 +29,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/prque" "github.com/ethereum/go-ethereum/common/prque"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
@ -810,7 +811,7 @@ func (q *queue) DeliverBodies(id string, txLists [][]*types.Transaction, txListH
return errInvalidBody return errInvalidBody
} }
for _, hash := range tx.BlobHashes() { for _, hash := range tx.BlobHashes() {
if hash[0] != params.BlobTxHashVersion { if !kzg4844.IsValidVersionedHash(hash[:]) {
return errInvalidBody return errInvalidBody
} }
} }

View file

@ -99,6 +99,7 @@ func BenchmarkFilters(b *testing.B) {
filter := sys.NewRangeFilter(0, -1, []common.Address{addr1, addr2, addr3, addr4}, nil) filter := sys.NewRangeFilter(0, -1, []common.Address{addr1, addr2, addr3, addr4}, nil)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
filter.begin = 0
logs, _ := filter.Logs(context.Background()) logs, _ := filter.Logs(context.Background())
if len(logs) != 4 { if len(logs) != 4 {
b.Fatal("expected 4 logs, got", len(logs)) b.Fatal("expected 4 logs, got", len(logs))

View file

@ -20,6 +20,7 @@ import (
"bytes" "bytes"
"context" "context"
"crypto/ecdsa" "crypto/ecdsa"
"crypto/sha256"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -1099,7 +1100,7 @@ func TestFillBlobTransaction(t *testing.T) {
emptyBlob = kzg4844.Blob{} emptyBlob = kzg4844.Blob{}
emptyBlobCommit, _ = kzg4844.BlobToCommitment(emptyBlob) emptyBlobCommit, _ = kzg4844.BlobToCommitment(emptyBlob)
emptyBlobProof, _ = kzg4844.ComputeBlobProof(emptyBlob, emptyBlobCommit) emptyBlobProof, _ = kzg4844.ComputeBlobProof(emptyBlob, emptyBlobCommit)
emptyBlobHash = types.BlobHash(&emptyBlobCommit) emptyBlobHash common.Hash = kzg4844.CalcBlobHashV1(sha256.New(), &emptyBlobCommit)
) )
b := newTestBackend(t, 1, genesis, beacon.New(ethash.NewFaker()), func(i int, b *core.BlockGen) { b := newTestBackend(t, 1, genesis, beacon.New(ethash.NewFaker()), func(i int, b *core.BlockGen) {
b.SetPoS() b.SetPoS()

View file

@ -19,6 +19,7 @@ package ethapi
import ( import (
"bytes" "bytes"
"context" "context"
"crypto/sha256"
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
@ -103,8 +104,9 @@ func (args *BlobTransactionArgs) setDefaults(ctx context.Context, b Backend) err
} }
} }
hashes := make([]common.Hash, n) hashes := make([]common.Hash, n)
hasher := sha256.New()
for i, c := range args.Commitments { for i, c := range args.Commitments {
hashes[i] = types.BlobHash(&c) hashes[i] = kzg4844.CalcBlobHashV1(hasher, &c)
} }
if args.BlobHashes != nil { if args.BlobHashes != nil {
for i, h := range hashes { for i, h := range hashes {

View file

@ -166,7 +166,6 @@ const (
BlobTxBytesPerFieldElement = 32 // Size in bytes of a field element BlobTxBytesPerFieldElement = 32 // Size in bytes of a field element
BlobTxFieldElementsPerBlob = 4096 // Number of field elements stored in a single data blob BlobTxFieldElementsPerBlob = 4096 // Number of field elements stored in a single data blob
BlobTxHashVersion = 0x01 // Version byte of the commitment hash
BlobTxBlobGasPerBlob = 1 << 17 // Gas consumption of a single data blob (== blob byte size) BlobTxBlobGasPerBlob = 1 << 17 // Gas consumption of a single data blob (== blob byte size)
BlobTxMinBlobGasprice = 1 // Minimum gas price for data blobs BlobTxMinBlobGasprice = 1 // Minimum gas price for data blobs
BlobTxBlobGaspriceUpdateFraction = 3338477 // Controls the maximum rate of change for blob gas price BlobTxBlobGaspriceUpdateFraction = 3338477 // Controls the maximum rate of change for blob gas price