Skip to content

Commit

Permalink
Golint fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
markkurossi committed Jul 15, 2020
1 parent a3c2ba2 commit c008db7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
7 changes: 2 additions & 5 deletions apps/garbled/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ import (
"github.com/markkurossi/mpc/p2p"
)

const (
OP_OT = iota
OP_RESULT
)

var (
port = ":8080"
verbose = false
Expand Down Expand Up @@ -365,6 +360,7 @@ func makeOutput(base, suffix string) (io.WriteCloser, error) {
}, nil
}

// OutputFile implements a buffered output file.
type OutputFile struct {
File *os.File
Buffered *bufio.Writer
Expand All @@ -374,6 +370,7 @@ func (out *OutputFile) Write(p []byte) (nn int, err error) {
return out.Buffered.Write(p)
}

// Close implements io.Closer.Close for the buffered output file.
func (out *OutputFile) Close() error {
if err := out.Buffered.Flush(); err != nil {
return err
Expand Down
26 changes: 24 additions & 2 deletions p2p/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,42 @@ import (
"github.com/markkurossi/mpc/ot"
)

// Conn implements a protocol connection.
type Conn struct {
closer io.Closer
io *bufio.ReadWriter
Stats IOStats
}

// IOStats implements I/O statistics.
type IOStats struct {
Sent uint64
Recvd uint64
}

// Add adds the argument stats to this IOStats and returns the sum.
func (stats IOStats) Add(o IOStats) IOStats {
return IOStats{
Sent: stats.Sent + o.Sent,
Recvd: stats.Recvd + o.Recvd,
}
}

// Sub subtracts the argument stats from this IOStats and returns the
// difference.
func (stats IOStats) Sub(o IOStats) IOStats {
return IOStats{
Sent: stats.Sent - o.Sent,
Recvd: stats.Recvd - o.Recvd,
}
}

// Sum returns sum of sent and received bytes.
func (stats IOStats) Sum() uint64 {
return stats.Sent + stats.Recvd
}

// NewConn creates a new connection around the argument connection.
func NewConn(conn io.ReadWriter) *Conn {
closer, _ := conn.(io.Closer)

Expand All @@ -53,10 +60,12 @@ func NewConn(conn io.ReadWriter) *Conn {
}
}

// Flush flushed any pending data in the connection.
func (c *Conn) Flush() error {
return c.io.Flush()
}

// Close flushes any pending data and closes the connection.
func (c *Conn) Close() error {
if err := c.Flush(); err != nil {
return err
Expand All @@ -67,15 +76,17 @@ func (c *Conn) Close() error {
return nil
}

// SendByte sends a byte value.
func (c *Conn) SendByte(val byte) error {
err := c.io.WriteByte(val)
if err != nil {
return err
}
c.Stats.Sent += 1
c.Stats.Sent++
return nil
}

// SendUint16 sends an uint16 value.
func (c *Conn) SendUint16(val int) error {
err := binary.Write(c.io, binary.BigEndian, uint16(val))
if err != nil {
Expand All @@ -85,6 +96,7 @@ func (c *Conn) SendUint16(val int) error {
return nil
}

// SendUint32 sends an uint32 value.
func (c *Conn) SendUint32(val int) error {
err := binary.Write(c.io, binary.BigEndian, uint32(val))
if err != nil {
Expand All @@ -94,6 +106,7 @@ func (c *Conn) SendUint32(val int) error {
return nil
}

// SendData sends binary data.
func (c *Conn) SendData(val []byte) error {
err := c.SendUint32(len(val))
if err != nil {
Expand All @@ -107,6 +120,7 @@ func (c *Conn) SendData(val []byte) error {
return nil
}

// SendLabel sends an OT label.
func (c *Conn) SendLabel(val ot.Label) error {
n, err := c.io.Write(val.Bytes())
if err != nil {
Expand All @@ -116,19 +130,22 @@ func (c *Conn) SendLabel(val ot.Label) error {
return nil
}

// SendString sends a string value.
func (c *Conn) SendString(val string) error {
return c.SendData([]byte(val))
}

// ReceiveByte receives a byte value.
func (c *Conn) ReceiveByte() (byte, error) {
val, err := c.io.ReadByte()
if err != nil {
return 0, err
}
c.Stats.Recvd += 1
c.Stats.Recvd++
return val, nil
}

// ReceiveUint16 receives an uint16 value.
func (c *Conn) ReceiveUint16() (int, error) {
var buf [2]byte

Expand All @@ -141,6 +158,7 @@ func (c *Conn) ReceiveUint16() (int, error) {
return int(binary.BigEndian.Uint16(buf[:])), nil
}

// ReceiveUint32 receives an uint32 value.
func (c *Conn) ReceiveUint32() (int, error) {
var buf [4]byte

Expand All @@ -153,6 +171,7 @@ func (c *Conn) ReceiveUint32() (int, error) {
return int(binary.BigEndian.Uint32(buf[:])), nil
}

// ReceiveData receives binary data.
func (c *Conn) ReceiveData() ([]byte, error) {
len, err := c.ReceiveUint32()
if err != nil {
Expand All @@ -169,6 +188,7 @@ func (c *Conn) ReceiveData() ([]byte, error) {
return result, nil
}

// ReceiveLabel receives an OT label.
func (c *Conn) ReceiveLabel() (ot.Label, error) {
var buf ot.LabelData
n, err := io.ReadFull(c.io, buf[:])
Expand All @@ -182,6 +202,7 @@ func (c *Conn) ReceiveLabel() (ot.Label, error) {
return result, nil
}

// ReceiveString receives a string value.
func (c *Conn) ReceiveString() (string, error) {
data, err := c.ReceiveData()
if err != nil {
Expand All @@ -190,6 +211,7 @@ func (c *Conn) ReceiveString() (string, error) {
return string(data), nil
}

// Receive implements OT receive for the bit value of a wire.
func (c *Conn) Receive(receiver *ot.Receiver, wire, bit uint) ([]byte, error) {

if err := c.SendUint32(int(wire)); err != nil {
Expand Down

0 comments on commit c008db7

Please sign in to comment.