Skip to content

Commit

Permalink
stashing before rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
badgersrus committed Sep 19, 2024
1 parent 32b7fd3 commit 990e0f4
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 66 deletions.
2 changes: 1 addition & 1 deletion go/enclave/components/rollup_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (rc *rollupConsumerImpl) extractAndVerifyRollups(br *common.BlockAndReceipt

var blobHashes []gethcommon.Hash
var err error
if _, blobHashes, err = ethadapter.MakeSidecar(blobs, " rollupConsumer: "); err != nil {
if _, blobHashes, err = ethadapter.MakeSidecar(blobs); err != nil {
return nil, fmt.Errorf("could not create blob sidecar and blob hashes. Cause: %w", err)
}

Expand Down
2 changes: 2 additions & 0 deletions go/enclave/storage/enclavedb/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ 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
6 changes: 3 additions & 3 deletions go/ethadapter/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const excessBlobBits = 6 // = math.floor(math.log2(BLS_MODULUS)) % 8

// MakeSidecar builds & returns the BlobTxSidecar and corresponding blob hashes from the raw blob
// data.
func MakeSidecar(blobs []*kzg4844.Blob, call string) (*types.BlobTxSidecar, []gethcommon.Hash, error) {
func MakeSidecar(blobs []*kzg4844.Blob) (*types.BlobTxSidecar, []gethcommon.Hash, error) {
sidecar := &types.BlobTxSidecar{}
var blobHashes []gethcommon.Hash
for i, blob := range blobs {
Expand All @@ -33,8 +33,8 @@ func MakeSidecar(blobs []*kzg4844.Blob, call string) (*types.BlobTxSidecar, []ge
return nil, nil, fmt.Errorf("cannot compute KZG proof for fast commitment verification of blob %d in tx candidate: %w", i, err)
}
sidecar.Proofs = append(sidecar.Proofs, proof)
blobHash := KZGToVersionedHash(commitment)
println("created blob hash at ", call, blobHash.Hex())
//blobHash := KZGToVersionedHash(commitment)
//println("created blob hash at ", call, blobHash.Hex())
blobHashes = append(blobHashes, KZGToVersionedHash(commitment))
}
return sidecar, blobHashes, nil
Expand Down
2 changes: 1 addition & 1 deletion go/ethadapter/mgmtcontractlib/mgmt_contract_lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (c *contractLibImpl) CreateBlobRollup(t *ethadapter.L1RollupTx) (types.TxDa
var blobHashes []gethcommon.Hash
var sidecar *types.BlobTxSidecar

if sidecar, blobHashes, err = ethadapter.MakeSidecar(blobs, "MgmtContract"); err != nil {
if sidecar, blobHashes, err = ethadapter.MakeSidecar(blobs); err != nil {
return nil, fmt.Errorf("failed to make sidecar: %w", err)
}

Expand Down
1 change: 1 addition & 0 deletions go/host/enclave/guardian.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ func (g *Guardian) periodicRollupProduction() {
}

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)
continue
Expand Down
2 changes: 2 additions & 0 deletions go/host/l1/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ func (p *Publisher) ExtractTenTransactionsAndBlobs(block *types.Block) ([]*ethad
}

func (p *Publisher) FetchLatestSeqNo() (*big.Int, error) {
//hash := *p.mgmtContractLib.GetContractAddr()
//println("contract address: ", hash.Hex())
return p.ethClient.FetchLastBatchSeqNo(*p.mgmtContractLib.GetContractAddr())
}

Expand Down
1 change: 0 additions & 1 deletion integration/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ func PrefundWallets(ctx context.Context, faucetWallet wallet.Wallet, faucetClien
txHashes[idx] = signedTx.Hash()
}

time.Sleep(15 * time.Second)
// Then we await the receipts in parallel.
wg := sync.WaitGroup{}
for _, txHash := range txHashes {
Expand Down
10 changes: 5 additions & 5 deletions integration/ethereummock/beacon_client_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ 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)
}
//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()

Expand Down
9 changes: 5 additions & 4 deletions integration/ethereummock/mgmt_contract_lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,16 @@ func (m *mockContractLib) CreateBlobRollup(t *ethadapter.L1RollupTx) (types.TxDa

var blobHashes []gethcommon.Hash
var sidecar *types.BlobTxSidecar
if sidecar, blobHashes, err = ethadapter.MakeSidecar(blobs, "MockMgmtContract"); err != nil {
if sidecar, blobHashes, err = ethadapter.MakeSidecar(blobs); err != nil {
return nil, fmt.Errorf("failed to make sidecar: %w", err)
}

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

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

l1rh := ethadapter.L1RollupHashes{BlobHashes: blobHashes}
if err = enc.Encode(l1rh); err != nil {
if err := enc.Encode(t); err != nil {
panic(err)
}

Expand Down Expand Up @@ -147,7 +148,7 @@ func decodeTx(tx *types.Transaction) ethadapter.L1Transaction {
var t ethadapter.L1Transaction
switch tx.To().Hex() {
case rollupTxAddr.Hex():
t = &ethadapter.L1RollupHashes{}
t = &ethadapter.L1RollupTx{}
case storeSecretTxAddr.Hex():
t = &ethadapter.L1RespondSecretTx{}
case depositTxAddr.Hex():
Expand Down
9 changes: 6 additions & 3 deletions integration/ethereummock/mock_l1_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,12 @@ func printBlock(b *types.Block, m *Node) string {
}

switch l1Tx := t.(type) {
case *ethadapter.L1RollupHashes:
blobHashes := l1Tx.BlobHashes
txs = append(txs, fmt.Sprintf("r_%d(nonce=%d)", common.ShortHash(blobHashes[0]), tx.Nonce()))
case *ethadapter.L1RollupTx:
r, err := common.DecodeRollup(l1Tx.Rollup)
if err != nil {
testlog.Logger().Crit("failed to decode rollup")
}
txs = append(txs, fmt.Sprintf("r_%d(nonce=%d)", common.ShortHash(r.Hash()), tx.Nonce()))

case *ethadapter.L1DepositTx:
var to uint64
Expand Down
62 changes: 15 additions & 47 deletions integration/ethereummock/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,46 +147,21 @@ func (m *Node) Nonce(gethcommon.Address) (uint64, error) {
return 0, nil
}

func (m *Node) getRollupFromBlock(block *types.Block) (*common.ExtRollup, error) {
func (m *Node) getRollupFromBlock(block *types.Block) *common.ExtRollup {
for _, tx := range block.Transactions() {
decodedTx := m.mgmtContractLib.DecodeTx(tx)
if decodedTx == nil {
m.logger.Warn("Decoded transaction is nil", "txHash", tx.Hash())
continue
}
switch l1tx := decodedTx.(type) {
case *ethadapter.L1RollupHashes:
println(l1tx)
slot, err := ethadapter.TimeToSlot(block.Time(), MockGenesisBlock.Time(), SecondsPerSlot)
if err != nil {
m.logger.Error("Failed to calculate slot", "error", err)
return nil, err
}

blobs, err := m.BeaconServer.LoadBlobs(slot)
if err != nil {
m.logger.Error("Error loading blobs", "error", err)
return nil, err
}
if len(blobs) > 0 {
println("blobs loaded from cache")
}

encodedRlp, err := ethadapter.DecodeBlobs(blobs)
if err != nil {
m.logger.Error("Error decoding blobs", "error", err)
return nil, err
case *ethadapter.L1RollupTx:
r, err := common.DecodeRollup(l1tx.Rollup)
if err == nil {
return r
}

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

func (m *Node) FetchLastBatchSeqNo(gethcommon.Address) (*big.Int, error) {
Expand All @@ -201,11 +176,9 @@ func (m *Node) FetchLastBatchSeqNo(gethcommon.Address) (*big.Int, error) {
m.logger.Error("Error fetching block by hash", "error", err)
break
}
rollup, err := m.getRollupFromBlock(currentBlock)
if err != nil {
return nil, fmt.Errorf("Error getting rollups from blocks: ", err.Error())
}
rollup := m.getRollupFromBlock(currentBlock)
if rollup != nil {
println("Last batch number from rollup: ", rollup.Header.LastBatchSeqNo)
return big.NewInt(int64(rollup.Header.LastBatchSeqNo)), nil
}
}
Expand Down Expand Up @@ -466,7 +439,6 @@ func (m *Node) startMining() {
mempool := make([]*types.Transaction, 0)
z := int32(0)
interrupt := &z
blockSlot := 1
for {
select {
case <-m.exitMiningCh:
Expand All @@ -484,8 +456,6 @@ func (m *Node) startMining() {
atomic.StoreInt32(interrupt, 1)
c := int32(0)
interrupt = &c
currentBlockSlot := blockSlot
blockSlot++

// Generate a random number, and wait for that number of ms. Equivalent to PoW
// Include all rollups received during this period.
Expand All @@ -496,22 +466,20 @@ func (m *Node) startMining() {
return
}

block, blobs := NewBlock(canonicalBlock, m.l2ID, toInclude, uint64(currentBlockSlot))
block, blobs := NewBlock(canonicalBlock, m.l2ID, toInclude, uint64(time.Now().Unix()))
blobPointers := make([]*kzg4844.Blob, len(blobs))
for i := range blobs {
blobPointers[i] = blobs[i] // Assign the address of each blob
}
// Fetch blobs for the block
slot, _ := ethadapter.TimeToSlot(block.Time(), MockGenesisBlock.Time(), SecondsPerSlot)
if len(blobs) > 1 {
println("rollup blobs: ", len(blobs))
for _, blob := range blobs {
commitment, _ := kzg4844.BlobToCommitment(blob)
println("blob hash: ", ethadapter.KZGToVersionedHash(commitment).Hex())
}
if len(blobs) > 0 {
//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++
m.miningCh <- block
})
}
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: 10 * time.Second,
ReceiptTimeout: 5 * time.Second,
StoppingDelay: 15 * time.Second,
NodeWithInboundP2PDisabled: 2,
L1BeaconPort: integration.TestPorts.TestInMemoryMonteCarloSimulationPort + integration.DefaultPrysmGatewayPortOffset,
Expand Down

0 comments on commit 990e0f4

Please sign in to comment.