Skip to content

Commit

Permalink
Remove .Status() from .Accepted() (#3124)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph committed Jun 18, 2024
1 parent 5d5b9cf commit 2e72c7c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
13 changes: 4 additions & 9 deletions snow/consensus/snowman/snowman_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ package snowman

import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/snow/consensus/snowball"
)

// Tracks the state of a snowman block
type snowmanBlock struct {
// parameters to initialize the snowball instance with
params snowball.Parameters
t *Topological

// block that this node contains. For the genesis, this value will be nil
blk Block
Expand All @@ -38,7 +36,7 @@ func (n *snowmanBlock) AddChild(child Block) {
// if the snowball instance is nil, this is the first child. So the instance
// should be initialized.
if n.sb == nil {
n.sb = snowball.NewTree(snowball.SnowballFactory, n.params, childID)
n.sb = snowball.NewTree(snowball.SnowballFactory, n.t.params, childID)
n.children = make(map[ids.ID]Block)
} else {
n.sb.Add(childID)
Expand All @@ -47,11 +45,8 @@ func (n *snowmanBlock) AddChild(child Block) {
n.children[childID] = child
}

func (n *snowmanBlock) Accepted() bool {
func (n *snowmanBlock) Decided() bool {
// if the block is nil, then this is the genesis which is defined as
// accepted
if n.blk == nil {
return true
}
return n.blk.Status() == choices.Accepted
return n.blk == nil || n.blk.Height() <= n.t.lastAcceptedHeight
}
19 changes: 12 additions & 7 deletions snow/consensus/snowman/topological.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (ts *Topological) Initialize(
ts.lastAcceptedID = lastAcceptedID
ts.lastAcceptedHeight = lastAcceptedHeight
ts.blocks = map[ids.ID]*snowmanBlock{
lastAcceptedID: {params: ts.params},
lastAcceptedID: {t: ts},
}
ts.preferredHeights = make(map[uint64]ids.ID)
ts.preference = lastAcceptedID
Expand Down Expand Up @@ -162,8 +162,8 @@ func (ts *Topological) Add(blk Block) error {
// add the block as a child of its parent, and add the block to the tree
parentNode.AddChild(blk)
ts.blocks[blkID] = &snowmanBlock{
params: ts.params,
blk: blk,
t: ts,
blk: blk,
}

// If we are extending the preference, this is the new preference
Expand Down Expand Up @@ -278,7 +278,12 @@ func (ts *Topological) RecordPoll(ctx context.Context, voteBag bag.Bag[ids.ID])

// Runtime = |live set| ; Space = Constant
// Traverse from the preferred ID to the last accepted ancestor.
for block := startBlock; !block.Accepted(); {
//
// It is guaranteed that the first decided block we encounter is the last
// accepted block because the startBlock is the preferred block. The
// preferred block is guaranteed to either be the last accepted block or
// extend the accepted chain.
for block := startBlock; !block.Decided(); {
blkID := block.blk.ID()
ts.preferredIDs.Add(blkID)
ts.preferredHeights[block.blk.Height()] = blkID
Expand Down Expand Up @@ -349,7 +354,7 @@ func (ts *Topological) calculateInDegree(votes bag.Bag[ids.ID]) {
}

// If the vote is for the last accepted block, the vote is dropped
if votedBlock.Accepted() {
if votedBlock.Decided() {
continue
}

Expand All @@ -373,7 +378,7 @@ func (ts *Topological) calculateInDegree(votes bag.Bag[ids.ID]) {

// iterate through all the block's ancestors and set up the inDegrees of
// the blocks
for n := ts.blocks[parentID]; !n.Accepted(); n = ts.blocks[parentID] {
for n := ts.blocks[parentID]; !n.Decided(); n = ts.blocks[parentID] {
parentID = n.blk.Parent()

// Increase the inDegree by one
Expand Down Expand Up @@ -417,7 +422,7 @@ func (ts *Topological) pushVotes() []votes {

// If the block is accepted, then we don't need to push votes to the
// parent block
if block.Accepted() {
if block.Decided() {
continue
}

Expand Down

0 comments on commit 2e72c7c

Please sign in to comment.