Skip to content

Commit

Permalink
stashing failed rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
badgersrus committed Aug 20, 2024
1 parent 36e4815 commit ae801e1
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 69 deletions.
20 changes: 3 additions & 17 deletions go/enclave/components/rollup_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package components
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ten-protocol/go-ten/go/enclave/core"
"github.com/ten-protocol/go-ten/go/enclave/storage"
"github.com/ten-protocol/go-ten/go/ethadapter"
Expand Down Expand Up @@ -128,13 +126,14 @@ func (rc *rollupConsumerImpl) extractRollups(ctx context.Context, br *common.Blo
continue
}

println("fetching blobs with block: ", br.Block.Hash().Hex(), " and blobHashes: ", rollupHashes.BlobHashes[0].Hash.Hex())
blobs, err := rc.blobResolver.FetchBlobs(ctx, br.Block.Header(), rollupHashes.BlobHashes)
if err != nil {
println("CONSUMER FAILED fetch blobs: ", err.Error())
rc.logger.Crit("could not fetch blobs consumer", log.ErrKey, err)
return nil
}
r, err := reconstructRollup(blobs)
println("CONSUMER SUCCESS fetch blobs")
r, err := ethadapter.ReconstructRollup(blobs)
if err != nil {
rc.logger.Crit("could not recreate rollup from blobs.", log.ErrKey, err)
}
Expand All @@ -144,16 +143,3 @@ func (rc *rollupConsumerImpl) extractRollups(ctx context.Context, br *common.Blo
}
return rollups
}

// Function to reconstruct rollup from blobs
func reconstructRollup(blobs []*kzg4844.Blob) (*common.ExtRollup, error) {
data, err := ethadapter.DecodeBlobs(blobs)
if err != nil {
fmt.Println("Error decoding rollup blob:", err)
}
var rollup common.ExtRollup
if err := rlp.DecodeBytes(data, &rollup); err != nil {
return nil, fmt.Errorf("could not decode rollup. Cause: %w", err)
}
return &rollup, nil
}
213 changes: 171 additions & 42 deletions go/ethadapter/beacon_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,89 @@ func (bc *BeaconHTTPClient) BeaconGenesis(ctx context.Context) (APIGenesisRespon
return genesisResp, nil
}

//func (bc *BeaconHTTPClient) BeaconBlobSideCars(ctx context.Context, slot uint64, hashes []IndexedBlobHash) (APIGetBlobSidecarsResponse, error) {
// reqPath := path.Join(sidecarsMethodPrefix, strconv.FormatUint(slot, 10))
// var reqQuery url.Values
// reqQuery = url.Values{}
// for i := range hashes {
// reqQuery.Add("indices", strconv.FormatUint(hashes[i].Index, 10))
// }
// var resp APIGetBlobSidecarsResponse
//
// err := bc.request(ctx, &resp, reqPath, reqQuery)
//
// if err != nil {
// return APIGetBlobSidecarsResponse{}, err
// }
// return resp, nil
//}

func (bc *BeaconHTTPClient) BeaconBlobSideCars(ctx context.Context, slot uint64, hashes []IndexedBlobHash) (APIGetBlobSidecarsResponse, error) {
return testBeaconBlobSideCars(bc, ctx, slot, hashes)
}

