Skip to content

Commit

Permalink
versioned market
Browse files Browse the repository at this point in the history
  • Loading branch information
antmat committed Jan 25, 2019
1 parent 69a1f75 commit dadc712
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 6 deletions.
14 changes: 13 additions & 1 deletion blockchain/addresses.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
package blockchain

import "fmt"

const (
defaultContractRegistryAddr = "0xd1a6f3d1ae33b4b19565a6b283d7a05c5a0decb0"

sidechainSNMAddressKey = "sidechainSNMAddress"
masterchainSNMAddressKey = "masterchainSNMAddress"
blacklistAddressKey = "blacklistAddress"
marketAddressKey = "marketV2Address"
profileRegistryAddressKey = "profileRegistryAddress"
oracleUsdAddressKey = "oracleUsdAddress"
gatekeeperMasterchainAddressKey = "gatekeeperMasterchainAddress"
gatekeeperSidechainAddressKey = "gatekeeperSidechainAddress"
testnetFaucetAddressKey = "testnetFaucetAddress"
oracleMultiSigAddressKey = "oracleMultiSigAddress"
)

func marketAddressKey(version uint) (string, error) {
switch version {
case 1:
return "marketAddress", nil
case 2:
return "marketV2Address", nil
default:
return "", fmt.Errorf("invalid market version %d", version)
}
}
12 changes: 8 additions & 4 deletions blockchain/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func NewAPI(ctx context.Context, opts ...Option) (API, error) {
}

