|
| 1 | +package self_healing |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/hex" |
| 6 | + "fmt" |
| 7 | + "sort" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + audittypes "github.com/LumeraProtocol/lumera/x/audit/v1/types" |
| 13 | + "github.com/LumeraProtocol/supernode/v2/p2p" |
| 14 | + "github.com/LumeraProtocol/supernode/v2/pkg/logtrace" |
| 15 | + "github.com/LumeraProtocol/supernode/v2/pkg/lumera" |
| 16 | + "github.com/LumeraProtocol/supernode/v2/pkg/storagechallenge/deterministic" |
| 17 | + "lukechampine.com/blake3" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + defaultPollInterval = 10 * time.Second |
| 22 | + filesPerChallenger = uint32(2) |
| 23 | + recipientReplicaCount = uint32(5) |
| 24 | +) |
| 25 | + |
| 26 | +type Config struct { |
| 27 | + Enabled bool |
| 28 | + PollInterval time.Duration |
| 29 | +} |
| 30 | + |
| 31 | +type Service struct { |
| 32 | + cfg Config |
| 33 | + identity string |
| 34 | + lumera lumera.Client |
| 35 | + p2p p2p.Client |
| 36 | +} |
| 37 | + |
| 38 | +func NewService(identity string, lumeraClient lumera.Client, p2pClient p2p.Client, cfg Config) (*Service, error) { |
| 39 | + identity = strings.TrimSpace(identity) |
| 40 | + if identity == "" { |
| 41 | + return nil, fmt.Errorf("identity is empty") |
| 42 | + } |
| 43 | + if lumeraClient == nil || lumeraClient.Node() == nil || lumeraClient.Audit() == nil { |
| 44 | + return nil, fmt.Errorf("lumera client is missing required modules") |
| 45 | + } |
| 46 | + if p2pClient == nil { |
| 47 | + return nil, fmt.Errorf("p2p client is nil") |
| 48 | + } |
| 49 | + if cfg.PollInterval <= 0 { |
| 50 | + cfg.PollInterval = defaultPollInterval |
| 51 | + } |
| 52 | + |
| 53 | + return &Service{cfg: cfg, identity: identity, lumera: lumeraClient, p2p: p2pClient}, nil |
| 54 | +} |
| 55 | + |
| 56 | +func (s *Service) Run(ctx context.Context) error { |
| 57 | + if !s.cfg.Enabled { |
| 58 | + <-ctx.Done() |
| 59 | + return nil |
| 60 | + } |
| 61 | + |
| 62 | + ticker := time.NewTicker(s.cfg.PollInterval) |
| 63 | + defer ticker.Stop() |
| 64 | + |
| 65 | + var lastRunEpoch uint64 |
| 66 | + var lastRunOK bool |
| 67 | + |
| 68 | + for { |
| 69 | + select { |
| 70 | + case <-ctx.Done(): |
| 71 | + return ctx.Err() |
| 72 | + case <-ticker.C: |
| 73 | + height, ok := s.latestHeight(ctx) |
| 74 | + if !ok { |
| 75 | + continue |
| 76 | + } |
| 77 | + params, ok := s.auditParams(ctx) |
| 78 | + if !ok { |
| 79 | + continue |
| 80 | + } |
| 81 | + epochID, ok := deterministic.EpochID(height, params.EpochZeroHeight, params.EpochLengthBlocks) |
| 82 | + if !ok { |
| 83 | + continue |
| 84 | + } |
| 85 | + if lastRunOK && lastRunEpoch == epochID { |
| 86 | + continue |
| 87 | + } |
| 88 | + |
| 89 | + anchorResp, err := s.lumera.Audit().GetEpochAnchor(ctx, epochID) |
| 90 | + if err != nil || anchorResp == nil || anchorResp.Anchor.EpochId != epochID { |
| 91 | + continue |
| 92 | + } |
| 93 | + |
| 94 | + anchor := anchorResp.Anchor |
| 95 | + challengers := deterministic.SelectChallengers(anchor.ActiveSupernodeAccounts, anchor.Seed, epochID, params.ScChallengersPerEpoch) |
| 96 | + if !contains(challengers, s.identity) { |
| 97 | + lastRunEpoch = epochID |
| 98 | + lastRunOK = true |
| 99 | + continue |
| 100 | + } |
| 101 | + |
| 102 | + if err := s.runEpoch(ctx, anchor); err != nil { |
| 103 | + logtrace.Warn(ctx, "self-healing epoch run error", logtrace.Fields{"epoch_id": epochID, "error": err.Error()}) |
| 104 | + lastRunEpoch = epochID |
| 105 | + lastRunOK = false |
| 106 | + continue |
| 107 | + } |
| 108 | + lastRunEpoch = epochID |
| 109 | + lastRunOK = true |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func (s *Service) latestHeight(ctx context.Context) (int64, bool) { |
| 115 | + resp, err := s.lumera.Node().GetLatestBlock(ctx) |
| 116 | + if err != nil || resp == nil { |
| 117 | + return 0, false |
| 118 | + } |
| 119 | + if sdkBlk := resp.GetSdkBlock(); sdkBlk != nil { |
| 120 | + return sdkBlk.Header.Height, true |
| 121 | + } |
| 122 | + if blk := resp.GetBlock(); blk != nil { |
| 123 | + return blk.Header.Height, true |
| 124 | + } |
| 125 | + return 0, false |
| 126 | +} |
| 127 | + |
| 128 | +func (s *Service) auditParams(ctx context.Context) (audittypes.Params, bool) { |
| 129 | + resp, err := s.lumera.Audit().GetParams(ctx) |
| 130 | + if err != nil || resp == nil { |
| 131 | + return audittypes.Params{}, false |
| 132 | + } |
| 133 | + p := resp.Params.WithDefaults() |
| 134 | + if err := p.Validate(); err != nil { |
| 135 | + return audittypes.Params{}, false |
| 136 | + } |
| 137 | + return p, true |
| 138 | +} |
| 139 | + |
| 140 | +func (s *Service) runEpoch(ctx context.Context, anchor audittypes.EpochAnchor) error { |
| 141 | + to := time.Now().UTC() |
| 142 | + from := to.Add(-24 * time.Hour) |
| 143 | + |
| 144 | + keys, err := s.p2p.GetLocalKeys(ctx, &from, to) |
| 145 | + if err != nil { |
| 146 | + return fmt.Errorf("get local keys: %w", err) |
| 147 | + } |
| 148 | + if len(keys) == 0 { |
| 149 | + return nil |
| 150 | + } |
| 151 | + sort.Strings(keys) |
| 152 | + |
| 153 | + fileKeys := deterministic.SelectFileKeys(keys, anchor.Seed, anchor.EpochId, s.identity, filesPerChallenger) |
| 154 | + for _, fileKey := range fileKeys { |
| 155 | + replicas, err := deterministic.SelectReplicaSet(anchor.TargetSupernodeAccounts, fileKey, recipientReplicaCount) |
| 156 | + if err != nil { |
| 157 | + continue |
| 158 | + } |
| 159 | + recipient := selectRecipient(replicas, s.identity) |
| 160 | + if recipient == "" { |
| 161 | + continue |
| 162 | + } |
| 163 | + challengeID := deriveChallengeID(anchor.Seed, anchor.EpochId, fileKey, s.identity, recipient) |
| 164 | + logtrace.Info(ctx, "self-healing challenge planned", logtrace.Fields{ |
| 165 | + "epoch_id": anchor.EpochId, |
| 166 | + "challenge_id": challengeID, |
| 167 | + "file_key": fileKey, |
| 168 | + "challenger_id": s.identity, |
| 169 | + "recipient_id": recipient, |
| 170 | + }) |
| 171 | + } |
| 172 | + return nil |
| 173 | +} |
| 174 | + |
| 175 | +func deriveChallengeID(seed []byte, epochID uint64, fileKey, challenger, recipient string) string { |
| 176 | + msg := []byte("sh:challenge:" + hex.EncodeToString(seed) + ":" + strconv.FormatUint(epochID, 10) + ":" + fileKey + ":" + challenger + ":" + recipient) |
| 177 | + sum := blake3.Sum256(msg) |
| 178 | + return hex.EncodeToString(sum[:]) |
| 179 | +} |
| 180 | + |
| 181 | +func selectRecipient(replicas []string, self string) string { |
| 182 | + for _, id := range replicas { |
| 183 | + if id != self { |
| 184 | + return id |
| 185 | + } |
| 186 | + } |
| 187 | + return "" |
| 188 | +} |
| 189 | + |
| 190 | +func contains(items []string, target string) bool { |
| 191 | + for _, it := range items { |
| 192 | + if it == target { |
| 193 | + return true |
| 194 | + } |
| 195 | + } |
| 196 | + return false |
| 197 | +} |
0 commit comments