Skip to content

Commit

Permalink
Move types and utils from pkg to subpackages to avoid cyclic dependen…
Browse files Browse the repository at this point in the history
…cies
  • Loading branch information
Max-Levitskiy committed Jul 3, 2023
1 parent 5a8a555 commit 1327c08
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 103 deletions.
11 changes: 6 additions & 5 deletions contract/pkg/actcapture/activity_capture_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
"github.com/cerebellum-network/cere-ddc-sdk-go/contract/pkg"
"github.com/cerebellum-network/cere-ddc-sdk-go/contract/pkg/sdktypes"
"github.com/cerebellum-network/cere-ddc-sdk-go/contract/pkg/utils"
log "github.com/sirupsen/logrus"
"math/big"
)
Expand All @@ -26,7 +27,7 @@ type (
}

activityCaptureContract struct {
client pkg.BlockchainClient
client sdktypes.BlockchainClient
account types.AccountID
keyringPair signature.KeyringPair
contractAddress types.AccountID
Expand All @@ -37,7 +38,7 @@ type (
}
)

func CreateActivityCaptureContract(client pkg.BlockchainClient, contractAddressSS58 string, secret string) ActivityCaptureContract {
func CreateActivityCaptureContract(client sdktypes.BlockchainClient, contractAddressSS58 string, secret string) ActivityCaptureContract {
keyringPair, err := signature.KeyringPairFromSecret(secret, 42)
if err != nil {
log.WithError(err).Fatal("Can't initialize keyring pair for activity capture contract")
Expand All @@ -63,7 +64,7 @@ func CreateActivityCaptureContract(client pkg.BlockchainClient, contractAddressS
log.WithError(err).WithField("method", getEraSettings).Fatal("Can't decode method getEraSettingsMethod")
}

contractAddress, err := pkg.DecodeAccountIDFromSS58(contractAddressSS58)
contractAddress, err := utils.DecodeAccountIDFromSS58(contractAddressSS58)
if err != nil {
log.WithError(err).WithField("contractAddressSS58", contractAddressSS58).Fatal("Can't decode contract address SS58")
}
Expand Down Expand Up @@ -101,7 +102,7 @@ func (a *activityCaptureContract) SetCommit(ctx context.Context, hash []byte, ga
From := types.U64(from)
To := types.U64(to)

call := pkg.ContractCall{
call := sdktypes.ContractCall{
ContractAddress: a.contractAddress,
ContractAddressSS58: a.contractAddressSS58,
From: a.keyringPair,
Expand Down
16 changes: 8 additions & 8 deletions contract/pkg/bucket/ddc_bucket_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
_ "embed"
"encoding/hex"
"errors"
"github.com/cerebellum-network/cere-ddc-sdk-go/contract/pkg/sdktypes"
"reflect"
"time"

"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/cerebellum-network/cere-ddc-sdk-go/contract/pkg"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -50,11 +50,11 @@ type (
CDNNodeGet(nodeId uint32) (*CDNNodeStatus, error)
AccountGet(account types.AccountID) (*Account, error)
AddContractEventHandler(event string, handler func(interface{})) error
GetEventDispatcher() map[types.Hash]pkg.ContractEventDispatchEntry
GetEventDispatcher() map[types.Hash]sdktypes.ContractEventDispatchEntry
}

ddcBucketContract struct {
contract pkg.BlockchainClient
contract sdktypes.BlockchainClient
lastAccessTime time.Time
contractAddressSS58 string
keyringPair signature.KeyringPair
Expand All @@ -64,7 +64,7 @@ type (
cdnClusterGetMethodId []byte
cdnNodeGetMethodId []byte
accountGetMethodId []byte
eventDispatcher map[types.Hash]pkg.ContractEventDispatchEntry
eventDispatcher map[types.Hash]sdktypes.ContractEventDispatchEntry
}
)

Expand All @@ -85,7 +85,7 @@ var eventDispatchTable = map[string]reflect.Type{
GrantPermissionEventId: reflect.TypeOf(GrantPermissionEvent{}),
RevokePermissionEventId: reflect.TypeOf(RevokePermissionEvent{})}

func CreateDdcBucketContract(client pkg.BlockchainClient, contractAddressSS58 string) DdcBucketContract {
func CreateDdcBucketContract(client sdktypes.BlockchainClient, contractAddressSS58 string) DdcBucketContract {
bucketGetMethodId, err := hex.DecodeString(bucketGetMethod)
if err != nil {
log.WithError(err).WithField("method", bucketGetMethod).Fatal("Can't decode method bucketGetMethod")
Expand Down Expand Up @@ -116,12 +116,12 @@ func CreateDdcBucketContract(client pkg.BlockchainClient, contractAddressSS58 st
log.WithError(err).WithField("method", accountGetMethod).Fatal("Can't decode method accountGetMethod")
}

eventDispatcher := make(map[types.Hash]pkg.ContractEventDispatchEntry)
eventDispatcher := make(map[types.Hash]sdktypes.ContractEventDispatchEntry)
for k, v := range eventDispatchTable {
if key, err := types.NewHashFromHexString(k); err != nil {
log.WithError(err).WithField("hash", k).Fatalf("Bad event hash for event %s", v.Name())
} else {
eventDispatcher[key] = pkg.ContractEventDispatchEntry{ArgumentType: v}
eventDispatcher[key] = sdktypes.ContractEventDispatchEntry{ArgumentType: v}
}
}

Expand Down Expand Up @@ -224,6 +224,6 @@ func (d *ddcBucketContract) GetLastAccessTime() time.Time {
return d.lastAccessTime
}

func (d *ddcBucketContract) GetEventDispatcher() map[types.Hash]pkg.ContractEventDispatchEntry {
func (d *ddcBucketContract) GetEventDispatcher() map[types.Hash]sdktypes.ContractEventDispatchEntry {
return d.eventDispatcher
}
4 changes: 2 additions & 2 deletions contract/pkg/cache/ddc_bucket_contract_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package cache

import (
"encoding/hex"
"github.com/cerebellum-network/cere-ddc-sdk-go/contract/pkg/sdktypes"
"strconv"
"time"

"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/cerebellum-network/cere-ddc-sdk-go/contract/pkg"
"github.com/cerebellum-network/cere-ddc-sdk-go/contract/pkg/bucket"
"github.com/golang/groupcache/singleflight"
"github.com/patrickmn/go-cache"
Expand Down Expand Up @@ -184,7 +184,7 @@ func (d *ddcBucketContractCached) AddContractEventHandler(event string, handler
return d.ddcBucketContract.AddContractEventHandler(event, handler)
}

func (d *ddcBucketContractCached) GetEventDispatcher() map[types.Hash]pkg.ContractEventDispatchEntry {
func (d *ddcBucketContractCached) GetEventDispatcher() map[types.Hash]sdktypes.ContractEventDispatchEntry {
return d.ddcBucketContract.GetEventDispatcher()
}

Expand Down
4 changes: 2 additions & 2 deletions contract/pkg/cache/ddc_bucket_contract_cache_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package cache

import (
"github.com/cerebellum-network/cere-ddc-sdk-go/contract/pkg/sdktypes"
"testing"
"time"

"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/cerebellum-network/cere-ddc-sdk-go/contract/pkg"
"github.com/cerebellum-network/cere-ddc-sdk-go/contract/pkg/bucket"
"github.com/patrickmn/go-cache"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -60,7 +60,7 @@ func (d *mockedDdcBucketContract) AddContractEventHandler(event string, handler
return nil
}

func (d *mockedDdcBucketContract) GetEventDispatcher() map[types.Hash]pkg.ContractEventDispatchEntry {
func (d *mockedDdcBucketContract) GetEventDispatcher() map[types.Hash]sdktypes.ContractEventDispatchEntry {
return nil
}

Expand Down
99 changes: 28 additions & 71 deletions contract/pkg/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package pkg
import (
"bytes"
"context"
"github.com/cerebellum-network/cere-ddc-sdk-go/contract/pkg/sdktypes"
"github.com/cerebellum-network/cere-ddc-sdk-go/contract/pkg/utils"
"os/signal"
"reflect"
"sync"
Expand All @@ -22,69 +24,16 @@ const (
)

type (
BlockchainClient interface {
CallToReadEncoded(contractAddressSS58 string, fromAddress string, method []byte, args ...interface{}) (string, error)
CallToExec(ctx context.Context, contractCall ContractCall) (types.Hash, error)
Deploy(ctx context.Context, deployCall DeployCall) (types.AccountID, error)
SetEventDispatcher(contractAddressSS58 string, dispatcher map[types.Hash]ContractEventDispatchEntry) error
}

blockchainClient struct {
*gsrpc.SubstrateAPI
eventContractAccount types.AccountID
eventDispatcher map[types.Hash]ContractEventDispatchEntry
eventDispatcher map[types.Hash]sdktypes.ContractEventDispatchEntry
eventContextCancel context.CancelFunc
connectMutex sync.Mutex
}

ContractCall struct {
ContractAddress types.AccountID
ContractAddressSS58 string
From signature.KeyringPair
Value float64
GasLimit float64
Method []byte
Args []interface{}
}

DeployCall struct {
Code []byte
Salt []byte
From signature.KeyringPair
Value float64
GasLimit float64
Method []byte
Args []interface{}
}

ContractEventDispatchEntry struct {
ArgumentType reflect.Type
Handler ContractEventHandler
}

ContractEventHandler func(interface{})

Response struct {
DebugMessage string `json:"debugMessage"`
GasConsumed int `json:"gasConsumed"`
Result struct {
Ok struct {
Data string `json:"data"`
Flags int `json:"flags"`
} `json:"Ok"`
} `json:"result"`
}

Request struct {
Origin string `json:"origin"`
Dest string `json:"dest"`
GasLimit uint `json:"gasLimit"`
InputData string `json:"inputData"`
Value int `json:"value"`
}
)

func CreateBlockchainClient(apiUrl string) BlockchainClient {
func CreateBlockchainClient(apiUrl string) sdktypes.BlockchainClient {
substrateAPI, err := gsrpc.NewSubstrateAPI(apiUrl)
if err != nil {
log.WithError(err).WithField("apiUrl", apiUrl).Fatal("Can't connect to blockchainClient")
Expand All @@ -95,8 +44,8 @@ func CreateBlockchainClient(apiUrl string) BlockchainClient {
}
}

func (b *blockchainClient) SetEventDispatcher(contractAddressSS58 string, dispatcher map[types.Hash]ContractEventDispatchEntry) error {
contract, err := DecodeAccountIDFromSS58(contractAddressSS58)
func (b *blockchainClient) SetEventDispatcher(contractAddressSS58 string, dispatcher map[types.Hash]sdktypes.ContractEventDispatchEntry) error {
contract, err := utils.DecodeAccountIDFromSS58(contractAddressSS58)
if err != nil {
return err
}
Expand Down Expand Up @@ -129,6 +78,7 @@ func (b *blockchainClient) listenContractEvents() error {
b.eventContextCancel = cancel
watchdog := time.NewTicker(time.Minute)
eventArrived := true
var lastEventBlock types.BlockNumber
go func() {
defer sub.Unsubscribe()
for {
Expand Down Expand Up @@ -158,6 +108,13 @@ func (b *blockchainClient) listenContractEvents() error {
break
}
eventArrived = true
block, err := b.RPC.Chain.GetBlock(evt.Block)
if err != nil {
log.WithError(err).Warn("Error fetching block")
break
}
lastEventBlock = block.Block.Header.Number
print(lastEventBlock)

// parse all events for this block
for _, chng := range evt.Changes {
Expand All @@ -179,8 +136,8 @@ func (b *blockchainClient) listenContractEvents() error {
}

// Identify the event by matching one of its topics against known signatures. The topics are sorted so
// the the needed one may be in the arbitrary position.
var dispatchEntry ContractEventDispatchEntry
// the needed one may be in the arbitrary position.
var dispatchEntry sdktypes.ContractEventDispatchEntry
found := false
for _, topic := range e.Topics {
dispatchEntry, found = b.eventDispatcher[topic]
Expand Down Expand Up @@ -217,7 +174,7 @@ func (b *blockchainClient) listenContractEvents() error {
}

func (b *blockchainClient) CallToReadEncoded(contractAddressSS58 string, fromAddress string, method []byte, args ...interface{}) (string, error) {
data, err := GetContractData(method, args...)
data, err := utils.GetContractData(method, args...)
if err != nil {
return "", errors.Wrap(err, "getMessagesData")
}
Expand All @@ -230,27 +187,27 @@ func (b *blockchainClient) CallToReadEncoded(contractAddressSS58 string, fromAdd
return res.Result.Ok.Data, nil
}

func (b *blockchainClient) callToRead(contractAddressSS58 string, fromAddress string, data []byte) (Response, error) {
params := Request{
func (b *blockchainClient) callToRead(contractAddressSS58 string, fromAddress string, data []byte) (sdktypes.Response, error) {
params := sdktypes.Request{
Origin: fromAddress,
Dest: contractAddressSS58,
GasLimit: 500_000_000_000,
InputData: codec.HexEncodeToString(data),
}

res, err := withRetryOnClosedNetwork(b, func() (Response, error) {
res := Response{}
res, err := withRetryOnClosedNetwork(b, func() (sdktypes.Response, error) {
res := sdktypes.Response{}
return res, b.Client.Call(&res, "contracts_call", params)
})
if err != nil {
return Response{}, errors.Wrap(err, "call")
return sdktypes.Response{}, errors.Wrap(err, "call")
}

return res, nil
}

func (b *blockchainClient) CallToExec(ctx context.Context, contractCall ContractCall) (types.Hash, error) {
data, err := GetContractData(contractCall.Method, contractCall.Args...)
func (b *blockchainClient) CallToExec(ctx context.Context, contractCall sdktypes.ContractCall) (types.Hash, error) {
data, err := utils.GetContractData(contractCall.Method, contractCall.Args...)
if err != nil {
return types.Hash{}, err
}
Expand Down Expand Up @@ -285,13 +242,13 @@ func (b *blockchainClient) CallToExec(ctx context.Context, contractCall Contract
return hash, err
}

func (b *blockchainClient) Deploy(ctx context.Context, deployCall DeployCall) (types.AccountID, error) {
func (b *blockchainClient) Deploy(ctx context.Context, deployCall sdktypes.DeployCall) (types.AccountID, error) {
deployer, err := types.NewAccountID(deployCall.From.PublicKey)
if err != nil {
return types.AccountID{}, err
}

data, err := GetContractData(deployCall.Method, deployCall.Args...)
data, err := utils.GetContractData(deployCall.Method, deployCall.Args...)
if err != nil {
return types.AccountID{}, err
}
Expand Down Expand Up @@ -435,7 +392,7 @@ func (b *blockchainClient) submitAndWaitExtrinsic(ctx context.Context, extrinsic

func withRetryOnClosedNetwork[T any](b *blockchainClient, f func() (T, error)) (T, error) {
result, err := f()
if isClosedNetworkError(err) {
if utils.IsClosedNetworkError(err) {
if b.reconnect() != nil {
return result, err
}
Expand All @@ -449,7 +406,7 @@ func (b *blockchainClient) reconnect() error {
b.connectMutex.Lock()
defer b.connectMutex.Unlock()
_, err := b.RPC.State.GetRuntimeVersionLatest()
if !isClosedNetworkError(err) {
if !utils.IsClosedNetworkError(err) {
return nil
}

Expand Down
7 changes: 4 additions & 3 deletions contract/pkg/mock/ddc_bucket_contract_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package mock
import (
"encoding/json"
"errors"
"github.com/cerebellum-network/cere-ddc-sdk-go/contract/pkg/sdktypes"
"math"
"math/big"
"time"

"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/cerebellum-network/cere-ddc-sdk-go/contract/pkg"
"github.com/cerebellum-network/cere-ddc-sdk-go/contract/pkg/bucket"
"github.com/cerebellum-network/cere-ddc-sdk-go/contract/pkg/utils"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -228,7 +229,7 @@ func CreateBucket(bucketId uint32, clusterId uint32, bucketParams string, writer
func getAccountIDs(ss58Addresses []string) []types.AccountID {
accountIDs := make([]types.AccountID, len(ss58Addresses))
for i, address := range ss58Addresses {
if accountID, err := pkg.DecodeAccountIDFromSS58(address); err != nil {
if accountID, err := utils.DecodeAccountIDFromSS58(address); err != nil {
log.Fatal("Failed decode private key ed25519")
} else {
accountIDs[i] = accountID
Expand All @@ -238,6 +239,6 @@ func getAccountIDs(ss58Addresses []string) []types.AccountID {
return accountIDs
}

func (d *ddcBucketContractMock) GetEventDispatcher() map[types.Hash]pkg.ContractEventDispatchEntry {
func (d *ddcBucketContractMock) GetEventDispatcher() map[types.Hash]sdktypes.ContractEventDispatchEntry {
return nil
}
Loading

0 comments on commit 1327c08

Please sign in to comment.