[DEV-340] Make connectToDAG return RuleErrors (#144)

* [DEV-340] Make connectToDAG return RuleErrors

* [DEV-340] Add comment to make the error source clearer
This commit is contained in:
Ori Newman 2019-01-03 16:13:32 +02:00 committed by stasatdaglabs
parent 53e8e5a10a
commit 75a6d65075
2 changed files with 25 additions and 10 deletions

View File

@ -477,6 +477,11 @@ func (dag *BlockDAG) connectToDAG(node *blockNode, parentNodes blockSet, block *
writeErr)
}
// If dag.connectBlock returned a rule error, return it here after updating DB
if err != nil {
return err
}
return nil
}

View File

@ -960,21 +960,31 @@ func TestFinality(t *testing.T) {
// Here we check that a block with lower blue score than the last finality
// point will get rejected
nodeWithLowBlueScore, err := buildNodeToDag(setFromSlice(dag.genesis))
if err != nil {
t.Fatalf("TestFinality: buildNodeToDag unexpectedly returned an error: %v", err)
_, err = buildNodeToDag(setFromSlice(dag.genesis))
if err == nil {
t.Errorf("TestFinality: buildNodeToDag expected an error but got <nil>")
}
if !dag.index.NodeStatus(nodeWithLowBlueScore).KnownInvalid() {
t.Errorf("TestFinality: nodeWithLowBlueScore was expected to be invalid, but got valid instead")
rErr, ok := err.(RuleError)
if ok {
if rErr.ErrorCode != ErrFinality {
t.Errorf("TestFinality: buildNodeToDag expected an error with code %v but instead got %v", ErrFinality, rErr.ErrorCode)
}
} else {
t.Errorf("TestFinality: buildNodeToDag got unexpected error: %v", rErr)
}
// Here we check that a block that doesn't have the last finality point in
// its selected parent chain will get rejected
nodeWithFinalityPointInAnticone, err := buildNodeToDag(setFromSlice(altChainTip))
if err != nil {
t.Fatalf("TestFinality: buildNodeToDag unexpectedly returned an error: %v", err)
_, err = buildNodeToDag(setFromSlice(altChainTip))
if err == nil {
t.Errorf("TestFinality: buildNodeToDag expected an error but got <nil>")
}
if !dag.index.NodeStatus(nodeWithFinalityPointInAnticone).KnownInvalid() {
t.Errorf("TestFinality: invalidNode was expected to be invalid, but got valid instead")
rErr, ok = err.(RuleError)
if ok {
if rErr.ErrorCode != ErrFinality {
t.Errorf("TestFinality: buildNodeToDag expected an error with code %v but instead got %v", ErrFinality, rErr.ErrorCode)
}
} else {
t.Errorf("TestFinality: buildNodeToDag got unexpected error: %v", rErr)
}
}