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

blockchain: Allow named blocks in chaingen harness. #1701

Merged
Merged
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
24 changes: 12 additions & 12 deletions blockchain/agendas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func TestFixedSequenceLocks(t *testing.T) {
enableSeqLocks(tx, 0)
})
g.SaveTipCoinbaseOuts()
g.Accepted()
g.AcceptTipBlock()

// ---------------------------------------------------------------------
// Create block that spends from an output created in the previous
Expand All @@ -396,7 +396,7 @@ func TestFixedSequenceLocks(t *testing.T) {
enableSeqLocks(tx, 0)
b.AddTransaction(tx)
})
g.Accepted()
g.AcceptTipBlock()

// ---------------------------------------------------------------------
// Create block that involves reorganize to a sequence lock spending
Expand All @@ -420,7 +420,7 @@ func TestFixedSequenceLocks(t *testing.T) {
b.AddTransaction(tx)
})
g.SaveTipCoinbaseOuts()
g.Accepted()
g.AcceptTipBlock()
g.ExpectTip("b2")

// ---------------------------------------------------------------------
Expand All @@ -435,7 +435,7 @@ func TestFixedSequenceLocks(t *testing.T) {
enableSeqLocks(b.STransactions[0], 0)
})
g.SaveTipCoinbaseOuts()
g.Accepted()
g.AcceptTipBlock()

// ---------------------------------------------------------------------
// Create block that involves a sequence lock on a ticket.
Expand All @@ -449,7 +449,7 @@ func TestFixedSequenceLocks(t *testing.T) {
enableSeqLocks(b.STransactions[5], 0)
})
g.SaveTipCoinbaseOuts()
g.Accepted()
g.AcceptTipBlock()

// ---------------------------------------------------------------------
// Create two blocks such that the tip block involves a sequence lock
Expand All @@ -467,7 +467,7 @@ func TestFixedSequenceLocks(t *testing.T) {
b.AddTransaction(tx)
})
g.SaveTipCoinbaseOuts()
g.Accepted()
g.AcceptTipBlock()

outs = g.OldestCoinbaseOuts()
g.NextBlock("b6", nil, outs[1:], replaceFixSeqLocksVersions,
Expand All @@ -478,7 +478,7 @@ func TestFixedSequenceLocks(t *testing.T) {
b.AddTransaction(tx)
})
g.SaveTipCoinbaseOuts()
g.Accepted()
g.AcceptTipBlock()

// ---------------------------------------------------------------------
// Create block that involves a sequence lock spending from a regular
Expand All @@ -498,7 +498,7 @@ func TestFixedSequenceLocks(t *testing.T) {
b.AddTransaction(tx)
})
g.SaveTipCoinbaseOuts()
g.Accepted()
g.AcceptTipBlock()

// ---------------------------------------------------------------------
// Create block that involves a sequence lock spending from a block
Expand All @@ -512,7 +512,7 @@ func TestFixedSequenceLocks(t *testing.T) {
outs = g.OldestCoinbaseOuts()
g.NextBlock("b8", nil, outs[1:], replaceFixSeqLocksVersions)
g.SaveTipCoinbaseOuts()
g.Accepted()
g.AcceptTipBlock()

outs = g.OldestCoinbaseOuts()
g.NextBlock("b9", nil, outs[1:], replaceFixSeqLocksVersions,
Expand All @@ -523,7 +523,7 @@ func TestFixedSequenceLocks(t *testing.T) {
b.AddTransaction(tx)
})
g.SaveTipCoinbaseOuts()
g.Accepted()
g.AcceptTipBlock()

// ---------------------------------------------------------------------
// Create two blocks such that the tip block involves a sequence lock
Expand All @@ -550,7 +550,7 @@ func TestFixedSequenceLocks(t *testing.T) {
b.AddTransaction(tx)
})
g.SaveTipCoinbaseOuts()
g.Accepted()
g.AcceptTipBlock()

outs = g.OldestCoinbaseOuts()
g.NextBlock("b11", nil, outs[1:], replaceFixSeqLocksVersions,
Expand All @@ -563,5 +563,5 @@ func TestFixedSequenceLocks(t *testing.T) {
b.AddTransaction(tx)
})
g.SaveTipCoinbaseOuts()
g.Accepted()
g.AcceptTipBlock()
}
10 changes: 5 additions & 5 deletions blockchain/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func TestForceHeadReorg(t *testing.T) {
blockName := fmt.Sprintf("bbm%d", i)
g.NextBlock(blockName, nil, outs[1:])
g.SaveTipCoinbaseOuts()
g.Accepted()
g.AcceptTipBlock()
}
g.AssertTipHeight(uint32(stakeValidationHeight) + uint32(coinbaseMaturity))

