Skip to content

Commit

Permalink
multi: update blockchain and mempool error types.
Browse files Browse the repository at this point in the history
This updates the blockchain and mempool error types to
leverage go 1.13 errors.Is/As functionality as well as confirm to
the error infrastructure best practices.
  • Loading branch information
dnldd committed Oct 14, 2020
1 parent 2b1dbb4 commit 44ae570
Show file tree
Hide file tree
Showing 17 changed files with 699 additions and 537 deletions.
11 changes: 6 additions & 5 deletions blockchain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1260,21 +1260,22 @@ func (b *BlockChain) forceHeadReorganization(formerBest chainhash.Hash, newBest
// We can't reorganize the chain unless our head block matches up with
// b.bestChain.
if !formerBestNode.hash.IsEqual(&formerBest) {
return ruleError(ErrForceReorgWrongChain, "tried to force reorg "+
"on wrong chain")
str := "tried to force reorg on wrong chain"
return ruleError(ErrForceReorgWrongChain, str)
}

// Child to reorganize to is missing.
newBestNode := b.index.LookupNode(&newBest)
if newBestNode == nil || newBestNode.parent != formerBestNode.parent {
return ruleError(ErrForceReorgMissingChild, "missing child of "+
"common parent for forced reorg")
str := "missing child of common parent for forced reorg"
return ruleError(ErrForceReorgMissingChild, str)
}

// Don't allow a reorganize to a known invalid chain.
newBestNodeStatus := b.index.NodeStatus(newBestNode)
if newBestNodeStatus.KnownInvalid() {
return ruleError(ErrKnownInvalidBlock, "block is known to be invalid")
str := "block is known to be invalid"
return ruleError(ErrKnownInvalidBlock, str)
}

// Reorganize the chain and flush any potential unsaved changes to the
Expand Down
22 changes: 6 additions & 16 deletions blockchain/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ func TestForceHeadReorg(t *testing.T) {
//
// rejectForceTipReorg forces the chain instance to reorganize the
// current tip of the main chain from the given block to the given
// block and expected it to be rejected with the provided error code.
rejectForceTipReorg := func(fromTipName, toTipName string, code ErrorCode) {
// block and expected it to be rejected with the provided error kind.
rejectForceTipReorg := func(fromTipName, toTipName string, kind ErrorKind) {
from := g.BlockByName(fromTipName)
to := g.BlockByName(toTipName)
t.Logf("Testing forced reorg from %s (hash %s, height %d) "+
Expand All @@ -175,25 +175,15 @@ func TestForceHeadReorg(t *testing.T) {
to.Header.Height)
}

// Ensure the error code is of the expected type and the reject
// code matches the value specified in the test instance.
var rerr RuleError
if !errors.As(err, &rerr) {
t.Fatalf("forced header reorg from block %q (hash %s, "+
"height %d) to block %q (hash %s, height %d) "+
"returned unexpected error type -- got %T, "+
"want blockchain.RuleError", fromTipName,
from.BlockHash(), from.Header.Height, toTipName,
to.BlockHash(), to.Header.Height, err)
}
if rerr.ErrorCode != code {
// Ensure the error kind is of the expected type and matches
// the value specified in the test instance.
if !errors.Is(err, kind) {
t.Fatalf("forced header reorg from block %q (hash %s, "+
"height %d) to block %q (hash %s, height %d) "+
"does not have expected reject code -- got %v, "+
"want %v", fromTipName,
from.BlockHash(), from.Header.Height, toTipName,
to.BlockHash(), to.Header.Height, rerr.ErrorCode,
code)
to.BlockHash(), to.Header.Height, err, kind)
}
}

Expand Down
20 changes: 7 additions & 13 deletions blockchain/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,8 @@ func (g *chaingenHarness) AcceptTipBlock() {
}

// 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) {
// generator to be rejected with the provided error kind.
func (g *chaingenHarness) RejectBlock(blockName string, kind ErrorKind) {
g.t.Helper()

msgBlock := g.BlockByName(blockName)
Expand All @@ -548,27 +548,21 @@ func (g *chaingenHarness) RejectBlock(blockName string, code ErrorCode) {
blockName, block.Hash(), blockHeight)
}

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

// 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) {
func (g *chaingenHarness) RejectTipBlock(kind ErrorKind) {
g.t.Helper()

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

// ExpectTip expects the provided block to be the current tip of the main chain
Expand Down
Loading

0 comments on commit 44ae570

Please sign in to comment.