func (api *BasicAPI) setupContractRegistry(ctx context.Context) error {
registry, err := NewRegistry(ctx, api.options.contractRegistry, api.options.sidechain)
registry, err := NewRegistry(ctx, api.options.version, api.options.contractRegistry, api.options.sidechain)
if err != nil {
return fmt.Errorf("failed to setup contract registry: %s", err)
}
Expand Down Expand Up @@ -405,7 +405,7 @@ func NewRegistry(ctx context.Context, address common.Address, opts *chainOpts) (
registry := &BasicContractRegistry{
registryContract: contract,
}
if err := registry.setup(ctx); err != nil {
if err := registry.setup(ctx, version); err != nil {
return nil, err
}
return registry, nil
Expand Down Expand Up @@ -444,15 +444,19 @@ func (m *BasicContractRegistry) readContract(ctx context.Context, key string, ta
return nil
}

func (m *BasicContractRegistry) setup(ctx context.Context) error {
func (m *BasicContractRegistry) setup(ctx context.Context, version uint) error {
marketKey, err := marketAddressKey(version)
if err != nil {
return err
}
addresses := []struct {
key string
target *common.Address
}{
{sidechainSNMAddressKey, &m.sidechainSNMAddress},
{masterchainSNMAddressKey, &m.masterchainSNMAddress},
{blacklistAddressKey, &m.blacklistAddress},
{marketAddressKey, &m.marketAddress},
{marketKey, &m.marketAddress},
{profileRegistryAddressKey, &m.profileRegistryAddress},
{oracleUsdAddressKey, &m.oracleUsdAddress},
{gatekeeperMasterchainAddressKey, &m.gatekeeperMasterchainAddress},
Expand Down
8 changes: 8 additions & 0 deletions blockchain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Config struct {
ContractRegistryAddr common.Address
BlocksBatchSize uint64
MasterchainGasPrice *big.Int
Version uint
}

func NewDefaultConfig() (*Config, error) {
Expand All @@ -32,6 +33,7 @@ func NewDefaultConfig() (*Config, error) {
Endpoint: *endpoint,
SidechainEndpoint: *sidechainEndpoint,
ContractRegistryAddr: common.HexToAddress(defaultContractRegistryAddr),
Version: defaultVersion,
}, nil
}

Expand All @@ -42,6 +44,7 @@ func (m *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
ContractRegistryAddr common.Address `yaml:"contract_registry"`
BlocksBatchSize uint64 `yaml:"blocks_batch_size"`
MasterchainGasPrice GasPrice `yaml:"masterchain_gas_price"`
Version uint
}

if err := unmarshal(&cfg); err != nil {
Expand All @@ -56,6 +59,10 @@ func (m *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
cfg.SidechainEndpoint = defaultSidechainEndpoint
}

if cfg.Version == 0 {
cfg.Version = defaultVersion
}

endpoint, err := url.Parse(cfg.MasterchainEndpoint)
if err != nil {
return err
Expand All @@ -75,6 +82,7 @@ func (m *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
m.ContractRegistryAddr = cfg.ContractRegistryAddr
m.BlocksBatchSize = cfg.BlocksBatchSize
m.MasterchainGasPrice = cfg.MasterchainGasPrice.Int
m.Version = cfg.Version

return nil
}
11 changes: 10 additions & 1 deletion blockchain/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const (
defaultLogParsePeriod = time.Second
defaultMasterchainGasLimit = 500000
defaultSidechainGasLimit = 2000000
defaultBlockBatchSize = 50
defaultBlockBatchSize = 500
defaultVersion = 1
)

// chainOpts describes common options
Expand Down Expand Up @@ -60,6 +61,7 @@ type options struct {
contractRegistry common.Address
blocksBatchSize uint64
niceMarket bool
version uint
}

func defaultOptions() *options {
Expand Down Expand Up @@ -125,6 +127,7 @@ func WithConfig(cfg *Config) Option {
}
o.blocksBatchSize = cfg.BlocksBatchSize
o.masterchain.gasPrice = cfg.MasterchainGasPrice
o.version = cfg.Version
}
}
}
Expand Down Expand Up @@ -165,3 +168,9 @@ func WithNiceMarket() Option {
o.niceMarket = true
}
}

func WithVersion(version uint) Option {
return func(o *options) {
o.version = version
}
}
4 changes: 4 additions & 0 deletions etc/node.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
migration_dwh:
endpoint:


# Local Node settings
node:
# Node's port to listen for client connection
Expand Down
76 changes: 76 additions & 0 deletions insonmnia/migration/migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package migration

import (
"context"
"crypto/ecdsa"

"github.com/ethereum/go-ethereum/crypto"
"github.com/sonm-io/core/blockchain"
"github.com/sonm-io/core/insonmnia/dwh"
"github.com/sonm-io/core/proto"
"github.com/sonm-io/core/util/multierror"
"github.com/sonm-io/core/util/xgrpc"
)

type MarketShredder struct {
api blockchain.API
dwh sonm.DWHClient
}

func NewV1MarketShredder(ctx context.Context, bcCfg *blockchain.Config, dwhCfg dwh.YAMLConfig) (*MarketShredder, error) {
api, err := blockchain.NewAPI(ctx, blockchain.WithConfig(bcCfg), blockchain.WithVersion(1))
if err != nil {
return nil, err
}

cc, err := xgrpc.NewClient(ctx, dwhCfg.Endpoint, credentials)
if err != nil {
return err
}

dwh := sonm.NewDWHClient(cc)
}

func NewMarketShredder(api blockchain.API, dwh sonm.DWHClient) *MarketShredder {
return &MarketShredder{
api: api,
dwh: dwh,
}
}

func (m *MarketShredder) WeDontNeedNoWaterLetTheMothefuckerBurn(ctx context.Context, pKey *ecdsa.PrivateKey) error {
author := crypto.PubkeyToAddress(pKey.PublicKey)
ordersRequest := &sonm.OrdersRequest{
AuthorID: sonm.NewEthAddress(author),
}
orders, err := m.dwh.GetOrders(ctx, ordersRequest)
if err != nil {
return err
}
market := m.api.Market()
merr := multierror.NewMultiError()
for _, order := range orders.GetOrders() {
if err := market.CancelOrder(ctx, pKey, order.GetOrder().GetId().Unwrap()); err != nil {
merr = multierror.Append(merr, err)
}
}

dealsRequest := &sonm.DealsRequest{
AnyUserID: sonm.NewEthAddress(author),
}
deals, err := m.dwh.GetDeals(ctx, dealsRequest)
if err != nil {
return err
}

for _, deal := range deals.GetDeals() {
// We only match masterID, but master cannot close deals.
if deal.GetDeal().GetConsumerID().Unwrap() != author && deal.GetDeal().GetSupplierID().Unwrap() != author {
continue
}
if err := market.CloseDeal(ctx, pKey, deal.GetDeal().GetId().Unwrap(), sonm.BlacklistType_BLACKLIST_NOBODY); err != nil {
merr = multierror.Append(merr, err)
}
}
return merr.ErrorOrNil()
}

0 comments on commit dadc712

Please sign in to comment.