Skip to content

Commit

Permalink
stashing
Browse files Browse the repository at this point in the history
  • Loading branch information
badgersrus committed Sep 19, 2024
1 parent 2a226a8 commit 32b7fd3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion integration/ethereummock/beacon_client_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (f *BeaconMock) StoreBlobs(slot uint64, blobs []*kzg4844.Blob) error {
for _, blob := range blobs {
commitment, _ := kzg4844.BlobToCommitment(blob)
versionedHash := ethadapter.KZGToVersionedHash(commitment)
println("Storing Blob wth versioned hash: ", versionedHash.Hex(), " at slot: ", slot)
println("MockBeacon storing blob hash: ", versionedHash.Hex(), " at slot: ", slot)
}
f.blobsLock.Lock()
defer f.blobsLock.Unlock()
Expand Down
15 changes: 14 additions & 1 deletion integration/ethereummock/mock_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/ethereum/go-ethereum/trie"
"github.com/ten-protocol/go-ten/go/ethadapter"
"math/big"
)

Expand Down Expand Up @@ -37,12 +38,24 @@ func NewBlock(parent *types.Block, nodeID common.Address, txs []*types.Transacti
BaseFee: nil,
}
var blobs []*kzg4844.Blob
seenBlobs := make(map[common.Hash]bool)

for _, tx := range txs {
if tx.BlobHashes() != nil {
for i := range tx.BlobTxSidecar().Blobs {
blobPtr := &tx.BlobTxSidecar().Blobs[i]
blobs = append(blobs, blobPtr)
commitment, err := kzg4844.BlobToCommitment(blobPtr)
if err != nil {
// Handle error appropriately
continue
}
versionedHash := ethadapter.KZGToVersionedHash(commitment)

// Check if the blob has already been seen
if !seenBlobs[versionedHash] {
blobs = append(blobs, blobPtr)
seenBlobs[versionedHash] = true
}
}
}
}
Expand Down
28 changes: 18 additions & 10 deletions integration/ethereummock/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (m *Node) Nonce(gethcommon.Address) (uint64, error) {
return 0, nil
}

func (m *Node) getRollupFromBlock(block *types.Block) *common.ExtRollup {
func (m *Node) getRollupFromBlock(block *types.Block) (*common.ExtRollup, error) {
for _, tx := range block.Transactions() {
decodedTx := m.mgmtContractLib.DecodeTx(tx)
if decodedTx == nil {
Expand All @@ -157,16 +157,16 @@ func (m *Node) getRollupFromBlock(block *types.Block) *common.ExtRollup {
switch l1tx := decodedTx.(type) {
case *ethadapter.L1RollupHashes:
println(l1tx)
slot, err := ethadapter.TimeToSlot(block.Time(), MockGenesisBlock.Time(), uint64(12))
slot, err := ethadapter.TimeToSlot(block.Time(), MockGenesisBlock.Time(), SecondsPerSlot)
if err != nil {
m.logger.Error("Failed to calculate slot", "error", err)
return nil
return nil, err
}

blobs, err := m.BeaconServer.LoadBlobs(slot)
if err != nil {
m.logger.Error("Error loading blobs", "error", err)
return nil
return nil, err
}
if len(blobs) > 0 {
println("blobs loaded from cache")
Expand All @@ -175,18 +175,18 @@ func (m *Node) getRollupFromBlock(block *types.Block) *common.ExtRollup {
encodedRlp, err := ethadapter.DecodeBlobs(blobs)
if err != nil {
m.logger.Error("Error decoding blobs", "error", err)
return nil
return nil, err
}

r, err := common.DecodeRollup(encodedRlp)
if err != nil {
m.logger.Error("Could not decode rollup", "error", err)
return nil
return nil, err
}
return r
return r, nil
}
}
return nil
return nil, nil
}

func (m *Node) FetchLastBatchSeqNo(gethcommon.Address) (*big.Int, error) {
Expand All @@ -201,7 +201,10 @@ func (m *Node) FetchLastBatchSeqNo(gethcommon.Address) (*big.Int, error) {
m.logger.Error("Error fetching block by hash", "error", err)
break
}
rollup := m.getRollupFromBlock(currentBlock)
rollup, err := m.getRollupFromBlock(currentBlock)
if err != nil {
return nil, fmt.Errorf("Error getting rollups from blocks: ", err.Error())
}
if rollup != nil {
return big.NewInt(int64(rollup.Header.LastBatchSeqNo)), nil
}
Expand Down Expand Up @@ -500,7 +503,12 @@ func (m *Node) startMining() {
}
// Fetch blobs for the block
slot, _ := ethadapter.TimeToSlot(block.Time(), MockGenesisBlock.Time(), SecondsPerSlot)
if len(blobs) > 0 {
if len(blobs) > 1 {
println("rollup blobs: ", len(blobs))
for _, blob := range blobs {
commitment, _ := kzg4844.BlobToCommitment(blob)
println("blob hash: ", ethadapter.KZGToVersionedHash(commitment).Hex())
}
_ = m.BeaconServer.StoreBlobs(slot, blobs)
}
blockSlot++
Expand Down

0 comments on commit 32b7fd3

Please sign in to comment.