Parallel syntax change; SecretConnection implements net.Conn

This commit is contained in:
Jae Kwon
2015-07-15 12:13:10 -07:00
parent 5b41cc4fa5
commit d13a593afd
3 changed files with 93 additions and 80 deletions

View File

@ -27,10 +27,12 @@ func peerHandshake(conn net.Conn, ourNodeInfo *types.NodeInfo) (*types.NodeInfo,
var peerNodeInfo = new(types.NodeInfo) var peerNodeInfo = new(types.NodeInfo)
var err1 error var err1 error
var err2 error var err2 error
Parallel(func() { Parallel(
func() {
var n int64 var n int64
binary.WriteBinary(ourNodeInfo, conn, &n, &err1) binary.WriteBinary(ourNodeInfo, conn, &n, &err1)
}, func() { },
func() {
var n int64 var n int64
binary.ReadBinary(peerNodeInfo, conn, &n, &err2) binary.ReadBinary(peerNodeInfo, conn, &n, &err2)
log.Info("Peer handshake", "peerNodeInfo", peerNodeInfo) log.Info("Peer handshake", "peerNodeInfo", peerNodeInfo)

View File

@ -7,6 +7,8 @@ import (
"crypto/sha256" "crypto/sha256"
"encoding/binary" "encoding/binary"
"errors" "errors"
"net"
"time"
//"fmt" //"fmt"
"io" "io"
"sync" "sync"
@ -17,6 +19,7 @@ import (
acm "github.com/tendermint/tendermint/account" acm "github.com/tendermint/tendermint/account"
bm "github.com/tendermint/tendermint/binary" bm "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common"
) )
// 2 + 1024 == 1026 total frame size // 2 + 1024 == 1026 total frame size
@ -25,6 +28,7 @@ const dataMaxSize = 1024
const totalFrameSize = dataMaxSize + dataLenSize const totalFrameSize = dataMaxSize + dataLenSize
const sealedFrameSize = totalFrameSize + secretbox.Overhead const sealedFrameSize = totalFrameSize + secretbox.Overhead
// Implements net.Conn
type SecretConnection struct { type SecretConnection struct {
conn io.ReadWriteCloser conn io.ReadWriteCloser
recvBuffer []byte recvBuffer []byte
@ -157,8 +161,16 @@ func (sc *SecretConnection) Read(data []byte) (n int, err error) {
return return
} }
func (sc *SecretConnection) Close() error { // Implements net.Conn
return sc.conn.Close() func (sc *SecretConnection) Close() error { return sc.conn.Close() }
func (sc *SecretConnection) LocalAddr() net.Addr { return sc.conn.(net.Conn).LocalAddr() }
func (sc *SecretConnection) RemoteAddr() net.Addr { return sc.conn.(net.Conn).RemoteAddr() }
func (sc *SecretConnection) SetDeadline(t time.Time) error { return sc.conn.(net.Conn).SetDeadline(t) }
func (sc *SecretConnection) SetReadDeadline(t time.Time) error {
return sc.conn.(net.Conn).SetReadDeadline(t)
}
func (sc *SecretConnection) SetWriteDeadline(t time.Time) error {
return sc.conn.(net.Conn).SetWriteDeadline(t)
} }
func genEphKeys() (ephPub, ephPriv *[32]byte) { func genEphKeys() (ephPub, ephPriv *[32]byte) {
@ -246,17 +258,13 @@ type authSigMessage struct {
func shareAuthSignature(sc *SecretConnection, pubKey acm.PubKeyEd25519, signature acm.SignatureEd25519) (acm.PubKeyEd25519, acm.SignatureEd25519, error) { func shareAuthSignature(sc *SecretConnection, pubKey acm.PubKeyEd25519, signature acm.SignatureEd25519) (acm.PubKeyEd25519, acm.SignatureEd25519, error) {
var recvMsg authSigMessage var recvMsg authSigMessage
var err1, err2 error var err1, err2 error
var wg sync.WaitGroup
wg.Add(2)
go func() { Parallel(
defer wg.Done() func() {
msgBytes := bm.BinaryBytes(authSigMessage{pubKey, signature}) msgBytes := bm.BinaryBytes(authSigMessage{pubKey, signature})
_, err1 = sc.Write(msgBytes) _, err1 = sc.Write(msgBytes)
}() },
func() {
go func() {
defer wg.Done()
// NOTE relies on atomicity of small data. // NOTE relies on atomicity of small data.
readBuffer := make([]byte, dataMaxSize) readBuffer := make([]byte, dataMaxSize)
_, err2 = sc.Read(readBuffer) _, err2 = sc.Read(readBuffer)
@ -265,9 +273,8 @@ func shareAuthSignature(sc *SecretConnection, pubKey acm.PubKeyEd25519, signatur
} }
n := int64(0) // not used. n := int64(0) // not used.
recvMsg = bm.ReadBinary(authSigMessage{}, bytes.NewBuffer(readBuffer), &n, &err2).(authSigMessage) recvMsg = bm.ReadBinary(authSigMessage{}, bytes.NewBuffer(readBuffer), &n, &err2).(authSigMessage)
}() })
wg.Wait()
if err1 != nil { if err1 != nil {
return nil, nil, err1 return nil, nil, err1
} }

View File

@ -37,7 +37,8 @@ func makeSecretConnPair(tb testing.TB) (fooSecConn, barSecConn *SecretConnection
barPrvKey := acm.PrivKeyEd25519(CRandBytes(32)) barPrvKey := acm.PrivKeyEd25519(CRandBytes(32))
barPubKey := barPrvKey.PubKey().(acm.PubKeyEd25519) barPubKey := barPrvKey.PubKey().(acm.PubKeyEd25519)
Parallel(func() { Parallel(
func() {
var err error var err error
fooSecConn, err = MakeSecretConnection(fooConn, fooPrvKey) fooSecConn, err = MakeSecretConnection(fooConn, fooPrvKey)
if err != nil { if err != nil {
@ -48,7 +49,8 @@ func makeSecretConnPair(tb testing.TB) (fooSecConn, barSecConn *SecretConnection
tb.Errorf("Unexpected fooSecConn.RemotePubKey. Expected %v, got %v", tb.Errorf("Unexpected fooSecConn.RemotePubKey. Expected %v, got %v",
barPubKey, fooSecConn.RemotePubKey()) barPubKey, fooSecConn.RemotePubKey())
} }
}, func() { },
func() {
var err error var err error
barSecConn, err = MakeSecretConnection(barConn, barPrvKey) barSecConn, err = MakeSecretConnection(barConn, barPrvKey)
if barSecConn == nil { if barSecConn == nil {
@ -92,7 +94,8 @@ func TestSecretConnectionReadWrite(t *testing.T) {
return return
} }
// In parallel, handle reads and writes // In parallel, handle reads and writes
Parallel(func() { Parallel(
func() {
// Node writes // Node writes
for _, nodeWrite := range nodeWrites { for _, nodeWrite := range nodeWrites {
n, err := nodeSecretConn.Write([]byte(nodeWrite)) n, err := nodeSecretConn.Write([]byte(nodeWrite))
@ -106,7 +109,8 @@ func TestSecretConnectionReadWrite(t *testing.T) {
} }
} }
nodeConn.PipeWriter.Close() nodeConn.PipeWriter.Close()
}, func() { },
func() {
// Node reads // Node reads
readBuffer := make([]byte, dataMaxSize) readBuffer := make([]byte, dataMaxSize)
for { for {