Skip to content

Commit b1bbe6e

Browse files
Merge pull request #303 from ava-labs/remove-rejected-blocks
removed rejected blocks from the block reqeusts
2 parents 6c0c931 + 9d34e9e commit b1bbe6e

File tree

3 files changed

+127
-1
lines changed

3 files changed

+127
-1
lines changed

snow/engine/common/requests.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
package common
55

66
import (
7+
"fmt"
8+
"strings"
9+
710
"github.com/ava-labs/avalanchego/ids"
811
)
912

@@ -88,3 +91,15 @@ func (r *Requests) Contains(containerID ids.ID) bool {
8891
_, ok := r.idToReq[containerID]
8992
return ok
9093
}
94+
95+
func (r Requests) String() string {
96+
sb := strings.Builder{}
97+
sb.WriteString(fmt.Sprintf("Requests: (Num Validators = %d)", len(r.reqsToID)))
98+
for vdr, reqs := range r.reqsToID {
99+
sb.WriteString(fmt.Sprintf("\n VDR[%s]: (Outstanding Requests %d)", vdr, len(reqs)))
100+
for reqID, containerID := range reqs {
101+
sb.WriteString(fmt.Sprintf("\n Request[%d]: %s", reqID, containerID))
102+
}
103+
}
104+
return sb.String()
105+
}

snow/engine/snowman/transitive.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,18 @@ func (t *Transitive) issueFrom(vdr ids.ShortID, blk snowman.Block) (bool, error)
474474
return false, nil
475475
}
476476
}
477-
return t.Consensus.Issued(blk), nil
477+
478+
// Remove any outstanding requests for this block
479+
t.blkReqs.RemoveAny(blkID)
480+
481+
issued := t.Consensus.Issued(blk)
482+
if issued {
483+
t.blocked.Abandon(blkID)
484+
}
485+
486+
// Tracks performance statistics
487+
t.numRequests.Set(float64(t.blkReqs.Len()))
488+
return issued, t.errs.Err
478489
}
479490

480491
// issueWithAncestors attempts to issue the branch ending with [blk] to consensus.

snow/engine/snowman/transitive_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,3 +2004,103 @@ func TestEngineBuildBlockLimit(t *testing.T) {
20042004
t.Fatalf("Should have sent a query to the peer")
20052005
}
20062006
}
2007+
2008+
func TestEngineReceiveNewRejectedBlock(t *testing.T) {
2009+
vdr, _, sender, vm, te, gBlk := setup(t)
2010+
2011+
acceptedBlk := &snowman.TestBlock{
2012+
TestDecidable: choices.TestDecidable{
2013+
IDV: ids.GenerateTestID(),
2014+
StatusV: choices.Processing,
2015+
},
2016+
ParentV: gBlk,
2017+
HeightV: 1,
2018+
BytesV: []byte{1},
2019+
}
2020+
rejectedBlk := &snowman.TestBlock{
2021+
TestDecidable: choices.TestDecidable{
2022+
IDV: ids.GenerateTestID(),
2023+
StatusV: choices.Unknown,
2024+
},
2025+
ParentV: gBlk,
2026+
HeightV: 1,
2027+
BytesV: []byte{2},
2028+
}
2029+
pendingBlk := &snowman.TestBlock{
2030+
TestDecidable: choices.TestDecidable{
2031+
IDV: ids.GenerateTestID(),
2032+
StatusV: choices.Processing,
2033+
},
2034+
ParentV: rejectedBlk,
2035+
HeightV: 2,
2036+
BytesV: []byte{3},
2037+
}
2038+
2039+
vm.ParseBlockF = func(b []byte) (snowman.Block, error) {
2040+
switch {
2041+
case bytes.Equal(b, acceptedBlk.Bytes()):
2042+
return acceptedBlk, nil
2043+
case bytes.Equal(b, rejectedBlk.Bytes()):
2044+
return rejectedBlk, nil
2045+
case bytes.Equal(b, pendingBlk.Bytes()):
2046+
return pendingBlk, nil
2047+
default:
2048+
t.Fatalf("Unknown block bytes")
2049+
return nil, nil
2050+
}
2051+
}
2052+
2053+
var (
2054+
asked bool
2055+
reqID uint32
2056+
)
2057+
sender.PushQueryF = func(_ ids.ShortSet, rID uint32, _ ids.ID, blkBytes []byte) {
2058+
asked = true
2059+
reqID = rID
2060+
}
2061+
2062+
if err := te.Put(vdr, 0, acceptedBlk.ID(), acceptedBlk.Bytes()); err != nil {
2063+
t.Fatal(err)
2064+
}
2065+
2066+
if !asked {
2067+
t.Fatalf("Didn't query for the new block")
2068+
}
2069+
2070+
vm.GetBlockF = func(blkID ids.ID) (snowman.Block, error) {
2071+
if blkID != acceptedBlk.ID() {
2072+
t.Fatalf("Wrong block requested")
2073+
}
2074+
return acceptedBlk, nil
2075+
}
2076+
2077+
if err := te.Chits(vdr, reqID, []ids.ID{acceptedBlk.ID()}); err != nil {
2078+
t.Fatal(err)
2079+
}
2080+
2081+
sender.PushQueryF = nil
2082+
asked = false
2083+
2084+
sender.GetF = func(_ ids.ShortID, rID uint32, _ ids.ID) {
2085+
asked = true
2086+
reqID = rID
2087+
}
2088+
2089+
if err := te.Put(vdr, 0, pendingBlk.ID(), pendingBlk.Bytes()); err != nil {
2090+
t.Fatal(err)
2091+
}
2092+
2093+
if !asked {
2094+
t.Fatalf("Didn't request the missing block")
2095+
}
2096+
2097+
rejectedBlk.StatusV = choices.Rejected
2098+
2099+
if err := te.Put(vdr, reqID, rejectedBlk.ID(), rejectedBlk.Bytes()); err != nil {
2100+
t.Fatal(err)
2101+
}
2102+
2103+
if te.blkReqs.Len() != 0 {
2104+
t.Fatalf("Should have finished all requests")
2105+
}
2106+
}

0 commit comments

Comments
 (0)