parser: replace genesis "blockheight" with zero

This commit is contained in:
George Tankersley
2019-02-25 18:57:06 +00:00
committed by George Tankersley
parent 51614ecd2b
commit 6d28a409bc
3 changed files with 49 additions and 0 deletions

View File

@@ -52,6 +52,9 @@ func (b *block) HasSaplingTransactions() bool {
return false
}
// see https://github.com/zcash-hackworks/lightwalletd/issues/17#issuecomment-467110828
const genesisTargetDifficulty = 520617983
// GetHeight() extracts the block height from the coinbase transaction. See
// BIP34. Returns block height on success, or -1 on error.
func (b *block) GetHeight() int {
@@ -74,6 +77,11 @@ func (b *block) GetHeight() int {
blockHeight <<= 8
blockHeight = blockHeight | uint32(heightBytes[i])
}
if blockHeight == genesisTargetDifficulty {
blockHeight = 0
}
b.height = int(blockHeight)
return int(blockHeight)
}

View File

@@ -42,6 +42,46 @@ func TestBlockParser(t *testing.T) {
t.Error("Read wrong version in a test block.")
break
}
if block.GetTxCount() < 1 {
t.Error("No transactions in block")
break
}
}
}
func TestGenesisBlockParser(t *testing.T) {
blockFile, err := os.Open("../testdata/mainnet_genesis")
if err != nil {
t.Fatal(err)
}
defer blockFile.Close()
scan := bufio.NewScanner(blockFile)
for i := 0; scan.Scan(); i++ {
blockDataHex := scan.Text()
blockData, err := hex.DecodeString(blockDataHex)
if err != nil {
t.Error(err)
continue
}
block := NewBlock()
blockData, err = block.ParseFromSlice(blockData)
if err != nil {
t.Error(err)
continue
}
// Some basic sanity checks
if block.hdr.Version != 4 {
t.Error("Read wrong version in genesis block.")
break
}
if block.GetHeight() != 0 {
t.Errorf("Got wrong height for genesis block: %d", block.GetHeight())
}
}
}