aboutsummaryrefslogtreecommitdiffstats
path: root/whisper
diff options
context:
space:
mode:
authorBas van Kervel <basvankervel@gmail.com>2017-06-21 18:58:00 +0800
committerBas van Kervel <basvankervel@gmail.com>2017-06-21 18:58:00 +0800
commitc62d5422bb24cfa53ee480330f9d355f68c071a4 (patch)
tree4bfb3e6a2149e89509e999a60f3066fb3acf87f4 /whisper
parenta4e4c76cb30f34f8a06431802fdc3b9ba0643d47 (diff)
downloadgo-tangerine-c62d5422bb24cfa53ee480330f9d355f68c071a4.tar
go-tangerine-c62d5422bb24cfa53ee480330f9d355f68c071a4.tar.gz
go-tangerine-c62d5422bb24cfa53ee480330f9d355f68c071a4.tar.bz2
go-tangerine-c62d5422bb24cfa53ee480330f9d355f68c071a4.tar.lz
go-tangerine-c62d5422bb24cfa53ee480330f9d355f68c071a4.tar.xz
go-tangerine-c62d5422bb24cfa53ee480330f9d355f68c071a4.tar.zst
go-tangerine-c62d5422bb24cfa53ee480330f9d355f68c071a4.zip
whisper: use hexutil.UnmarshalFixedText for topic parsing
Diffstat (limited to 'whisper')
-rw-r--r--whisper/whisperv5/topic.go22
-rw-r--r--whisper/whisperv5/topic_test.go37
2 files changed, 26 insertions, 33 deletions
diff --git a/whisper/whisperv5/topic.go b/whisper/whisperv5/topic.go
index 5fea2f28b..d1996c460 100644
--- a/whisper/whisperv5/topic.go
+++ b/whisper/whisperv5/topic.go
@@ -19,9 +19,6 @@
package whisperv5
import (
- "encoding/json"
- "fmt"
-
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
)
@@ -47,19 +44,12 @@ func (topic *TopicType) String() string {
return string(common.ToHex(topic[:]))
}
-func (t *TopicType) MarshalJSON() ([]byte, error) {
- return json.Marshal(hexutil.Bytes(t[:]))
+// MarshalText returns the hex representation of t.
+func (t TopicType) MarshalText() ([]byte, error) {
+ return hexutil.Bytes(t[:]).MarshalText()
}
-// UnmarshalJSON parses a hex representation to a topic.
-func (t *TopicType) UnmarshalJSON(input []byte) error {
- var data hexutil.Bytes
- if err := json.Unmarshal(input, &data); err != nil {
- return err
- }
- if len(data) != TopicLength {
- return fmt.Errorf("unmarshalJSON failed: topic must be exactly %d bytes(%d)", TopicLength, len(input))
- }
- *t = BytesToTopic(data)
- return nil
+// UnmarshalText parses a hex representation to a topic.
+func (t *TopicType) UnmarshalText(input []byte) error {
+ return hexutil.UnmarshalFixedText("Topic", input, t[:])
}
diff --git a/whisper/whisperv5/topic_test.go b/whisper/whisperv5/topic_test.go
index 63593963b..54bbeaf85 100644
--- a/whisper/whisperv5/topic_test.go
+++ b/whisper/whisperv5/topic_test.go
@@ -16,7 +16,10 @@
package whisperv5
-import "testing"
+import (
+ "encoding/json"
+ "testing"
+)
var topicStringTests = []struct {
topic TopicType
@@ -53,15 +56,6 @@ var bytesToTopicTests = []struct {
{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: nil},
}
-func TestBytesToTopic(t *testing.T) {
- for i, tst := range bytesToTopicTests {
- top := BytesToTopic(tst.data)
- if top != tst.topic {
- t.Fatalf("failed test %d: have %v, want %v.", i, t, tst.topic)
- }
- }
-}
-
var unmarshalTestsGood = []struct {
topic TopicType
data []byte
@@ -94,14 +88,23 @@ var unmarshalTestsUgly = []struct {
{topic: TopicType{0x01, 0x00, 0x00, 0x00}, data: []byte(`"0x00000001"`)},
}
+func TestBytesToTopic(t *testing.T) {
+ for i, tst := range bytesToTopicTests {
+ top := BytesToTopic(tst.data)
+ if top != tst.topic {
+ t.Fatalf("failed test %d: have %v, want %v.", i, t, tst.topic)
+ }
+ }
+}
+
func TestUnmarshalTestsGood(t *testing.T) {
for i, tst := range unmarshalTestsGood {
var top TopicType
- err := top.UnmarshalJSON(tst.data)
+ err := json.Unmarshal(tst.data, &top)
if err != nil {
- t.Fatalf("failed test %d. input: %v. err: %v", i, tst.data, err)
+ t.Errorf("failed test %d. input: %v. err: %v", i, tst.data, err)
} else if top != tst.topic {
- t.Fatalf("failed test %d: have %v, want %v.", i, t, tst.topic)
+ t.Errorf("failed test %d: have %v, want %v.", i, t, tst.topic)
}
}
}
@@ -110,7 +113,7 @@ func TestUnmarshalTestsBad(t *testing.T) {
// in this test UnmarshalJSON() is supposed to fail
for i, tst := range unmarshalTestsBad {
var top TopicType
- err := top.UnmarshalJSON(tst.data)
+ err := json.Unmarshal(tst.data, &top)
if err == nil {
t.Fatalf("failed test %d. input: %v.", i, tst.data)
}
@@ -121,11 +124,11 @@ func TestUnmarshalTestsUgly(t *testing.T) {
// in this test UnmarshalJSON() is NOT supposed to fail, but result should be wrong
for i, tst := range unmarshalTestsUgly {
var top TopicType
- err := top.UnmarshalJSON(tst.data)
+ err := json.Unmarshal(tst.data, &top)
if err != nil {
- t.Fatalf("failed test %d. input: %v.", i, tst.data)
+ t.Errorf("failed test %d. input: %v.", i, tst.data)
} else if top == tst.topic {
- t.Fatalf("failed test %d: have %v, want %v.", i, top, tst.topic)
+ t.Errorf("failed test %d: have %v, want %v.", i, top, tst.topic)
}
}
}