Fixed policy serializing and added default policy to allow receiving errors, read results, and status updates

gql_cataclysm
noah metz 2023-08-28 14:52:28 -06:00
parent 4ce2a642c5
commit ba344bddcf
2 changed files with 39 additions and 7 deletions

@ -609,6 +609,19 @@ func NewNode(ctx *Context, key ed25519.PrivateKey, node_type NodeType, buffer_si
policies = map[PolicyType]Policy{} policies = map[PolicyType]Policy{}
} }
default_policy := NewAllNodesPolicy(Tree{
ErrorSignalType.String(): nil,
ReadResultSignalType.String(): nil,
StatusSignalType.String(): nil,
})
all_nodes_policy, exists := policies[AllNodesPolicyType]
if exists == true {
policies[AllNodesPolicyType] = all_nodes_policy.Merge(&default_policy)
} else {
policies[AllNodesPolicyType] = &default_policy
}
node := &Node{ node := &Node{
Key: key, Key: key,
ID: id, ID: id,
@ -816,6 +829,9 @@ func (node NodeDB) Serialize() []byte {
for _, extension := range(node.Extensions) { for _, extension := range(node.Extensions) {
ser = append(ser, extension.Serialize()...) ser = append(ser, extension.Serialize()...)
} }
for _, policy := range(node.Policies) {
ser = append(ser, policy.Serialize()...)
}
for _, qsignal := range(node.QueuedSignals) { for _, qsignal := range(node.QueuedSignals) {
ser = append(ser, qsignal.Serialize()...) ser = append(ser, qsignal.Serialize()...)
} }
@ -848,6 +864,18 @@ func (extension ExtensionDB) Serialize() []byte {
return append(header_bytes, extension.Data...) return append(header_bytes, extension.Data...)
} }
func (header PolicyDBHeader) Serialize() []byte {
ret := make([]byte, POLICY_DB_HEADER_LEN)
binary.BigEndian.PutUint64(ret[0:8], header.TypeHash)
binary.BigEndian.PutUint64(ret[0:8], header.Length)
return ret
}
func (policy PolicyDB) Serialize() []byte {
header_bytes := policy.Header.Serialize()
return append(header_bytes, policy.Data...)
}
// Write a node to the database // Write a node to the database
func WriteNode(ctx *Context, node *Node) error { func WriteNode(ctx *Context, node *Node) error {
ctx.Log.Logf("db", "DB_WRITE: %s", node.ID) ctx.Log.Logf("db", "DB_WRITE: %s", node.ID)
@ -857,6 +885,8 @@ func WriteNode(ctx *Context, node *Node) error {
return err return err
} }
ctx.Log.Logf("db_data", "DB_DATA: %+v", bytes)
id_bytes := node.ID.Serialize() id_bytes := node.ID.Serialize()
ctx.Log.Logf("db", "DB_WRITE_ID: %+v", id_bytes) ctx.Log.Logf("db", "DB_WRITE_ID: %+v", id_bytes)

@ -8,7 +8,7 @@ import (
) )
func TestNodeDB(t *testing.T) { func TestNodeDB(t *testing.T) {
ctx := logTestContext(t, []string{"signal", "node"}) ctx := logTestContext(t, []string{"signal", "node", "db"})
node_type := NodeType("test") node_type := NodeType("test")
err := ctx.RegisterNodeType(node_type, []ExtType{GroupExtType}) err := ctx.RegisterNodeType(node_type, []ExtType{GroupExtType})
fatalErr(t, err) fatalErr(t, err)
@ -21,7 +21,7 @@ func TestNodeDB(t *testing.T) {
} }
func TestNodeRead(t *testing.T) { func TestNodeRead(t *testing.T) {
ctx := logTestContext(t, []string{}) ctx := logTestContext(t, []string{"test"})
node_type := NodeType("TEST") node_type := NodeType("TEST")
err := ctx.RegisterNodeType(node_type, []ExtType{GroupExtType, ECDHExtType}) err := ctx.RegisterNodeType(node_type, []ExtType{GroupExtType, ECDHExtType})
fatalErr(t, err) fatalErr(t, err)
@ -37,15 +37,17 @@ func TestNodeRead(t *testing.T) {
ctx.Log.Logf("test", "N1: %s", n1_id) ctx.Log.Logf("test", "N1: %s", n1_id)
ctx.Log.Logf("test", "N2: %s", n2_id) ctx.Log.Logf("test", "N2: %s", n2_id)
policy := NewAllNodesPolicy(nil) n1_policy := NewPerNodePolicy(map[NodeID]Tree{
n2_id: Tree{
ReadSignalType.String(): nil,
},
})
n2_listener := NewListenerExt(10) n2_listener := NewListenerExt(10)
n2 := NewNode(ctx, n2_key, node_type, 10, map[PolicyType]Policy{ n2 := NewNode(ctx, n2_key, node_type, 10, nil, NewGroupExt(nil), NewECDHExt(), n2_listener)
AllNodesPolicyType: &policy,
}, NewGroupExt(nil), NewECDHExt(), n2_listener)
n1 := NewNode(ctx, n1_key, node_type, 10, map[PolicyType]Policy{ n1 := NewNode(ctx, n1_key, node_type, 10, map[PolicyType]Policy{
AllNodesPolicyType: &policy, PerNodePolicyType: &n1_policy,
}, NewGroupExt(nil), NewECDHExt()) }, NewGroupExt(nil), NewECDHExt())
read_sig := NewReadSignal(map[ExtType][]string{ read_sig := NewReadSignal(map[ExtType][]string{