func testBeaconBlobSideCars(bc *BeaconHTTPClient, ctx context.Context, slot uint64, hashes []IndexedBlobHash) (APIGetBlobSidecarsResponse, error) {
reqPath := path.Join(sidecarsMethodPrefix, strconv.FormatUint(slot, 10))
var reqQuery url.Values
//reqQuery = url.Values{}
//for i := range hashes {
// reqQuery.Add("indices", strconv.FormatUint(hashes[i].Index, 10))
//}
var resp APIGetBlobSidecarsResponse

err := bc.request(ctx, &resp, reqPath, reqQuery)

if err != nil {
println("ERROR GETTING SIDECAR with hash: ", hashes[0].Hash.Hex(), " with err: ", err.Error())
return APIGetBlobSidecarsResponse{}, err
}
return resp, nil

response := resp.Data
if len(response) < len(hashes) {
return APIGetBlobSidecarsResponse{}, fmt.Errorf("expected at least %d blobs for slot %d but only got %d", len(hashes), slot, len(response))
}

outputsFound := make([]bool, len(hashes))
filteredResponse := APIGetBlobSidecarsResponse{}

for _, blobItem := range response {
commitment := kzg4844.Commitment(blobItem.KZGCommitment)
proof := kzg4844.Proof(blobItem.KZGProof)
blobPtr := &blobItem.Blob
versionedHash := KZGToVersionedHash(commitment)

// Try to match the versioned hash with one of the provided hashes
var outputIdx int
var found bool
for outputIdx = range hashes {
if hashes[outputIdx].Hash == versionedHash {
if outputsFound[outputIdx] {
// Duplicate, skip this one
break
}
found = true
outputsFound[outputIdx] = true
break
}
}

if !found {
continue
}

// Verify the blob proof
err = kzg4844.VerifyBlobProof(blobPtr, commitment, proof)
if err != nil {
return APIGetBlobSidecarsResponse{}, fmt.Errorf("failed to verify blob proof for blob at slot(%d) with index(%d)", slot, blobItem.Index)
}

// Add the matching item to the filtered response
filteredResponse.Data = append(filteredResponse.Data, blobItem)
}

// Ensure all expected hashes were found
for i, found := range outputsFound {
if !found {
return APIGetBlobSidecarsResponse{}, fmt.Errorf("missing blob %v in slot %v, can't reconstruct rollup payload", hashes[i], slot)
}
}

return filteredResponse, nil
}

