Skip to content

Commit

Permalink
oracle, leveldb, cmd : move midpointPrice to the leveldb driver packa…
Browse files Browse the repository at this point in the history
…ge and modify the way initializing bytes array
  • Loading branch information
tok-kkk committed Aug 7, 2018
1 parent ea20879 commit c72d0cc
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 92 deletions.
2 changes: 1 addition & 1 deletion cmd/darknode/darknode.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func main() {
}
defer store.Release()

midpointPriceStorer := oracle.NewMidpointPriceStorer()
midpointPriceStorer := leveldb.NewMidpointPriceStorer()

// Get own nonce from leveldb, if present and store multiaddress.
multi, err := store.SwarmMultiAddressStore().MultiAddress(multiAddr.Address())
Expand Down
2 changes: 1 addition & 1 deletion grpc/oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var _ = Describe("Oracle", func() {
Expect(err).ShouldNot(HaveOccurred())

oracler = oracle.NewOracler(client, &ecdsaKey, multiAddrStorer, 10)
midpointPriceStorer = oracle.NewMidpointPriceStorer()
midpointPriceStorer = leveldb.NewMidpointPriceStorer()
service = NewOracleService(oracle.NewServer(oracler, identity.Address(ecdsaKey.Address()), multiAddrStorer, midpointPriceStorer, 10), time.Microsecond)
serviceMultiAddr = client.MultiAddress()
server = NewServer()
Expand Down
39 changes: 39 additions & 0 deletions leveldb/oracle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package leveldb

import (
"sync"

"github.com/republicprotocol/republic-go/oracle"
)

// midpointPriceStorer implements MidpointPriceStorer interface with an
// in-memory map implementation. Data will be lost every time it restarts.
type midpointPriceStorer struct {
mu *sync.Mutex
prices oracle.MidpointPrice
}

// NewMidpointPriceStorer returns a new MidpointPriceStorer.
func NewMidpointPriceStorer() oracle.MidpointPriceStorer {
return &midpointPriceStorer{
mu: new(sync.Mutex),
}
}

// PutMidpointPrice implements the MidpointPriceStorer interface.
func (storer *midpointPriceStorer) PutMidpointPrice(midPointPrice oracle.MidpointPrice) error {
storer.mu.Lock()
defer storer.mu.Unlock()

storer.prices = midPointPrice

return nil
}

// MidpointPrice implements the MidpointPriceStorer interface.
func (storer *midpointPriceStorer) MidpointPrice() (oracle.MidpointPrice, error) {
storer.mu.Lock()
defer storer.mu.Unlock()

return storer.prices, nil
}
38 changes: 38 additions & 0 deletions leveldb/oracle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package leveldb_test

import (
"math/rand"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/republicprotocol/republic-go/leveldb"

"github.com/republicprotocol/republic-go/oracle"
"github.com/republicprotocol/republic-go/testutils"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

var _ = Describe("MidpointPrice storage", func() {

Context("when storing and retrieving data", func() {

It("should be able to get the right data we store", func() {
storer := NewMidpointPriceStorer()
emptyPrice, err := storer.MidpointPrice()
Expect(err).ShouldNot(HaveOccurred())
Expect(emptyPrice.Equals(oracle.MidpointPrice{})).Should(BeTrue())

price := testutils.RandMidpointPrice()
err = storer.PutMidpointPrice(price)
Expect(err).ShouldNot(HaveOccurred())

storedPrice, err := storer.MidpointPrice()
Expect(err).ShouldNot(HaveOccurred())
Expect(price.Equals(storedPrice)).Should(BeTrue())
})
})
})
18 changes: 9 additions & 9 deletions oracle/midpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ func (midpointPrice MidpointPrice) Equals(other MidpointPrice) bool {
func (midpointPrice MidpointPrice) Hash() []byte {
data := make([]byte, 0)
for i := range midpointPrice.TokenPairs {
tokensBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(tokensBytes, midpointPrice.TokenPairs[i])
data = append(data, tokensBytes...)
priceBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(priceBytes, midpointPrice.Prices[i])
data = append(data, priceBytes...)
tokensBytes := [8]byte{}
binary.LittleEndian.PutUint64(tokensBytes[:], midpointPrice.TokenPairs[i])
data = append(data, tokensBytes[:]...)
priceBytes := [8]byte{}
binary.LittleEndian.PutUint64(priceBytes[:], midpointPrice.Prices[i])
data = append(data, priceBytes[:]...)
}

nonceBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(nonceBytes, midpointPrice.Nonce)
data = append(data, nonceBytes...)
nonceBytes := [8]byte{}
binary.LittleEndian.PutUint64(nonceBytes[:], midpointPrice.Nonce)
data = append(data, nonceBytes[:]...)
return crypto.Keccak256(data)
}
8 changes: 4 additions & 4 deletions oracle/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import (

// A Client exposes methods for invoking RPCs on a remote server.
type Client interface {
// UpdateMidpoint is used to send updated midpoint information to a given
// multiAddress.
// UpdateMidpoint sends a midpointPrice to the target MultiAddress.
UpdateMidpoint(ctx context.Context, to identity.MultiAddress, midpointPrice MidpointPrice) error

// MultiAddress is used when finding random nodes to send information to.
// MultiAddress returns the multiAddress of the client.
MultiAddress() identity.MultiAddress
}

Expand Down Expand Up @@ -71,7 +70,8 @@ func (oracler *oracler) UpdateMidpoint(ctx context.Context, midpointPrice Midpoi

type Server interface {
// UpdateMidpoint verifies and stores updated midpoint information into a
// storer and broadcasts this information to the network.
// storer and broadcasts this information to the network if it is new
// information.
UpdateMidpoint(ctx context.Context, midpointPrice MidpointPrice) error
}

Expand Down
41 changes: 0 additions & 41 deletions oracle/store.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
package oracle

import (
"errors"
"sync"
)

// ErrCursorOutOfRange is returned when an iterator cursor is used to read a
// value outside the range of the iterator.
var ErrCursorOutOfRange = errors.New("cursor out of range")

// MidpointPriceStorer is used for retrieving/storing MidpointPrice.
type MidpointPriceStorer interface {

Expand All @@ -19,35 +10,3 @@ type MidpointPriceStorer interface {
// MidpointPrice returns the latest mid-point price of the given token.
MidpointPrice() (MidpointPrice, error)
}

// midpointPriceStorer implements MidpointPriceStorer interface with an
// in-memory map implementation. Data will be lost every time it restarts.
type midpointPriceStorer struct {
mu *sync.Mutex
prices MidpointPrice
}

// NewMidpointPriceStorer returns a new MidpointPriceStorer.
func NewMidpointPriceStorer() MidpointPriceStorer {
return &midpointPriceStorer{
mu: new(sync.Mutex),
}
}

// PutMidpointPrice implements the MidpointPriceStorer interface.
func (storer *midpointPriceStorer) PutMidpointPrice(midPointPrice MidpointPrice) error {
storer.mu.Lock()
defer storer.mu.Unlock()

storer.prices = midPointPrice

return nil
}

// MidpointPrice implements the MidpointPriceStorer interface.
func (storer *midpointPriceStorer) MidpointPrice() (MidpointPrice, error) {
storer.mu.Lock()
defer storer.mu.Unlock()

return storer.prices, nil
}
35 changes: 0 additions & 35 deletions oracle/store_test.go
Original file line number Diff line number Diff line change
@@ -1,36 +1 @@
package oracle_test

import (
"math/rand"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/republicprotocol/republic-go/oracle"
"github.com/republicprotocol/republic-go/testutils"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

var _ = Describe("MidpointPrice storage", func() {

Context("when storing and retrieving data", func() {

It("should be able to get the right data we store", func() {
storer := NewMidpointPriceStorer()
emptyPrice, err := storer.MidpointPrice()
Expect(err).ShouldNot(HaveOccurred())
Expect(emptyPrice.Equals(MidpointPrice{})).Should(BeTrue())

price := testutils.RandMidpointPrice()
err = storer.PutMidpointPrice(price)
Expect(err).ShouldNot(HaveOccurred())

storedPrice, err := storer.MidpointPrice()
Expect(err).ShouldNot(HaveOccurred())
Expect(price.Equals(storedPrice)).Should(BeTrue())
})
})
})
3 changes: 2 additions & 1 deletion testutils/mock_oracler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/pkg/errors"
"github.com/republicprotocol/republic-go/identity"
"github.com/republicprotocol/republic-go/leveldb"
"github.com/republicprotocol/republic-go/oracle"
)

Expand All @@ -16,7 +17,7 @@ type MockOracleClient struct {
}

func NewMockOracleClient(addr identity.Address, hub map[identity.Address]oracle.Server) (oracle.Client, oracle.MidpointPriceStorer, error) {
storer := oracle.NewMidpointPriceStorer()
storer := leveldb.NewMidpointPriceStorer()
return &MockOracleClient{
addr: addr,
store: storer,
Expand Down

0 comments on commit c72d0cc

Please sign in to comment.