-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from mit-pdos/optiks
move from coniks to optiks
- Loading branch information
Showing
54 changed files
with
3,393 additions
and
1,889 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package advrpc | ||
|
||
// advrpc provides a basic RPC lib on top of an adversarial network. | ||
// for testing, it returns the right bytes from the right rpc id. | ||
// however, its formal model says that rpc calls return arbitrary bytes. | ||
|
||
import ( | ||
"github.com/mit-pdos/pav/marshalutil" | ||
"github.com/mit-pdos/pav/netffi" | ||
"github.com/tchajed/marshal" | ||
) | ||
|
||
// # Server | ||
|
||
type Server struct { | ||
handlers map[uint64]func([]byte, *[]byte) | ||
} | ||
|
||
func (s *Server) handle(conn *netffi.Conn, rpcId uint64, data []byte) { | ||
f, ok0 := s.handlers[rpcId] | ||
if !ok0 { | ||
// adv gave bad rpcId. | ||
return | ||
} | ||
resp := new([]byte) | ||
f(data, resp) | ||
// ignore errors. if err, client will timeout, then retry. | ||
conn.Send(*resp) | ||
} | ||
|
||
func (s *Server) read(conn *netffi.Conn) { | ||
for { | ||
req, err0 := conn.Receive() | ||
if err0 { | ||
// connection done. quit thread. | ||
break | ||
} | ||
rpcId, data, err1 := marshalutil.ReadInt(req) | ||
if err1 { | ||
// adv didn't even give rpcId. | ||
continue | ||
} | ||
go func() { | ||
s.handle(conn, rpcId, data) | ||
}() | ||
} | ||
} | ||
|
||
func (s *Server) Serve(addr uint64) { | ||
l := netffi.Listen(addr) | ||
go func() { | ||
for { | ||
conn := l.Accept() | ||
go func() { | ||
s.read(conn) | ||
}() | ||
} | ||
}() | ||
} | ||
|
||
func NewServer(handlers map[uint64]func([]byte, *[]byte)) *Server { | ||
return &Server{handlers: handlers} | ||
} | ||
|
||
// # Client | ||
|
||
// Client is meant for exclusive use. | ||
type Client struct { | ||
conn *netffi.Conn | ||
} | ||
|
||
func Dial(addr uint64) *Client { | ||
c := netffi.Dial(addr) | ||
return &Client{conn: c} | ||
} | ||
|
||
// Call does an rpc, and returns error on fail. | ||
func (c *Client) Call(rpcId uint64, args []byte, reply *[]byte) bool { | ||
req0 := make([]byte, 0, 8+len(args)) | ||
req1 := marshal.WriteInt(req0, rpcId) | ||
req2 := marshal.WriteBytes(req1, args) | ||
if c.conn.Send(req2) { | ||
return true | ||
} | ||
|
||
resp, err0 := c.conn.Receive() | ||
if err0 { | ||
return true | ||
} | ||
*reply = resp | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package advrpc | ||
|
||
import ( | ||
"github.com/mit-pdos/pav/marshalutil" | ||
"github.com/tchajed/marshal" | ||
"math/rand/v2" | ||
"testing" | ||
) | ||
|
||
type Args struct { | ||
A, B uint64 | ||
} | ||
|
||
func Multiply(args *Args) uint64 { | ||
return args.A * args.B | ||
} | ||
|
||
func encArgs(args *Args) []byte { | ||
var b0 []byte | ||
b1 := marshal.WriteInt(b0, args.A) | ||
b2 := marshal.WriteInt(b1, args.B) | ||
return b2 | ||
} | ||
|
||
func decArgs(args []byte) (*Args, bool) { | ||
a, args0, err0 := marshalutil.ReadInt(args) | ||
if err0 { | ||
return nil, true | ||
} | ||
b, args1, err1 := marshalutil.ReadInt(args0) | ||
if err1 || len(args1) != 0 { | ||
return nil, true | ||
} | ||
return &Args{A: a, B: b}, false | ||
} | ||
|
||
func encReply(reply uint64) []byte { | ||
var b0 []byte | ||
return marshal.WriteInt(b0, reply) | ||
} | ||
|
||
func decReply(reply *[]byte) (uint64, bool) { | ||
if reply == nil { | ||
return 0, true | ||
} | ||
out, reply0, err0 := marshalutil.ReadInt(*reply) | ||
if err0 || len(reply0) != 0 { | ||
return 0, true | ||
} | ||
return out, false | ||
} | ||
|
||
func servStub(args []byte, reply *[]byte) { | ||
args0, err0 := decArgs(args) | ||
if err0 { | ||
*reply = nil | ||
} | ||
*reply = encReply(Multiply(args0)) | ||
} | ||
|
||
func TestRPC(t *testing.T) { | ||
h := map[uint64]func([]byte, *[]byte){ | ||
2: servStub, | ||
} | ||
s := NewServer(h) | ||
addr := makeUniqueAddr() | ||
s.Serve(addr) | ||
|
||
c := Dial(addr) | ||
args0 := &Args{A: 7, B: 8} | ||
args1 := encArgs(args0) | ||
reply0 := new([]byte) | ||
err1 := c.Call(2, args1, reply0) | ||
if err1 { | ||
t.Fatal() | ||
} | ||
|
||
reply1, err2 := decReply(reply0) | ||
if err2 { | ||
t.Fatal() | ||
} | ||
if reply1 != 7*8 { | ||
t.Fatal() | ||
} | ||
} | ||
|
||
func makeUniqueAddr() uint64 { | ||
port := uint64(rand.IntN(4000)) + 6000 | ||
// left shift to make IP 0.0.0.0. | ||
return port << 32 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,22 @@ | ||
module github.com/mit-pdos/pav | ||
|
||
go 1.22 | ||
go 1.23 | ||
|
||
require ( | ||
github.com/goose-lang/goose v0.9.0 | ||
github.com/google/keytransparency v0.3.0 | ||
github.com/goose-lang/primitive v0.1.0 | ||
github.com/goose-lang/std v0.5.0 | ||
github.com/mit-pdos/gokv v0.1.1 | ||
github.com/tchajed/marshal v0.6.2 | ||
golang.org/x/tools v0.24.0 | ||
golang.org/x/tools v0.26.0 | ||
) | ||
|
||
require ( | ||
github.com/goose-lang/primitive v0.1.0 // indirect | ||
golang.org/x/mod v0.20.0 // indirect | ||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect | ||
github.com/golang/protobuf v1.4.2 // indirect | ||
github.com/google/trillian v1.3.10 // indirect | ||
golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4 // indirect | ||
golang.org/x/mod v0.21.0 // indirect | ||
golang.org/x/sync v0.8.0 // indirect | ||
golang.org/x/text v0.16.0 // indirect | ||
google.golang.org/protobuf v1.25.0 // indirect | ||
) |
Oops, something went wrong.