Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid altering amt when empty #22

Merged
merged 3 commits into from
Jul 22, 2020
Merged

avoid altering amt when empty #22

merged 3 commits into from
Jul 22, 2020

Conversation

acruikshank
Copy link

closes #18

Motivation

Actions like calling ForEach or adding and removing elements put an empty AMT into a state that causes it to be serialized differently even though it is still the empty array. This is because these actions cause expVals to be expanded in the node from a nil value to a slice with nil values. While expVals isn't itself serialized, The fact that it is non-nil causes the node's Bmap value to be initialized from a nil value to []byte{0} in Flush, and that does serialize differently.

Proposed Changes

  1. Add a test to demonstrate the problem.
  2. Initialize Node.Bmap lazily in Flush.

This is probably the simplest solution that fixes the problem. It leaves open a bigger question about whether empty nodes should be structurally different when serialized from nodes containing one or more elements. I find it a little surprising, but not really wrong.

Copy link
Member

@Stebalien Stebalien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd expect all nodes to have this bitfield, empty or otherwise.

@@ -3,6 +3,7 @@ package amt
import (
"context"
"fmt"
"github.com/stretchr/testify/require"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import order nit

@acruikshank acruikshank requested a review from magik6k July 20, 2020 21:05
@whyrusleeping
Copy link
Member

I think I agree with @Stebalien , this value should always be set and never should be serialized as nil.

amt.go Outdated
@@ -495,11 +495,14 @@ func (n *Node) Flush(ctx context.Context, bs cbor.IpldStore, depth int) error {
if len(n.expVals) == 0 {
return nil
}
n.Bmap = []byte{0}
n.Bmap = nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always defaulting to having the bitfield set would just mean moving this line (before this change) to above the if

@Stebalien
Copy link
Member

Fix for byte arrays: whyrusleeping/cbor-gen#29

@Stebalien
Copy link
Member

Fix merged.

* Saves an allocation.
* Importantly, the bitfield is _always_ initialized.

Breaking: This will refuse to decode AMTs with mis-sized bitfields.
@Stebalien
Copy link
Member

Fix pushed. Could you take a look at this @whyrusleeping and @acruikshank?

@Kubuxu Kubuxu merged commit a63ecae into master Jul 22, 2020
@Kubuxu Kubuxu deleted the bug/cid-instability branch July 22, 2020 21:28
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.

ForEach mutates empty AMT
4 participants