Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159 from halseth/filter-header-checkpoints
Check filter header checkpoints against hardcoded list
- Loading branch information
Showing
3 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package chainsync | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/btcsuite/btcd/chaincfg" | ||
"github.com/btcsuite/btcd/chaincfg/chainhash" | ||
"github.com/btcsuite/btcd/wire" | ||
) | ||
|
||
// ErrCheckpointMismatch is returned if given filter headers don't pass our | ||
// control check. | ||
var ErrCheckpointMismatch = fmt.Errorf("checkpoint doesn't match") | ||
|
||
// filterHeaderCheckpoints holds a mapping from heights to filter headers for | ||
// various heights. We use them to check whether peers are serving us the | ||
// expected filter headers. | ||
var filterHeaderCheckpoints = map[wire.BitcoinNet]map[uint32]*chainhash.Hash{ | ||
// Mainnet filter header checkpoints. | ||
chaincfg.MainNetParams.Net: map[uint32]*chainhash.Hash{}, | ||
|
||
// Testnet filter header checkpoints. | ||
chaincfg.TestNet3Params.Net: map[uint32]*chainhash.Hash{}, | ||
} | ||
|
||
// ControlCFHeader controls the given filter header against our list of | ||
// checkpoints. It returns ErrCheckpointMismatch if we have a checkpoint at the | ||
// given height, and it doesn't match. | ||
func ControlCFHeader(params chaincfg.Params, fType wire.FilterType, | ||
height uint32, filterHeader *chainhash.Hash) error { | ||
|
||
if fType != wire.GCSFilterRegular { | ||
return fmt.Errorf("unsupported filter type %v", fType) | ||
} | ||
|
||
control, ok := filterHeaderCheckpoints[params.Net] | ||
if !ok { | ||
return nil | ||
} | ||
|
||
hash, ok := control[height] | ||
if !ok { | ||
return nil | ||
} | ||
|
||
if *filterHeader != *hash { | ||
return ErrCheckpointMismatch | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func hashFromStr(hexStr string) *chainhash.Hash { | ||
hash, _ := chainhash.NewHashFromStr(hexStr) | ||
return hash | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package chainsync | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/btcsuite/btcd/chaincfg" | ||
"github.com/btcsuite/btcd/chaincfg/chainhash" | ||
"github.com/btcsuite/btcd/wire" | ||
) | ||
|
||
func TestControlCFHeader(t *testing.T) { | ||
t.Parallel() | ||
|
||
// We'll modify our backing list of checkpoints for this test. | ||
height := uint32(999) | ||
header := hashFromStr( | ||
"4a242283a406a7c089f671bb8df7671e5d5e9ba577cea1047d30a7f4919df193", | ||
) | ||
filterHeaderCheckpoints = map[wire.BitcoinNet]map[uint32]*chainhash.Hash{ | ||
chaincfg.MainNetParams.Net: map[uint32]*chainhash.Hash{ | ||
height: header, | ||
}, | ||
} | ||
|
||
// Expect the control at height to succeed. | ||
err := ControlCFHeader( | ||
chaincfg.MainNetParams, wire.GCSFilterRegular, height, header, | ||
) | ||
if err != nil { | ||
t.Fatalf("error checking height: %v", err) | ||
} | ||
|
||
// Pass an invalid header, this should return an error. | ||
header = hashFromStr( | ||
"000000000006a7c089f671bb8df7671e5d5e9ba577cea1047d30a7f4919df193", | ||
) | ||
err = ControlCFHeader( | ||
chaincfg.MainNetParams, wire.GCSFilterRegular, height, header, | ||
) | ||
if err != ErrCheckpointMismatch { | ||
t.Fatalf("expected ErrCheckpointMismatch, got %v", err) | ||
} | ||
|
||
// Finally, control an unknown height. This should also pass since we | ||
// don't have the checkpoint stored. | ||
err = ControlCFHeader( | ||
chaincfg.MainNetParams, wire.GCSFilterRegular, 99, header, | ||
) | ||
if err != nil { | ||
t.Fatalf("error checking height: %v", err) | ||
} | ||
} |