Use hashes for rescan, add hash+time to ntfns.

ok @davecgh
This commit is contained in:
Josh Rickmar 2014-07-16 20:35:11 -05:00
parent 1acb4b8151
commit de236d5395
4 changed files with 81 additions and 43 deletions

25
cmds.go
View file

@ -8,7 +8,6 @@ import (
"encoding/json"
"errors"
"github.com/conformal/btcdb"
"github.com/conformal/btcjson"
"github.com/conformal/btcwire"
)
@ -633,10 +632,10 @@ func NewOutPointFromWire(op *btcwire.OutPoint) *OutPoint {
// commands.
type RescanCmd struct {
id interface{}
BeginBlock int32
BeginBlock string
Addresses []string
OutPoints []OutPoint
EndBlock int64 // TODO: switch this and btcdb.AllShas to int32
EndBlock string
}
// Enforce that RescanCmd satisifies the btcjson.Cmd interface.
@ -644,12 +643,12 @@ var _ btcjson.Cmd = &RescanCmd{}
// NewRescanCmd creates a new RescanCmd, parsing the optional
// arguments optArgs which may either be empty or a single upper
// block height.
func NewRescanCmd(id interface{}, begin int32, addresses []string,
outpoints []OutPoint, optArgs ...int64) (*RescanCmd, error) {
// block hash.
func NewRescanCmd(id interface{}, begin string, addresses []string,
outpoints []OutPoint, optArgs ...string) (*RescanCmd, error) {
// Optional parameters set to their defaults.
end := btcdb.AllShas
var end string
if len(optArgs) > 0 {
if len(optArgs) > 1 {
@ -675,10 +674,10 @@ func parseRescanCmd(r *btcjson.RawCmd) (btcjson.Cmd, error) {
return nil, btcjson.ErrWrongNumberOfParams
}
var begin int32
var begin string
if err := json.Unmarshal(r.Params[0], &begin); err != nil {
return nil, errors.New("first parameter 'begin' must be a " +
"32-bit integer: " + err.Error())
"string: " + err.Error())
}
var addresses []string
@ -694,12 +693,12 @@ func parseRescanCmd(r *btcjson.RawCmd) (btcjson.Cmd, error) {
err.Error())
}
optArgs := make([]int64, 0, 1)
optArgs := make([]string, 0, 1)
if len(r.Params) > 3 {
var endblock int64
var endblock string
if err := json.Unmarshal(r.Params[3], &endblock); err != nil {
return nil, errors.New("fourth optional parameter " +
" 'endblock' must be an integer: " + err.Error())
"'endblock' must be a string: " + err.Error())
}
optArgs = append(optArgs, endblock)
}
@ -723,7 +722,7 @@ func (cmd *RescanCmd) MarshalJSON() ([]byte, error) {
params[0] = cmd.BeginBlock
params[1] = cmd.Addresses
params[2] = cmd.OutPoints
if cmd.EndBlock != btcdb.AllShas {
if cmd.EndBlock != "" {
params = append(params, cmd.EndBlock)
}

View file

@ -9,7 +9,6 @@ import (
"reflect"
"testing"
"github.com/conformal/btcdb"
"github.com/conformal/btcjson"
"github.com/davecgh/go-spew/spew"
)
@ -224,13 +223,13 @@ var cmdtests = []struct {
}
return NewRescanCmd(
float64(1),
270000,
"0000000000000002a775aec59dc6a9e4bb1c025cf1b8c2195dd9dc3998c827c5",
addrs,
ops)
},
result: &RescanCmd{
id: float64(1),
BeginBlock: 270000,
BeginBlock: "0000000000000002a775aec59dc6a9e4bb1c025cf1b8c2195dd9dc3998c827c5",
Addresses: []string{"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH"},
OutPoints: []OutPoint{
{
@ -240,7 +239,7 @@ var cmdtests = []struct {
Index: 1,
},
},
EndBlock: btcdb.AllShas,
EndBlock: "",
},
},
{
@ -257,14 +256,14 @@ var cmdtests = []struct {
}
return NewRescanCmd(
float64(1),
270000,
"0000000000000002a775aec59dc6a9e4bb1c025cf1b8c2195dd9dc3998c827c5",
addrs,
ops,
280000)
"0000000000000001c091ada69f444dc0282ecaabe4808ddbb2532e5555db0c03")
},
result: &RescanCmd{
id: float64(1),
BeginBlock: 270000,
BeginBlock: "0000000000000002a775aec59dc6a9e4bb1c025cf1b8c2195dd9dc3998c827c5",
Addresses: []string{"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH"},
OutPoints: []OutPoint{
{
@ -274,7 +273,7 @@ var cmdtests = []struct {
Index: 1,
},
},
EndBlock: 280000,
EndBlock: "0000000000000001c091ada69f444dc0282ecaabe4808ddbb2532e5555db0c03",
},
},
{

View file

@ -679,15 +679,17 @@ func (n *RedeemingTxNtfn) UnmarshalJSON(b []byte) error {
// RescanFinishedNtfn is type handling custom marshaling and
// unmarshaling of rescanfinished JSON websocket notifications.
type RescanFinishedNtfn struct {
LastProcessed int32
Hash string
Height int32
Time int64
}
// Enforce that RescanFinishedNtfn satisifies the btcjson.Cmd interface.
var _ btcjson.Cmd = &RescanFinishedNtfn{}
// NewRescanFinishedNtfn creates a new RescanFinshedNtfn.
func NewRescanFinishedNtfn(last int32) *RescanFinishedNtfn {
return &RescanFinishedNtfn{last}
func NewRescanFinishedNtfn(hash string, height int32, time int64) *RescanFinishedNtfn {
return &RescanFinishedNtfn{hash, height, time}
}
// parseRescanFinishedNtfn parses a RawCmd into a concrete type satisifying
@ -698,17 +700,29 @@ func parseRescanFinishedNtfn(r *btcjson.RawCmd) (btcjson.Cmd, error) {
return nil, ErrNotANtfn
}
if len(r.Params) != 1 {
if len(r.Params) != 3 {
return nil, btcjson.ErrWrongNumberOfParams
}
var last int32
if err := json.Unmarshal(r.Params[0], &last); err != nil {
return nil, errors.New("first parameter 'last' must be a " +
var hash string
if err := json.Unmarshal(r.Params[0], &hash); err != nil {
return nil, errors.New("first parameter 'hash' must be a " +
"string: " + err.Error())
}
var height int32
if err := json.Unmarshal(r.Params[1], &height); err != nil {
return nil, errors.New("second parameter 'height' must be a " +
"32-bit integer: " + err.Error())
}
return NewRescanFinishedNtfn(last), nil
var time int64
if err := json.Unmarshal(r.Params[2], &time); err != nil {
return nil, errors.New("third parameter 'time' must be a " +
"64-bit integer: " + err.Error())
}
return NewRescanFinishedNtfn(hash, height, time), nil
}
// Id satisifies the btcjson.Cmd interface by returning nil for a
@ -727,7 +741,9 @@ func (n *RescanFinishedNtfn) Method() string {
// interface.
func (n *RescanFinishedNtfn) MarshalJSON() ([]byte, error) {
params := []interface{}{
n.LastProcessed,
n.Hash,
n.Height,
n.Time,
}
// No ID for notifications.
@ -763,15 +779,17 @@ func (n *RescanFinishedNtfn) UnmarshalJSON(b []byte) error {
// RescanProgressNtfn is type handling custom marshaling and
// unmarshaling of rescanprogress JSON websocket notifications.
type RescanProgressNtfn struct {
LastProcessed int32
Hash string
Height int32
Time int64
}
// Enforce that RescanProgressNtfn satisifies the btcjson.Cmd interface.
var _ btcjson.Cmd = &RescanProgressNtfn{}
// NewRescanProgressNtfn creates a new RescanProgressNtfn.
func NewRescanProgressNtfn(last int32) *RescanProgressNtfn {
return &RescanProgressNtfn{last}
func NewRescanProgressNtfn(hash string, height int32, time int64) *RescanProgressNtfn {
return &RescanProgressNtfn{hash, height, time}
}
// parseRescanProgressNtfn parses a RawCmd into a concrete type satisifying
@ -782,17 +800,29 @@ func parseRescanProgressNtfn(r *btcjson.RawCmd) (btcjson.Cmd, error) {
return nil, ErrNotANtfn
}
if len(r.Params) != 1 {
if len(r.Params) != 3 {
return nil, btcjson.ErrWrongNumberOfParams
}
var last int32
if err := json.Unmarshal(r.Params[0], &last); err != nil {
return nil, errors.New("first parameter 'last' must be a " +
var hash string
if err := json.Unmarshal(r.Params[0], &hash); err != nil {
return nil, errors.New("first parameter 'hash' must be a " +
"string: " + err.Error())
}
var height int32
if err := json.Unmarshal(r.Params[1], &height); err != nil {
return nil, errors.New("second parameter 'height' must be a " +
"32-bit integer: " + err.Error())
}
return NewRescanProgressNtfn(last), nil
var time int64
if err := json.Unmarshal(r.Params[2], &time); err != nil {
return nil, errors.New("third parameter 'time' must be a " +
"64-bit integer: " + err.Error())
}
return NewRescanProgressNtfn(hash, height, time), nil
}
// Id satisifies the btcjson.Cmd interface by returning nil for a
@ -811,7 +841,9 @@ func (n *RescanProgressNtfn) Method() string {
// interface.
func (n *RescanProgressNtfn) MarshalJSON() ([]byte, error) {
params := []interface{}{
n.LastProcessed,
n.Hash,
n.Height,
n.Time,
}
// No ID for notifications.

View file

@ -127,19 +127,27 @@ var ntfntests = []struct {
{
name: "rescanfinished",
f: func() btcjson.Cmd {
return btcws.NewRescanFinishedNtfn(12345)
return btcws.NewRescanFinishedNtfn(
"00000000b8980ec1fe96bc1b4425788ddc88dd36699521a448ebca2020b38699",
12345, 1240784732)
},
result: &btcws.RescanFinishedNtfn{
LastProcessed: 12345,
Hash: "00000000b8980ec1fe96bc1b4425788ddc88dd36699521a448ebca2020b38699",
Height: 12345,
Time: 1240784732,
},
},
{
name: "rescanprogress",
f: func() btcjson.Cmd {
return btcws.NewRescanProgressNtfn(12345)
return btcws.NewRescanProgressNtfn(
"00000000b8980ec1fe96bc1b4425788ddc88dd36699521a448ebca2020b38699",
12345, 1240784732)
},
result: &btcws.RescanProgressNtfn{
LastProcessed: 12345,
Hash: "00000000b8980ec1fe96bc1b4425788ddc88dd36699521a448ebca2020b38699",
Height: 12345,
Time: 1240784732,
},
},
{