Skip to content
Merged
15 changes: 8 additions & 7 deletions agglayer/agglayer_client_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ func (c *AgglayerClientCache) GetCertificateHeader(
}

// SendCertificate sends a certificate to the Agglayer client. (no cache)
func (c *AgglayerClientCache) SendCertificate(ctx context.Context,
certificate *agglayertypes.Certificate, validatorSignature []byte) (common.Hash, error) {
return c.agglayerClient.SendCertificate(ctx, certificate, validatorSignature)
func (c *AgglayerClientCache) SendCertificate(
ctx context.Context,
certificate *agglayertypes.Certificate) (common.Hash, error) {
return c.agglayerClient.SendCertificate(ctx, certificate)
}

// GetEpochConfiguration retrieves the current epoch configuration from the Agglayer client. (no cache)
Expand All @@ -126,8 +127,8 @@ func (c *AgglayerClientCache) GetLatestPendingCertificateHeader(ctx context.Cont
return c.agglayerClient.GetLatestPendingCertificateHeader(ctx, networkID)
}

// GetNetworkStatus retrieves the network status for a given network ID from the Agglayer client. (no cache)
func (c *AgglayerClientCache) GetNetworkStatus(
ctx context.Context, networkID uint32) (agglayertypes.NetworkStatus, error) {
return c.agglayerClient.GetNetworkStatus(ctx, networkID)
// GetNetworkState retrieves the network state for a given network ID from the Agglayer client. (no cache)
func (c *AgglayerClientCache) GetNetworkState(
ctx context.Context, networkID uint32) (agglayertypes.NetworkState, error) {
return c.agglayerClient.GetNetworkState(ctx, networkID)
}
4 changes: 2 additions & 2 deletions agglayer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ type AggLayerClientCertificateIDQuerier interface {

// AgglayerClientInterface is the interface that defines the methods that the AggLayerClient will implement
type AgglayerClientInterface interface {
SendCertificate(ctx context.Context, certificate *types.Certificate, validatorSignature []byte) (common.Hash, error)
SendCertificate(ctx context.Context, certificate *types.Certificate) (common.Hash, error)
GetCertificateHeader(ctx context.Context, certificateHash common.Hash) (*types.CertificateHeader, error)
GetNetworkStatus(ctx context.Context, networkID uint32) (types.NetworkStatus, error)
GetNetworkState(ctx context.Context, networkID uint32) (types.NetworkState, error)
AggLayerClientGetEpochConfiguration
AggLayerClientRecoveryQuerier
AggLayerClientCertificateIDQuerier
Expand Down
132 changes: 95 additions & 37 deletions agglayer/grpc/agglayer_grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ func (a *AgglayerGRPCClient) GetEpochConfiguration(ctx context.Context) (*types.
// SendCertificate sends a certificate to the AggLayer
// It returns the certificate ID
func (a *AgglayerGRPCClient) SendCertificate(ctx context.Context,
certificate *types.Certificate,
validatorSignature []byte) (common.Hash, error) {
certificate *types.Certificate) (common.Hash, error) {
protoCert, err := ConvertCertToProtoCertificate(certificate)
if err != nil {
return common.Hash{}, err
Expand Down Expand Up @@ -147,23 +146,23 @@ func (a *AgglayerGRPCClient) GetCertificateHeader(
return convertProtoCertificateHeader(response.CertificateHeader), nil
}

func (a *AgglayerGRPCClient) GetNetworkStatus(ctx context.Context, networkID uint32) (types.NetworkStatus, error) {
status, err := a.networkStateService.GetNetworkStatus(ctx, &v1.GetNetworkStatusRequest{
func (a *AgglayerGRPCClient) GetNetworkState(ctx context.Context, networkID uint32) (types.NetworkState, error) {
status, err := a.networkStateService.GetNetworkState(ctx, &v1.GetNetworkStateRequest{
NetworkId: networkID,
})
if err != nil {
return types.NetworkStatus{}, fmt.Errorf("failed to get network status: %w",
return types.NetworkState{}, fmt.Errorf("failed to get network status: %w",
aggkitgrpc.RepackGRPCErrorWithDetails(err))
}

if !status.HasNetworkStatus() {
return types.NetworkStatus{}, errors.New("network status is not available")
if !status.HasNetworkState() {
return types.NetworkState{}, errors.New("network state is not available")
}

return convertProtoNetworkStatus(status.NetworkStatus)
return convertProtoNetworkState(status.NetworkState)
}

func convertProtoNetworkStatus(status *v1nodetypes.NetworkStatus) (types.NetworkStatus, error) {
func convertProtoNetworkState(status *v1nodetypes.NetworkState) (types.NetworkState, error) {
var settledCertID *common.Hash
if status.SettledCertificateId != nil {
certID := common.BytesToHash(status.SettledCertificateId.Value.Value)
Expand All @@ -190,18 +189,14 @@ func convertProtoNetworkStatus(status *v1nodetypes.NetworkStatus) (types.Network
}
}

latestPendingStatus := types.Pending
if status.LatestPendingStatus != "" {
if err := latestPendingStatus.UnmarshalJSON([]byte(status.LatestPendingStatus)); err != nil {
return types.NetworkStatus{},
fmt.Errorf("failed to unmarshal latest pending status %q from NetworkStatus struct: %w",
status.LatestPendingStatus, err)
}
var latestPendingError string
if status.LatestPendingError != nil {
latestPendingError = string(status.LatestPendingError.Message)
}

return types.NetworkStatus{
Status: status.NetworkStatus,
NetworkType: status.NetworkType,
return types.NetworkState{
Status: status.NetworkStatus.String(),
NetworkType: status.NetworkType.String(),
NetworkID: status.NetworkId,
SettledCertificateID: settledCertID,
SettledHeight: status.SettledHeight,
Expand All @@ -210,12 +205,25 @@ func convertProtoNetworkStatus(status *v1nodetypes.NetworkStatus) (types.Network
SettledLETLeafCount: status.SettledLetLeafCount,
SettledImportedBridgeExit: settledClaim,
LatestPendingHeight: status.LatestPendingHeight,
LatestPendingStatus: latestPendingStatus,
LatestPendingError: status.LatestPendingError,
LatestPendingStatus: convertProtoCertStatus(status.LatestPendingStatus),
LatestPendingError: latestPendingError,
LatestEpochWithSettlement: status.LatestEpochWithSettlement,
}, nil
}

func convertProtoCertStatus(status *v1nodetypes.CertificateStatus) *types.CertificateStatus {
if status == nil || status == v1nodetypes.CertificateStatus_CERTIFICATE_STATUS_UNSPECIFIED.Enum() {
return nil
}

// we do not have unspecified type of cert status which is a grpc proto standard to be on value 0,
// so if it is not unspecified, just subtract 1 from the status value to get our types
statusInt := int(*status) - 1
typesStatus := types.CertificateStatus(statusInt)

return &typesStatus
}

// ConvertCertToProtoCertificate converts a types.Certificate to a grpc v1nodetypes.Certificate
func ConvertCertToProtoCertificate(
certificate *types.Certificate,
Expand Down Expand Up @@ -274,22 +282,7 @@ func convertAggchainData(aggchainData types.AggchainData) (*v1types.AggchainData
case *types.AggchainDataProof:
return &v1types.AggchainData{
Data: &v1types.AggchainData_Generic{
Generic: &v1types.AggchainProof{
Proof: &v1types.AggchainProof_Sp1Stark{
Sp1Stark: &v1types.SP1StarkProof{
Version: ad.Version,
Proof: ad.Proof,
Vkey: ad.Vkey,
},
},
AggchainParams: &v1types.FixedBytes32{
Value: ad.AggchainParams.Bytes(),
},
Context: ad.Context,
Signature: &v1types.FixedBytes65{
Value: ad.Signature,
},
},
Generic: convertAggchainDataProofToProto(ad),
},
}, nil
case *types.AggchainDataSignature:
Expand All @@ -300,11 +293,76 @@ func convertAggchainData(aggchainData types.AggchainData) (*v1types.AggchainData
},
},
}, nil
case *types.AggchainDataMultisigWithProof:
return &v1types.AggchainData{
Data: &v1types.AggchainData_MultisigAndAggchainProof{
MultisigAndAggchainProof: &v1types.AggchainProofWithMultisig{
Multisig: convertMultisigToProtoMultisig(ad.Multisig),
AggchainProof: convertAggchainDataProofToProto(ad.AggchainProof),
},
},
}, nil
case *types.AggchainDataMultisig:
return &v1types.AggchainData{
Data: &v1types.AggchainData_Multisig{
Multisig: convertMultisigToProtoMultisig(ad.Multisig),
},
}, nil
default:
return nil, errUnknownAggchainData
}
}

// convertAggchainDataProofToProto converts an aggchain data proof to proto aggchain proof
func convertAggchainDataProofToProto(proof *types.AggchainDataProof) *v1types.AggchainProof {
if proof == nil {
return nil
}

return &v1types.AggchainProof{
Proof: &v1types.AggchainProof_Sp1Stark{
Sp1Stark: &v1types.SP1StarkProof{
Version: proof.Version,
Proof: proof.Proof,
Vkey: proof.Vkey,
},
},
AggchainParams: &v1types.FixedBytes32{
Value: proof.AggchainParams.Bytes(),
},
Context: proof.Context,
Signature: &v1types.FixedBytes65{
Value: proof.Signature,
},
}
}

// convertMultisigToProtoMultisig converts a multisig to a proto multisig
func convertMultisigToProtoMultisig(multisig *types.Multisig) *v1types.Multisig {
if multisig == nil {
return nil
}

protoEntries := make([]*v1types.ECDSAMultisig_ECDSAMultisigEntry, 0, len(multisig.Signatures))

for _, entry := range multisig.Signatures {
protoEntries = append(protoEntries, &v1types.ECDSAMultisig_ECDSAMultisigEntry{
Signature: &v1types.FixedBytes65{
Value: entry.Signature,
},
Index: entry.Index,
})
}

return &v1types.Multisig{
Data: &v1types.Multisig_Ecdsa{
Ecdsa: &v1types.ECDSAMultisig{
Signatures: protoEntries,
},
},
}
}

// convertProtoCertificateHeader converts a proto certificate header to a types certificate header
func convertProtoCertificateHeader(response *v1nodetypes.CertificateHeader) *types.CertificateHeader {
if response == nil {
Expand Down
Loading
Loading