Skip to content

Commit

Permalink
rejecting blocks failure
Browse files Browse the repository at this point in the history
  • Loading branch information
badgersrus committed Sep 19, 2024
1 parent 990e0f4 commit d069a8c
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 40 deletions.
7 changes: 7 additions & 0 deletions go/enclave/components/rollup_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ func verifyBlobHashes(rollupHashes *ethadapter.L1RollupHashes, blobHashes []geth

for i, hash := range rollupHashes.BlobHashes {
if hash != blobHashes[i] {
println("Are they indexed incorrectly?")
for _, h := range rollupHashes.BlobHashes {
for _, b := range blobHashes {
println("Blob hashes: ", b.Hex())
}
println("Rollup Blob hashes: ", h.Hex())
}
return fmt.Errorf("hash mismatch at index %d: rollupHash (%s) != blobHash (%s)", i, hash.Hex(), blobHashes[i].Hex())
}
}
Expand Down
1 change: 1 addition & 0 deletions go/enclave/enclave.go
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,7 @@ func (e *enclaveImpl) EnclavePublicConfig(context.Context) (*common.EnclavePubli
}

func (e *enclaveImpl) rejectBlockErr(ctx context.Context, cause error) *errutil.BlockRejectError {
println("REJECTING L1 BLOCK: ", cause.Error())
var hash common.L1BlockHash
l1Head, err := e.l1BlockProcessor.GetHead(ctx)
// todo - handle error
Expand Down
1 change: 0 additions & 1 deletion go/enclave/storage/enclavedb/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const (
func WriteBatchHeader(ctx context.Context, dbtx *sql.Tx, batch *core.Batch, convertedHash gethcommon.Hash, blockId int64, isCanonical bool) error {
header, err := rlp.EncodeToBytes(batch.Header)

println("ENCLAVE batch written: ", batch.Header.SequencerOrderNo.Uint64())
if err != nil {
return fmt.Errorf("could not encode batch header. Cause: %w", err)
}
Expand Down
5 changes: 2 additions & 3 deletions go/host/enclave/guardian.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,14 +606,13 @@ func (g *Guardian) periodicRollupProduction() {
case <-rollupCheckTicker.C:
if !g.state.IsUpToDate() {
// if we're behind the L1, we don't want to produce rollups
g.logger.Debug("skipping rollup production because L1 is not up to date", "state", g.state)
g.logger.Debug("Skipping rollup production because L1 is not up to date", "state", g.state)
continue
}

fromBatch, err := g.getLatestBatchNo()
println("GUARDIAN framBatch: ", fromBatch)
if err != nil {
g.logger.Error("encountered error while trying to retrieve latest sequence number", log.ErrKey, err)
g.logger.Error("Encountered error while trying to retrieve latest sequence number", log.ErrKey, err)
continue
}

Expand Down
1 change: 1 addition & 0 deletions go/host/l1/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ func (p *Publisher) PublishRollup(producedRollup *common.ExtRollup) {
p.logger.Error("Could not issue rollup tx", log.RollupHashKey, producedRollup.Hash(), log.ErrKey, err)
} else {
p.logger.Info("Rollup included in L1", log.RollupHashKey, producedRollup.Hash())
time.Sleep(500 * time.Millisecond)
}
}

Expand Down
89 changes: 67 additions & 22 deletions integration/ethereummock/beacon_client_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"github.com/ethereum/go-ethereum"

Check failure on line 8 in integration/ethereummock/beacon_client_mock.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/ethereum/go-ethereum/log"
"github.com/ten-protocol/go-ten/go/ethadapter"
Expand All @@ -25,8 +26,12 @@ const kzgBlobSize = 131072
type BeaconMock struct {
log log.Logger

blobs map[uint64][]*kzg4844.Blob
blobsLock sync.Mutex
// map of slots to versioned hashes to match the beacon APIs
slotToVersionedHashes map[uint64][]gethcommon.Hash
// map of versioned hash to blob for efficient lookup
versionedHashToBlob map[gethcommon.Hash]*kzg4844.Blob

mu sync.Mutex

beaconSrv *http.Server
beaconAPIListener net.Listener
Expand All @@ -38,11 +43,12 @@ type BeaconMock struct {

func NewBeaconMock(log log.Logger, genesisTime uint64, secondsPerSlot uint64, port int) *BeaconMock {
return &BeaconMock{
log: log,
genesisTime: genesisTime,
secondsPerSlot: secondsPerSlot,
port: port,
blobs: make(map[uint64][]*kzg4844.Blob),
log: log,
genesisTime: genesisTime,
secondsPerSlot: secondsPerSlot,
port: port,
slotToVersionedHashes: make(map[uint64][]gethcommon.Hash),
versionedHashToBlob: make(map[gethcommon.Hash]*kzg4844.Blob),
}
}

Expand Down Expand Up @@ -177,29 +183,68 @@ func (f *BeaconMock) Start(host string) error {

// StoreBlobs stores the array of blobs against the slot number.
func (f *BeaconMock) StoreBlobs(slot uint64, blobs []*kzg4844.Blob) error {
//for _, blob := range blobs {
// commitment, _ := kzg4844.BlobToCommitment(blob)
// versionedHash := ethadapter.KZGToVersionedHash(commitment)
// println("MockBeacon storing blob hash: ", versionedHash.Hex(), " at slot: ", slot)
//}
f.blobsLock.Lock()
defer f.blobsLock.Unlock()

if len(blobs) > 0 {
f.blobs[slot] = append(f.blobs[slot], blobs...)
f.mu.Lock()
defer f.mu.Unlock()

for _, blob := range blobs {
commitment, err := kzg4844.BlobToCommitment(blob)
if err != nil {
return fmt.Errorf("failed to compute commitment: %w", err)
}

versionedHash := ethadapter.KZGToVersionedHash(commitment)
f.slotToVersionedHashes[slot] = append(f.slotToVersionedHashes[slot], versionedHash)
f.versionedHashToBlob[versionedHash] = blob
}
return nil
}

// LoadBlobs retrieves the array of blobs for a given slot.
func (f *BeaconMock) LoadBlobs(slot uint64) ([]*kzg4844.Blob, error) {
f.blobsLock.Lock()
defer f.blobsLock.Unlock()
// LoadBlobs retrieves blobs for a given slot and an optional list of versioned hashes.
// If versionedHashes is nil or empty, it returns all blobs for the slot.
// Otherwise, it returns only the blobs that match the provided versioned hashes.
func (f *BeaconMock) LoadBlobs(slot uint64, versionedHashes ...gethcommon.Hash) ([]*kzg4844.Blob, error) {
f.mu.Lock()
defer f.mu.Unlock()

blobs, exists := f.blobs[slot]
// Retrieve the list of versioned hashes stored for the slot.
storedHashes, exists := f.slotToVersionedHashes[slot]
if !exists {
return nil, fmt.Errorf("no blobs found for slot %d: %w", slot, ethereum.NotFound)
}

// If no specific versionedHashes are provided, return all blobs for the slot.
if len(versionedHashes) == 0 {
var allBlobs []*kzg4844.Blob
for _, vh := range storedHashes {
blob, exists := f.versionedHashToBlob[vh]
if !exists {
return nil, fmt.Errorf("blob for hash %s not found", vh.Hex())
}
allBlobs = append(allBlobs, blob)
}
return allBlobs, nil
}

// Create a map for quick lookup of stored hashes.
hashSet := make(map[gethcommon.Hash]struct{}, len(storedHashes))
for _, h := range storedHashes {
hashSet[h] = struct{}{}
}

// Retrieve the blobs that match the provided versioned hashes.
var blobs []*kzg4844.Blob
for _, vh := range versionedHashes {
if _, found := hashSet[vh]; found {
blob, exists := f.versionedHashToBlob[vh]
if !exists {
return nil, fmt.Errorf("blob for hash %s not found", vh.Hex())
}
blobs = append(blobs, blob)
} else {
return nil, fmt.Errorf("versioned hash %s not found in slot %d", vh.Hex(), slot)
}
}

return blobs, nil
}

Expand Down
11 changes: 7 additions & 4 deletions integration/ethereummock/mgmt_contract_lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ func (m *mockContractLib) DecodeTx(tx *types.Transaction) ethadapter.L1Transacti
return nil
}

if tx.To().Hex() == rollupTxAddr.Hex() {
return &ethadapter.L1RollupHashes{
BlobHashes: tx.BlobHashes(),
}
}
return decodeTx(tx)
}

Expand All @@ -76,12 +81,12 @@ func (m *mockContractLib) CreateBlobRollup(t *ethadapter.L1RollupTx) (types.TxDa
return nil, fmt.Errorf("failed to make sidecar: %w", err)
}

//hashesTx := ethadapter.L1RollupHashes{BlobHashes: blobHashes}
hashesTx := ethadapter.L1RollupHashes{BlobHashes: blobHashes}

var buf bytes.Buffer
enc := gob.NewEncoder(&buf)

if err := enc.Encode(t); err != nil {
if err := enc.Encode(hashesTx); err != nil {
panic(err)
}

Expand Down Expand Up @@ -147,8 +152,6 @@ func decodeTx(tx *types.Transaction) ethadapter.L1Transaction {
// so this is a way that we can differentiate different contract calls
var t ethadapter.L1Transaction
switch tx.To().Hex() {
case rollupTxAddr.Hex():
t = &ethadapter.L1RollupTx{}
case storeSecretTxAddr.Hex():
t = &ethadapter.L1RespondSecretTx{}
case depositTxAddr.Hex():
Expand Down
19 changes: 10 additions & 9 deletions integration/ethereummock/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,9 @@ func createLegacyTx(txData types.TxData) (types.TxData, error) {

func createBlobTx(txData types.TxData) (types.TxData, error) {
tx := types.NewTx(txData)
//gasTip, _ := uint256.FromBig(tx.GasTipCap())
return &types.BlobTx{
To: *tx.To(),
Data: tx.Data(),
//Gas: tx.Gas(),
//GasTipCap: gasTip,
To: *tx.To(),
Data: tx.Data(),
BlobHashes: tx.BlobHashes(),
Sidecar: tx.BlobTxSidecar(),
}, nil
Expand Down Expand Up @@ -154,11 +151,15 @@ func (m *Node) getRollupFromBlock(block *types.Block) *common.ExtRollup {
continue
}
switch l1tx := decodedTx.(type) {
case *ethadapter.L1RollupTx:
r, err := common.DecodeRollup(l1tx.Rollup)
if err == nil {
return r
case *ethadapter.L1RollupHashes:
slot, err := ethadapter.TimeToSlot(block.Time(), MockGenesisBlock.Time(), SecondsPerSlot)

Check failure on line 155 in integration/ethereummock/node.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)
blobs, _ := m.BeaconServer.LoadBlobs(slot, l1tx.BlobHashes...)
r, err := ethadapter.ReconstructRollup(blobs)
if err != nil {
m.logger.Error("could not recreate rollup from blobs. Cause: %w", err)
return nil
}
return r
}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion integration/simulation/simulation_in_mem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestInMemoryMonteCarloSimulation(t *testing.T) {
StartPort: integration.TestPorts.TestInMemoryMonteCarloSimulationPort,
IsInMem: true,
L1TenData: &params.L1TenData{},
ReceiptTimeout: 5 * time.Second,
ReceiptTimeout: 30 * time.Second,
StoppingDelay: 15 * time.Second,
NodeWithInboundP2PDisabled: 2,
L1BeaconPort: integration.TestPorts.TestInMemoryMonteCarloSimulationPort + integration.DefaultPrysmGatewayPortOffset,
Expand Down

0 comments on commit d069a8c

Please sign in to comment.