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

refactor: Use slices instead of maps #188

Merged
merged 1 commit into from
Sep 22, 2020
Merged

Conversation

dergoegge
Copy link
Contributor

was part of my experiments in #180.
Refactor ProveBatch, verifyBatchProof and IngestBatchProof to use slices.
Preallocation of slices also helps with GC pressure.
also includes the ProofPositions function which is also part of #185, kinda stupid but otherwise this doesn't build.

accumulator/utils.go Show resolved Hide resolved
return fmt.Errorf("block proof mismatch")
}
// preallocating polNodes helps with garbage collection
polNodes := make([]polNode, len(trees)*3)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

preallocating the polNodes before populating the pollard was the most significant improvement to gc pressure.
my assumption is that this helps because the polNodes are next to each other in memory so it's easier for the gc to do its job.

@dergoegge dergoegge mentioned this pull request Aug 21, 2020
accumulator/pollardproof.go Show resolved Hide resolved
accumulator/batchproof.go Outdated Show resolved Hide resolved
accumulator/batchproof.go Outdated Show resolved Hide resolved
// the positions that are computed/not included in the proof. (also includes the targets)
computedPositions := make([]uint64, 0, len(targets)*int(forestRows))
for row := uint8(0); row < forestRows; row++ {
computedPositions = append(computedPositions, targets...)
Copy link
Member

Choose a reason for hiding this comment

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

This could be moved outside the loop right?

Copy link
Member

Choose a reason for hiding this comment

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

oh nvm targets are mutated.

EDIT: No wait so all the targets are already allocated in the first go. All the ones afterwards just are duplicates.

Also seems like the computed positions are only used for pre-allocating in verifyBatchProof in batchproof.go so this could be moved outside the loop I think. Would also help in further lessening the gc pressure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think it can. I think my naming here (targets, nextTargets) is a bit off so that might be why it seems wrong. On every iteration targets holds the positions of the nodes on that row that are known, for row=0 it's the actual targets contained in the proof, on the rows above it's the the positions that can be computed. The nextTargets variable is used to store the positions on the next row that can be computed and at the end of each iteration targets is assigned to nextTargets.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also seems like the computed positions are only used for pre-allocating in verifyBatchProof in batchproof.go so this could be moved outside the loop I think. Would also help in further lessening the gc pressure.

the computed positions are also needed in #185, but yeah in verifyBatchProof they are unnecessary

// and has led to a number of bugs. It *is* special in a way, in that
// the bottom row is the only thing you actually prove and add/delete,
// but it'd be nice if it could all be treated uniformly.
if len(bp.Proof) < 2 || len(targets) < 2 {
Copy link
Member

Choose a reason for hiding this comment

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

What would be a case where this clause is true?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i added a comment there, let me know if that explains it.

rootMatches++
}
}
if len(rootCandidates) != rootMatches {
Copy link
Member

Choose a reason for hiding this comment

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

Since all roots should match in order for the batchProof to be valid, wouldn't it be better to return false in the block at line 232 if any root doesn't match a rootCandidate?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

rootCandidates only holds a subset of the roots, so it could happen that a root does not match a candidate but by making sure that all candidates were matched to some root it still works out.
also added a comment there.

@dergoegge dergoegge force-pushed the useslices branch 2 times, most recently from 96cf573 to 06336b8 Compare September 5, 2020 19:02
@kcalvinalvin
Copy link
Member

Is there any code that would cause backwards incompatibility with master? Right now, It's fine when I point the csn to my local bridgenode based off of this PR but crashes with this message when I point it at the current default server.

[N] calvin@bitcoin ~/b/u/g/s/g/m/u/c/utreexoclient> ./utreexoclient -checksig=0
Creating new pollarddata
Verify failed: txo 6749762ae220c10705556799dcec9bb6a54a7b881eb4b961323a3363b00db518:0 position 103 leafdata 7b9562ebac55529d473d4d0bb616ee5d9f69898b1cc72051c204321666abea6d proof b628e99d6a24cbb09b35253756439ba827d27bc0b20c0d02a7bed7fc1dae4287
sib exists, af6aa1d9405b73e596cb3251c357f7dfdbbbece7f2db02176440098482bd64a2
panic: height 381 LeafData / Proof mismatch

goroutine 9 [running]:
github.com/mit-dci/utreexo/csn.(*Csn).IBDThread(0xc00000c1e0, 0xc000034150)
        /home/calvin/bitcoin-projects/utreexo1/go/src/github.com/mit-dci/utreexo/csn/ibd.go:56 +0xa29
created by github.com/mit-dci/utreexo/csn.(*Csn).Start
        /home/calvin/bitcoin-projects/utreexo1/go/src/github.com/mit-dci/utreexo/csn/init.go:101 +0x243

@dergoegge
Copy link
Contributor Author

dergoegge commented Sep 17, 2020

Is there any code that would cause backwards incompatibility with master?

That's a great test. The roots should be exactly the same for every block.

I think it errors because on master right now the 0-tree root is being send.

// TODO change this for the real thing; no need to prove 0-tree root.
// but we still need to verify it and tag it as a target.
if pos == f.numLeaves-1 && pos&1 == 0 {
proofTree[pos] = f.data.read(pos)
// fmt.Printf("%d add as root\n", pos)
continue
}

In this PR however those are not send/expected by the csn. I will check if that is actually the problem here.

Edit: Actually the 0-tree roots are still being send. This is something else.

@adiabat adiabat merged commit b3afd5d into mit-dci:master Sep 22, 2020
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.

3 participants