Skip to content

Commit

Permalink
feat(core): initial contact flow implementation (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Jul 29, 2018
1 parent 0df4e79 commit dd99d24
Show file tree
Hide file tree
Showing 15 changed files with 316 additions and 164 deletions.
19 changes: 19 additions & 0 deletions core/api/p2p/event.go
@@ -0,0 +1,19 @@
package p2p

import "time"

func NewOutgoingEvent(sender, receiver string, kind Kind) *Event {
return &Event{
SenderAPIVersion: Version,
CreatedAt: time.Now(),
Kind: kind,
SenderID: sender,
ReceiverID: receiver,
Direction: Event_Outgoing,
}
}

func (e *Event) Validate() error {
// FIXME: generate validation
return nil
}
190 changes: 51 additions & 139 deletions core/api/p2p/event.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions core/api/p2p/event.proto
Expand Up @@ -56,10 +56,8 @@ message Event {
// Attributes is a nested protobuf message containing per-event-type additional attributes.
bytes attributes = 14;

oneof context {
// ConversationID needs to be set if the event belongs to a conversation.
string conversation_id = 15 [(gogoproto.customname) = "ConversationID"];
}
// ConversationID needs to be set if the event belongs to a conversation.
string conversation_id = 15 [(gogoproto.customname) = "ConversationID"];

//
// enums
Expand Down
12 changes: 12 additions & 0 deletions core/api/p2p/kind.go
@@ -0,0 +1,12 @@
package p2p

import "github.com/gogo/protobuf/proto"

func (e *Event) SetAttrs(attrs proto.Message) error {
raw, err := proto.Marshal(attrs)
if err != nil {
return err
}
e.Attributes = raw
return nil
}
1 change: 0 additions & 1 deletion core/cmd/berty/client.go
Expand Up @@ -46,7 +46,6 @@ func newClientCommand() *cobra.Command {
}

func clientUnary(opts *clientOptions) error {
fmt.Println(opts.endpoint)
ctx := context.Background()

zap.L().Debug("dialing node", zap.String("addr", opts.nodeAddress), zap.String("protocol", "gRPC"))
Expand Down
16 changes: 16 additions & 0 deletions core/entity/contact.go
@@ -0,0 +1,16 @@
package entity

func (c *Contact) Validate() error {
if c == nil {
return ErrInvalidEntity
}
return nil
}

func (c *Contact) Filtered() *Contact {
return &Contact{
DisplayName: c.DisplayName,
DisplayStatus: c.DisplayStatus,
// FIXME: share sigchain
}
}
7 changes: 7 additions & 0 deletions core/entity/errors.go
@@ -0,0 +1,7 @@
package entity

import "errors"

var (
ErrInvalidEntity = errors.New("invalid entity")
)
9 changes: 9 additions & 0 deletions core/node/errors.go
@@ -0,0 +1,9 @@
package node

import "errors"

var (
ErrInvalidInput = errors.New("invalid input")
ErrNotImplemented = errors.New("not implemented")
ErrEntityAlreadyExists = errors.New("entity already exists")
)
37 changes: 34 additions & 3 deletions core/node/node.go
@@ -1,15 +1,30 @@
package node

import "github.com/jinzhu/gorm"
import (
"fmt"

"github.com/jinzhu/gorm"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"

"github.com/berty/berty/core/api/p2p"
"github.com/berty/berty/core/entity"
)

// Node is the top-level object of a Berty peer
type Node struct {
db *gorm.DB
sql *gorm.DB
myself *entity.Contact
outgoingEvents chan *p2p.Event
}

// New initializes a new Node object
func New(opts ...NewNodeOption) *Node {
n := &Node{}
n := &Node{
// FIXME: fetch myself from db
myself: &entity.Contact{ID: "init"},
outgoingEvents: make(chan *p2p.Event, 100),
}

for _, opt := range opts {
opt(n)
Expand All @@ -19,8 +34,24 @@ func New(opts ...NewNodeOption) *Node {

// Start is the node's mainloop
func (n *Node) Start() error {
if err := n.Validate(); err != nil {
return errors.Wrap(err, "node is misconfigured")
}
select {}
}

// Validate returns an error if object is invalid
func (n *Node) Validate() error {
if n == nil || n.sql == nil {
return errors.New("missing required fields")
}
return nil
}

// NewNodeOption is a callback used to configure a Node during intiailization phase
type NewNodeOption func(n *Node)

// NewID returns a unique ID prefixed with our contact ID
func (n *Node) NewID() string {
return fmt.Sprintf("%s:%s", n.myself.ID, uuid.Must(uuid.NewV4()).String())
}

0 comments on commit dd99d24

Please sign in to comment.