Skip to content
This repository has been archived by the owner on Aug 21, 2021. It is now read-only.

Commit

Permalink
Change Type References
Browse files Browse the repository at this point in the history
Since we moved orders from being byte arrays to pointers to custom types,
we have lots of references that have to be updated. This gets everything
compiling, though we still need to confirm that all tests will pass.
  • Loading branch information
AusIV committed Nov 22, 2017
1 parent 339cce3 commit 276826f
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 79 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ bin/ingest: $(BASE) cmd/ingest/main.go
cd $(BASE) && $(GOSTATIC) -o bin/ingest cmd/ingest/main.go

bin/initialize: $(BASE) cmd/initialize/main.go
cd $(BASE) && CGO_ENABLED=0 $(GOSTATIC) -o bin/initialize cmd/initialize/main.go
cd $(BASE) && $(GOSTATIC) -o bin/initialize cmd/initialize/main.go

bin/simplerelay: $(BASE) cmd/simplerelay/main.go
cd $(BASE) && CGO_ENABLED=0 $(GOSTATIC) -o bin/simplerelay cmd/simplerelay/main.go
Expand Down
9 changes: 6 additions & 3 deletions accounts/interface.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package accounts

import "math/big"
import (
"math/big"
"github.com/notegio/openrelay/types"
)

type Account interface {
Blacklisted() bool
Discount() *big.Int
}

type AccountService interface {
Get([20]byte) Account
Set([20]byte, Account) error
Get(*types.Address) Account
Set(*types.Address, Account) error
}
5 changes: 3 additions & 2 deletions accounts/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package accounts
import (
"encoding/json"
"github.com/notegio/openrelay/config"
"github.com/notegio/openrelay/types"
"gopkg.in/redis.v3"
"math/big"
)
Expand All @@ -12,7 +13,7 @@ type redisAccountService struct {
baseFee config.BaseFee
}

