Skip to content

Empty arrays are sometimes serialized as null, and sometimes as [] #12214

Open
@aatifsyed

Description

@aatifsyed

Checklist

  • This is not a security-related bug/issue. If it is, please follow please follow the security policy.
  • I have searched on the issue tracker and the lotus forum, and there is no existing related issue or discussion.
  • I am running the Latest release, the most recent RC(release canadiate) for the upcoming release or the dev branch(master), or have an issue updating to any of these.
  • I did not make any code changes to lotus.

Lotus component

  • lotus daemon - chain sync
  • lotus fvm/fevm - Lotus FVM and FEVM interactions
  • lotus miner/worker - sealing
  • lotus miner - proving(WindowPoSt/WinningPoSt)
  • lotus JSON-RPC API
  • lotus message management (mpool)
  • Other

Lotus Version

$ ./lotus --version
lotus version 1.25.2+calibnet+git.f51f83bfe

Repro Steps

N/A

Describe the Bug

Background

In Go, [] slices have three states, which are serialized differently:

  1. Uninitialized (never assigned), serialized as null
  2. Initialized, but empty, serialized as []
  3. Initialized, but non-empty, serialized as [<item>]
var unrelated bool = true
var s []int
if unrelated {
	s = []int{}
}
ser, _ := json.Marshal(s)
fmt.Printf("%s\n", ser) // `null` or `[]`

playground link

Motivation

Specification of JSON types in the Common Node API FIP
The current draft of the spec represents slices as null | [].

However, the fact that unrelated in the example above affects serialization output causes:

  • Issues for nodes like forest that want to reproduce Lotus' RPC output.
    • To exactly match lotus, we'd need to track initialization stack and program logic that Lotus does, which is basically non-deterministic from the outside.
  • Issues for clients when switching nodes.

Fix

Lotus should always serialize slices as JSON Arrays, and the Common Node API should specify serialization as such.
AIUI this is a longstanding quirk of go that has existing solutions:

Logging Information

N/A

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    📌 Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions