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

[Merged by Bors] - tortoise: reuse decoded votes #5009

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion tortoise/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (s *state) layer(lid types.LayerID) *layerInfo {
layer, exist := s.layers[lid]
if !exist {
layersNumber.Inc()
layer = &layerInfo{lid: lid}
layer = &layerInfo{lid: lid, opinions: map[types.Hash32]votes{}}
s.layers[lid] = layer
}
return layer
Expand Down Expand Up @@ -179,6 +179,10 @@ type layerInfo struct {
verifying verifyingInfo
coinflip sign

// unique opinions recorded from the ballots in this layer.
// ballot votes an opinion and encodes sidecar
opinions map[types.Hash32]votes

opinion types.Hash32
// a pointer to the value stored on the previous layerInfo object
// it is stored as a pointer so that when previous layerInfo is evicted
Expand Down
42 changes: 31 additions & 11 deletions tortoise/tortoise.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
t.layers[genesis] = &layerInfo{
lid: genesis,
hareTerminated: true,
opinions: map[types.Hash32]votes{},
}
t.verifying = newVerifying(config, t.state)
t.full = newFullTortoise(config, t.state)
Expand Down Expand Up @@ -748,11 +749,23 @@
zap.Uint32("lid", ballot.Layer.Uint32()),
)

votes, min, err := decodeVotes(t.evicted, binfo.layer, base, ballot.Opinion.Votes)
if err != nil {
return nil, 0, err
layer := t.layer(binfo.layer)

existing, exists := layer.opinions[ballot.Opinion.Hash]
var min types.LayerID
if exists {
binfo.votes = existing
} else {
var (
votes votes
err error
)
votes, min, err = decodeVotes(t.evicted, binfo.layer, base, ballot.Opinion.Votes)
if err != nil {
return nil, 0, err
}

Check warning on line 766 in tortoise/tortoise.go

View check run for this annotation

Codecov / codecov/patch

tortoise/tortoise.go#L765-L766

Added lines #L765 - L766 were not covered by tests
binfo.votes = votes
}
binfo.votes = votes
t.logger.Debug("decoded exceptions",
zap.Stringer("block", binfo.id),
zap.Uint32("lid", binfo.layer.Uint32()),
Expand All @@ -771,15 +784,22 @@
}

t.state.addBallot(ballot)
for current := ballot.votes.tail; current != nil && !current.lid.Before(min); current = current.prev {
for i, block := range current.supported {
existing := t.getBlock(block.header())
if existing != nil {
current.supported[i] = existing
} else {
t.addBlock(block)
layer := t.layer(ballot.layer)
existing, exists := layer.opinions[ballot.opinion()]
if exists {
ballot.votes = existing
} else {
for current := ballot.votes.tail; current != nil && !current.lid.Before(min); current = current.prev {
for i, block := range current.supported {
existing := t.getBlock(block.header())
if existing != nil {
current.supported[i] = existing
} else {
t.addBlock(block)
}
}
}
layer.opinions[ballot.opinion()] = ballot.votes
}
if !ballot.layer.After(t.processed) {
if err := t.countBallot(ballot); err != nil {
Expand Down
Loading