@@ -6,26 +6,41 @@ import (
66 "bytes"
77 "context"
88 "encoding/json"
9+ "strconv"
910 "sync"
11+ "time"
1012
1113 "github.com/obolnetwork/charon/app/errors"
1214 "github.com/obolnetwork/charon/app/log"
1315 "github.com/obolnetwork/charon/app/z"
1416 "github.com/obolnetwork/charon/core"
1517)
1618
19+ // NewMemDBMetadata returns a new in-memory partial signature database instance.
20+ func NewMemDBMetadata (slotDuration uint64 , genesisTime time.Time ) MemDBMetadata {
21+ return MemDBMetadata {
22+ slotDuration : slotDuration ,
23+ genesisTime : genesisTime ,
24+ }
25+ }
26+
27+ type MemDBMetadata struct {
28+ slotDuration uint64
29+ genesisTime time.Time
30+ }
31+
1732// NewMemDB returns a new in-memory partial signature database instance.
18- func NewMemDB (threshold int , deadliner core.Deadliner ) * MemDB {
33+ func NewMemDB (ctx context. Context , threshold int , deadliner core.Deadliner , metadata MemDBMetadata ) * MemDB {
1934 return & MemDB {
2035 entries : make (map [key ][]core.ParSignedData ),
2136 keysByDuty : make (map [core.Duty ][]key ),
2237 threshold : threshold ,
2338 deadliner : deadliner ,
39+ metadata : metadata ,
2440 }
2541}
2642
27- // MemDB is a placeholder in-memory partial signature database.
28- // It will be replaced with a BadgerDB implementation.
43+ // MemDB is an in-memory partial signature database.
2944type MemDB struct {
3045 mu sync.Mutex
3146 internalSubs []func (context.Context , core.Duty , core.ParSignedDataSet ) error
@@ -35,6 +50,8 @@ type MemDB struct {
3550 keysByDuty map [core.Duty ][]key
3651 threshold int
3752 deadliner core.Deadliner
53+
54+ metadata MemDBMetadata
3855}
3956
4057// SubscribeInternal registers a callback when an internal
@@ -146,6 +163,18 @@ func (db *MemDB) store(k key, value core.ParSignedData) ([]core.ParSignedData, b
146163 db .mu .Lock ()
147164 defer db .mu .Unlock ()
148165
166+ now := time .Now ().UnixMilli ()
167+
168+ slotStart := (uint64 (db .metadata .genesisTime .Unix ()) + k .Duty .Slot * db .metadata .slotDuration ) * 1000 // in ms
169+ timeSinceSlotStart := float64 (now - int64 (slotStart )) / 1000 // in seconds
170+ switch k .Duty .Type {
171+ case core .DutyAttester :
172+ timeSinceSlotStart -= 4.0
173+ case core .DutyAggregator , core .DutySyncContribution :
174+ timeSinceSlotStart -= 8.0
175+ }
176+ parsigStored .WithLabelValues (k .Duty .Type .String (), strconv .FormatInt (int64 (value .ShareIdx ), 10 )).Observe (timeSinceSlotStart )
177+
149178 for _ , s := range db .entries [k ] {
150179 if s .ShareIdx == value .ShareIdx {
151180 equal , err := parSignedDataEqual (s , value )
0 commit comments