Moved Hash function to single implementation

gql_cataclysm v0.2.3
noah metz 2023-07-27 23:26:58 -06:00
parent f314b46415
commit d40e561728
4 changed files with 26 additions and 23 deletions

@ -9,18 +9,25 @@ import (
"encoding/binary" "encoding/binary"
) )
type NodeType string type Type interface {
func (node NodeType) Hash() uint64 { String() string
hash := sha512.Sum512([]byte(fmt.Sprintf("NODE: %s", string(node)))) Prefix() string
return binary.BigEndian.Uint64(hash[(len(hash)-9):(len(hash)-1)])
} }
type ExtType string func Hash(t Type) uint64 {
func (ext ExtType) Hash() uint64 { hash := sha512.Sum512([]byte(fmt.Sprintf("%s%s", t.Prefix(), t.String())))
hash := sha512.Sum512([]byte(fmt.Sprintf("EXTENSION: %s", string(ext))))
return binary.BigEndian.Uint64(hash[(len(hash)-9):(len(hash)-1)]) return binary.BigEndian.Uint64(hash[(len(hash)-9):(len(hash)-1)])
} }
type NodeType string
func (node NodeType) Prefix() string { return "NODE: " }
func (node NodeType) String() string { return string(node) }
type ExtType string
func (ext ExtType) Prefix() string { return "EXTENSION: " }
func (ext ExtType) String() string { return string(ext) }
//Function to load an extension from bytes //Function to load an extension from bytes
type ExtensionLoadFunc func(*Context, []byte) (Extension, error) type ExtensionLoadFunc func(*Context, []byte) (Extension, error)
@ -64,7 +71,7 @@ type Context struct {
// Register a NodeType to the context, with the list of extensions it requires // Register a NodeType to the context, with the list of extensions it requires
func (ctx *Context) RegisterNodeType(node_type NodeType, extensions []ExtType) error { func (ctx *Context) RegisterNodeType(node_type NodeType, extensions []ExtType) error {
type_hash := node_type.Hash() type_hash := Hash(node_type)
_, exists := ctx.Types[type_hash] _, exists := ctx.Types[type_hash]
if exists == true { if exists == true {
return fmt.Errorf("Cannot register node type %s, type already exists in context", node_type) return fmt.Errorf("Cannot register node type %s, type already exists in context", node_type)
@ -72,7 +79,7 @@ func (ctx *Context) RegisterNodeType(node_type NodeType, extensions []ExtType) e
ext_found := map[ExtType]bool{} ext_found := map[ExtType]bool{}
for _, extension := range(extensions) { for _, extension := range(extensions) {
_, in_ctx := ctx.Extensions[extension.Hash()] _, in_ctx := ctx.Extensions[Hash(extension)]
if in_ctx == false { if in_ctx == false {
return fmt.Errorf("Cannot register node type %s, required extension %s not in context", node_type, extension) return fmt.Errorf("Cannot register node type %s, required extension %s not in context", node_type, extension)
} }
@ -98,7 +105,7 @@ func (ctx *Context) RegisterExtension(ext_type ExtType, load_fn ExtensionLoadFun
return fmt.Errorf("def has no load function") return fmt.Errorf("def has no load function")
} }
type_hash := ext_type.Hash() type_hash := Hash(ext_type)
_, exists := ctx.Extensions[type_hash] _, exists := ctx.Extensions[type_hash]
if exists == true { if exists == true {
return fmt.Errorf("Cannot register extension of type %s, type already exists in context", ext_type) return fmt.Errorf("Cannot register extension of type %s, type already exists in context", ext_type)

@ -367,7 +367,7 @@ func NewResolveContext(ctx *Context, server *Node, gql_ext *GQLExt, r *http.Requ
return &ResolveContext{ return &ResolveContext{
Context: ctx, Context: ctx,
GQLContext: ctx.Extensions[GQLExtType.Hash()].Data.(*GQLExtContext), GQLContext: ctx.Extensions[Hash(GQLExtType)].Data.(*GQLExtContext),
Server: server, Server: server,
User: user, User: user,
}, nil }, nil
@ -402,7 +402,7 @@ func GQLHandler(ctx *Context, server *Node, gql_ext *GQLExt) func(http.ResponseW
query := GQLPayload{} query := GQLPayload{}
json.Unmarshal(str, &query) json.Unmarshal(str, &query)
gql_context := ctx.Extensions[GQLExtType.Hash()].Data.(*GQLExtContext) gql_context := ctx.Extensions[Hash(GQLExtType)].Data.(*GQLExtContext)
params := graphql.Params{ params := graphql.Params{
Schema: gql_context.Schema, Schema: gql_context.Schema,
@ -532,7 +532,7 @@ func GQLWSHandler(ctx * Context, server *Node, gql_ext *GQLExt) func(http.Respon
} }
} else if msg.Type == "subscribe" { } else if msg.Type == "subscribe" {
ctx.Log.Logf("gqlws", "SUBSCRIBE: %+v", msg.Payload) ctx.Log.Logf("gqlws", "SUBSCRIBE: %+v", msg.Payload)
gql_context := ctx.Extensions[GQLExtType.Hash()].Data.(*GQLExtContext) gql_context := ctx.Extensions[Hash(GQLExtType)].Data.(*GQLExtContext)
params := graphql.Params{ params := graphql.Params{
Schema: gql_context.Schema, Schema: gql_context.Schema,
Context: req_ctx, Context: req_ctx,

@ -193,7 +193,7 @@ func GetCtx[T Extension, C any](ctx *Context) (C, error) {
var zero T var zero T
var zero_ctx C var zero_ctx C
ext_type := zero.Type() ext_type := zero.Type()
type_hash := ext_type.Hash() type_hash := Hash(ext_type)
ext_info, ok := ctx.Extensions[type_hash] ext_info, ok := ctx.Extensions[type_hash]
if ok == false { if ok == false {
return zero_ctx, fmt.Errorf("%s is not an extension in ctx", ext_type) return zero_ctx, fmt.Errorf("%s is not an extension in ctx", ext_type)
@ -228,7 +228,7 @@ func (node *Node) Serialize() ([]byte, error) {
node_db := NodeDB{ node_db := NodeDB{
Header: NodeDBHeader{ Header: NodeDBHeader{
Magic: NODE_DB_MAGIC, Magic: NODE_DB_MAGIC,
TypeHash: node.Type.Hash(), TypeHash: Hash(node.Type),
NumExtensions: uint32(len(extensions)), NumExtensions: uint32(len(extensions)),
NumQueuedSignals: uint32(len(node.SignalQueue)), NumQueuedSignals: uint32(len(node.SignalQueue)),
}, },
@ -244,7 +244,7 @@ func (node *Node) Serialize() ([]byte, error) {
} }
node_db.Extensions[i] = ExtensionDB{ node_db.Extensions[i] = ExtensionDB{
Header: ExtensionDBHeader{ Header: ExtensionDBHeader{
TypeHash: ext_type.Hash(), TypeHash: Hash(ext_type),
Length: uint64(len(ser)), Length: uint64(len(ser)),
}, },
Data: ser, Data: ser,
@ -262,7 +262,7 @@ func NewNode(ctx *Context, id NodeID, node_type NodeType, queued_signals []Queue
panic("Attempted to create an existing node") panic("Attempted to create an existing node")
} }
def, exists := ctx.Types[node_type.Hash()] def, exists := ctx.Types[Hash(node_type)]
if exists == false { if exists == false {
panic("Node type %s not registered in Context") panic("Node type %s not registered in Context")
} }

@ -3,15 +3,11 @@ package graphvent
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"crypto/sha512"
"encoding/binary"
) )
type PolicyType string type PolicyType string
func (policy PolicyType) Hash() uint64 { func (policy PolicyType) Prefix() string { return "POLICY: " }
hash := sha512.Sum512([]byte(fmt.Sprintf("POLICY: %s", string(policy)))) func (policy PolicyType) String() string { return string(policy) }
return binary.BigEndian.Uint64(hash[(len(hash)-9):(len(hash)-1)])
}
const ( const (
RequirementOfPolicyType = PolicyType("REQUIREMENT_OF") RequirementOfPolicyType = PolicyType("REQUIREMENT_OF")