parser: extract height from coinbase transaction
This commit is contained in:
parent
768e5242dc
commit
7736b2464b
|
@ -24,6 +24,28 @@ func (b *block) GetTxCount() int {
|
||||||
return len(b.vtx)
|
return len(b.vtx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 {
|
||||||
|
coinbaseScript := bytestring.String(b.vtx[0].transparentInputs[0].ScriptSig)
|
||||||
|
var heightByte byte
|
||||||
|
if ok := coinbaseScript.ReadByte(&heightByte); !ok {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
heightLen := int(heightByte)
|
||||||
|
var heightBytes = make([]byte, heightLen)
|
||||||
|
if ok := coinbaseScript.ReadBytes(&heightBytes, heightLen); !ok {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
// uint32 should last us a while (Nov 2018)
|
||||||
|
var blockHeight uint32
|
||||||
|
for i := heightLen - 1; i >= 0; i-- {
|
||||||
|
blockHeight <<= 8
|
||||||
|
blockHeight = blockHeight | uint32(heightBytes[i])
|
||||||
|
}
|
||||||
|
return int(blockHeight)
|
||||||
|
}
|
||||||
|
|
||||||
func (b *block) ParseFromSlice(data []byte) (rest []byte, err error) {
|
func (b *block) ParseFromSlice(data []byte) (rest []byte, err error) {
|
||||||
hdr := NewBlockHeader()
|
hdr := NewBlockHeader()
|
||||||
data, err = hdr.ParseFromSlice(data)
|
data, err = hdr.ParseFromSlice(data)
|
||||||
|
|
Loading…
Reference in New Issue