-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregion.go
More file actions
129 lines (110 loc) · 3.58 KB
/
Copy pathregion.go
File metadata and controls
129 lines (110 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// vm/region.go
package vm
import (
"time"
"github.com/rhombus-tech/vm/core"
"github.com/rhombus-tech/vm/interfaces"
)
// Region represents a TEE execution region
type Region struct {
ID string `json:"id"`
TEEPairs []interfaces.TEEPairConfig `json:"tee_pairs"` // Updated to use TEEPairConfig
Status RegionStatus `json:"status"` // Use enum type
CreatedAt time.Time `json:"created_at"`
LastUpdated time.Time `json:"last_updated"`
Attestations [2]core.TEEAttestation `json:"attestations"`
MaxObjects int `json:"max_objects"`
MaxEvents int `json:"max_events"`
}
// Region status type
type RegionStatus string
const (
RegionStatusActive RegionStatus = "active"
RegionStatusInactive RegionStatus = "inactive"
RegionStatusError RegionStatus = "error"
)
// Validate checks if the region configuration is valid
func (r *Region) Validate() error {
if r.ID == "" {
return ErrInvalidRegionID
}
if len(r.TEEPairs) == 0 {
return ErrInvalidTEE
}
if r.Status == "" {
return ErrInvalidRegion
}
if r.CreatedAt.IsZero() {
return ErrInvalidRegion
}
return nil
}
// IsActive checks if the region is in active status
func (r *Region) IsActive() bool {
return r.Status == RegionStatusActive
}
// CanExecute checks if the region can execute new tasks
func (r *Region) CanExecute() bool {
return r.IsActive() && len(r.TEEPairs) >= 1
}
// GetTEEPair returns the primary TEE pair for execution
func (r *Region) GetTEEPair() (*interfaces.TEEPairConfig, error) {
if len(r.TEEPairs) == 0 {
return nil, ErrInvalidTEE
}
return &r.TEEPairs[0], nil
}
// UpdateStatus updates the region status and last updated timestamp
func (r *Region) UpdateStatus(status RegionStatus) { // Updated to use RegionStatus type
r.Status = status
r.LastUpdated = time.Now().UTC()
}
// AddTEEPair adds a new TEE pair to the region if not already present
func (r *Region) AddTEEPair(pair interfaces.TEEPairConfig) {
// Check if pair already exists
for _, existing := range r.TEEPairs {
if existing.SGXEndpoint == pair.SGXEndpoint &&
existing.SEVEndpoint == pair.SEVEndpoint {
return
}
}
r.TEEPairs = append(r.TEEPairs, pair)
}
// RemoveTEEPair removes a TEE pair from the region
func (r *Region) RemoveTEEPair(pair interfaces.TEEPairConfig) {
newPairs := make([]interfaces.TEEPairConfig, 0, len(r.TEEPairs))
for _, existing := range r.TEEPairs {
if existing.SGXEndpoint != pair.SGXEndpoint ||
existing.SEVEndpoint != pair.SEVEndpoint {
newPairs = append(newPairs, existing)
}
}
r.TEEPairs = newPairs
}
// Add helper methods for working with TEE pairs
func (r *Region) GetTEEPairByID(id string) (*interfaces.TEEPairConfig, error) {
for _, pair := range r.TEEPairs {
if pair.ID == id {
return &pair, nil
}
}
return nil, ErrInvalidTEE
}
func (r *Region) HasTEEPair(id string) bool {
for _, pair := range r.TEEPairs {
if pair.ID == id {
return true
}
}
return false
}
func (r *Region) UpdateTEEPair(pair interfaces.TEEPairConfig) error {
for i, existing := range r.TEEPairs {
if existing.ID == pair.ID {
r.TEEPairs[i] = pair
r.LastUpdated = time.Now().UTC()
return nil
}
}
return ErrInvalidTEE
}