type ClientPool[T any] struct {
Expand Down Expand Up @@ -219,7 +286,6 @@ func (cl *L1BeaconClient) fetchSidecars(ctx context.Context, slot uint64, hashes
println("FETCHING BeaconBlobSideCars with hashes: ", hashes[0].Hash.Hex())
resp, err := f.BeaconBlobSideCars(ctx, slot, hashes)
if err != nil {
println("ERROR FETCHING SIDECARS: ", err.Error())
cl.pool.MoveToNext()
errs = append(errs, err)
} else {
Expand All @@ -229,13 +295,70 @@ func (cl *L1BeaconClient) fetchSidecars(ctx context.Context, slot uint64, hashes
return APIGetBlobSidecarsResponse{}, errors.Join(errs...)
}

//// GetBlobSidecars fetches blob sidecars that were confirmed in the specified
//// L1 block with the given indexed hashes.
//// Order of the returned sidecars is guaranteed to be that of the hashes.
//// Blob data is not checked for validity.
//func (cl *L1BeaconClient) GetBlobSidecars(ctx context.Context, b *types.Header, hashes []IndexedBlobHash) ([]*BlobSidecar, error) {
// if len(hashes) == 0 {
// return []*BlobSidecar{}, nil
// }
// slotFn, err := cl.GetTimeToSlotFn(ctx)
// if err != nil {
// return nil, fmt.Errorf("failed to get time to slot function: %w", err)
// }
// slot, err := slotFn(b.Time)
// if err != nil {
// return nil, fmt.Errorf("error in converting ref.Time to slot: %w", err)
// }
//
// resp, err := cl.fetchSidecars(ctx, slot, hashes)
// if err != nil {
// return nil, fmt.Errorf("failed to fetch blob sidecars for slot %v block %v: %w", slot, b, err)
// }
//
// apiSidecar := make([]*APIBlobSidecar, 0, len(hashes))
// // filter and order by versioned hashes
// for _, h := range hashes {
// for _, apisc := range resp.Data {
// commitment := kzg4844.Commitment(apisc.KZGCommitment)
// versionedHash := KZGToVersionedHash(commitment)
//
// if h.Hash == versionedHash {
// apiSidecar = append(apiSidecar, apisc)
// break
// }
// }
// }
//
// if len(hashes) != len(apiSidecar) {
// fmt.Printf("expected %v sidecars but got %v", len(hashes), len(apiSidecar))
// return nil, fmt.Errorf("expected %v sidecars but got %v", len(hashes), len(apiSidecar))
// }
//
// blobSidecars := make([]*BlobSidecar, 0, len(hashes))
// for _, apisc := range apiSidecar {
// blobSidecars = append(blobSidecars, apisc.BlobSidecar())
// }
//
// return blobSidecars, nil
//}

// GetBlobSidecars fetches blob sidecars that were confirmed in the specified
// L1 block with the given indexed hashes.
// Order of the returned sidecars is guaranteed to be that of the hashes.
// Blob data is not checked for validity.
func (cl *L1BeaconClient) GetBlobSidecars(ctx context.Context, b *types.Header, hashes []IndexedBlobHash) ([]*BlobSidecar, error) {
func (cl *L1BeaconClient) GetBlobSidecars(ctx context.Context, b *types.Header, hashes []IndexedBlobHash) ([]*kzg4844.Blob, error) {
return testGetBlobSidecars(cl, ctx, b, hashes)
}

// GetBlobSidecars fetches blob sidecars that were confirmed in the specified
// L1 block with the given indexed hashes.
// Order of the returned sidecars is guaranteed to be that of the hashes.
// Blob data is not checked for validity.
func testGetBlobSidecars(cl *L1BeaconClient, ctx context.Context, b *types.Header, hashes []IndexedBlobHash) ([]*kzg4844.Blob, error) {
if len(hashes) == 0 {
return []*BlobSidecar{}, nil
return []*kzg4844.Blob{}, nil
}
slotFn, err := cl.GetTimeToSlotFn(ctx)
if err != nil {
Expand All @@ -251,40 +374,30 @@ func (cl *L1BeaconClient) GetBlobSidecars(ctx context.Context, b *types.Header,
return nil, fmt.Errorf("failed to fetch blob sidecars for slot %v block %v: %w", slot, b, err)
}

apiscs := make([]*APIBlobSidecar, 0, len(hashes))
// filter and order by hashes
for _, h := range hashes {
for _, apisc := range resp.Data {
if h.Index == uint64(apisc.Index) {
apiscs = append(apiscs, apisc)
break
}
}
}

if len(hashes) != len(apiscs) {
fmt.Printf("expected %v sidecars but got %v", len(hashes), len(apiscs))
return nil, fmt.Errorf("expected %v sidecars but got %v", len(hashes), len(apiscs))
response := resp.Data
if len(hashes) != len(response) {
return nil, fmt.Errorf("expected %v sidecars but got %v", len(hashes), len(response))
}

bscs := make([]*BlobSidecar, 0, len(hashes))
for _, apisc := range apiscs {
bscs = append(bscs, apisc.BlobSidecar())
blobs := make([]*kzg4844.Blob, 0, len(hashes))
for _, sidecars := range response {
blobPtr := sidecars.Blob
blobs = append(blobs, &blobPtr)
}

return bscs, nil
return blobs, nil
}

// FetchBlobs fetches blobs that were confirmed in the specified L1 block with the given indexed
// hashes. The order of the returned blobs will match the order of `hashes`. Confirms each
// blob's validity by checking its proof against the commitment, and confirming the commitment
// hashes to the expected value. Returns error if any blob is found invalid.
func (cl *L1BeaconClient) FetchBlobs(ctx context.Context, b *types.Header, hashes []IndexedBlobHash) ([]*kzg4844.Blob, error) {
blobSidecars, err := cl.GetBlobSidecars(ctx, b, hashes)
blobs, err := cl.GetBlobSidecars(ctx, b, hashes)
if err != nil {
return nil, fmt.Errorf("failed to get blob sidecars for Block Header %s: %w", b.Hash().Hex(), err)
}
return blobsFromSidecars(blobSidecars, hashes)
return blobs, nil
}

func blobsFromSidecars(blobSidecars []*BlobSidecar, hashes []IndexedBlobHash) ([]*kzg4844.Blob, error) {
Expand All @@ -293,29 +406,45 @@ func blobsFromSidecars(blobSidecars []*BlobSidecar, hashes []IndexedBlobHash) ([
}

out := make([]*kzg4844.Blob, len(hashes))
matchedSidecars := make([]bool, len(hashes))

println("Blob sidecase length: ", len(blobSidecars))
for i, ih := range hashes {
sidecar := blobSidecars[i]
if sidx := uint64(sidecar.Index); sidx != ih.Index {
return nil, fmt.Errorf("expected sidecars to be ordered by hashes, but got %d != %d", sidx, ih.Index)
var matchedSidecar *BlobSidecar
var found bool

for _, sidecar := range blobSidecars {
if uint64(sidecar.Index) == ih.Index {
// Ensure the blob's KZG commitment hashes to the expected value
hash := KZGToVersionedHash(kzg4844.Commitment(sidecar.KZGCommitment))
if hash == ih.Hash {
matchedSidecar = sidecar
found = true
break
}
}
}

// make sure the blob's kzg commitment hashes to the expected value
hash := KZGToVersionedHash(kzg4844.Commitment(sidecar.KZGCommitment))
if hash != ih.Hash {
println("UNSUCCESSFUL blob hash to commitment hash comparison expected: ", ih.Hash.Hex(), " but got: ", hash.Hex())
return nil, fmt.Errorf("expected hash %s for blob at index %d but got %s", ih.Hash, ih.Index, hash)
if !found {
return nil, fmt.Errorf("expected sidecar for hash %s at index %d but did not find it", ih.Hash.Hex(), ih.Index)
}

println("SUCCESSFUL blob hash to commitment hash comparison: ", hash.Hex())
println("SUCCESSFUL blob hash to commitment hash comparison: ", ih.Hash.Hex())

// confirm blob data is valid by verifying its proof against the commitment
if err := VerifyBlobProof(&sidecar.Blob, kzg4844.Commitment(sidecar.KZGCommitment), kzg4844.Proof(sidecar.KZGProof)); err != nil {
// Confirm blob data is valid by verifying its proof against the commitment
if err := VerifyBlobProof(&matchedSidecar.Blob, kzg4844.Commitment(matchedSidecar.KZGCommitment), kzg4844.Proof(matchedSidecar.KZGProof)); err != nil {
return nil, fmt.Errorf("blob at index %d failed verification: %w", i, err)
}
out[i] = &sidecar.Blob
out[i] = &matchedSidecar.Blob
matchedSidecars[i] = true
}

// Ensure all sidecars have been matched
for i, matched := range matchedSidecars {
if !matched {
return nil, fmt.Errorf("missing blob sidecar for hash %s at index %d", hashes[i].Hash.Hex(), hashes[i].Index)
}
}

return out, nil
}

Expand Down
9 changes: 0 additions & 9 deletions go/ethadapter/mgmtcontractlib/mgmt_contract_lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ func (c *contractLibImpl) DecodeTx(tx *types.Transaction) ethadapter.L1Transacti
case AddRollupMethod:
if tx.Type() == types.BlobTxType {
blobHashes := ethadapter.ToIndexedBlobHashes(tx.BlobHashes()...)
println("DECODE TX blob hash: ", blobHashes[0].Hash.Hex())

return &ethadapter.L1RollupHashes{
BlobHashes: blobHashes,
}
Expand Down Expand Up @@ -193,13 +191,6 @@ func (c *contractLibImpl) CreateBlobRollup(t *ethadapter.L1RollupTx) (types.TxDa
return nil, fmt.Errorf("failed to make sidecar: %w", err)
}

println("creating rollup blob tx: ", decodedRollup.Hash().Hex())
println("creating rollup blob seq no: ", decodedRollup.Header.LastBatchSeqNo)

for _, blobH := range blobHashes {
println("blob hash: ", blobH.Hex())
}

return &types.BlobTx{
To: *c.addr,
Data: data,
Expand Down
3 changes: 2 additions & 1 deletion go/host/l1/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,13 @@ func (p *Publisher) ExtractObscuroRelevantTransactions(block *types.Block) ([]*e
continue
}

println("PUBLISHER fetching blobs with block: ", block.Hash().Hex(), " and blobHashes: ", rollupHashes.BlobHashes[0].Hash.Hex())
blobs, err := p.blobResolver.FetchBlobs(p.sendingContext, block.Header(), rollupHashes.BlobHashes)
if err != nil {
println("PUBLISHER FAILED fetch blobs: ", err.Error())
p.logger.Crit("could not fetch blobs publisher", log.ErrKey, err)
return nil, nil, nil
}
println("PUBLISHER SUCCESS fetch blobs")
encodedRlp, err := ethadapter.DecodeBlobs(blobs)
if err != nil {
p.logger.Crit("could not decode blobs.", log.ErrKey, err)
Expand Down

0 comments on commit ae801e1

Please sign in to comment.