Skip to content

Commit

Permalink
pr comments before ethadapter move
Browse files Browse the repository at this point in the history
  • Loading branch information
badgersrus committed Dec 19, 2024
1 parent b809268 commit 5d05937
Show file tree
Hide file tree
Showing 18 changed files with 63 additions and 76 deletions.
1 change: 0 additions & 1 deletion contracts/src/management/ManagementContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ contract ManagementContract is Initializable, OwnableUpgradeable {

// Enclaves can request the Network Secret given an attestation request report
function RequestNetworkSecret(string calldata requestReport) public {
// currently this is a no-op, nodes will monitor for these transactions and respond to them
emit NetworkSecretRequested(msg.sender, requestReport);
}

Expand Down
4 changes: 2 additions & 2 deletions go/common/enclave.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ type EnclaveAdmin interface {
// MakeActive - backup sequencer enclave can become active at the command of the host
MakeActive() SystemError

// SubmitL1Block - Used for the host to submit L1 blocks to the enclave, these may be:
// SubmitL1Block - Used for the host to submit L1 pre-processed blocks to the enclave, these may be:
// a. historic block - if the enclave is behind and in the process of catching up with the L1 state
// b. the latest block published by the L1, to which the enclave should respond with a rollup
// It is the responsibility of the host to gossip the returned rollup
// For good functioning the caller should always submit blocks ordered by height
// submitting a block before receiving ancestors of it, will result in it being ignored
SubmitL1Block(ctx context.Context, blockHeader *types.Header, processed *ProcessedL1Data) (*BlockSubmissionResponse, SystemError)
SubmitL1Block(ctx context.Context, processed *ProcessedL1Data) (*BlockSubmissionResponse, SystemError)

// SubmitBatch submits a batch received from the sequencer for processing.
SubmitBatch(ctx context.Context, batch *ExtBatch) SystemError
Expand Down
2 changes: 1 addition & 1 deletion go/common/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ type P2PHostService interface {

type L1RepoService interface {
Service
L1BlockRepository
L1DataService
}
10 changes: 5 additions & 5 deletions go/common/host/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// service names - these are the keys used to register known services with the host
const (
P2PName = "p2p"
L1BlockRepositoryName = "l1-block-repo"
L1DataServiceName = "l1-data-service"
L1PublisherName = "l1-publisher"
L2BatchRepositoryName = "l2-batch-repo"
EnclaveServiceName = "enclaves"
Expand Down Expand Up @@ -76,17 +76,17 @@ type P2PBatchRequestHandler interface {
HandleBatchRequest(requestID string, fromSeqNo *big.Int)
}

// L1BlockRepository provides an interface for the host to request L1 block data (live-streaming and historical)
type L1BlockRepository interface {
// L1DataService provides an interface for the host to request L1 block data (live-streaming and historical)
type L1DataService interface {
// Subscribe will register a block handler to receive new blocks as they arrive, returns unsubscribe func
Subscribe(handler L1BlockHandler) func()

FetchBlockByHeight(height *big.Int) (*types.Block, error)
// FetchNextBlock returns the next canonical block after a given block hash
// It returns the new block, a bool which is true if the block is the current L1 head and a bool if the block is on a different fork to prevBlock
FetchNextBlock(prevBlock gethcommon.Hash) (*types.Block, bool, error)
// ExtractTenTransactions returns the events and transactions relevant to Ten
ExtractTenTransactions(block *common.L1Block) (*common.ProcessedL1Data, error)
// GetTenRelevantTransactions returns the events and transactions relevant to Ten
GetTenRelevantTransactions(block *common.L1Block) (*common.ProcessedL1Data, error)
}

// L1BlockHandler is an interface for receiving new blocks from the repository as they arrive
Expand Down
12 changes: 6 additions & 6 deletions go/common/l1_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"github.com/ethereum/go-ethereum/crypto/kzg4844"
)

// L1TxType represents different types of L1 transactions
type L1TxType uint8 // Change to uint8 for RLP serialization
// L1TenEventType represents different types of L1 transactions we monitor for
type L1TenEventType uint8 // Change to uint8 for RLP serialization

const (
RollupTx L1TxType = iota
RollupTx L1TenEventType = iota
InitialiseSecretTx
SecretRequestTx
SecretResponseTx
Expand Down Expand Up @@ -49,8 +49,8 @@ func (tx *L1TxData) HasSequencerEnclaveID() bool {
return tx.SequencerEnclaveID != (gethcommon.Address{})
}

func (p *ProcessedL1Data) AddEvent(txType L1TxType, tx *L1TxData) {
eventType := uint8(txType)
func (p *ProcessedL1Data) AddEvent(tenEventType L1TenEventType, tx *L1TxData) {
eventType := uint8(tenEventType)

for i := range p.Events {
if p.Events[i].Type != eventType {
Expand All @@ -76,7 +76,7 @@ func (p *ProcessedL1Data) AddEvent(txType L1TxType, tx *L1TxData) {
})
}

func (p *ProcessedL1Data) GetEvents(txType L1TxType) []*L1TxData {
func (p *ProcessedL1Data) GetEvents(txType L1TenEventType) []*L1TxData {
if p == nil || len(p.Events) == 0 {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions go/enclave/enclave.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,11 @@ func (e *enclaveImpl) StreamL2Updates() (chan common.StreamL2UpdatesResponse, fu
}

// SubmitL1Block is used to update the enclave with an additional L1 block.
func (e *enclaveImpl) SubmitL1Block(ctx context.Context, blockHeader *types.Header, processed *common.ProcessedL1Data) (*common.BlockSubmissionResponse, common.SystemError) {
func (e *enclaveImpl) SubmitL1Block(ctx context.Context, processed *common.ProcessedL1Data) (*common.BlockSubmissionResponse, common.SystemError) {
if systemError := checkStopping(e.stopControl); systemError != nil {
return nil, systemError
}
return e.adminAPI.SubmitL1Block(ctx, blockHeader, processed)
return e.adminAPI.SubmitL1Block(ctx, processed)
}

func (e *enclaveImpl) SubmitBatch(ctx context.Context, extBatch *common.ExtBatch) common.SystemError {
Expand Down
8 changes: 2 additions & 6 deletions go/enclave/enclave_admin_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,13 @@ func (e *enclaveAdminService) MakeActive() common.SystemError {
}

// SubmitL1Block is used to update the enclave with an additional L1 block.
func (e *enclaveAdminService) SubmitL1Block(ctx context.Context, blockHeader *types.Header, processed *common.ProcessedL1Data) (*common.BlockSubmissionResponse, common.SystemError) {
func (e *enclaveAdminService) SubmitL1Block(ctx context.Context, processed *common.ProcessedL1Data) (*common.BlockSubmissionResponse, common.SystemError) {
e.dataInMutex.Lock()
defer e.dataInMutex.Unlock()
blockHeader := processed.BlockHeader

e.logger.Info("SubmitL1Block", log.BlockHeightKey, blockHeader.Number, log.BlockHashKey, blockHeader.Hash())

// Verify the block header matches the one in processedData
if blockHeader.Hash() != processed.BlockHeader.Hash() {
return nil, e.rejectBlockErr(ctx, fmt.Errorf("block header mismatch"))
}

// TODO verify proof provided with block processed.Proof

result, err := e.ingestL1Block(ctx, processed)
Expand Down
8 changes: 1 addition & 7 deletions go/enclave/rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,13 @@ func (s *RPCServer) RPCEncryptionKey(ctx context.Context, _ *generated.RPCEncryp
}

func (s *RPCServer) SubmitL1Block(ctx context.Context, request *generated.SubmitBlockRequest) (*generated.SubmitBlockResponse, error) {
bl, err := s.decodeBlock(request.EncodedBlock)
if err != nil {
s.logger.Error("Error decoding block", log.ErrKey, err)
return nil, err
}

processedData, err := s.decodeProcessedData(request.EncodedProcessedData)
if err != nil {
s.logger.Error("Error decoding receipts", log.ErrKey, err)
return nil, err
}

blockSubmissionResponse, err := s.enclave.SubmitL1Block(ctx, bl, processedData)
blockSubmissionResponse, err := s.enclave.SubmitL1Block(ctx, processedData)
if err != nil {
var rejErr *errutil.BlockRejectError
isReject := errors.As(err, &rejErr)
Expand Down
3 changes: 1 addition & 2 deletions go/host/container/host_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,14 @@ func NewHostContainerFromConfig(cfg *hostconfig.HostConfig, logger gethlog.Logge
}, logger)

mgmtContractLib := mgmtcontractlib.NewMgmtContractLib(&cfg.ManagementContractAddress, logger)
// obscuroRelevantContracts := []gethcommon.Address{cfg.ManagementContractAddress, cfg.MessageBusAddress}
beaconClient := ethadapter.NewBeaconHTTPClient(new(http.Client), cfg.L1BeaconUrl)
beaconFallback := ethadapter.NewBeaconHTTPClient(new(http.Client), cfg.L1BlobArchiveUrl)
blobResolver := l1.NewBlobResolver(ethadapter.NewL1BeaconClient(beaconClient, beaconFallback))
contractAddresses := map[l1.ContractType][]gethcommon.Address{
l1.MgmtContract: {cfg.ManagementContractAddress},
l1.MsgBus: {cfg.MessageBusAddress},
}
l1Repo := l1.NewL1Repository(l1Client, logger, mgmtContractLib, blobResolver, contractAddresses)
l1Repo := l1.NewL1DataService(l1Client, logger, mgmtContractLib, blobResolver, contractAddresses)
// we can add more fallback clients as they become available
return NewHostContainer(cfg, services, aggP2P, l1Client, l1Repo, enclaveClients, mgmtContractLib, ethWallet, rpcServer, logger, metricsService, blobResolver)
}
Expand Down
19 changes: 9 additions & 10 deletions go/host/enclave/guardian.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const (
type guardianServiceLocator interface {
P2P() host.P2P
L1Publisher() host.L1Publisher
L1Repo() host.L1BlockRepository
L1Data() host.L1DataService
L2Repo() host.L2BatchRepository
LogSubs() host.LogSubscriptionManager
Enclaves() host.EnclaveService
Expand Down Expand Up @@ -142,7 +142,7 @@ func (g *Guardian) Start() error {
txUnsub := g.sl.P2P().SubscribeForTx(g)

// note: not keeping the unsubscribe functions because the lifespan of the guardian is the same as the host
l1Unsub := g.sl.L1Repo().Subscribe(g)
l1Unsub := g.sl.L1Data().Subscribe(g)
batchesUnsub := g.sl.L2Repo().SubscribeNewBatches(g)

g.cleanupFuncs = []func(){txUnsub, l1Unsub, batchesUnsub}
Expand Down Expand Up @@ -242,8 +242,7 @@ func (g *Guardian) HandleBatch(batch *common.ExtBatch) {
// todo - @matt - does it make sense to use a timeout context?
err := g.submitL2Batch(context.Background(), batch)
if err != nil {
// FIXME change back to Error
g.logger.Warn("Error submitting batch to enclave", log.ErrKey, err)
g.logger.Error("Error submitting batch to enclave", log.ErrKey, err)
}
}

Expand Down Expand Up @@ -352,12 +351,12 @@ func (g *Guardian) provideSecret() error {

// keep checking L1 blocks until we find a secret response for our request or timeout
err = retry.Do(func() error {
nextBlock, _, err := g.sl.L1Repo().FetchNextBlock(awaitFromBlock)
nextBlock, _, err := g.sl.L1Data().FetchNextBlock(awaitFromBlock)
if err != nil {
return fmt.Errorf("next block after block=%s not found - %w", awaitFromBlock, err)
}

processedData, err := g.sl.L1Repo().ExtractTenTransactions(nextBlock)
processedData, err := g.sl.L1Data().GetTenRelevantTransactions(nextBlock)
if err != nil {
return fmt.Errorf("failed to extract Ten transactions from block=%s", nextBlock.Hash())
}
Expand Down Expand Up @@ -422,7 +421,7 @@ func (g *Guardian) catchupWithL1() error {
enclaveHead = g.l1StartHash
}

l1Block, isLatest, err := g.sl.L1Repo().FetchNextBlock(enclaveHead)
l1Block, isLatest, err := g.sl.L1Data().FetchNextBlock(enclaveHead)
if err != nil {
if errors.Is(err, l1.ErrNoNextBlock) {
if g.state.hostL1Head == gethutil.EmptyHash {
Expand Down Expand Up @@ -472,15 +471,15 @@ func (g *Guardian) submitL1Block(block *common.L1Block, isLatest bool) (bool, er
g.logger.Debug("Unable to submit block, enclave is busy processing data")
return false, nil
}
processedData, err := g.sl.L1Repo().ExtractTenTransactions(block)
processedData, err := g.sl.L1Data().GetTenRelevantTransactions(block)
if err != nil {
g.submitDataLock.Unlock() // lock must be released before returning
return false, fmt.Errorf("could not extract ten transaction for block=%s - %w", block.Hash(), err)
}

rollupTxs, syncContracts := g.getRollupsAndContractAddrTxs(*processedData)

resp, err := g.enclaveClient.SubmitL1Block(context.Background(), block.Header(), processedData)
resp, err := g.enclaveClient.SubmitL1Block(context.Background(), processedData)
g.submitDataLock.Unlock() // lock is only guarding the enclave call, so we can release it now
if err != nil {
if strings.Contains(err.Error(), errutil.ErrBlockAlreadyProcessed.Error()) {
Expand All @@ -489,7 +488,7 @@ func (g *Guardian) submitL1Block(block *common.L1Block, isLatest bool) (bool, er
// note: logging this because we don't expect it to happen often and would like visibility on that.
g.logger.Info("L1 block already processed by enclave, trying the next block", "block", block.Hash())
nextHeight := big.NewInt(0).Add(block.Number(), big.NewInt(1))
nextCanonicalBlock, err := g.sl.L1Repo().FetchBlockByHeight(nextHeight)
nextCanonicalBlock, err := g.sl.L1Data().FetchBlockByHeight(nextHeight)
if err != nil {
return false, fmt.Errorf("failed to fetch next block after forking block=%s: %w", block.Hash(), err)
}
Expand Down
2 changes: 1 addition & 1 deletion go/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func NewHost(config *hostconfig.HostConfig, hostServices *ServicesRegistry, p2p
subsService := events.NewLogEventManager(hostServices, logger)
l2Repo.SubscribeValidatedBatches(batchListener{newHeads: host.newHeads})
hostServices.RegisterService(hostcommon.P2PName, p2p)
hostServices.RegisterService(hostcommon.L1BlockRepositoryName, l1Repo)
hostServices.RegisterService(hostcommon.L1DataServiceName, l1Repo)
maxWaitForL1Receipt := 6 * config.L1BlockTime // wait ~10 blocks to see if tx gets published before retrying
retryIntervalForL1Receipt := config.L1BlockTime // retry ~every block
l1Publisher := l1.NewL1Publisher(
Expand Down
Loading

0 comments on commit 5d05937

Please sign in to comment.