Skip to content

Commit

Permalink
Merge pull request #77 from republicprotocol/monday-deployment
Browse files Browse the repository at this point in the history
Oracle, hierarchical pod optimisation and FOK orders.
  • Loading branch information
loongy committed Aug 7, 2018
2 parents 97d0f4f + c72d0cc commit 12f053b
Show file tree
Hide file tree
Showing 54 changed files with 2,192 additions and 1,234 deletions.
6 changes: 6 additions & 0 deletions cmd/confgen/confgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ import (

func main() {
network := flag.String("network", "nightly", "Republic Protocol network")
oracleAddress := flag.String("oracleAddress", "", "Oracle address")

flag.Parse()

if *oracleAddress == "" {
log.Fatalf("oracle address not specified")
}

keystore, err := crypto.RandomKeystore()
if err != nil {
log.Fatalf("cannot create keystore: %v", err)
Expand Down Expand Up @@ -50,6 +55,7 @@ func main() {
Host: "0.0.0.0",
Port: "18514",
Address: identity.Address(keystore.Address()),
OracleAddress: identity.Address(*oracleAddress),
BootstrapMultiAddresses: []identity.MultiAddress{},
Logs: logger.Options{
Plugins: []logger.PluginOptions{
Expand Down
1 change: 1 addition & 0 deletions cmd/darknode/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Config struct {
Logs logger.Options `json:"logs"`

Address identity.Address `json:"address"`
OracleAddress identity.Address `json:"oracleAddress"`
BootstrapMultiAddresses identity.MultiAddresses `json:"bootstrapMultiAddresses"`
Host string `json:"host"`
Port string `json:"port"`
Expand Down
35 changes: 23 additions & 12 deletions cmd/darknode/darknode.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/republicprotocol/republic-go/leveldb"
"github.com/republicprotocol/republic-go/logger"
"github.com/republicprotocol/republic-go/ome"
"github.com/republicprotocol/republic-go/oracle"
"github.com/republicprotocol/republic-go/orderbook"
"github.com/republicprotocol/republic-go/registry"
"github.com/republicprotocol/republic-go/smpc"
Expand Down Expand Up @@ -84,6 +85,8 @@ func main() {
}
defer store.Release()

midpointPriceStorer := leveldb.NewMidpointPriceStorer()

// Get own nonce from leveldb, if present and store multiaddress.
multi, err := store.SwarmMultiAddressStore().MultiAddress(multiAddr.Address())
if err != nil {
Expand All @@ -103,20 +106,25 @@ func main() {
log.Fatalf("cannot sign own multiaddress: %v", err)
}
multiAddr.Signature = multiAddrSignature

if err := store.SwarmMultiAddressStore().PutMultiAddress(multiAddr); err != nil {
if err := store.SwarmMultiAddressStore().InsertMultiAddress(multiAddr); err != nil {
log.Fatalf("cannot store own multiaddress in leveldb: %v", err)
}
log.Printf("current nonce %v, length of signature: %v", multiAddr.Nonce, len(multiAddr.Signature))

// New gRPC components
server := grpc.NewServer()

swarmClient := grpc.NewSwarmClient(store.SwarmMultiAddressStore(), multiAddr.Address())
swarmer := swarm.NewSwarmer(swarmClient, store.SwarmMultiAddressStore(), config.Alpha, &config.Keystore.EcdsaKey)
swarmService := grpc.NewSwarmService(swarm.NewServer(swarmer, store.SwarmMultiAddressStore(), config.Alpha), time.Millisecond)
swarmer := swarm.NewSwarmer(swarmClient, store.SwarmMultiAddressStore(), config.Alpha, &crypter)
swarmService := grpc.NewSwarmService(swarm.NewServer(swarmer, store.SwarmMultiAddressStore(), config.Alpha, &crypter), time.Millisecond)
swarmService.Register(server)

orderbook := orderbook.NewOrderbook(config.Keystore.RsaKey, store.OrderbookPointerStore(), store.OrderbookOrderStore(), store.OrderbookOrderFragmentStore(), &contractBinder, 5*time.Second, 32)
oracleClient := grpc.NewOracleClient(multiAddr.Address(), store.SwarmMultiAddressStore())
oracler := oracle.NewOracler(oracleClient, &config.Keystore.EcdsaKey, store.SwarmMultiAddressStore(), config.Alpha)
oracleService := grpc.NewOracleService(oracle.NewServer(oracler, config.OracleAddress, store.SwarmMultiAddressStore(), midpointPriceStorer, config.Alpha), time.Millisecond)
oracleService.Register(server)

orderbook := orderbook.NewOrderbook(config.Address, config.Keystore.RsaKey, store.OrderbookPointerStore(), store.OrderbookOrderStore(), store.OrderbookOrderFragmentStore(), &contractBinder, 5*time.Second, 32)
orderbookService := grpc.NewOrderbookService(orderbook)
orderbookService.Register(server)

Expand Down Expand Up @@ -181,19 +189,22 @@ func main() {

// Bootstrap into the network
fmtStr := "bootstrapping\n"
for _, multiAddr := range config.BootstrapMultiAddresses {
multi, err := store.SwarmMultiAddressStore().MultiAddress(multiAddr.Address())
for _, bootstrapMulti := range config.BootstrapMultiAddresses {
if bootstrapMulti.Address() == multiAddr.Address() {
continue
}
multi, err := store.SwarmMultiAddressStore().MultiAddress(bootstrapMulti.Address())
if err != nil && err != swarm.ErrMultiAddressNotFound {
logger.Network(logger.LevelError, fmt.Sprintf("cannot get bootstrap details from store: %v", err))
logger.Network(logger.LevelError, fmt.Sprintf("cannot get bootstrap multi-address from store: %v", err))
continue
}
if err == nil {
multiAddr.Nonce = multi.Nonce
bootstrapMulti.Nonce = multi.Nonce
}
if err := store.SwarmMultiAddressStore().PutMultiAddress(multiAddr); err != nil {
if err := store.SwarmMultiAddressStore().InsertMultiAddress(bootstrapMulti); err != nil {
logger.Network(logger.LevelError, fmt.Sprintf("cannot store bootstrap multiaddress in store: %v", err))
}
fmtStr += " " + multiAddr.String() + "\n"
fmtStr += " " + bootstrapMulti.String() + "\n"
}
log.Printf(fmtStr)
pingNetwork(swarmer)
Expand All @@ -206,7 +217,7 @@ func main() {
if err != nil {
logger.Error(fmt.Sprintf("cannot get previous epoch: %v", err))
}
gen := ome.NewComputationGenerator(store.SomerOrderFragmentStore())
gen := ome.NewComputationGenerator(config.Address, store.SomerOrderFragmentStore())
matcher := ome.NewMatcher(store.SomerComputationStore(), smpcer)
confirmer := ome.NewConfirmer(store.SomerComputationStore(), &contractBinder, 5*time.Second, 2)
settler := ome.NewSettler(store.SomerComputationStore(), smpcer, &contractBinder)
Expand Down
29 changes: 21 additions & 8 deletions contract/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/republicprotocol/republic-go/contract/bindings"
"github.com/republicprotocol/republic-go/crypto"
"github.com/republicprotocol/republic-go/identity"
"github.com/republicprotocol/republic-go/logger"
"github.com/republicprotocol/republic-go/order"
"github.com/republicprotocol/republic-go/registry"
"github.com/republicprotocol/republic-go/stackint"
Expand Down Expand Up @@ -217,38 +218,36 @@ func (binder *Binder) submitMatch(buy, sell order.ID) (*types.Transaction, error
}

// Settle the order pair which gets confirmed by the Orderbook
func (binder *Binder) Settle(buy order.Order, sell order.Order) (err error) {
func (binder *Binder) Settle(buy order.Order, sell order.Order) error {
binder.mu.Lock()
defer binder.mu.Unlock()

// Submit orders
if _, sendTxErr := binder.sendTx(func() (*types.Transaction, error) {
return binder.submitOrder(buy)
}); sendTxErr != nil {
err = fmt.Errorf("cannot settle buy = %v: %v", buy.ID, sendTxErr)
logger.Warn(fmt.Sprintf("cannot settle buy = %v: %v", buy.ID, sendTxErr))
}
if _, sendTxErr := binder.sendTx(func() (*types.Transaction, error) {
return binder.submitOrder(sell)
}); sendTxErr != nil {
err = fmt.Errorf("cannot settle sell = %v: %v", sell.ID, sendTxErr)
logger.Warn(fmt.Sprintf("cannot settle sell = %v: %v", sell.ID, sendTxErr))
}

// Submit match
tx, sendTxErr := binder.sendTx(func() (*types.Transaction, error) {
return binder.submitMatch(buy.ID, sell.ID)
})
if sendTxErr != nil {
err = fmt.Errorf("cannot settle buy = %v, sell = %v: %v", buy.ID, sell.ID, sendTxErr)
return err
return fmt.Errorf("cannot settle buy = %v, sell = %v: %v", buy.ID, sell.ID, sendTxErr)
}

// Wait for last transaction
if _, waitErr := binder.conn.PatchedWaitMined(context.Background(), tx); waitErr != nil {
err = fmt.Errorf("cannot wait to settle buy = %v, sell = %v: %v", buy.ID, sell.ID, waitErr)
return err
return fmt.Errorf("cannot wait to settle buy = %v, sell = %v: %v", buy.ID, sell.ID, waitErr)
}

return err
return nil
}

// Register a new dark node with the dark node registrar
Expand Down Expand Up @@ -578,6 +577,20 @@ func (binder *Binder) Epoch() (registry.Epoch, error) {
return binder.epoch(epoch)
}

func (binder *Binder) EpochHash() ([32]byte, error) {
binder.mu.RLock()
defer binder.mu.RUnlock()

epoch, err := binder.darknodeRegistry.CurrentEpoch(binder.callOpts)
if err != nil {
return [32]byte{}, err
}
var res [32]byte
copy(res[:], epoch.Epochhash.Bytes())

return res, nil
}

// PreviousEpoch returns the previous Epoch which includes the Pod configuration.
func (binder *Binder) PreviousEpoch() (registry.Epoch, error) {
binder.mu.RLock()
Expand Down
Loading

0 comments on commit 12f053b

Please sign in to comment.