Expand All @@ -230,7 +230,7 @@ func TestForceHeadReorg(t *testing.T) {
//
// ... -> b1(0)
g.NextBlock("b1", outs[0], ticketOuts[0])
g.Accepted()
g.AcceptTipBlock()

// Create a fork from b1 with an invalid block due to committing to an
// invalid number of votes. Since verifying the header commitment is a
Expand All @@ -242,7 +242,7 @@ func TestForceHeadReorg(t *testing.T) {
g.NextBlock("b2bad0", outs[1], ticketOuts[1], func(b *wire.MsgBlock) {
b.Header.Voters++
})
g.Rejected(ErrTooManyVotes)
g.RejectTipBlock(ErrTooManyVotes)

// Create a fork from b1 with an invalid block due to committing to an
// invalid input amount. Since verifying the fraud proof necessarily
Expand All @@ -257,7 +257,7 @@ func TestForceHeadReorg(t *testing.T) {
g.NextBlock("b2bad1", outs[1], ticketOuts[1], func(b *wire.MsgBlock) {
b.Transactions[1].TxIn[0].ValueIn--
})
g.Rejected(ErrFraudAmountIn)
g.RejectTipBlock(ErrFraudAmountIn)

// Create some forks from b1. There should not be a reorg since b1 is
// the current tip and b2 is seen first.
Expand All @@ -270,7 +270,7 @@ func TestForceHeadReorg(t *testing.T) {
// \-> b2bad1(1)
g.SetTip("b1")
g.NextBlock("b2", outs[1], ticketOuts[1])
g.Accepted()
g.AcceptTipBlock()

g.SetTip("b1")
g.NextBlock("b3", outs[1], ticketOuts[1])
Expand Down
62 changes: 39 additions & 23 deletions blockchain/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,70 +307,86 @@ func newChaingenHarness(t *testing.T, params *chaincfg.Params, dbName string) (*
return &harness, teardownFunc
}

// Accepted processes the current tip block associated with the harness
// generator and expects it to be accepted to the main chain.
func (g *chaingenHarness) Accepted() {
// AcceptBlock processes the block associated with the given name in the
// harness generator and expects it to be accepted to the main chain.
func (g *chaingenHarness) AcceptBlock(blockName string) {
g.t.Helper()

msgBlock := g.Tip()
msgBlock := g.BlockByName(blockName)
blockHeight := msgBlock.Header.Height
block := dcrutil.NewBlock(msgBlock)
g.t.Logf("Testing block %s (hash %s, height %d)", g.TipName(), block.Hash(),
g.t.Logf("Testing block %s (hash %s, height %d)", blockName, block.Hash(),
blockHeight)

forkLen, isOrphan, err := g.chain.ProcessBlock(block, BFNone)
if err != nil {
g.t.Fatalf("block %q (hash %s, height %d) should have been accepted: %v",
g.TipName(), block.Hash(), blockHeight, err)
blockName, block.Hash(), blockHeight, err)
}

// Ensure the main chain and orphan flags match the values specified in the
// test.
isMainChain := !isOrphan && forkLen == 0
if !isMainChain {
g.t.Fatalf("block %q (hash %s, height %d) unexpected main chain flag "+
"-- got %v, want true", g.TipName(), block.Hash(), blockHeight,
"-- got %v, want true", blockName, block.Hash(), blockHeight,
isMainChain)
}
if isOrphan {
g.t.Fatalf("block %q (hash %s, height %d) unexpected orphan flag -- "+
"got %v, want false", g.TipName(), block.Hash(), blockHeight,
"got %v, want false", blockName, block.Hash(), blockHeight,
isOrphan)
}
}

// Rejected expects the current tip block associated with the harness generator
// to be rejected with the provided error code.
func (g *chaingenHarness) Rejected(code ErrorCode) {
// AcceptTipBlock processes the current tip block associated with the harness
// generator and expects it to be accepted to the main chain.
func (g *chaingenHarness) AcceptTipBlock() {
g.t.Helper()

msgBlock := g.Tip()
g.AcceptBlock(g.TipName())
}

// RejectBlock expects the block associated with the given name in the harness
// generator to be rejected with the provided error code.
func (g *chaingenHarness) RejectBlock(blockName string, code ErrorCode) {
g.t.Helper()

msgBlock := g.BlockByName(blockName)
blockHeight := msgBlock.Header.Height
block := dcrutil.NewBlock(msgBlock)
g.t.Logf("Testing block %s (hash %s, height %d)", g.TipName(), block.Hash(),
g.t.Logf("Testing block %s (hash %s, height %d)", blockName, block.Hash(),
blockHeight)

_, _, err := g.chain.ProcessBlock(block, BFNone)
if err == nil {
g.t.Fatalf("block %q (hash %s, height %d) should not have been accepted",
g.TipName(), block.Hash(), blockHeight)
blockName, block.Hash(), blockHeight)
}

// Ensure the error code is of the expected type and the reject code matches
// the value specified in the test instance.
rerr, ok := err.(RuleError)
if !ok {
g.t.Fatalf("block %q (hash %s, height %d) returned unexpected error "+
"type -- got %T, want blockchain.RuleError", g.TipName(),
"type -- got %T, want blockchain.RuleError", blockName,
block.Hash(), blockHeight, err)
}
if rerr.ErrorCode != code {
g.t.Fatalf("block %q (hash %s, height %d) does not have expected reject "+
"code -- got %v, want %v", g.TipName(), block.Hash(), blockHeight,
"code -- got %v, want %v", blockName, block.Hash(), blockHeight,
rerr.ErrorCode, code)
}
}

// RejectTipBlock expects the current tip block associated with the harness
// generator to be rejected with the provided error code.
func (g *chaingenHarness) RejectTipBlock(code ErrorCode) {
g.t.Helper()

g.RejectBlock(g.TipName(), code)
}

// ExpectTip expects the provided block to be the current tip of the main chain
// associated with the harness generator.
func (g *chaingenHarness) ExpectTip(tipName string) {
Expand Down Expand Up @@ -554,7 +570,7 @@ func (g *chaingenHarness) AdvanceToStakeValidationHeight() {
// genesis -> bfb
g.CreatePremineBlock("bfb", 0)
g.AssertTipHeight(1)
g.Accepted()
g.AcceptTipBlock()

// ---------------------------------------------------------------------
// Generate enough blocks to have mature coinbase outputs to work with.
Expand All @@ -566,7 +582,7 @@ func (g *chaingenHarness) AdvanceToStakeValidationHeight() {
blockName := fmt.Sprintf("bm%d", i)
g.NextBlock(blockName, nil, nil)
g.SaveTipCoinbaseOuts()
g.Accepted()
g.AcceptTipBlock()
}
g.AssertTipHeight(uint32(coinbaseMaturity) + 1)

Expand All @@ -586,7 +602,7 @@ func (g *chaingenHarness) AdvanceToStakeValidationHeight() {
blockName := fmt.Sprintf("bse%d", i)
g.NextBlock(blockName, nil, ticketOuts)
g.SaveTipCoinbaseOuts()
g.Accepted()
g.AcceptTipBlock()
}
g.AssertTipHeight(uint32(stakeEnabledHeight))

Expand Down Expand Up @@ -617,7 +633,7 @@ func (g *chaingenHarness) AdvanceToStakeValidationHeight() {
blockName := fmt.Sprintf("bsv%d", i)
g.NextBlock(blockName, nil, ticketOuts)
g.SaveTipCoinbaseOuts()
g.Accepted()
g.AcceptTipBlock()
}
g.AssertTipHeight(uint32(stakeValidationHeight))
}
Expand Down Expand Up @@ -681,7 +697,7 @@ func (g *chaingenHarness) AdvanceFromSVHToActiveAgenda(voteID string) {
chaingen.ReplaceBlockVersion(int32(deploymentVer)),
chaingen.ReplaceVoteVersions(deploymentVer))
g.SaveTipCoinbaseOuts()
g.Accepted()
g.AcceptTipBlock()
}
g.TestThresholdState(voteID, ThresholdStarted)

Expand All @@ -706,7 +722,7 @@ func (g *chaingenHarness) AdvanceFromSVHToActiveAgenda(voteID string) {
chaingen.ReplaceStakeVersion(deploymentVer),
chaingen.ReplaceVotes(vbPrevBlockValid|yesChoice.Bits, deploymentVer))
g.SaveTipCoinbaseOuts()
g.Accepted()
g.AcceptTipBlock()
}
g.AssertTipHeight(uint32(stakeValidationHeight + ruleChangeInterval*2 - 1))
g.AssertBlockVersion(int32(deploymentVer))
Expand Down Expand Up @@ -734,7 +750,7 @@ func (g *chaingenHarness) AdvanceFromSVHToActiveAgenda(voteID string) {
chaingen.ReplaceVoteVersions(deploymentVer),
)
g.SaveTipCoinbaseOuts()
g.Accepted()
g.AcceptTipBlock()
}
g.AssertTipHeight(uint32(stakeValidationHeight + ruleChangeInterval*3 - 1))
g.AssertBlockVersion(int32(deploymentVer))
Expand Down
Loading