From fc07555ad3a527ab1ee601fe91a90c50f27871f6 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Fri, 11 Apr 2014 09:16:58 -0500 Subject: [PATCH] Update for latest btcjson API changes. This commit modifies the AllVerboseTxNtfn command to use a parser along with the standard btcjson.RegisterCustomCmd instead of the now removed RegisterCustomCmdGenerator function. This is possible due to the recent changes that defers the unmarshalling of the parameters of a RawCmd to the parser. --- notifications.go | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/notifications.go b/notifications.go index 8af72cef5..902e48d7f 100644 --- a/notifications.go +++ b/notifications.go @@ -85,8 +85,8 @@ func init() { parseWalletLockStateNtfn, `TODO(jrick) fillmein`) btcjson.RegisterCustomCmd(AllTxNtfnMethod, parseAllTxNtfn, `TODO(flam) fillmein`) - btcjson.RegisterCustomCmdGenerator(AllVerboseTxNtfnMethod, - generateAllVerboseTxNtfn) + btcjson.RegisterCustomCmd(AllVerboseTxNtfnMethod, + parseAllVerboseTxNtfn, `TODO(flam) fillmein`) } // BlockDetails describes details of a tx in a block. @@ -1099,6 +1099,26 @@ func NewAllVerboseTxNtfn(rawTx *btcjson.TxRawResult) *AllVerboseTxNtfn { } } +// parseAllVerboseTxNtfn parses a RawCmd into a concrete type satisifying +// the btcjson.Cmd interface. This is used when registering the notification +// with the btcjson parser. +func parseAllVerboseTxNtfn(r *btcjson.RawCmd) (btcjson.Cmd, error) { + if r.Id != nil { + return nil, ErrNotANtfn + } + + if len(r.Params) != 1 { + return nil, btcjson.ErrWrongNumberOfParams + } + + var rawTx *btcjson.TxRawResult + if err := json.Unmarshal(r.Params[0], &rawTx); err != nil { + return nil, err + } + + return NewAllVerboseTxNtfn(rawTx), nil +} + // Id satisifies the btcjson.Cmd interface by returning nil for a // notification ID. func (n *AllVerboseTxNtfn) Id() interface{} { @@ -1130,10 +1150,6 @@ func (n *AllVerboseTxNtfn) MarshalJSON() ([]byte, error) { return json.Marshal(raw) } -func generateAllVerboseTxNtfn() btcjson.Cmd { - return new(AllVerboseTxNtfn) -} - // UnmarshalJSON unmarshals the JSON encoding of n into n. Part of // the btcjson.Cmd interface. func (n *AllVerboseTxNtfn) UnmarshalJSON(b []byte) error { @@ -1142,19 +1158,15 @@ func (n *AllVerboseTxNtfn) UnmarshalJSON(b []byte) error { return err } - if r.Id != nil { - return ErrNotANtfn - } - - if len(r.Params) != 1 { - return btcjson.ErrWrongNumberOfParams - } - - var rawTx *btcjson.TxRawResult - if err := json.Unmarshal(r.Params[0], &rawTx); err != nil { + newNtfn, err := parseAllVerboseTxNtfn(&r) + if err != nil { return err } - *n = *NewAllVerboseTxNtfn(rawTx) + concreteNtfn, ok := newNtfn.(*AllVerboseTxNtfn) + if !ok { + return btcjson.ErrInternal + } + *n = *concreteNtfn return nil }