From e2f1ca99fdb2e7e57e76e596b4d396a0864b7395 Mon Sep 17 00:00:00 2001 From: Stephen Buttolph Date: Mon, 17 Jun 2024 18:19:29 -0400 Subject: [PATCH 1/2] Remove .Status() from .Accepted() --- snow/consensus/snowman/snowman_block.go | 13 ++++--------- snow/consensus/snowman/topological.go | 14 +++++++------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/snow/consensus/snowman/snowman_block.go b/snow/consensus/snowman/snowman_block.go index 7e8d339d201..236c93645a0 100644 --- a/snow/consensus/snowman/snowman_block.go +++ b/snow/consensus/snowman/snowman_block.go @@ -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 @@ -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) @@ -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 } diff --git a/snow/consensus/snowman/topological.go b/snow/consensus/snowman/topological.go index 96f717f32ba..5eed65182c7 100644 --- a/snow/consensus/snowman/topological.go +++ b/snow/consensus/snowman/topological.go @@ -127,7 +127,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 @@ -163,8 +163,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 @@ -289,7 +289,7 @@ 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(); { + for block := startBlock; !block.Decided(); { blkID := block.blk.ID() ts.preferredIDs.Add(blkID) ts.preferredHeights[block.blk.Height()] = blkID @@ -360,7 +360,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 } @@ -384,7 +384,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 @@ -428,7 +428,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 } From 410a253f21e5749f23688aae61121810e7d7e19f Mon Sep 17 00:00:00 2001 From: Stephen Buttolph Date: Tue, 18 Jun 2024 11:19:02 -0400 Subject: [PATCH 2/2] add comment --- snow/consensus/snowman/topological.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/snow/consensus/snowman/topological.go b/snow/consensus/snowman/topological.go index 5eed65182c7..e27f6657998 100644 --- a/snow/consensus/snowman/topological.go +++ b/snow/consensus/snowman/topological.go @@ -289,6 +289,11 @@ 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. + // + // 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)