Skip to content

Fix marshaling#17

Merged
zelig merged 5 commits intoethersphere:masterfrom
brainiac-five:fix/marshaling
Dec 31, 2025
Merged

Fix marshaling#17
zelig merged 5 commits intoethersphere:masterfrom
brainiac-five:fix/marshaling

Conversation

@brainiac-five
Copy link
Copy Markdown
Collaborator

This PR fixes #15 by correcting the fork PO bit decoding in Unmarshaling().

Marshaling and unmarshaling used different bit order within bitMap.

This PR adds 7- to

for i := 0; i < 256; i++ {
if bitMap[i/8]&(1<<(i%8)) != 0 {
poMap = append(poMap, int8(i))
c++
}

to become

https://github.com/brainiac-five/pot/blob/a39473f129580ccb1db627856d6c4f9de004c0c4/pkg/elements/persist.go#L108-L112

Alternatively the marshaling could be changed to take the 7- out in

setBitMap := func(n uint8) {
bitMap[n/8] |= 1 << ((7 - n) % 8)
}

Both ways work and pass all tests in this project and potjs.

The assumption of this PR is that going from left to right within a byte (from highest bit down) like PO calculation in

for j := start; j < 8; j++ {
if (oxo>>uint8(7-j))&0x01 != 0 {
return i*8 + j
}
}
}

is the intended way.

Without the fix of this PR the new test

https://github.com/brainiac-five/pot/blob/a39473f129580ccb1db627856d6c4f9de004c0c4/kvs_test.go#L112-L136

fails with:

--- FAIL: TestPotKvs_Save (0.00s)
    --- FAIL: TestPotKvs_Save/Save_KVS_with_two_items,_after-load_values_exist (0.00s)
        kvs_test.go:140: 
            	Error Trace:	/Users/b5/gopot/kvs_test.go:140
            	Error:      	Received unexpected error:
            	            	not found
            	Test:       	TestPotKvs_Save/Save_KVS_with_two_items,_after-load_values_exist
        kvs_test.go:141: 
            	Error Trace:	/Users/b5/gopot/kvs_test.go:141
            	Error:      	Not equal: 
            	            	expected: []byte{0x90, 0xf3, 0xe9, 0xad, 0x9b, 0x11, 0xa0, 0xe5, 0xaf, 0x7c, 0xf0, 0x5b, 0xf9, 0xd, 0xb0, 0x49, 0x43, 0xba, 0x62, 0x82, 0x3, 0xb3, 0x11, 0x6e, 0x33, 0xf7, 0xe, 0x28, 0xa4, 0x11, 0xb8, 0x5e, 0x8b, 0x94, 0x22, 0x23, 0xc8, 0x3a, 0x98, 0x4, 0x78, 0xd3, 0xb3, 0xda, 0x15, 0x51, 0x58, 0x3a, 0x8b, 0xc0, 0xea, 0x62, 0xf8, 0xe2, 0xe1, 0x2f, 0xf5, 0x3b, 0x56, 0x2a, 0x19, 0xf8, 0x33, 0xc, 0x47, 0x96, 0xc2, 0xf5, 0x8c, 0xaa, 0x10, 0x1b, 0x83, 0xc, 0x3, 0xd0, 0xf9, 0xa3, 0xc2, 0x65, 0xad, 0xb4, 0xba, 0x3, 0xc, 0x9e, 0xf6, 0xb8, 0x8c, 0xa3, 0xc8, 0xe8, 0x49, 0xb3, 0xb1, 0x1b}
            	            	actual  : []byte(nil)
            	            	
            	            	Diff:
            	            	--- Expected
            	            	+++ Actual
            	            	@@ -1,9 +1,2 @@
            	            	-([]uint8) (len=96) {
            	            	- 00000000  90 f3 e9 ad 9b 11 a0 e5  af 7c f0 5b f9 0d b0 49  |.........|.[...I|
            	            	- 00000010  43 ba 62 82 03 b3 11 6e  33 f7 0e 28 a4 11 b8 5e  |C.b....n3..(...^|
            	            	- 00000020  8b 94 22 23 c8 3a 98 04  78 d3 b3 da 15 51 58 3a  |.."#.:..x....QX:|
            	            	- 00000030  8b c0 ea 62 f8 e2 e1 2f  f5 3b 56 2a 19 f8 33 0c  |...b.../.;V*..3.|
            	            	- 00000040  47 96 c2 f5 8c aa 10 1b  83 0c 03 d0 f9 a3 c2 65  |G..............e|
            	            	- 00000050  ad b4 ba 03 0c 9e f6 b8  8c a3 c8 e8 49 b3 b1 1b  |............I...|
            	            	-}
            	            	+([]uint8) <nil>
            	            	 
            	Test:       	TestPotKvs_Save/Save_KVS_with_two_items,_after-load_values_exist
FAIL
exit status 1
FAIL	github.com/ethersphere/proximity-order-trie	1.208s

This test should obviously work. What it adds is to have two elements being saved and retrieved after loading the kvs, instead of only one like other tests do.

This PR also has the correct go.mod package name ethersphere/proximity-order-trie.

@zelig zelig merged commit 4c39490 into ethersphere:master Dec 31, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Save/Load Loses Key-Value Pairs

2 participants