2018-09-27 17:52:40 -07:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-01-07 11:20:47 -08:00
|
|
|
"github.com/zcash-hackworks/lightwalletd/parser/internal/bytestring"
|
|
|
|
"github.com/zcash-hackworks/lightwalletd/walletrpc"
|
2018-09-27 17:52:40 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type block struct {
|
2018-12-14 18:54:33 -08:00
|
|
|
hdr *blockHeader
|
|
|
|
vtx []*Transaction
|
2018-12-14 18:54:59 -08:00
|
|
|
height int
|
2018-09-27 17:52:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewBlock() *block {
|
2018-12-14 18:54:59 -08:00
|
|
|
return &block{height: -1}
|
2018-09-27 17:52:40 -07:00
|
|
|
}
|
|
|
|
|
2018-09-28 17:44:34 -07:00
|
|
|
func (b *block) GetVersion() int {
|
|
|
|
return int(b.hdr.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *block) GetTxCount() int {
|
|
|
|
return len(b.vtx)
|
|
|
|
}
|
|
|
|
|
2018-12-14 18:54:33 -08:00
|
|
|
func (b *block) Transactions() []*Transaction {
|
|
|
|
// TODO: these should NOT be mutable
|
|
|
|
return b.vtx
|
|
|
|
}
|
|
|
|
|
2018-12-04 15:21:53 -08:00
|
|
|
// GetDisplayHash returns the block hash in big-endian display order.
|
|
|
|
func (b *block) GetDisplayHash() []byte {
|
|
|
|
return b.hdr.GetDisplayHash()
|
2018-11-16 16:37:31 -08:00
|
|
|
}
|
|
|
|
|
2018-12-04 15:21:53 -08:00
|
|
|
// TODO: encode hash endianness in a type?
|
|
|
|
|
2018-12-10 22:38:57 -08:00
|
|
|
// GetEncodableHash returns the block hash in little-endian wire order.
|
|
|
|
func (b *block) GetEncodableHash() []byte {
|
|
|
|
return b.hdr.GetEncodableHash()
|
2018-11-16 16:37:31 -08:00
|
|
|
}
|
|
|
|
|
2018-11-17 19:02:49 -08:00
|
|
|
func (b *block) HasSaplingTransactions() bool {
|
|
|
|
for _, tx := range b.vtx {
|
|
|
|
if tx.HasSaplingTransactions() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-02-25 10:57:06 -08:00
|
|
|
// see https://github.com/zcash-hackworks/lightwalletd/issues/17#issuecomment-467110828
|
|
|
|
const genesisTargetDifficulty = 520617983
|
|
|
|
|
2018-11-16 16:02:56 -08:00
|
|
|
// 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 {
|
2018-12-14 18:54:59 -08:00
|
|
|
if b.height != -1 {
|
|
|
|
return b.height
|
|
|
|
}
|
2018-11-16 16:02:56 -08:00
|
|
|
coinbaseScript := bytestring.String(b.vtx[0].transparentInputs[0].ScriptSig)
|
2019-05-04 03:37:00 -07:00
|
|
|
var heightNum int64
|
|
|
|
if ok := coinbaseScript.ReadScriptInt64(&heightNum); !ok {
|
2018-11-16 16:02:56 -08:00
|
|
|
return -1
|
|
|
|
}
|
2019-05-04 03:37:00 -07:00
|
|
|
if heightNum < 0 {
|
2018-11-16 16:02:56 -08:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
// uint32 should last us a while (Nov 2018)
|
2019-05-14 07:26:50 -07:00
|
|
|
if heightNum > int64(^uint32(0)) {
|
|
|
|
return -1
|
|
|
|
}
|
2019-05-04 03:37:00 -07:00
|
|
|
blockHeight := uint32(heightNum)
|
2019-02-25 10:57:06 -08:00
|
|
|
|
|
|
|
if blockHeight == genesisTargetDifficulty {
|
|
|
|
blockHeight = 0
|
|
|
|
}
|
|
|
|
|
2018-12-14 18:54:59 -08:00
|
|
|
b.height = int(blockHeight)
|
2018-11-16 16:02:56 -08:00
|
|
|
return int(blockHeight)
|
|
|
|
}
|
|
|
|
|
2019-01-07 11:20:47 -08:00
|
|
|
func (b *block) ToCompact() *walletrpc.CompactBlock {
|
|
|
|
compactBlock := &walletrpc.CompactBlock{
|
2018-12-04 15:21:53 -08:00
|
|
|
//TODO ProtoVersion: 1,
|
|
|
|
Height: uint64(b.GetHeight()),
|
2018-12-10 22:38:57 -08:00
|
|
|
Hash: b.GetEncodableHash(),
|
2019-05-04 04:18:42 -07:00
|
|
|
PrevHash: b.hdr.HashPrevBlock,
|
2018-12-17 15:46:50 -08:00
|
|
|
Time: b.hdr.Time,
|
2018-11-16 16:37:31 -08:00
|
|
|
}
|
2018-12-11 13:58:43 -08:00
|
|
|
|
|
|
|
// Only Sapling transactions have a meaningful compact encoding
|
2019-01-07 11:20:47 -08:00
|
|
|
saplingTxns := make([]*walletrpc.CompactTx, 0, len(b.vtx))
|
2018-11-16 16:37:31 -08:00
|
|
|
for idx, tx := range b.vtx {
|
2018-12-11 13:58:43 -08:00
|
|
|
if tx.HasSaplingTransactions() {
|
|
|
|
saplingTxns = append(saplingTxns, tx.ToCompact(idx))
|
|
|
|
}
|
2018-11-16 16:37:31 -08:00
|
|
|
}
|
2018-12-11 13:58:43 -08:00
|
|
|
compactBlock.Vtx = saplingTxns
|
2018-11-16 16:37:31 -08:00
|
|
|
return compactBlock
|
|
|
|
}
|
|
|
|
|
2018-09-27 17:52:40 -07:00
|
|
|
func (b *block) ParseFromSlice(data []byte) (rest []byte, err error) {
|
|
|
|
hdr := NewBlockHeader()
|
|
|
|
data, err = hdr.ParseFromSlice(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "parsing block header")
|
|
|
|
}
|
|
|
|
|
|
|
|
s := bytestring.String(data)
|
|
|
|
var txCount int
|
|
|
|
if ok := s.ReadCompactSize(&txCount); !ok {
|
|
|
|
return nil, errors.New("could not read tx_count")
|
|
|
|
}
|
|
|
|
data = []byte(s)
|
|
|
|
|
2018-12-14 18:54:33 -08:00
|
|
|
vtx := make([]*Transaction, 0, txCount)
|
2018-09-27 17:52:40 -07:00
|
|
|
for i := 0; len(data) > 0; i++ {
|
|
|
|
tx := NewTransaction()
|
|
|
|
data, err = tx.ParseFromSlice(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, fmt.Sprintf("parsing transaction %d", i))
|
|
|
|
}
|
|
|
|
vtx = append(vtx, tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.hdr = hdr
|
|
|
|
b.vtx = vtx
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|