func (accountService *redisAccountService) Get(address [20]byte) Account {
func (accountService *redisAccountService) Get(address *types.Address) Account {
acct := &account{false, new(big.Int), 0, 0}
acctJSON, err := accountService.redisClient.Get("account::" + string(address[:])).Result()
if err != nil {
Expand All @@ -30,7 +31,7 @@ func (accountService *redisAccountService) Get(address [20]byte) Account {
return acct
}

func (accountService *redisAccountService) Set(address [20]byte, acct Account) error {
func (accountService *redisAccountService) Set(address *types.Address, acct Account) error {
data, err := json.Marshal(acct)
if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions affiliates/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package affiliates

import (
"math/big"
"github.com/notegio/openrelay/types"
)

type Affiliate interface {
Fee() *big.Int
}

type AffiliateService interface {
Get([20]byte) (Affiliate, error)
Set([20]byte, Affiliate) error
Get(*types.Address) (Affiliate, error)
Set(*types.Address, Affiliate) error
}
5 changes: 3 additions & 2 deletions affiliates/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package affiliates
import (
"encoding/json"
"github.com/notegio/openrelay/config"
"github.com/notegio/openrelay/types"
"gopkg.in/redis.v3"
"math/big"
)
Expand All @@ -12,7 +13,7 @@ type redisAffiliateService struct {
baseFee config.BaseFee
}

func (affiliateService *redisAffiliateService) Get(address [20]byte) (Affiliate, error) {
func (affiliateService *redisAffiliateService) Get(address *types.Address) (Affiliate, error) {
acct := &affiliate{new(big.Int), 100}
acctJSON, err := affiliateService.redisClient.Get("affiliate::" + string(address[:])).Result()
if err != nil {
Expand All @@ -30,7 +31,7 @@ func (affiliateService *redisAffiliateService) Get(address [20]byte) (Affiliate,
return acct, nil
}

func (affiliateService *redisAffiliateService) Set(address [20]byte, acct Affiliate) error {
func (affiliateService *redisAffiliateService) Set(address *types.Address, acct Affiliate) error {
data, err := json.Marshal(acct)
if err != nil {
return err
Expand Down
7 changes: 4 additions & 3 deletions cmd/getbalance/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package main
import (
"os"
"github.com/notegio/openrelay/funds"
"github.com/notegio/openrelay/types"
"log"
"encoding/hex"
"fmt"
)

func hexToBytes(address string) [20]byte {
func hexToBytes(address string) *types.Address {
slice, err := hex.DecodeString(address)
if err != nil {
return [20]byte{}
return &types.Address{}
}
output := [20]byte{}
output := &types.Address{}
copy(output[:], slice[:])
return output
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/initialize/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"gopkg.in/redis.v3"
"github.com/notegio/openrelay/config"
"github.com/notegio/openrelay/affiliates"
"github.com/notegio/openrelay/types"
"math/big"
"os"
"fmt"
Expand All @@ -25,7 +26,7 @@ func main() {
affiliateService := affiliates.NewRedisAffiliateService(redisClient)
for _, address := range(authorizedAddresses) {
if addressBytes, err := hex.DecodeString(address); err == nil {
addressArray := [20]byte{}
addressArray := &types.Address{}
copy(addressArray[:], addressBytes[:])
affiliate := affiliates.NewAffiliate(baseFeeInt, 100)
affiliateService.Set(addressArray, affiliate)
Expand Down
5 changes: 5 additions & 0 deletions common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ package common
import (
"encoding/hex"
"github.com/ethereum/go-ethereum/common"
"github.com/notegio/openrelay/types"
)

func BytesToAddress(data [20]byte) common.Address {
return common.HexToAddress(hex.EncodeToString(data[:]))
}

func ToGethAddress(data *types.Address) common.Address {
return common.HexToAddress(hex.EncodeToString(data[:]))
}

func HexToBytes(hexString string) ([20]byte, error) {
slice, err := hex.DecodeString(hexString)
result := [20]byte{}
Expand Down
24 changes: 13 additions & 11 deletions config/feeToken.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,41 @@ import (
"encoding/hex"
"gopkg.in/redis.v3"
"time"
"github.com/notegio/openrelay/types"

)

type FeeToken interface {
Get() ([20]byte, error)
Set([20]byte) error
Get() (*types.Address, error)
Set(*types.Address) error
}

type staticFeeToken struct {
value [20]byte
value *types.Address
}

func (feeToken *staticFeeToken) Get() ([20]byte, error) {
func (feeToken *staticFeeToken) Get() (*types.Address, error) {
return feeToken.value, nil
}

func (feeToken *staticFeeToken) Set(address [20]byte) error {
func (feeToken *staticFeeToken) Set(address *types.Address) error {
feeToken.value = address
return nil
}

type redisFeeToken struct {
redisClient *redis.Client
cachedValue [20]byte
cachedValue *types.Address
cacheExpiration int64
}

func (feeToken *redisFeeToken) Get() ([20]byte, error) {
func (feeToken *redisFeeToken) Get() (*types.Address, error) {
if feeToken.cacheExpiration > time.Now().Unix() {
// The fee token is unlikely to change, so caching it should be fine.
// Doesn't hurt to check periodically just in case though.
return feeToken.cachedValue, nil
}
result := [20]byte{}
result := &types.Address{}
val, err := feeToken.redisClient.Get("feeToken::address").Result()
if err != nil {
return result, err
Expand All @@ -49,14 +51,14 @@ func (feeToken *redisFeeToken) Get() ([20]byte, error) {
return result, nil
}

func (feeToken *redisFeeToken) Set(value [20]byte) error {
func (feeToken *redisFeeToken) Set(value *types.Address) error {
return feeToken.redisClient.Set("feeToken::address", hex.EncodeToString(value[:]), 0).Err()
}

func NewFeeToken(client *redis.Client) FeeToken {
return &redisFeeToken{client, [20]byte{}, 0}
return &redisFeeToken{client, &types.Address{}, 0}
}

func StaticFeeToken(address [20]byte) FeeToken {
func StaticFeeToken(address *types.Address) FeeToken {
return &staticFeeToken{address}
}
24 changes: 13 additions & 11 deletions config/tokenProxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,41 @@ import (
"encoding/hex"
"gopkg.in/redis.v3"
"time"
"github.com/notegio/openrelay/types"

)

type TokenProxy interface {
Get() ([20]byte, error)
Set([20]byte) error
Get() (*types.Address, error)
Set(*types.Address) error
}

type staticTokenProxy struct {
value [20]byte
value *types.Address
}

func (tokenProxy *staticTokenProxy) Get() ([20]byte, error) {
func (tokenProxy *staticTokenProxy) Get() (*types.Address, error) {
return tokenProxy.value, nil
}

func (tokenProxy *staticTokenProxy) Set(address [20]byte) error {
func (tokenProxy *staticTokenProxy) Set(address *types.Address) error {
tokenProxy.value = address
return nil
}

type redisTokenProxy struct {
redisClient *redis.Client
cachedValue [20]byte
cachedValue *types.Address
cacheExpiration int64
}

func (tokenProxy *redisTokenProxy) Get() ([20]byte, error) {
func (tokenProxy *redisTokenProxy) Get() (*types.Address, error) {
if tokenProxy.cacheExpiration > time.Now().Unix() {
// The token proxy shouldn't change often, but it doesn't hurt to check
// periodically.
return tokenProxy.cachedValue, nil
}
result := [20]byte{}
result := &types.Address{}
val, err := tokenProxy.redisClient.Get("tokenProxy::address").Result()
if err != nil {
return result, err
Expand All @@ -50,14 +52,14 @@ func (tokenProxy *redisTokenProxy) Get() ([20]byte, error) {
return result, nil
}

func (tokenProxy *redisTokenProxy) Set(value [20]byte) error {
func (tokenProxy *redisTokenProxy) Set(value *types.Address) error {
return tokenProxy.redisClient.Set("tokenProxy::address", hex.EncodeToString(value[:]), 0).Err()
}

func NewTokenProxy(client *redis.Client) TokenProxy {
return &redisTokenProxy{client, [20]byte{}, 0}
return &redisTokenProxy{client, &types.Address{}, 0}
}

func StaticTokenProxy(address [20]byte) TokenProxy {
func StaticTokenProxy(address *types.Address) TokenProxy {
return &staticTokenProxy{address}
}
17 changes: 9 additions & 8 deletions funds/balance_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,33 @@ import (
"github.com/ethereum/go-ethereum/ethclient"
orCommon "github.com/notegio/openrelay/common"
tokenModule "github.com/notegio/openrelay/token"
"github.com/notegio/openrelay/types"
"math/big"
)

type BalanceChecker interface {
GetBalance(tokenAddress, userAddress [20]byte) (*big.Int, error)
GetAllowance(tokenAddress, ownerAddress, spenderAddress [20]byte) (*big.Int, error)
GetBalance(tokenAddress, userAddress *types.Address) (*big.Int, error)
GetAllowance(tokenAddress, ownerAddress, spenderAddress *types.Address) (*big.Int, error)
}

type rpcBalanceChecker struct {
conn bind.ContractBackend
}

func (funds *rpcBalanceChecker) GetBalance(tokenAddrBytes, userAddrBytes [20]byte) (*big.Int, error) {
token, err := tokenModule.NewToken(orCommon.BytesToAddress(tokenAddrBytes), funds.conn)
func (funds *rpcBalanceChecker) GetBalance(tokenAddrBytes, userAddrBytes *types.Address) (*big.Int, error) {
token, err := tokenModule.NewToken(orCommon.ToGethAddress(tokenAddrBytes), funds.conn)
if err != nil {
return nil, err
}
return token.BalanceOf(nil, orCommon.BytesToAddress(userAddrBytes))
return token.BalanceOf(nil, orCommon.ToGethAddress(userAddrBytes))
}

func (funds *rpcBalanceChecker) GetAllowance(tokenAddrBytes, ownerAddress, spenderAddress [20]byte) (*big.Int, error) {
token, err := tokenModule.NewToken(orCommon.BytesToAddress(tokenAddrBytes), funds.conn)
func (funds *rpcBalanceChecker) GetAllowance(tokenAddrBytes, ownerAddress, spenderAddress *types.Address) (*big.Int, error) {
token, err := tokenModule.NewToken(orCommon.ToGethAddress(tokenAddrBytes), funds.conn)
if err != nil {
return nil, err
}
return token.Allowance(nil, orCommon.BytesToAddress(ownerAddress), orCommon.BytesToAddress(spenderAddress))
return token.Allowance(nil, orCommon.ToGethAddress(ownerAddress), orCommon.ToGethAddress(spenderAddress))
}

func NewRpcBalanceChecker(rpcUrl string) (BalanceChecker, error) {
Expand Down
6 changes: 3 additions & 3 deletions funds/filled_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"reflect"
)

func doLookup(order *types.Order, oldValue [32]byte, lookupFn func(*types.Order) ([32]byte, error), valChan chan [32]byte, changeChan chan bool) {
func doLookup(order *types.Order, oldValue *types.Uint256, lookupFn func(*types.Order) (*types.Uint256, error), valChan chan *types.Uint256, changeChan chan bool) {
value, err := lookupFn(order)
if err != nil {
log.Printf(err.Error())
Expand All @@ -28,8 +28,8 @@ func (consumer *FillConsumer) Consume(msg channels.Delivery) {
orderBytes := [441]byte{}
copy(orderBytes[:], []byte(msg.Payload()))
order := types.OrderFromBytes(orderBytes)
cancelledChan := make(chan [32]byte)
filledChan := make(chan [32]byte)
cancelledChan := make(chan *types.Uint256)
filledChan := make(chan *types.Uint256)
changes := make(chan bool, 2)
go doLookup(order, order.TakerTokenAmountCancelled, consumer.lookup.GetAmountCancelled, cancelledChan, changes)
go doLookup(order, order.TakerTokenAmountFilled, consumer.lookup.GetAmountFilled, filledChan, changes)
Expand Down
Loading

0 comments on commit 276826f

Please sign in to comment.