rpc: update for API and protobuf changes

This commit is contained in:
George Tankersley
2018-12-04 18:21:53 -05:00
parent dda5dffe6b
commit 38d61b509c
16 changed files with 209 additions and 190 deletions

View File

@@ -4,7 +4,7 @@ import (
"fmt"
"github.com/gtank/ctxd/parser/internal/bytestring"
"github.com/gtank/ctxd/proto"
"github.com/gtank/ctxd/rpc"
"github.com/pkg/errors"
)
@@ -25,11 +25,13 @@ func (b *block) GetTxCount() int {
return len(b.vtx)
}
// GetHash returns the block hash in big-endian display order.
func (b *block) GetHash() []byte {
return b.hdr.GetHash()
// GetDisplayHash returns the block hash in big-endian display order.
func (b *block) GetDisplayHash() []byte {
return b.hdr.GetDisplayHash()
}
// TODO: encode hash endianness in a type?
// getEncodableHash returns the block hash in little-endian wire order.
func (b *block) getEncodableHash() []byte {
return b.hdr.getEncodableHash()
@@ -66,14 +68,14 @@ func (b *block) GetHeight() int {
return int(blockHeight)
}
func (b *block) ToCompact() *proto.CompactBlock {
compactBlock := &proto.CompactBlock{
BlockID: &proto.BlockFilter{
BlockHeight: uint64(b.GetHeight()),
BlockHash: b.getEncodableHash(),
},
func (b *block) ToCompact() *rpc.CompactBlock {
compactBlock := &rpc.CompactBlock{
//TODO ProtoVersion: 1,
Height: uint64(b.GetHeight()),
Hash: b.getEncodableHash(),
//TODO Time: b.hdr.Time,
}
compactBlock.Vtx = make([]*proto.CompactTx, len(b.vtx))
compactBlock.Vtx = make([]*rpc.CompactTx, len(b.vtx))
for idx, tx := range b.vtx {
compactBlock.Vtx[idx] = tx.ToCompact(idx)
}

View File

@@ -150,8 +150,8 @@ func parseNBits(b []byte) *big.Int {
return new(big.Int).SetBytes(targetBytes)
}
// GetHash returns the bytes of a block hash in big-endian order.
func (hdr *blockHeader) GetHash() []byte {
// GetDisplayHash returns the bytes of a block hash in big-endian order.
func (hdr *blockHeader) GetDisplayHash() []byte {
if hdr.cachedHash != nil {
return hdr.cachedHash
}

View File

@@ -123,7 +123,7 @@ func TestBlockHeader(t *testing.T) {
break
}
hash := blockHeader.GetHash()
hash := blockHeader.GetDisplayHash()
// This is not necessarily true for anything but our current test cases.
for _, b := range hash[:4] {

View File

@@ -76,7 +76,7 @@ func TestCompactBlocks(t *testing.T) {
t.Errorf("incorrect block height in testnet block %d", test.BlockHeight)
continue
}
if hex.EncodeToString(block.GetHash()) != test.BlockHash {
if hex.EncodeToString(block.GetDisplayHash()) != test.BlockHash {
t.Errorf("incorrect block hash in testnet block %x", test.BlockHash)
continue
}
@@ -90,7 +90,7 @@ func TestCompactBlocks(t *testing.T) {
encodedCompact := hex.EncodeToString(marshaled)
if encodedCompact != test.Compact {
t.Errorf("wrong data for compact testnet block %d\nhave: %s\nwant: %s\n", test.BlockHeight, encodedCompact, test.Compact)
continue
break
}
}

File diff suppressed because one or more lines are too long

View File

@@ -4,7 +4,7 @@ import (
"crypto/sha256"
"github.com/gtank/ctxd/parser/internal/bytestring"
"github.com/gtank/ctxd/proto"
"github.com/gtank/ctxd/rpc"
"github.com/pkg/errors"
)
@@ -126,8 +126,8 @@ func (p *spend) ParseFromSlice(data []byte) ([]byte, error) {
return []byte(s), nil
}
func (p *spend) ToCompact() *proto.CompactSpend {
return &proto.CompactSpend{
func (p *spend) ToCompact() *rpc.CompactSpend {
return &rpc.CompactSpend{
Nf: p.nullifier,
}
}
@@ -173,8 +173,8 @@ func (p *output) ParseFromSlice(data []byte) ([]byte, error) {
return []byte(s), nil
}
func (p *output) ToCompact() *proto.CompactOutput {
return &proto.CompactOutput{
func (p *output) ToCompact() *rpc.CompactOutput {
return &rpc.CompactOutput{
Cmu: p.cmu,
Epk: p.ephemeralKey,
Ciphertext: p.encCiphertext[:52],
@@ -269,8 +269,8 @@ type transaction struct {
txId []byte
}
// GetHash returns the transaction hash in big-endian display order.
func (tx *transaction) GetHash() []byte {
// GetDisplayHash returns the transaction hash in big-endian display order.
func (tx *transaction) GetDisplayHash() []byte {
if tx.txId != nil {
return tx.txId
}
@@ -300,12 +300,13 @@ func (tx *transaction) HasSaplingTransactions() bool {
return tx.version >= 4 && (len(tx.shieldedSpends)+len(tx.shieldedOutputs)) > 0
}
func (tx *transaction) ToCompact(index int) *proto.CompactTx {
ctx := &proto.CompactTx{
Index: uint64(index), // index is contextual
Hash: tx.getEncodableHash(),
Spends: make([]*proto.CompactSpend, len(tx.shieldedSpends)),
Outputs: make([]*proto.CompactOutput, len(tx.shieldedOutputs)),
func (tx *transaction) ToCompact(index int) *rpc.CompactTx {
ctx := &rpc.CompactTx{
Index: uint64(index), // index is contextual
Hash: tx.getEncodableHash(),
//Fee: 0, // TODO: calculate fees
Spends: make([]*rpc.CompactSpend, len(tx.shieldedSpends)),
Outputs: make([]*rpc.CompactOutput, len(tx.shieldedOutputs)),
}
for i, spend := range tx.shieldedSpends {
ctx.Spends[i] = spend.ToCompact()