Allow more BIP0022 fields in GetBlockTemplateCmd.

BIP0022 defines optional fields in a getblocktemplate request for long
polling and template tweaking.

In addition, for template tweaking, there are two fields, sigoplimit and
sizelimit, which are atypical in that they are allowed to be either
booleans or numeric.  This requires the fields to be represented as
interfaces which means any code making use of the struct will need to use
type assertions or a type switch.

This commit updates GetBlockTemplateCmd accordingly.

ok @jcvernaleo
This commit is contained in:
Dave Collins 2014-06-16 22:07:51 -05:00
parent dc84f95fe9
commit 5290cb1186

View file

@ -2173,6 +2173,21 @@ func (cmd *GetBlockHashCmd) UnmarshalJSON(b []byte) error {
type TemplateRequest struct {
Mode string `json:"mode,omitempty"`
Capabilities []string `json:"capabilities,omitempty"`
// Optional long polling.
LongPollID string `json:"longpollid,omitempty"`
// Optional template tweaking. SigOpLimit and SizeLimit can be int64
// or bool.
SigOpLimit interface{} `json:"sigoplimit,omitempty"`
SizeLimit interface{} `json:"sizelimit,omitempty"`
MaxVersion uint32 `json:"maxversion,omitempty"`
}
// isFloatInt64 returns whether the passed float64 is a whole number that safely
// fits into a 64-bit integer.
func isFloatInt64(a float64) bool {
return a == float64(int64(a))
}
// GetBlockTemplateCmd is a type handling custom marshaling and
@ -2194,6 +2209,38 @@ func NewGetBlockTemplateCmd(id interface{}, optArgs ...*TemplateRequest) (*GetBl
return nil, ErrTooManyOptArgs
}
request = optArgs[0]
switch val := request.SigOpLimit.(type) {
case nil:
case bool:
case int64:
case float64:
if !isFloatInt64(val) {
return nil, errors.New("the sigoplimit field " +
"must be unspecified, a boolean, or a " +
"64-bit integer")
}
request.SigOpLimit = int64(val)
default:
return nil, errors.New("the sigoplimit field " +
"must be unspecified, a boolean, or a 64-bit " +
"integer")
}
switch val := request.SizeLimit.(type) {
case nil:
case bool:
case int64:
case float64:
if !isFloatInt64(val) {
return nil, errors.New("the sizelimit field " +
"must be unspecified, a boolean, or a " +
"64-bit integer")
}
request.SizeLimit = int64(val)
default:
return nil, errors.New("the sizelimit field " +
"must be unspecified, a boolean, or a 64-bit " +
"integer")
}
}
return &GetBlockTemplateCmd{
id: id,