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

Forest.getroots behavior #331

Open
kcalvinalvin opened this issue Nov 6, 2021 · 0 comments
Open

Forest.getroots behavior #331

kcalvinalvin opened this issue Nov 6, 2021 · 0 comments

Comments

@kcalvinalvin
Copy link
Member

hmmm.... I wonder how that is possible, given code on this branch to compare the roots succeeds

It's because the getRoots() function is based off of forest.numLeaves and forest.rows and those things correctly get updated during an undo.

// getRoots returns all the roots of the trees
func (f *Forest) getRoots() []Hash {
positionList := NewPositionList()
defer positionList.Free()
getRootsForwards(f.numLeaves, f.rows, &positionList.list)
roots := make([]Hash, len(positionList.list))
for i, _ := range roots {
roots[i] = f.data.read(positionList.list[i])
}
return roots
}

However, the underlying forest didn't change. So the getRoots() function is grabbing non-roots. This actually wouldn't happen with Pollard because you keep updating the roots and don't calculate it on the fly.

Try:

func undoOnceRandom(blocks int32) error {
        f := NewForest(RamForest, nil, "", 0)

        sc := newSimChain(0x07)
        sc.lookahead = 0
        for b := int32(0); b < blocks; b++ {

                adds, durations, delHashes := sc.NextBlock(rand.Uint32() & 0x03)

                fmt.Printf("\t\tblock %d del %d add %d - %s\n",
                        sc.blockHeight, len(delHashes), len(adds), f.Stats())

                bp, err := f.ProveBatch(delHashes)
                if err != nil {
                        return err
                }
                beforeString := f.ToString()
                ub, err := f.Modify(adds, bp.Targets)
                if err != nil {
                        return err
                }

                // undo every 3rd block
                if b%3 == 2 {
                        fmt.Print(ub.ToString())
                        err := f.Undo(*ub)
                        if err != nil {
                                return err
                        }
                        afterString := f.ToString()

                        if beforeString != afterString {
                                err := fmt.Errorf("expected:\n %s\n but got:\n%s\n",
                                        beforeString, afterString)
                                panic(err)
                        }
                        sc.BackOne(adds, durations, delHashes)
                }

        }
        return nil
}

Originally posted by @kcalvinalvin in #330 (comment)

Should this result in an error? Not sure if this is the right behavior for getRoots() when non-roots are returned. Or maybe use some other method to keep track of roots so this doesn't happen.

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

No branches or pull requests

1 participant