From ba344bddcfa8cc1ba529373a38e0e6c9753824a1 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Mon, 28 Aug 2023 14:52:28 -0600 Subject: [PATCH] Fixed policy serializing and added default policy to allow receiving errors, read results, and status updates --- node.go | 30 ++++++++++++++++++++++++++++++ node_test.go | 16 +++++++++------- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/node.go b/node.go index 28965ee..dc4b780 100644 --- a/node.go +++ b/node.go @@ -609,6 +609,19 @@ func NewNode(ctx *Context, key ed25519.PrivateKey, node_type NodeType, buffer_si 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{ Key: key, ID: id, @@ -816,6 +829,9 @@ func (node NodeDB) Serialize() []byte { for _, extension := range(node.Extensions) { ser = append(ser, extension.Serialize()...) } + for _, policy := range(node.Policies) { + ser = append(ser, policy.Serialize()...) + } for _, qsignal := range(node.QueuedSignals) { ser = append(ser, qsignal.Serialize()...) } @@ -848,6 +864,18 @@ func (extension ExtensionDB) Serialize() []byte { 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 func WriteNode(ctx *Context, node *Node) error { ctx.Log.Logf("db", "DB_WRITE: %s", node.ID) @@ -857,6 +885,8 @@ func WriteNode(ctx *Context, node *Node) error { return err } + ctx.Log.Logf("db_data", "DB_DATA: %+v", bytes) + id_bytes := node.ID.Serialize() ctx.Log.Logf("db", "DB_WRITE_ID: %+v", id_bytes) diff --git a/node_test.go b/node_test.go index d6458e6..6525884 100644 --- a/node_test.go +++ b/node_test.go @@ -8,7 +8,7 @@ import ( ) func TestNodeDB(t *testing.T) { - ctx := logTestContext(t, []string{"signal", "node"}) + ctx := logTestContext(t, []string{"signal", "node", "db"}) node_type := NodeType("test") err := ctx.RegisterNodeType(node_type, []ExtType{GroupExtType}) fatalErr(t, err) @@ -21,7 +21,7 @@ func TestNodeDB(t *testing.T) { } func TestNodeRead(t *testing.T) { - ctx := logTestContext(t, []string{}) + ctx := logTestContext(t, []string{"test"}) node_type := NodeType("TEST") err := ctx.RegisterNodeType(node_type, []ExtType{GroupExtType, ECDHExtType}) fatalErr(t, err) @@ -37,15 +37,17 @@ func TestNodeRead(t *testing.T) { ctx.Log.Logf("test", "N1: %s", n1_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 := NewNode(ctx, n2_key, node_type, 10, map[PolicyType]Policy{ - AllNodesPolicyType: &policy, - }, NewGroupExt(nil), NewECDHExt(), n2_listener) + n2 := NewNode(ctx, n2_key, node_type, 10, nil, NewGroupExt(nil), NewECDHExt(), n2_listener) n1 := NewNode(ctx, n1_key, node_type, 10, map[PolicyType]Policy{ - AllNodesPolicyType: &policy, + PerNodePolicyType: &n1_policy, }, NewGroupExt(nil), NewECDHExt()) read_sig := NewReadSignal(map[ExtType][]string{