From 20817711352ff269f9dff145a598416b050f5eaa Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Mon, 30 Oct 2023 13:23:08 -0600 Subject: [PATCH] Sort maps when serializing so they can be serialized consistently --- node.go | 2 +- serialize.go | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/node.go b/node.go index 7ef758e..e80e3bc 100644 --- a/node.go +++ b/node.go @@ -293,7 +293,7 @@ func nodeLoop(ctx *Context, node *Node) error { } validated := ed25519.Verify(msg.Source, sig_data, msg.Signature) if validated == false { - ctx.Log.Logf("signal", "SIGNAL_VERIFY_ERR: %s - %s", node.ID, reflect.TypeOf(msg.Signal)) + ctx.Log.Logf("signal_verify", "SIGNAL_VERIFY_ERR: %s - %s", node.ID, reflect.TypeOf(msg.Signal)) continue } diff --git a/serialize.go b/serialize.go index 68cdecc..8bd598e 100644 --- a/serialize.go +++ b/serialize.go @@ -9,6 +9,7 @@ import ( "math" "reflect" "sort" + "bytes" ) const ( @@ -826,7 +827,7 @@ func SerializeMap(ctx *Context, value reflect.Value) (Chunks, error) { return NewChunks([]byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}), nil } - map_chunks := Chunks{} + map_chunks := []Chunks{} map_size := uint64(0) map_iter := value.MapRange() for map_iter.Next() { @@ -838,19 +839,30 @@ func SerializeMap(ctx *Context, value reflect.Value) (Chunks, error) { if err != nil { return Chunks{}, err } - map_chunks = map_chunks.AddChunksToEnd(key_chunks) val_chunks, err := SerializeValue(ctx, val) if err != nil { return Chunks{}, err } - map_chunks = map_chunks.AddChunksToEnd(val_chunks) + + chunks := key_chunks.AddChunksToEnd(val_chunks) + map_chunks = append(map_chunks, chunks) } + // Sort map_chunks + sort.Slice(map_chunks, func(i, j int) bool { + return bytes.Compare(map_chunks[i].First.Data, map_chunks[j].First.Data) < 0 + }) + chunks := Chunks{} + for _, chunk := range(map_chunks) { + chunks = chunks.AddChunksToEnd(chunk) + } + + size_data := make([]byte, 8) binary.BigEndian.PutUint64(size_data, map_size) - return NewChunks(size_data).AddChunksToEnd(map_chunks), nil + return NewChunks(size_data).AddChunksToEnd(chunks), nil } func DeserializeMap(ctx *Context, reflect_type reflect.Type, data []byte) (reflect.Value, []byte, error) {