From 6bab1e1b6b05eba48ee7dd8f2e705a11e4a4a021 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 17 Dec 2024 15:57:29 -0800 Subject: [PATCH] submissions: centralize hashing and formatting --- pkg/model/script.go | 15 ++++++++++++++- pkg/service/script_policy.go | 7 +++---- pkg/service/scripts.go | 8 +++----- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/pkg/model/script.go b/pkg/model/script.go index 87e9ac0..f35f839 100644 --- a/pkg/model/script.go +++ b/pkg/model/script.go @@ -1,6 +1,19 @@ package model -import "time" +import ( + "fmt" + "time" + + "github.com/dchest/siphash" +) + +func HashSource(source string) uint64{ + return siphash.Hash(0, 0, []byte(source)) +} + +func HashFormat(hash uint64) string{ + return fmt.Sprintf("%016x", hash) +} type Script struct { ID int64 `gorm:"primaryKey"` diff --git a/pkg/service/script_policy.go b/pkg/service/script_policy.go index 8c33d64..5bd08cd 100644 --- a/pkg/service/script_policy.go +++ b/pkg/service/script_policy.go @@ -2,7 +2,6 @@ package service import ( "context" - "fmt" "strconv" "git.itzana.me/strafesnet/maps-service/pkg/api" @@ -74,7 +73,7 @@ func (svc *Service) ListScriptPolicy(ctx context.Context, request *api.ListScrip for i := 0; i < len(items); i++ { resp = append(resp, api.ScriptPolicy{ ID: items[i].ID, - FromScriptHash: fmt.Sprintf("%x", items[i].FromScriptHash), + FromScriptHash: model.HashFormat(items[i].FromScriptHash), ToScriptID: items[i].ToScriptID, Policy: int32(items[i].Policy), }) @@ -121,7 +120,7 @@ func (svc *Service) GetScriptPolicy(ctx context.Context, params api.GetScriptPol return &api.ScriptPolicy{ ID: policy.ID, - FromScriptHash: fmt.Sprintf("%x", policy.FromScriptHash), + FromScriptHash: model.HashFormat(policy.FromScriptHash), ToScriptID: policy.ToScriptID, Policy: int32(policy.Policy), }, nil @@ -153,7 +152,7 @@ func (svc *Service) GetScriptPolicyFromHash(ctx context.Context, params api.GetS return &api.ScriptPolicy{ ID: policy.ID, - FromScriptHash: fmt.Sprintf("%x", policy.FromScriptHash), + FromScriptHash: model.HashFormat(policy.FromScriptHash), ToScriptID: policy.ToScriptID, Policy: int32(policy.Policy), }, nil diff --git a/pkg/service/scripts.go b/pkg/service/scripts.go index bf46dd7..dac9e62 100644 --- a/pkg/service/scripts.go +++ b/pkg/service/scripts.go @@ -2,12 +2,10 @@ package service import ( "context" - "fmt" "git.itzana.me/strafesnet/maps-service/pkg/api" "git.itzana.me/strafesnet/maps-service/pkg/datastore" "git.itzana.me/strafesnet/maps-service/pkg/model" - "github.com/dchest/siphash" ) // CreateScript implements createScript operation. @@ -28,7 +26,7 @@ func (svc *Service) CreateScript(ctx context.Context, req *api.ScriptCreate) (*a script, err := svc.DB.Scripts().Create(ctx, model.Script{ ID: 0, Name: req.Name, - Hash: siphash.Hash(0, 0, []byte(req.Source)), + Hash: model.HashSource(req.Source), Source: req.Source, SubmissionID: req.SubmissionID.Or(0), }) @@ -80,7 +78,7 @@ func (svc *Service) GetScript(ctx context.Context, params api.GetScriptParams) ( return &api.Script{ ID: script.ID, Name: script.Name, - Hash: fmt.Sprintf("%x", script.Hash), + Hash: model.HashFormat(script.Hash), Source: script.Source, SubmissionID: script.SubmissionID, }, nil @@ -107,7 +105,7 @@ func (svc *Service) UpdateScript(ctx context.Context, req *api.ScriptUpdate, par } if source, ok := req.Source.Get(); ok { pmap.Add("source", source) - pmap.Add("hash", siphash.Hash(0, 0, []byte(source))) + pmap.Add("hash", model.HashSource(source)) } if SubmissionID, ok := req.SubmissionID.Get(); ok { pmap.Add("submission_id", SubmissionID)