Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
4af77436
Commit
4af77436
authored
Apr 14, 2015
by
Péter Szilágyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
whisper: add utility functions for creating topics
parent
cb707ba5
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
58 additions
and
8 deletions
+58
-8
message.go
whisper/message.go
+2
-7
topic.go
whisper/topic.go
+26
-0
topic_test.go
whisper/topic_test.go
+30
-1
No files found.
whisper/message.go
View file @
4af77436
...
...
@@ -30,7 +30,7 @@ type Options struct {
From
*
ecdsa
.
PrivateKey
To
*
ecdsa
.
PublicKey
TTL
time
.
Duration
Topics
[]
[]
byte
Topics
[]
Topic
}
// NewMessage creates and initializes a non-signed, non-encrypted Whisper message.
...
...
@@ -75,13 +75,8 @@ func (self *Message) Wrap(pow time.Duration, options Options) (*Envelope, error)
return
nil
,
err
}
}
// Convert the user topic into whisper ones
topics
:=
make
([]
Topic
,
len
(
options
.
Topics
))
for
i
,
topic
:=
range
options
.
Topics
{
topics
[
i
]
=
NewTopic
(
topic
)
}
// Wrap the processed message, seal it and return
envelope
:=
NewEnvelope
(
options
.
TTL
,
t
opics
,
self
)
envelope
:=
NewEnvelope
(
options
.
TTL
,
options
.
T
opics
,
self
)
envelope
.
Seal
(
pow
)
return
envelope
,
nil
...
...
whisper/topic.go
View file @
4af77436
...
...
@@ -17,6 +17,32 @@ func NewTopic(data []byte) Topic {
return
Topic
(
prefix
)
}
// NewTopics creates a list of topics from a list of binary data elements, by
// iteratively calling NewTopic on each of them.
func
NewTopics
(
data
...
[]
byte
)
[]
Topic
{
topics
:=
make
([]
Topic
,
len
(
data
))
for
i
,
element
:=
range
data
{
topics
[
i
]
=
NewTopic
(
element
)
}
return
topics
}
// NewTopicFromString creates a topic using the binary data contents of the
// specified string.
func
NewTopicFromString
(
data
string
)
Topic
{
return
NewTopic
([]
byte
(
data
))
}
// NewTopicsFromStrings creates a list of topics from a list of textual data
// elements, by iteratively calling NewTopicFromString on each of them.
func
NewTopicsFromStrings
(
data
...
string
)
[]
Topic
{
topics
:=
make
([]
Topic
,
len
(
data
))
for
i
,
element
:=
range
data
{
topics
[
i
]
=
NewTopicFromString
(
element
)
}
return
topics
}
// String converts a topic byte array to a string representation.
func
(
self
*
Topic
)
String
()
string
{
return
string
(
self
[
:
])
...
...
whisper/topic_test.go
View file @
4af77436
...
...
@@ -15,10 +15,39 @@ var topicCreationTests = []struct {
}
func
TestTopicCreation
(
t
*
testing
.
T
)
{
// Create the topics individually
for
i
,
tt
:=
range
topicCreationTests
{
topic
:=
NewTopic
(
tt
.
data
)
if
bytes
.
Compare
(
topic
[
:
],
tt
.
hash
[
:
])
!=
0
{
t
.
Errorf
(
"test %d: hash mismatch: have %v, want %v."
,
i
,
topic
,
tt
.
hash
)
t
.
Errorf
(
"binary test %d: hash mismatch: have %v, want %v."
,
i
,
topic
,
tt
.
hash
)
}
}
for
i
,
tt
:=
range
topicCreationTests
{
topic
:=
NewTopicFromString
(
string
(
tt
.
data
))
if
bytes
.
Compare
(
topic
[
:
],
tt
.
hash
[
:
])
!=
0
{
t
.
Errorf
(
"textual test %d: hash mismatch: have %v, want %v."
,
i
,
topic
,
tt
.
hash
)
}
}
// Create the topics in batches
binaryData
:=
make
([][]
byte
,
len
(
topicCreationTests
))
for
i
,
tt
:=
range
topicCreationTests
{
binaryData
[
i
]
=
tt
.
data
}
textualData
:=
make
([]
string
,
len
(
topicCreationTests
))
for
i
,
tt
:=
range
topicCreationTests
{
textualData
[
i
]
=
string
(
tt
.
data
)
}
topics
:=
NewTopics
(
binaryData
...
)
for
i
,
tt
:=
range
topicCreationTests
{
if
bytes
.
Compare
(
topics
[
i
][
:
],
tt
.
hash
[
:
])
!=
0
{
t
.
Errorf
(
"binary batch test %d: hash mismatch: have %v, want %v."
,
i
,
topics
[
i
],
tt
.
hash
)
}
}
topics
=
NewTopicsFromStrings
(
textualData
...
)
for
i
,
tt
:=
range
topicCreationTests
{
if
bytes
.
Compare
(
topics
[
i
][
:
],
tt
.
hash
[
:
])
!=
0
{
t
.
Errorf
(
"textual batch test %d: hash mismatch: have %v, want %v."
,
i
,
topics
[
i
],
tt
.
hash
)
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment