Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Done testing pss
  • Loading branch information
tubackkhoa committed Jan 11, 2019
1 parent fcbe74e commit bf60d1d
Show file tree
Hide file tree
Showing 3 changed files with 366 additions and 104 deletions.
214 changes: 110 additions & 104 deletions OrderBook/common/common.go
Expand Up @@ -10,15 +10,21 @@ import (
"os"
"os/signal"
"sync"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/swarm"
bzzapi "github.com/ethereum/go-ethereum/swarm/api"
"github.com/ethereum/go-ethereum/swarm/pss"
"github.com/fatih/color"

// peer.ID = enode.ID, NodeID = enode.ID
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/protocols"
"github.com/ethereum/go-ethereum/rpc"
)

Expand Down Expand Up @@ -58,110 +64,110 @@ func init() {
log.Root().SetHandler(h)
}

// // NewSwarmService : short cut for create swarm service
// func NewSwarmService(stack *node.Node, bzzport int) func(ctx *node.ServiceContext) (node.Service, error) {
// return NewSwarmServiceWithProtocol(stack, bzzport, nil, nil, nil, nil)
// }

// func setInterval(someFunc func(), milliseconds int, async bool) chan bool {

// // How often to fire the passed in function
// // in milliseconds
// interval := time.Duration(milliseconds) * time.Millisecond

// // Setup the ticket and the channel to signal
// // the ending of the interval
// ticker := time.NewTicker(interval)
// clear := make(chan bool)

// // Put the selection in a go routine
// // so that the for loop is none blocking
// go func() {
// for {

// select {
// case <-ticker.C:
// if async {
// // This won't block
// go someFunc()
// } else {
// // This will block
// someFunc()
// }
// case <-clear:
// ticker.Stop()
// return
// }

// }
// }()

// // We return the channel so we can pass in
// // a value to it to clear the interval
// return clear

// }

// func NewSwarmServiceWithProtocolAndPrivateKey(stack *node.Node, bzzport int, specs []*protocols.Spec, protocols []*p2p.Protocol, topic *pss.Topic, pssprotos *[]*pss.Protocol, prvkey *ecdsa.PrivateKey) func(ctx *node.ServiceContext) (node.Service, error) {
// return func(ctx *node.ServiceContext) (node.Service, error) {

// // create the swarm overlay address
// //chbookaddr := crypto.PubkeyToAddress(prvkey.PublicKey)

// // configure and create a swarm instance
// bzzdir := stack.InstanceDir() // todo: what is the difference between this and datadir?
// bzzconfig := bzzapi.NewConfig()
// bzzconfig.Cors = "*" // allow access to all
// bzzconfig.NetworkID = BzzDefaultNetworkId
// bzzconfig.DeliverySkipCheck = true
// bzzconfig.SyncingSkipCheck = true
// bzzconfig.Path = bzzdir
// bzzconfig.Pss.AllowRaw = true
// bzzconfig.Init(prvkey)
// bzzconfig.Port = fmt.Sprintf("%d", bzzport)

// svc, err := swarm.NewSwarm(bzzconfig, nil)
// if err != nil {
// Log.Crit("unable to configure swarm", "err", err)
// return nil, err
// }

// for i, s := range specs {

// p, err := svc.RegisterPssProtocol(s, protocols[i], &pss.ProtocolParams{
// Asymmetric: true,
// Symmetric: true,
// })
// if err != nil {
// return nil, err
// }

// // wrap to handler
// // handler := pss.NewHandler(p.Handle)
// handler := p.Handle
// // register topic for this protocol
// if topic != nil {
// p.Pss.Register(topic, handler)
// }
// // append to pssprotos
// if pssprotos != nil {
// *pssprotos = append(*pssprotos, p)
// // LogError("Add item", "len", len(*pssprotos))
// }

// }

// return svc, nil
// }
// }

// // NewSwarmServiceWithProtocol : create new swarm service with protocol and topic
// func NewSwarmServiceWithProtocol(stack *node.Node, bzzport int, specs []*protocols.Spec, protocols []*p2p.Protocol, topic *pss.Topic, pssprotos *[]*pss.Protocol) func(ctx *node.ServiceContext) (node.Service, error) {
// // get the encrypted private key file
// // load the private key from the file content ?
// prvkey, _ := crypto.GenerateKey()
// return NewSwarmServiceWithProtocolAndPrivateKey(stack, bzzport, specs, protocols, topic, pssprotos, prvkey)
// }
// NewSwarmService : short cut for create swarm service
func NewSwarmService(stack *node.Node, bzzport int) func(ctx *node.ServiceContext) (node.Service, error) {
return NewSwarmServiceWithProtocol(stack, bzzport, nil, nil, nil, nil)
}

func setInterval(someFunc func(), milliseconds int, async bool) chan bool {

// How often to fire the passed in function
// in milliseconds
interval := time.Duration(milliseconds) * time.Millisecond

// Setup the ticket and the channel to signal
// the ending of the interval
ticker := time.NewTicker(interval)
clear := make(chan bool)

// Put the selection in a go routine
// so that the for loop is none blocking
go func() {
for {

select {
case <-ticker.C:
if async {
// This won't block
go someFunc()
} else {
// This will block
someFunc()
}
case <-clear:
ticker.Stop()
return
}

}
}()

// We return the channel so we can pass in
// a value to it to clear the interval
return clear

}

func NewSwarmServiceWithProtocolAndPrivateKey(stack *node.Node, bzzport int, specs []*protocols.Spec, protocols []*p2p.Protocol, topic *pss.Topic, pssprotos *[]*pss.Protocol, prvkey *ecdsa.PrivateKey) func(ctx *node.ServiceContext) (node.Service, error) {
return func(ctx *node.ServiceContext) (node.Service, error) {

// create the swarm overlay address
//chbookaddr := crypto.PubkeyToAddress(prvkey.PublicKey)

// configure and create a swarm instance
bzzdir := stack.InstanceDir() // todo: what is the difference between this and datadir?
bzzconfig := bzzapi.NewConfig()
bzzconfig.Cors = "*" // allow access to all
bzzconfig.NetworkID = BzzDefaultNetworkId
bzzconfig.DeliverySkipCheck = true
bzzconfig.SyncEnabled = true
bzzconfig.Path = bzzdir
bzzconfig.Pss.AllowRaw = true
bzzconfig.Init(prvkey)
bzzconfig.Port = fmt.Sprintf("%d", bzzport)

svc, err := swarm.NewSwarm(bzzconfig, nil)
if err != nil {
Log.Crit("unable to configure swarm", "err", err)
return nil, err
}

for i, s := range specs {

p, err := svc.RegisterPssProtocol(s, protocols[i], &pss.ProtocolParams{
Asymmetric: true,
Symmetric: true,
})
if err != nil {
return nil, err
}

// wrap to handler
// handler := pss.NewHandler(p.Handle)
handler := p.Handle
// register topic for this protocol
if topic != nil {
p.Pss.Register(topic, handler)
}
// append to pssprotos
if pssprotos != nil {
*pssprotos = append(*pssprotos, p)
// LogError("Add item", "len", len(*pssprotos))
}

}

return svc, nil
}
}

// NewSwarmServiceWithProtocol : create new swarm service with protocol and topic
func NewSwarmServiceWithProtocol(stack *node.Node, bzzport int, specs []*protocols.Spec, protocols []*p2p.Protocol, topic *pss.Topic, pssprotos *[]*pss.Protocol) func(ctx *node.ServiceContext) (node.Service, error) {
// get the encrypted private key file
// load the private key from the file content ?
prvkey, _ := crypto.GenerateKey()
return NewSwarmServiceWithProtocolAndPrivateKey(stack, bzzport, specs, protocols, topic, pssprotos, prvkey)
}

func LogInfo(msg string, ctx ...interface{}) {
Log.Info(color.HiGreenString(msg), ctx...)
Expand Down
9 changes: 9 additions & 0 deletions OrderBook/protocol/protocol_test.go
Expand Up @@ -178,6 +178,15 @@ func (api *FooAPI) PongCount() (int, error) {
return *api.pongcount, nil
}

func TestPss(t *testing.T) {
rawurl := "enode://ce24c4f944a0a3614b691d839a6a89339d17abac3d69c0d24e806db45d1bdbe7afa53c02136e5ad952f43e6e7285cd3971e367d8789f4eb7306770f5af78755d@127.0.0.1:30101?discport=0"
publicKey := "0x04ce24c4f944a0a3614b691d839a6a89339d17abac3d69c0d24e806db45d1bdbe7afa53c02136e5ad952f43e6e7285cd3971e367d8789f4eb7306770f5af78755d"
newNode, _ := discover.ParseNode(rawurl)
pKey := "0x04" + newNode.ID.String()

t.Logf("Node ID :%t", publicKey == pKey)
}

func Test2PeersCommunication(t *testing.T) {

// create the two nodes
Expand Down

0 comments on commit bf60d1d

Please sign in to comment.