Skip to content

Commit

Permalink
add timestamp to rollup table and allow lookup of rollup metadata by …
Browse files Browse the repository at this point in the history
…hash
  • Loading branch information
badgersrus committed Feb 28, 2024
1 parent 4915690 commit 94af724
Show file tree
Hide file tree
Showing 16 changed files with 624 additions and 623 deletions.
2 changes: 1 addition & 1 deletion go/common/enclave.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ type Enclave interface {
GetBatchBySeqNo(seqNo uint64) (*ExtBatch, SystemError)

// GetRollupData - retrieve the first batch sequence and start time for a given rollup.
GetRollupData(internalRollup []byte) (*PublicRollupMetadata, SystemError)
GetRollupData(hash L2RollupHash) (*PublicRollupMetadata, SystemError)

// CreateBatch - creates a new head batch extending the previous one for the latest known L1 head if the node is
// a sequencer. Will panic otherwise.
Expand Down
1,165 changes: 582 additions & 583 deletions go/common/rpc/generated/enclave.pb.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion go/common/rpc/generated/enclave.proto
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ message GetBatchResponse {
}

message GetRollupDataRequest {
bytes internalRollup = 1;
bytes hash = 1;
}

message GetRollupDataResponse {
Expand Down
26 changes: 4 additions & 22 deletions go/enclave/enclave.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import (
gethcommon "github.com/ethereum/go-ethereum/common"
gethcore "github.com/ethereum/go-ethereum/core"
gethlog "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
gethrpc "github.com/ethereum/go-ethereum/rpc"
)

Expand Down Expand Up @@ -296,15 +295,14 @@ func (e *enclaveImpl) GetBatchBySeqNo(seqNo uint64) (*common.ExtBatch, common.Sy
return b, nil
}

func (e *enclaveImpl) GetRollupData(internalRollup []byte) (*common.PublicRollupMetadata, common.SystemError) {
calldataRollupHeader := new(common.CalldataRollupHeader)
err := e.decryptDecompressAndDeserialise(internalRollup, calldataRollupHeader)
func (e *enclaveImpl) GetRollupData(hash common.L2RollupHash) (*common.PublicRollupMetadata, common.SystemError) {
rollupMetadata, err := e.storage.FetchRollupMetadata(hash)
if err != nil {
return nil, err
}
metadata := &common.PublicRollupMetadata{
FirstBatchSequence: calldataRollupHeader.FirstBatchSequence,
StartTime: calldataRollupHeader.StartTime,
FirstBatchSequence: rollupMetadata.FirstBatchSequence,
StartTime: rollupMetadata.StartTime,
}
return metadata, nil
}
Expand Down Expand Up @@ -976,19 +974,3 @@ func replayBatchesToValidState(storage storage.Storage, registry components.Batc

return nil
}

func (e *enclaveImpl) decryptDecompressAndDeserialise(blob []byte, obj any) error {
plaintextBlob, err := e.dataEncryptionService.Decrypt(blob)
if err != nil {
return err
}
serialisedBlob, err := e.dataCompressionService.Decompress(plaintextBlob)
if err != nil {
return err
}
err = rlp.DecodeBytes(serialisedBlob, obj)
if err != nil {
return err
}
return nil
}
2 changes: 1 addition & 1 deletion go/enclave/rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func (s *RPCServer) GetBatchBySeqNo(_ context.Context, request *generated.GetBat
}

func (s *RPCServer) GetRollupData(_ context.Context, request *generated.GetRollupDataRequest) (*generated.GetRollupDataResponse, error) {
rollupMetadata, err := s.enclave.GetRollupData(request.InternalRollup)
rollupMetadata, err := s.enclave.GetRollupData(gethcommon.BytesToHash(request.Hash))
if err != nil {
s.logger.Error("Error fetching rollup metadata", log.ErrKey, err)
return nil, err
Expand Down
28 changes: 21 additions & 7 deletions go/enclave/storage/enclavedb/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ const (
l1msgValue = "(?,?,?)"
selectL1Msg = "select message from l1_msg "

rollupInsert = "replace into rollup values (?,?,?,?,?)"
rollupSelect = "select hash from rollup where compression_block in "
rollupInsert = "replace into rollup values (?,?,?,?,?,?)"
rollupSelect = "select hash from rollup where compression_block in "
rollupSelectMetadata = "select start_seq, time_stamp from rollup where hash = ? "

updateCanonicalBlock = "update block set is_canonical=? where hash in "

// todo - do we need the is_canonical field?
updateCanonicalBatches = "update batch set is_canonical=? where l1_proof in "
)
Expand Down Expand Up @@ -76,10 +76,6 @@ func updateCanonicalValue(dbtx DBTransaction, isCanonical bool, values []common.
dbtx.ExecuteSQL(updateBatches, args...)
}

func FetchBlockHeader(db *sql.DB, hash common.L1BlockHash) (*types.Header, error) {
return fetchBlockHeader(db, " where hash=?", truncTo16(hash))
}

// todo - remove this. For now creates a "block" but without a body.
func FetchBlock(db *sql.DB, hash common.L1BlockHash) (*types.Block, error) {
return fetchBlock(db, " where hash=?", truncTo16(hash))
Expand Down Expand Up @@ -156,6 +152,7 @@ func WriteRollup(dbtx DBTransaction, rollup *common.RollupHeader, internalHeader
truncTo16(rollup.Hash()),
internalHeader.FirstBatchSequence.Uint64(),
rollup.LastBatchSeqNo,
internalHeader.StartTime,
data,
truncTo16(rollup.CompressionL1Head),
)
Expand Down Expand Up @@ -184,6 +181,23 @@ func FetchReorgedRollup(db *sql.DB, reorgedBlocks []common.L1BlockHash) (*common
return rollup, nil
}

func FetchRollupMetadata(db *sql.DB, hash common.L2RollupHash) (*common.PublicRollupMetadata, error) {
var startSeq int64
var startTime uint64

rollup := new(common.PublicRollupMetadata)
err := db.QueryRow(rollupSelectMetadata, truncTo16(hash)).Scan(&startSeq, &startTime)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, errutil.ErrNotFound
}
return nil, err
}
rollup.FirstBatchSequence = big.NewInt(startSeq)
rollup.StartTime = startTime
return rollup, nil
}

func fetchBlockHeader(db *sql.DB, whereQuery string, args ...any) (*types.Header, error) {
var header string
query := selectBlockHeader + " " + whereQuery
Expand Down
1 change: 1 addition & 0 deletions go/enclave/storage/init/edgelessdb/001_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ create table if not exists obsdb.rollup
hash binary(16),
start_seq int NOT NULL,
end_seq int NOT NULL,
time_stamp int NOT NULL,
header blob NOT NULL,
compression_block binary(16) NOT NULL,
INDEX (compression_block),
Expand Down
1 change: 1 addition & 0 deletions go/enclave/storage/init/sqlite/001_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ create table if not exists rollup
hash binary(16) primary key,
start_seq int NOT NULL,
end_seq int NOT NULL,
time_stamp int NOT NULL,
header blob NOT NULL,
compression_block binary(16) NOT NULL REFERENCES block
);
Expand Down
1 change: 1 addition & 0 deletions go/enclave/storage/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type BatchResolver interface {

// StoreRollup
StoreRollup(rollup *common.ExtRollup, header *common.CalldataRollupHeader) error
FetchRollupMetadata(hash common.L2RollupHash) (*common.PublicRollupMetadata, error)
FetchReorgedRollup(reorgedBlocks []common.L1BlockHash) (*common.L2BatchHash, error)
}

Expand Down
4 changes: 4 additions & 0 deletions go/enclave/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,10 @@ func (s *storageImpl) FetchReorgedRollup(reorgedBlocks []common.L1BlockHash) (*c
return enclavedb.FetchReorgedRollup(s.db.GetSQLDB(), reorgedBlocks)
}

func (s *storageImpl) FetchRollupMetadata(hash common.L2RollupHash) (*common.PublicRollupMetadata, error) {
return enclavedb.FetchRollupMetadata(s.db.GetSQLDB(), hash)
}

func (s *storageImpl) DebugGetLogs(txHash common.TxHash) ([]*tracers.DebugLogs, error) {
defer s.logDuration("DebugGetLogs", measure.NewStopwatch())
return enclavedb.DebugGetLogs(s.db.GetSQLDB(), txHash)
Expand Down
5 changes: 5 additions & 0 deletions go/host/enclave/guardian.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,11 @@ func (g *Guardian) processL1BlockTransactions(block *common.L1Block) {
g.logger.Error("Could not decode rollup.", log.ErrKey, err)
}
err = g.db.AddRollupHeader(r, block)

Check failure on line 473 in go/host/enclave/guardian.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)
// rlp, err := g.enclaveClient.GetRollupData(truncTo16(r.Header.Hash()))
rlp, err := g.enclaveClient.GetRollupData(r.Header.Hash())
println("ROLLUP DATA FirstBatchSequence", rlp.FirstBatchSequence.String())
println("ROLLUP DATA LasttBatchSequence", r.Header.LastBatchSeqNo)
println("ROLLUP DATA timestamp", rlp.StartTime)
if err != nil {
if errors.Is(err, errutil.ErrAlreadyExists) {
g.logger.Info("Rollup already stored", log.RollupHashKey, r.Hash())
Expand Down
5 changes: 2 additions & 3 deletions go/host/rpc/enclaverpc/enclave_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func NewClient(config *config.HostConfig, logger gethlog.Logger) common.Enclave
// connection is ready, break out of the loop
return nil
}, retry.NewBackoffAndRetryForeverStrategy([]time.Duration{500 * time.Millisecond, 1 * time.Second, 5 * time.Second}, 10*time.Second))

if err != nil {
// this should not happen as we retry forever...
logger.Crit("failed to connect to enclave", log.ErrKey, err)
Expand Down Expand Up @@ -495,11 +494,11 @@ func (c *Client) GetBatchBySeqNo(seqNo uint64) (*common.ExtBatch, common.SystemE
return common.DecodeExtBatch(batchMsg.Batch)
}

func (c *Client) GetRollupData(internalRollup []byte) (*common.PublicRollupMetadata, common.SystemError) {
func (c *Client) GetRollupData(hash common.L2RollupHash) (*common.PublicRollupMetadata, common.SystemError) {
timeoutCtx, cancel := context.WithTimeout(context.Background(), c.config.EnclaveRPCTimeout)
defer cancel()

response, err := c.protoClient.GetRollupData(timeoutCtx, &generated.GetRollupDataRequest{InternalRollup: internalRollup})
response, err := c.protoClient.GetRollupData(timeoutCtx, &generated.GetRollupDataRequest{Hash: hash.Bytes()})
if err != nil {
return nil, fmt.Errorf("rpc GetRollupData failed. Cause: %w", err)
}
Expand Down
1 change: 0 additions & 1 deletion integration/smartcontract/debug_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ func waitTxResult(client ethadapter.EthClient, tx *types.Transaction) (*types.Re
var err error
for start := time.Now(); time.Since(start) < _timeout; time.Sleep(time.Second) {
receipt, err = client.TransactionReceipt(tx.Hash())

if err != nil {
if errors.Is(err, ethereum.NotFound) {
continue
Expand Down
1 change: 0 additions & 1 deletion tools/hardhatdeployer/contract_deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ func (cd *contractDeployer) signAndSendTxWithReceipt(wallet wallet.Wallet, deplo
receipt, err = deployer.TransactionReceipt(signedTx.Hash())
return err
}, retry.NewTimeoutStrategy(timeoutWait, retryInterval))

if err != nil {
return nil, fmt.Errorf("failed to deploy contract - %w", err)
}
Expand Down
1 change: 0 additions & 1 deletion tools/walletextension/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,6 @@ func networkHealthRequestHandler(walletExt *walletextension.WalletExtension, use
}

err = userConn.WriteResponse(data)

if err != nil {
walletExt.Logger().Error("error writing success response", log.ErrKey, err)
}
Expand Down
2 changes: 0 additions & 2 deletions tools/walletextension/storage/database/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func NewSqliteDatabase(dbPath string) (*SqliteDatabase, error) {
user_id binary(20) PRIMARY KEY,
private_key binary(32)
);`)

if err != nil {
return nil, err
}
Expand All @@ -54,7 +53,6 @@ func NewSqliteDatabase(dbPath string) (*SqliteDatabase, error) {
signature binary(65),
FOREIGN KEY(user_id) REFERENCES users(user_id) ON DELETE CASCADE
);`)

if err != nil {
return nil, err
}
Expand Down

0 comments on commit 94af724

Please sign in to comment.