Skip to content

Commit 4cb8412

Browse files
authored
Merge pull request ethereum#104 from ethersphere/network-testing-framework-psstalk
pss updates + kademlia fixes
2 parents 3899c3e + 0628dba commit 4cb8412

35 files changed

+1957
-1845
lines changed

cmd/swarm/main.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,9 @@ var (
118118
Usage: "force mime type",
119119
}
120120
PssEnabledFlag = cli.BoolFlag{
121-
Name: "pss",
121+
Name: "pss",
122122
Usage: "Enable pss (message passing over swarm)",
123123
}
124-
PssPortFlag = cli.IntFlag{
125-
Name: "pssport",
126-
Usage: fmt.Sprintf("Websockets port for pss (default %d)", node.DefaultWSPort),
127-
}
128124
CorsStringFlag = cli.StringFlag{
129125
Name: "corsdomain",
130126
Usage: "Domain on which to send Access-Control-Allow-Origin header (multiple domains can be supplied separated by a ',')",
@@ -270,8 +266,15 @@ Cleans database of corrupted entries.
270266
SwarmUploadMimeType,
271267
// pss flags
272268
PssEnabledFlag,
273-
PssPortFlag,
274269
}
270+
rpcFlags := []cli.Flag{
271+
utils.WSEnabledFlag,
272+
utils.WSListenAddrFlag,
273+
utils.WSPortFlag,
274+
utils.WSApiFlag,
275+
utils.WSAllowedOriginsFlag,
276+
}
277+
app.Flags = append(app.Flags, rpcFlags...)
275278
app.Flags = append(app.Flags, debug.Flags...)
276279
app.Before = func(ctx *cli.Context) error {
277280
runtime.GOMAXPROCS(runtime.NumCPU())
@@ -306,13 +309,8 @@ func version(ctx *cli.Context) error {
306309

307310
func bzzd(ctx *cli.Context) error {
308311
cfg := defaultNodeConfig
309-
if ctx.GlobalIsSet(PssEnabledFlag.Name) {
310-
cfg.WSHost = "127.0.0.1"
311-
cfg.WSModules = []string{"eth","pss"}
312-
cfg.WSOrigins = []string{"*"}
313-
if ctx.GlobalIsSet(PssPortFlag.Name) {
314-
cfg.WSPort = ctx.GlobalInt(PssPortFlag.Name)
315-
}
312+
if ctx.GlobalBool(PssEnabledFlag.Name) && ctx.GlobalBool(utils.WSEnabledFlag.Name) {
313+
cfg.WSModules = append(cfg.WSModules, "pss")
316314
}
317315
utils.SetNodeConfig(ctx, &cfg)
318316
stack, err := node.New(&cfg)
@@ -366,7 +364,7 @@ func registerBzzService(ctx *cli.Context, stack *node.Node) {
366364
swapEnabled := ctx.GlobalBool(SwarmSwapEnabledFlag.Name)
367365
syncEnabled := ctx.GlobalBoolT(SwarmSyncEnabledFlag.Name)
368366
pssEnabled := ctx.GlobalBool(PssEnabledFlag.Name)
369-
367+
370368
ethapi := ctx.GlobalString(EthAPIFlag.Name)
371369
cors := ctx.GlobalString(CorsStringFlag.Name)
372370

p2p/simulations/network.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ func (self *Network) newConn(oneID, otherID discover.NodeID) (*Conn, error) {
255255
if other == nil {
256256
return nil, fmt.Errorf("other %v does not exist", other)
257257
}
258-
distance, _ := pot.NewBytesVal(one.Addr(), nil).PO(pot.NewBytesVal(other.Addr(), nil), 0)
258+
distance, _ := pot.DefaultPof(256)(one.Addr(), other.Addr(), 0)
259259
return &Conn{
260260
One: oneID,
261261
Other: otherID,

pot/address.go

Lines changed: 55 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
//
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// Package pot see doc.go
1618
package pot
1719

1820
import (
@@ -26,35 +28,40 @@ import (
2628
)
2729

2830
var (
29-
zeroAddr = &common.Hash{}
30-
zerosHex = zeroAddr.Hex()[2:]
3131
zerosBin = Address{}.Bin()
3232
)
3333

34-
var (
35-
addrlen = keylen
36-
)
37-
34+
// Address is an alias for common.Hash
3835
type Address common.Hash
3936

37+
// NewAddressFromBytes constructs an Address from a byte slice
38+
func NewAddressFromBytes(b []byte) Address {
39+
h := common.Hash{}
40+
copy(h[:], b)
41+
return Address(h)
42+
}
43+
4044
func (a Address) String() string {
4145
return fmt.Sprintf("%x", a[:])
4246
}
4347

48+
// MarshalJSON Address serialisation
4449
func (a *Address) MarshalJSON() (out []byte, err error) {
4550
return []byte(`"` + a.String() + `"`), nil
4651
}
4752

53+
// UnmarshalJSON Address deserialisation
4854
func (a *Address) UnmarshalJSON(value []byte) error {
4955
*a = Address(common.HexToHash(string(value[1 : len(value)-1])))
5056
return nil
5157
}
5258

53-
// the string form of the binary representation of an address (only first 8 bits)
59+
// Bin returns the string form of the binary representation of an address (only first 8 bits)
5460
func (a Address) Bin() string {
5561
return ToBin(a[:])
5662
}
5763

64+
// ToBin converts a byteslice to the string binary representation
5865
func ToBin(a []byte) string {
5966
var bs []string
6067
for _, b := range a {
@@ -63,6 +70,7 @@ func ToBin(a []byte) string {
6370
return strings.Join(bs, "")
6471
}
6572

73+
// Bytes returns the Address as a byte slice
6674
func (a Address) Bytes() []byte {
6775
return a[:]
6876
}
@@ -107,23 +115,23 @@ func posProximity(one, other Address, pos int) (ret int, eq bool) {
107115
return len(one) * 8, true
108116
}
109117

110-
// Address.ProxCmp compares the distances a->target and b->target.
118+
// ProxCmp compares the distances a->target and b->target.
111119
// Returns -1 if a is closer to target, 1 if b is closer to target
112120
// and 0 if they are equal.
113-
func (target Address) ProxCmp(a, b Address) int {
114-
for i := range target {
115-
da := a[i] ^ target[i]
116-
db := b[i] ^ target[i]
117-
if da > db {
121+
func (a Address) ProxCmp(x, y Address) int {
122+
for i := range a {
123+
dx := x[i] ^ a[i]
124+
dy := y[i] ^ a[i]
125+
if dx > dy {
118126
return 1
119-
} else if da < db {
127+
} else if dx < dy {
120128
return -1
121129
}
122130
}
123131
return 0
124132
}
125133

126-
// randomAddressAt(address, prox) generates a random address
134+
// RandomAddressAt (address, prox) generates a random address
127135
// at proximity order prox relative to address
128136
// if prox is negative a random address is generated
129137
func RandomAddressAt(self Address, prox int) (addr Address) {
@@ -148,71 +156,12 @@ func RandomAddressAt(self Address, prox int) (addr Address) {
148156
return
149157
}
150158

151-
// KeyRange(a0, a1, proxLimit) returns the address inclusive address
152-
// range that contain addresses closer to one than other
153-
// func KeyRange(one, other Address, proxLimit int) (start, stop Address) {
154-
// prox := proximity(one, other)
155-
// if prox >= proxLimit {
156-
// prox = proxLimit
157-
// }
158-
// start = CommonBitsAddrByte(one, other, byte(0x00), prox)
159-
// stop = CommonBitsAddrByte(one, other, byte(0xff), prox)
160-
// return
161-
// }
162-
163-
func CommonBitsAddrF(self, other Address, f func() byte, p int) (addr Address) {
164-
prox, _ := proximity(self, other)
165-
var pos int
166-
if p <= prox {
167-
prox = p
168-
}
169-
pos = prox / 8
170-
addr = self
171-
trans := byte(prox % 8)
172-
var transbytea byte
173-
if p > prox {
174-
transbytea = byte(0x7f)
175-
} else {
176-
transbytea = byte(0xff)
177-
}
178-
transbytea >>= trans
179-
transbyteb := transbytea ^ byte(0xff)
180-
addrpos := addr[pos]
181-
addrpos &= transbyteb
182-
if p > prox {
183-
addrpos ^= byte(0x80 >> trans)
184-
}
185-
addrpos |= transbytea & f()
186-
addr[pos] = addrpos
187-
for i := pos + 1; i < len(addr); i++ {
188-
addr[i] = f()
189-
}
190-
191-
return
192-
}
193-
194-
func CommonBitsAddr(self, other Address, prox int) (addr Address) {
195-
return CommonBitsAddrF(self, other, func() byte { return byte(rand.Intn(255)) }, prox)
196-
}
197-
198-
func CommonBitsAddrByte(self, other Address, b byte, prox int) (addr Address) {
199-
return CommonBitsAddrF(self, other, func() byte { return b }, prox)
200-
}
201-
202-
// randomAddressAt() generates a random address
159+
// RandomAddress generates a random address
203160
func RandomAddress() Address {
204161
return RandomAddressAt(Address{}, -1)
205162
}
206163

207-
// wraps an Address to implement the PotVal interface
208-
type HashAddress struct {
209-
Address
210-
}
211-
212-
func (a *HashAddress) String() string {
213-
return a.Address.Bin()
214-
}
215-
164+
// NewAddressFromString creates a byte slice from a string in binary representation
216165
func NewAddressFromString(s string) []byte {
217166
ha := [32]byte{}
218167

@@ -227,85 +176,16 @@ func NewAddressFromString(s string) []byte {
227176
return ha[:]
228177
}
229178

230-
func NewHashAddress(s string) *HashAddress {
231-
ha := NewAddressFromString(s)
232-
h := common.Hash{}
233-
copy(h[:], ha)
234-
return &HashAddress{Address(h)}
235-
}
236-
237-
func NewHashAddressFromBytes(b []byte) *HashAddress {
238-
h := common.Hash{}
239-
copy(h[:], b)
240-
return &HashAddress{Address(h)}
241-
}
242-
243-
// PO(addr, pos) return the proximity order of addr wrt to
244-
// the pinned address of the tree
245-
// assuming it is greater than or equal to pos
246-
func (self *HashAddress) PO(val PotVal, pos int) (po int, eq bool) {
247-
return posProximity(self.Address, val.(*HashAddress).Address, pos)
248-
}
249-
250-
type BoolAddress struct {
251-
addr []bool
252-
}
253-
254-
func NewBoolAddress(s string) *BoolAddress {
255-
return NewBoolAddressXOR(s, zerosBin[:len(s)])
256-
}
257-
258-
func NewBoolAddressXOR(s, t string) *BoolAddress {
259-
if len(s) != len(t) {
260-
panic("lengths do not match")
261-
}
262-
addr := make([]bool, len(s))
263-
for i, _ := range addr {
264-
addr[i] = s[i] != t[i]
265-
}
266-
return &BoolAddress{addr}
267-
}
268-
269-
func (self *BoolAddress) String() string {
270-
a := self.addr
271-
s := []byte(zerosBin)[:len(a)]
272-
for i, one := range a {
273-
if one {
274-
s[i] = byte('1')
275-
}
276-
}
277-
return string(s)
278-
}
279-
280-
func (self *BoolAddress) PO(val PotVal, pos int) (po int, eq bool) {
281-
a := self.addr
282-
b := val.(*BoolAddress).addr
283-
for po = pos; po < len(b); po++ {
284-
if a[po] != b[po] {
285-
return po, false
286-
}
287-
}
288-
return po, true
289-
}
290-
179+
// BytesAddress is an interface for elements addressable by a byte slice
291180
type BytesAddress interface {
292181
Address() []byte
293182
}
294183

295-
type bytesAddress struct {
296-
bytes []byte
297-
toBytes func(v AnyVal) []byte
298-
}
299-
300-
func NewBytesVal(v AnyVal, f func(v AnyVal) []byte) *bytesAddress {
301-
if f == nil {
302-
f = ToBytes
184+
// ToBytes turns the Val into bytes
185+
func ToBytes(v Val) []byte {
186+
if v == nil {
187+
return nil
303188
}
304-
b := f(v)
305-
return &bytesAddress{b, f}
306-
}
307-
308-
func ToBytes(v AnyVal) []byte {
309189
b, ok := v.([]byte)
310190
if !ok {
311191
ba, ok := v.(BytesAddress)
@@ -317,15 +197,17 @@ func ToBytes(v AnyVal) []byte {
317197
return b
318198
}
319199

320-
func (a *bytesAddress) String() string {
321-
return fmt.Sprintf("%08b", a.bytes)
322-
}
323-
func (a *bytesAddress) Address() []byte {
324-
return a.bytes
325-
}
326-
327-
func (a *bytesAddress) PO(val PotVal, i int) (int, bool) {
328-
return proximityOrder(a.bytes, a.toBytes(val), i)
200+
// DefaultPof returns a proximity order comparison operator function
201+
// where all
202+
func DefaultPof(max int) func(one, other Val, pos int) (int, bool) {
203+
return func(one, other Val, pos int) (int, bool) {
204+
po, eq := proximityOrder(ToBytes(one), ToBytes(other), pos)
205+
if po >= max {
206+
eq = true
207+
po = max
208+
}
209+
return po, eq
210+
}
329211
}
330212

331213
func proximityOrder(one, other []byte, pos int) (int, bool) {
@@ -346,3 +228,17 @@ func proximityOrder(one, other []byte, pos int) (int, bool) {
346228
}
347229
return len(one) * 8, true
348230
}
231+
232+
// Label displays the node's key in binary format
233+
func Label(v Val) string {
234+
if v == nil {
235+
return "<nil>"
236+
}
237+
if s, ok := v.(fmt.Stringer); ok {
238+
return s.String()
239+
}
240+
if b, ok := v.([]byte); ok {
241+
return ToBin(b)
242+
}
243+
panic(fmt.Sprintf("unsupported value type %T", v))
244+
}

0 commit comments

Comments
 (0)