Skip to content

Commit

Permalink
Remove OP_POW and work tickets
Browse files Browse the repository at this point in the history
  • Loading branch information
gz-c committed Aug 25, 2018
1 parent a43d268 commit 3d0398a
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 178 deletions.
2 changes: 1 addition & 1 deletion skycoin-messenger/factory/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func checkNodeServices(ns *NodeServices) (valid bool) {
return
}
}
if s.Key == EMPATY_PUBLIC_KEY {
if s.Key == EMPTY_PUBLIC_KEY {
return false
}
for _, k := range s.AllowNodes {
Expand Down
4 changes: 2 additions & 2 deletions skycoin-messenger/factory/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ const (
OP_REG_KEY
OP_REG_SIG

// POW
// POW (unused)
OP_POW

OP_SIZE
)

const RESP_PREFIX = 0x80

var EMPATY_PUBLIC_KEY = cipher.PubKey{}
var EMPTY_PUBLIC_KEY = cipher.PubKey{}
11 changes: 6 additions & 5 deletions skycoin-messenger/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"sync"
"time"

log "github.com/sirupsen/logrus"
"github.com/skycoin/net/conn"
"github.com/skycoin/net/factory"
"github.com/skycoin/net/msg"
"github.com/skycoin/skycoin/src/cipher"
"io/ioutil"
"sync"
"time"
)

type MessengerFactory struct {
Expand Down Expand Up @@ -333,7 +334,7 @@ func (f *MessengerFactory) ConnectWithConfig(address string, config *ConnConfig)
key, secKey, err = f.loadSeedConfig(config)
if err == nil {
conn.SetSecKey(secKey)
if config.TargetKey != EMPATY_PUBLIC_KEY {
if config.TargetKey != EMPTY_PUBLIC_KEY {
err = conn.RegWithKeys(key, config.TargetKey, config.Context)
} else {
err = conn.RegWithKey(key, config.Context)
Expand Down Expand Up @@ -403,7 +404,7 @@ func (f *MessengerFactory) connectUDPWithConfig(address string, config *ConnConf
key, secKey, err = f.loadSeedConfig(config)
if err == nil {
connection.SetSecKey(secKey)
if config.TargetKey != EMPATY_PUBLIC_KEY {
if config.TargetKey != EMPTY_PUBLIC_KEY {
err = connection.RegWithKeys(key, config.TargetKey, config.Context)
} else {
err = connection.RegWithKey(key, config.Context)
Expand Down
5 changes: 3 additions & 2 deletions skycoin-messenger/factory/op_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"crypto/aes"
"crypto/rand"
"fmt"
"github.com/skycoin/skycoin/src/cipher"
"io"
"net"
"sync"

"github.com/skycoin/skycoin/src/cipher"
)

func init() {
Expand Down Expand Up @@ -78,7 +79,7 @@ func (req *appConn) Execute(f *MessengerFactory, conn *Connection) (r resp, err
sent := make(map[string]struct{})
f.ForEachConn(func(connection *Connection) {
discoveryKey := connection.GetTargetKey()
if discoveryKey != req.Discovery && req.Discovery != EMPATY_PUBLIC_KEY {
if discoveryKey != req.Discovery && req.Discovery != EMPTY_PUBLIC_KEY {
return
}
_, ok := sent[discoveryKey.Hex()]
Expand Down
54 changes: 0 additions & 54 deletions skycoin-messenger/factory/op_pow.go

This file was deleted.

2 changes: 1 addition & 1 deletion skycoin-messenger/factory/op_reg.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (resp *regWithKeyResp) Run(conn *Connection) (err error) {
}
tpk := resp.PublicKey
t := conn.GetTargetKey()
if t != EMPATY_PUBLIC_KEY && t != tpk {
if t != EMPTY_PUBLIC_KEY && t != tpk {
tpk = t
}
err = conn.SetCrypto(pk, conn.GetSecKey(), tpk, resp.Num)
Expand Down
118 changes: 5 additions & 113 deletions skycoin-messenger/factory/transport.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package factory

import (
"crypto/hmac"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
cn "github.com/skycoin/net/conn"
"github.com/skycoin/net/msg"
"github.com/skycoin/skycoin/src/cipher"
"io"
"net"
"strconv"
"sync"
"sync/atomic"
"time"

log "github.com/sirupsen/logrus"
cn "github.com/skycoin/net/conn"
"github.com/skycoin/skycoin/src/cipher"
)

type Transport struct {
Expand Down Expand Up @@ -47,10 +46,6 @@ type Transport struct {

discoveryConn *Connection

ticketSeqCounter uint32
unChargeMsgs []*msg.UDPMessage
unChargeMsgsMutex sync.Mutex

fieldsMutex sync.RWMutex
}

Expand All @@ -59,9 +54,6 @@ type transportPair struct {
fromApp, fromNode, toNode, toApp cipher.PubKey
fromConn, toConn *Connection
fromHostPort, toHostPort, fromIp, toIp string
tickets map[uint32]*workTicket
lastTicket *workTicket
ticketsMutex sync.Mutex
timeoutTimer *time.Timer
closed bool
lastCheckedTime time.Time
Expand Down Expand Up @@ -91,53 +83,6 @@ func (p *transportPair) close() {
globalTransportPairManagerInstance.del(keys)
}

func (p *transportPair) submitTicket(ticket *workTicket) (ok uint, err error) {
p.ticketsMutex.Lock()
defer p.ticketsMutex.Unlock()

if p.lastCheckedTime.IsZero() {
p.lastCheckedTime = time.Now()
} else if time.Now().Sub(p.lastCheckedTime) > 30*time.Second {
if len(p.tickets) > 10 {
err = errors.New("too many uncheck tickets")
return
}
}

if len(ticket.Codes) > 0 {
if p.lastTicket == nil {
clone := *ticket
p.lastTicket = &clone
return
}
t := p.lastTicket
p.lastTicket = nil
for i, c := range ticket.Codes {
if hmac.Equal(t.Codes[i], c) {
ok++
} else {
return
}
}
return
}

t, o := p.tickets[ticket.Seq]
if !o {
clone := *ticket
p.tickets[ticket.Seq] = &clone
return
}
delete(p.tickets, ticket.Seq)
if !hmac.Equal(t.Code, ticket.Code) {
err = errors.New("ticket code is not valid")
return
}
ok = msgsEveryTicket
p.lastCheckedTime = time.Now()
return
}

func (p *transportPair) setFromConn(fromConn *Connection) (err error) {
p.fieldsMutex.Lock()
addr := fromConn.GetRemoteAddr().String()
Expand Down Expand Up @@ -206,7 +151,6 @@ func (m *transportPairManager) create(fromApp, fromNode, toNode, toApp cipher.Pu
fromNode: fromNode,
toNode: toNode,
toApp: toApp,
tickets: make(map[uint32]*workTicket),
}
p.timeoutTimer = time.AfterFunc(120*time.Second, func() {
p.close()
Expand All @@ -230,8 +174,6 @@ func (m *transportPairManager) del(keys string) {
m.pairsMutex.Unlock()
}

const msgsEveryTicket = 1000

func NewTransport(creator *MessengerFactory, appConn *Connection, fromNode, toNode, fromApp, toApp cipher.PubKey) *Transport {
if appConn == nil {
panic("appConn can not be nil")
Expand All @@ -252,61 +194,12 @@ func NewTransport(creator *MessengerFactory, appConn *Connection, fromNode, toNo
clientSide: cs,
factory: NewMessengerFactory(),
conns: make(map[uint32]net.Conn),
unChargeMsgs: make([]*msg.UDPMessage, 0, msgsEveryTicket-1),
}
ticketFunc := func(m *msg.UDPMessage) {
c := atomic.AddUint32(&t.ticketSeqCounter, 1)
if c%msgsEveryTicket != 0 {
t.unChargeMsgsMutex.Lock()
t.unChargeMsgs = append(t.unChargeMsgs, m)
t.unChargeMsgsMutex.Unlock()
return
}
t.unChargeMsgsMutex.Lock()
t.unChargeMsgs = t.unChargeMsgs[:0]
t.unChargeMsgsMutex.Unlock()
t.sendTicket(c/msgsEveryTicket, m)
}
if cs {
t.factory.BeforeReadOnConn = ticketFunc
} else {
t.factory.BeforeSendOnConn = ticketFunc
}
t.factory.Parent = creator
t.factory.SetDefaultSeedConfig(creator.GetDefaultSeedConfig())
return t
}

func (t *Transport) sendTicket(seq uint32, m *msg.UDPMessage) {
mac := hmac.New(sha256.New, t.FromNode[:])
mac.Write(m.Body)
code := mac.Sum(nil)
t.discoveryConn.writeOP(OP_POW, &workTicket{
Seq: seq,
Code: code,
})
}

func (t *Transport) sendLastTicket() {
c := atomic.AddUint32(&t.ticketSeqCounter, 1)
if c < msgsEveryTicket {
return
}
t.unChargeMsgsMutex.Lock()
codes := make([][]byte, len(t.unChargeMsgs))
for i, m := range t.unChargeMsgs {
mac := hmac.New(sha256.New, t.FromNode[:])
mac.Write(m.Body)
code := mac.Sum(nil)
codes[i] = code
}
t.unChargeMsgsMutex.Unlock()
t.discoveryConn.writeOP(OP_POW, &workTicket{
Codes: codes,
Last: true,
})
}

func (t *Transport) SetOnAcceptedUDPCallback(fn func(connection *Connection)) {
t.factory.OnAcceptedUDPCallback = fn
}
Expand Down Expand Up @@ -629,7 +522,7 @@ func (t *Transport) accept() {

func (t *Transport) getDiscoveryKey() cipher.PubKey {
if t.discoveryConn == nil {
return EMPATY_PUBLIC_KEY
return EMPTY_PUBLIC_KEY
}
return t.discoveryConn.GetTargetKey()
}
Expand All @@ -641,7 +534,6 @@ func (t *Transport) Close() {
if t.factory == nil {
return
}
t.sendLastTicket()

var key cipher.PubKey
if t.clientSide {
Expand Down

0 comments on commit 3d0398a

Please sign in to comment.