Skip to content

Commit b1cb716

Browse files
authored
various fixes for the encoding/decoding of the execution data with post values (#393)
* fix missing newvalue in json * fix tests * add debug error messages * JSON marshal the new value * add a json-decoding test from kaustinen * remove debug trace
1 parent 6ac575e commit b1cb716

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

proof_json.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (vp *VerkleProof) UnmarshalJSON(data []byte) error {
147147
var aux verkleProofMarshaller
148148
err := json.Unmarshal(data, &aux)
149149
if err != nil {
150-
return err
150+
return fmt.Errorf("verkle proof unmarshal error: %w", err)
151151
}
152152

153153
vp.DepthExtensionPresent, err = PrefixedHexStringToBytes(aux.DepthExtensionPresent)
@@ -198,7 +198,7 @@ func (ssd StemStateDiff) MarshalJSON() ([]byte, error) {
198198
func (ssd *StemStateDiff) UnmarshalJSON(data []byte) error {
199199
var aux stemStateDiffMarshaller
200200
if err := json.Unmarshal(data, &aux); err != nil {
201-
return err
201+
return fmt.Errorf("stemdiff unmarshal error: %w", err)
202202
}
203203

204204
stem, err := PrefixedHexStringToBytes(aux.Stem)
@@ -219,22 +219,27 @@ type suffixStateDiffMarshaller struct {
219219
}
220220

221221
func (ssd SuffixStateDiff) MarshalJSON() ([]byte, error) {
222-
var cvstr *string
222+
var cvstr, nvstr *string
223223
if ssd.CurrentValue != nil {
224224
tempstr := HexToPrefixedString(ssd.CurrentValue[:])
225225
cvstr = &tempstr
226226
}
227+
if ssd.NewValue != nil {
228+
tempstr := HexToPrefixedString(ssd.NewValue[:])
229+
nvstr = &tempstr
230+
}
227231
return json.Marshal(&suffixStateDiffMarshaller{
228232
Suffix: ssd.Suffix,
229233
CurrentValue: cvstr,
234+
NewValue: nvstr,
230235
})
231236
}
232237

233238
func (ssd *SuffixStateDiff) UnmarshalJSON(data []byte) error {
234239
aux := &suffixStateDiffMarshaller{}
235240

236241
if err := json.Unmarshal(data, &aux); err != nil {
237-
return err
242+
return fmt.Errorf("suffix diff unmarshal error: %w", err)
238243
}
239244

240245
if aux.CurrentValue != nil && len(*aux.CurrentValue) != 64 && len(*aux.CurrentValue) != 0 && len(*aux.CurrentValue) != 66 {

proof_json_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package verkle
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
)
7+
8+
func TestJSONDeserialization(t *testing.T) {
9+
str := `{
10+
"stem": "0x97233a822ee74c294ccec8e4e0c65106b374d4423d5d09236d0f6c6647e185",
11+
"suffixDiffs": [
12+
{ "suffix": 0, "currentValue": null, "newValue": null },
13+
{ "suffix": 1, "currentValue": null, "newValue": null },
14+
{ "suffix": 2, "currentValue": null, "newValue": null },
15+
{ "suffix": 3, "currentValue": null, "newValue": null },
16+
{ "suffix": 4, "currentValue": null, "newValue": null }
17+
]
18+
}`
19+
var statediff StemStateDiff
20+
err := json.Unmarshal([]byte(str), &statediff)
21+
if err != nil {
22+
t.Fatal(err)
23+
}
24+
}

proof_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ func TestSuffixStateDiffJSONMarshalUn(t *testing.T) {
473473
},
474474
}
475475

476-
expectedJSON := `{"suffix":65,"currentValue":"0x102030405060708090a0b0c0d0e0f000112233445566778899aabbccddeeff00"}`
476+
expectedJSON := `{"suffix":65,"currentValue":"0x102030405060708090a0b0c0d0e0f000112233445566778899aabbccddeeff00","newValue":null}`
477477
actualJSON, err := json.Marshal(ssd)
478478
if err != nil {
479479
t.Errorf("error marshalling SuffixStateDiff to JSON: %v", err)
@@ -514,7 +514,7 @@ func TestStemStateDiffJSONMarshalUn(t *testing.T) {
514514
}},
515515
}
516516

517-
expectedJSON := `{"stem":"0x0a000000000000000000000000000000000000000000000000000000000000","suffixDiffs":[{"suffix":65,"currentValue":"0x102030405060708090a0b0c0d0e0f000112233445566778899aabbccddeeff00"}]}`
517+
expectedJSON := `{"stem":"0x0a000000000000000000000000000000000000000000000000000000000000","suffixDiffs":[{"suffix":65,"currentValue":"0x102030405060708090a0b0c0d0e0f000112233445566778899aabbccddeeff00","newValue":null}]}`
518518
actualJSON, err := json.Marshal(ssd)
519519
if err != nil {
520520
t.Errorf("error marshalling SuffixStateDiff to JSON: %v", err)
@@ -543,7 +543,7 @@ func TestSuffixStateDiffJSONMarshalUnCurrentValueNil(t *testing.T) {
543543
CurrentValue: nil,
544544
}
545545

546-
expectedJSON := `{"suffix":65,"currentValue":null}`
546+
expectedJSON := `{"suffix":65,"currentValue":null,"newValue":null}`
547547
actualJSON, err := json.Marshal(ssd)
548548
if err != nil {
549549
t.Errorf("error marshalling SuffixStateDiff to JSON: %v", err)

0 commit comments

Comments
 (0)