Skip to content

Commit 1d24203

Browse files
committed
rlp: add support for boolean encoding/decoding
1 parent 0dd6911 commit 1d24203

4 files changed

Lines changed: 53 additions & 0 deletions

File tree

‎rlp/decode.go‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ func makeDecoder(typ reflect.Type, tags tags) (dec decoder, err error) {
183183
return decodeBigIntNoPtr, nil
184184
case isUint(kind):
185185
return decodeUint, nil
186+
case kind == reflect.Bool:
187+
return decodeBool, nil
186188
case kind == reflect.String:
187189
return decodeString, nil
188190
case kind == reflect.Slice || kind == reflect.Array:
@@ -211,6 +213,15 @@ func decodeUint(s *Stream, val reflect.Value) error {
211213
return nil
212214
}
213215

216+
func decodeBool(s *Stream, val reflect.Value) error {
217+
b, err := s.Bool()
218+
if err != nil {
219+
return wrapStreamError(err, val.Type())
220+
}
221+
val.SetBool(b)
222+
return nil
223+
}
224+
214225
func decodeString(s *Stream, val reflect.Value) error {
215226
b, err := s.Bytes()
216227
if err != nil {
@@ -697,6 +708,24 @@ func (s *Stream) uint(maxbits int) (uint64, error) {
697708
}
698709
}
699710

711+
// Bool reads an RLP string of up to 1 byte and returns its contents
712+
// as an boolean. If the input does not contain an RLP string, the
713+
// returned error will be ErrExpectedString.
714+
func (s *Stream) Bool() (bool, error) {
715+
num, err := s.uint(8)
716+
if err != nil {
717+
return false, err
718+
}
719+
switch num {
720+
case 0:
721+
return false, nil
722+
case 1:
723+
return true, nil
724+
default:
725+
return false, fmt.Errorf("rlp: invalid boolean value: %d", num)
726+
}
727+
}
728+
700729
// List starts decoding an RLP list. If the input does not contain a
701730
// list, the returned error will be ErrExpectedList. When the list's
702731
// end has been reached, any Stream operation will return EOL.

‎rlp/decode_test.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package rlp
1919
import (
2020
"bytes"
2121
"encoding/hex"
22+
"errors"
2223
"fmt"
2324
"io"
2425
"math/big"
@@ -116,6 +117,9 @@ func TestStreamErrors(t *testing.T) {
116117
{"817F", calls{"Uint"}, nil, ErrCanonSize},
117118
{"8180", calls{"Uint"}, nil, nil},
118119

120+
// Non-valid boolean
121+
{"02", calls{"Bool"}, nil, errors.New("rlp: invalid boolean value: 2")},
122+
119123
// Size tags must use the smallest possible encoding.
120124
// Leading zero bytes in the size tag are also rejected.
121125
{"8100", calls{"Uint"}, nil, ErrCanonSize},
@@ -315,6 +319,11 @@ var (
315319
)
316320

317321
var decodeTests = []decodeTest{
322+
// booleans
323+
{input: "01", ptr: new(bool), value: true},
324+
{input: "80", ptr: new(bool), value: false},
325+
{input: "02", ptr: new(bool), error: "rlp: invalid boolean value: 2"},
326+
318327
// integers
319328
{input: "05", ptr: new(uint32), value: uint32(5)},
320329
{input: "80", ptr: new(uint32), value: uint32(0)},

‎rlp/encode.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ func makeWriter(typ reflect.Type) (writer, error) {
361361
return writeBigIntNoPtr, nil
362362
case isUint(kind):
363363
return writeUint, nil
364+
case kind == reflect.Bool:
365+
return writeBool, nil
364366
case kind == reflect.String:
365367
return writeString, nil
366368
case kind == reflect.Slice && isByte(typ.Elem()):
@@ -398,6 +400,15 @@ func writeUint(val reflect.Value, w *encbuf) error {
398400
return nil
399401
}
400402

403+
func writeBool(val reflect.Value, w *encbuf) error {
404+
if val.Bool() {
405+
w.str = append(w.str, 0x01)
406+
} else {
407+
w.str = append(w.str, 0x80)
408+
}
409+
return nil
410+
}
411+
401412
func writeBigIntPtr(val reflect.Value, w *encbuf) error {
402413
ptr := val.Interface().(*big.Int)
403414
if ptr == nil {

‎rlp/encode_test.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ type encTest struct {
7171
}
7272

7373
var encTests = []encTest{
74+
// booleans
75+
{val: true, output: "01"},
76+
{val: false, output: "80"},
77+
7478
// integers
7579
{val: uint32(0), output: "80"},
7680
{val: uint32(127), output: "7F"},

0 commit comments

Comments
 (0)