From ba6fa883ecedc42f7745340014d19e7ea4c58001 Mon Sep 17 00:00:00 2001 From: fearlessfe <505380967@qq.com> Date: Thu, 7 Nov 2024 23:11:17 +0800 Subject: [PATCH] feat: add ssz test for AttestationData --- src/spec_tests/ssz_static/root.zig | 8 +++---- src/ssz/ssz.zig | 34 +++++++++++++++++++++++++++++- src/yaml/parse.zig | 4 ++++ 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/spec_tests/ssz_static/root.zig b/src/spec_tests/ssz_static/root.zig index 89ddc61..b4bffe9 100644 --- a/src/spec_tests/ssz_static/root.zig +++ b/src/spec_tests/ssz_static/root.zig @@ -59,7 +59,7 @@ const StructMultiPhase = union { // test cases for all phases const CommonUnion = union { // AggregateAndProof: types.AggregateAndProof, - // AttestationData: types.AttestationData, + AttestationData: types.AttestationData, // AttesterSlashing: types.AttesterSlashing, // BeaconBlock: types.BeaconBlock, BeaconBlockHeader: types.BeaconBlockHeader, @@ -71,9 +71,9 @@ const CommonUnion = union { Eth1Data: types.Eth1Data, Fork: types.Fork, ForkData: types.ForkData, - // HistoricalBatch: types.HistoricalBatch, - // IndexedAttestation: types.IndexedAttestation, - // PendingAttestation: types.PendingAttestation, + // HistoricalBatch: types.HistoricalBatch, // need to know the lenght in spec + // IndexedAttestation: types.IndexedAttestation, // need to know the limit in spec + // PendingAttestation: types.PendingAttestation, // parse yaml // ProposerSlashing: types.ProposerSlashing, // SignedBeaconBlock: types.SignedBeaconBlock, // SignedBeaconBlockHeader: types.SignedBeaconBlockHeader, diff --git a/src/ssz/ssz.zig b/src/ssz/ssz.zig index 32fd3b6..6a38284 100644 --- a/src/ssz/ssz.zig +++ b/src/ssz/ssz.zig @@ -1028,12 +1028,44 @@ pub fn hashTreeRoot(value: anytype, out: *[32]u8, allocator: Allocator) !void { switch (type_info.pointer.size) { .One => try hashTreeRoot(value.*, out, allocator), .Slice => { - switch (@typeInfo(type_info.pointer.child)) { + const slice_info = @typeInfo(type_info.pointer.child); + switch (slice_info) { .int => { var list = ArrayList(u8).init(allocator); defer list.deinit(); const chunks = try pack(value, &list); + // TODO: slice should have a limit try merkleize(chunks, null, out); + var length: [32]u8 = [_]u8{0} ** 32; + std.mem.writeInt(u64, length[0..8], value.len, .little); + mixInLength(out.*, length, out); + }, + .array => { + switch (@typeInfo(slice_info.array.child)) { + .int => { + var list = ArrayList(u8).init(allocator); + defer list.deinit(); + const chunks = try pack(value, &list); + try merkleize(chunks, null, out); + }, + .bool => { + var list = ArrayList(u8).init(allocator); + defer list.deinit(); + const chunks = try packBits(value, list); + try merkleize(chunks, null, out); + }, + .array => { + var chunks = ArrayList(chunk).init(allocator); + defer chunks.deinit(); + var tmp: chunk = undefined; + for (value) |item| { + try hashTreeRoot(item, &tmp, allocator); + try chunks.append(tmp); + } + try merkleize(chunks.items, null, out); + }, + else => return error.NotSupported, + } }, else => return error.UnSupportedPointerType, } diff --git a/src/yaml/parse.zig b/src/yaml/parse.zig index 0a49af8..b7ab393 100644 --- a/src/yaml/parse.zig +++ b/src/yaml/parse.zig @@ -589,6 +589,10 @@ const Parser = struct { self.token_it.seekBy(-1); break; }, + .flow_map_end => { + self.token_it.seekTo(key_pos + 1); + break; + }, else => { // TODO key not being a literal // return error.Unhandled;