proxy-pool/internal/controlplane/snapshotwire/checksum.go
youfak 6b6fb54075
Some checks are pending
ci / proto (push) Waiting to run
ci / test (ubuntu-latest) (push) Waiting to run
ci / test (windows-latest) (push) Waiting to run
ci / race (push) Waiting to run
ci / integration (push) Waiting to run
feat: publish initial worker snapshots
2026-07-31 13:22:45 +08:00

29 lines
797 B
Go

package snapshotwire
import (
"crypto/sha256"
"errors"
"fmt"
controlplanev1 "proxy-pool/gen/controlplane/v1"
"google.golang.org/protobuf/proto"
)
var ErrNilSnapshot = errors.New("nil worker snapshot")
// Checksum is the canonical wire checksum for a full snapshot. The checksum
// field itself is excluded so both Controller and Gateway can verify it.
func Checksum(full *controlplanev1.WorkerSnapshot) ([sha256.Size]byte, error) {
if full == nil {
return [sha256.Size]byte{}, ErrNilSnapshot
}
copy := proto.Clone(full).(*controlplanev1.WorkerSnapshot)
copy.Checksum = nil
encoded, err := proto.MarshalOptions{Deterministic: true}.Marshal(copy)
if err != nil {
return [sha256.Size]byte{}, fmt.Errorf("marshal worker snapshot: %w", err)
}
return sha256.Sum256(encoded), nil
}