From b38e26bcc2fad9e4b0a898fc680835125b2a8627 Mon Sep 17 00:00:00 2001 From: yahortsaryk Date: Mon, 28 Aug 2023 16:30:54 +0200 Subject: [PATCH] fix: using correct substrate rpc types in public methods signatures --- contract/pkg/bucket/ddc_bucket_contract.go | 307 +++++++++--------- contract/pkg/bucket/messages.go | 66 ++-- contract/pkg/bucket/result.go | 1 + .../pkg/cache/ddc_bucket_contract_cache.go | 240 ++++++++------ .../cache/ddc_bucket_contract_cache_test.go | 191 ++++++++--- contract/pkg/client.go | 1 + contract/pkg/mock/ddc_bucket_contract_mock.go | 215 +++++++++--- contract/pkg/utils.go | 3 +- 8 files changed, 663 insertions(+), 361 deletions(-) diff --git a/contract/pkg/bucket/ddc_bucket_contract.go b/contract/pkg/bucket/ddc_bucket_contract.go index 1335b10..513b82a 100644 --- a/contract/pkg/bucket/ddc_bucket_contract.go +++ b/contract/pkg/bucket/ddc_bucket_contract.go @@ -9,6 +9,7 @@ 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" log "github.com/sirupsen/logrus" ) @@ -46,28 +47,28 @@ const ( adminTransferCdnNodeOwnershipMethod = "cd9821be" bucketGetMethod = "3802cb77" accountGetMethod = "1d4220fa" - accountDepositMethod = "1" - accountBondMethod = "2" - accountUnbondMethod = "3" - accountGetUsdPerCereMethod = "4" - accountSetUsdPerCereMethod = "5" - accountWithdrawUnbondedMethod = "6" - getAccountsMethod = "7" - bucketCreateMethod = "" - bucketChangeOwnerMethod = "" - bucketAllocIntoClusterMethod = "" - bucketSettlePaymentMethod = "" - bucketChangeParamsMethod = "" - bucketListMethod = "" - bucketListForAccountMethod = "" - bucketSetAvailabilityMethod = "" - bucketSetResourceCapMethod = "" - betBucketWritersMethod = "" - betBucketReadersMethod = "" - bucketSetWriterPermMethod = "" - bucketRevokeWriterPermMethod = "" - bucketSetReaderPermMethod = "" - bucketRevokeReaderPermMethod = "" + accountDepositMethod = "c311af62" + accountBondMethod = "e9fad0bf" + accountUnbondMethod = "f7ea2c67" + accountGetUsdPerCereMethod = "e4a4652a" + accountSetUsdPerCereMethod = "48d45ee8" + accountWithdrawUnbondedMethod = "98173716" + getAccountsMethod = "ef03ead7" + bucketCreateMethod = "0aeb2379" + bucketChangeOwnerMethod = "c7d0c2cd" + bucketAllocIntoClusterMethod = "4c482d19" + bucketSettlePaymentMethod = "15974555" + bucketChangeParamsMethod = "9f2d075b" + bucketListMethod = "417ab584" + bucketListForAccountMethod = "c434cf57" + bucketSetAvailabilityMethod = "053eb3ce" + bucketSetResourceCapMethod = "85010c6d" + getBucketWritersMethod = "499cd4b7" + getBucketReadersMethod = "b9a7cc1c" + bucketSetWriterPermMethod = "ea2e477a" + bucketRevokeWriterPermMethod = "2b3d8dd1" + bucketSetReaderPermMethod = "fc0e94ea" + bucketRevokeReaderPermMethod = "e9bfed5a" BucketCreatedEventId = "004464634275636b65743a3a4275636b65744372656174656400000000000000" BucketAllocatedEventId = "004464634275636b65743a3a4275636b6574416c6c6f63617465640000000000" @@ -112,56 +113,57 @@ type ( AccountGetUsdPerCere() (balance Balance, err error) AccountSetUsdPerCere(usdPerCere Balance) error AccountWithdrawUnbonded() error - GetAccounts() ([]types.AccountID, error) - - BucketGet(bucketId uint32) (*BucketInfo, error) - BucketCreate(bucketParams BucketParams, clusterId uint32, oenrtId types.AccountID) (bucketId uint32, err error) - BucketChangeOwner(bucketId uint32, ownerId types.AccountID) error - BucketAllocIntoCluster(bucketId uint32, resource Resource) error - BucketSettlePayment(bucketId uint32) error - BucketChangeParams(bucketId uint32, bucketParams BucketParams) error - BucketList(offset uint32, limit uint32, ownerId string) []*BucketInfo - BucketListForAccount(ownerId types.AccountID) ([]*Bucket, error) - BucketSetAvailability(bucketId uint32, publicAvailability bool) error - BucketSetResourceCap(bucketId uint32, newResourceCap Resource) error - GetBucketWriters(bucketId uint32) ([]types.AccountID, error) - GetBucketReaders(bucketId uint32) ([]types.AccountID, error) - BucketSetWriterPerm(bucketId uint32, writer types.AccountID) error - BucketRevokeWriterPerm(bucketId uint32, writer types.AccountID) error - BucketSetReaderPerm(bucketId uint32, reader types.AccountID) error - BucketRevokeReaderPerm(bucketId uint32, reader types.AccountID) error - - ClusterGet(clusterId uint32) (*ClusterInfo, error) - ClusterCreate(cluster *NewCluster) (clusterId uint32, err error) - ClusterAddNode(clusterId uint32, nodeKey string, vNodes [][]Token) error - ClusterRemoveNode(clusterId uint32, nodeKey string) error - ClusterResetNode(clusterId uint32, nodeKey string, vNodes [][]Token) error - ClusterReplaceNode(clusterId uint32, vNodes [][]Token, newNodeKey string) error - ClusterAddCdnNode(clusterId uint32, cdnNodeKey string) error - ClusterRemoveCdnNode(clusterId uint32, cdnNodeKey string) error - ClusterSetParams(clusterId uint32, params Params) error - ClusterRemove(clusterId uint32) error - ClusterSetNodeStatus(clusterId uint32, nodeKey string, statusInCluster string) error - ClusterSetCdnNodeStatus(clusterId uint32, cdnNodeKey string, statusInCluster string) error - ClusterList(offset uint32, limit uint32, filterManagerId string) []*ClusterInfo - NodeGet(nodeKey string) (*NodeInfo, error) - NodeCreate(nodeKey string, params Params, capacity Resource) (key string, err error) - NodeRemove(nodeKey string) error - NodeSetParams(nodeKey string, params Params) error - NodeList(offset uint32, limit uint32, filterManagerId string) ([]*NodeInfo, error) - CDNNodeGet(nodeKey string) (*CDNNodeInfo, error) - CDNNodeCreate(nodeKey string, params CDNNodeParams) error - CDNNodeRemove(nodeKey string) error - CDNNodeSetParams(nodeKey string, params CDNNodeParams) error - CDNNodeList(offset uint32, limit uint32, filterManagerId string) ([]*CDNNodeInfo, error) - AccountGet(account types.AccountID) (*Account, error) - HasPermission(account types.AccountID, permission string) (bool, error) - GrantTrustedManagerPermission(managerId types.AccountID) error - RevokeTrustedManagerPermission(managerId types.AccountID) error - AdminGrantPermission(grantee types.AccountID, permission string) error - AdminRevokePermission(grantee types.AccountID, permission string) error - AdminTransferNodeOwnership(nodeKey string, newOwner types.AccountID) error - AdminTransferCdnNodeOwnership(cdnNodeKey string, newOwner types.AccountID) error + GetAccounts() ([]AccountId, error) + + BucketGet(bucketId BucketId) (*BucketInfo, error) + BucketCreate(bucketParams BucketParams, clusterId ClusterId, ownerId types.OptionAccountID) (bucketId BucketId, err error) + BucketChangeOwner(bucketId BucketId, ownerId AccountId) error + BucketAllocIntoCluster(bucketId BucketId, resource Resource) error + BucketSettlePayment(bucketId BucketId) error + BucketChangeParams(bucketId BucketId, bucketParams BucketParams) error + BucketList(offset uint32, limit uint32, ownerId types.OptionAccountID) (*BucketListInfo, error) + BucketListForAccount(ownerId AccountId) ([]*Bucket, error) + BucketSetAvailability(bucketId BucketId, publicAvailability bool) error + BucketSetResourceCap(bucketId BucketId, newResourceCap Resource) error + GetBucketWriters(bucketId BucketId) ([]AccountId, error) + GetBucketReaders(bucketId BucketId) ([]AccountId, error) + BucketSetWriterPerm(bucketId BucketId, writer AccountId) error + BucketRevokeWriterPerm(bucketId BucketId, writer AccountId) error + BucketSetReaderPerm(bucketId BucketId, reader AccountId) error + BucketRevokeReaderPerm(bucketId BucketId, reader AccountId) error + + ClusterGet(clusterId ClusterId) (*ClusterInfo, error) + ClusterCreate(cluster *NewCluster) (clusterId ClusterId, err error) + ClusterAddNode(clusterId ClusterId, nodeKey NodeKey, vNodes [][]Token) error + ClusterRemoveNode(clusterId ClusterId, nodeKey NodeKey) error + ClusterResetNode(clusterId ClusterId, nodeKey NodeKey, vNodes [][]Token) error + ClusterReplaceNode(clusterId ClusterId, vNodes [][]Token, newNodeKey NodeKey) error + ClusterAddCdnNode(clusterId ClusterId, nodeKey CdnNodeKey) error + ClusterRemoveCdnNode(clusterId ClusterId, nodeKey CdnNodeKey) error + ClusterSetParams(clusterId ClusterId, params Params) error + ClusterRemove(clusterId ClusterId) error + ClusterSetNodeStatus(clusterId ClusterId, nodeKey NodeKey, statusInCluster string) error + ClusterSetCdnNodeStatus(clusterId ClusterId, nodeKey CdnNodeKey, statusInCluster string) error + ClusterList(offset uint32, limit uint32, filterManagerId types.OptionAccountID) (*ClusterListInfo, error) + + NodeGet(nodeKey NodeKey) (*NodeInfo, error) + NodeCreate(nodeKey NodeKey, params Params, capacity Resource) (key NodeKey, err error) + NodeRemove(nodeKey NodeKey) error + NodeSetParams(nodeKey NodeKey, params Params) error + NodeList(offset uint32, limit uint32, filterProviderId types.OptionAccountID) (*NodeListInfo, error) + CdnNodeGet(nodeKey CdnNodeKey) (*CdnNodeInfo, error) + CdnNodeCreate(nodeKey CdnNodeKey, params CDNNodeParams) error + CdnNodeRemove(nodeKey CdnNodeKey) error + CdnNodeSetParams(nodeKey CdnNodeKey, params CDNNodeParams) error + CdnNodeList(offset uint32, limit uint32, filterProviderId types.OptionAccountID) (*CdnNodeListInfo, error) + AccountGet(account AccountId) (*Account, error) + HasPermission(account AccountId, permission string) (bool, error) + GrantTrustedManagerPermission(managerId AccountId) error + RevokeTrustedManagerPermission(managerId AccountId) error + AdminGrantPermission(grantee AccountId, permission string) error + AdminRevokePermission(grantee AccountId, permission string) error + AdminTransferNodeOwnership(nodeKey NodeKey, newOwner AccountId) error + AdminTransferCdnNodeOwnership(nodeKey CdnNodeKey, newOwner AccountId) error AddContractEventHandler(event string, handler func(interface{})) error GetEventDispatcher() map[types.Hash]pkg.ContractEventDispatchEntry } @@ -219,8 +221,8 @@ type ( bucketListForAccountMethodId []byte bucketSetAvailabilityMethodId []byte bucketSetResourceCapMethodId []byte - betBucketWritersMethodId []byte - betBucketReadersMethodId []byte + getBucketWritersMethodId []byte + getBucketReadersMethodId []byte bucketSetWriterPermMethodId []byte bucketRevokeWriterPermMethodId []byte bucketSetReaderPermMethodId []byte @@ -504,14 +506,14 @@ func CreateDdcBucketContract(client pkg.BlockchainClient, contractAddressSS58 st log.WithError(err).WithField("method", bucketSetResourceCapMethod).Fatal("Can't decode method bucketSetResourceCapMethodId") } - betBucketWritersMethodId, err := hex.DecodeString(bucketSetResourceCapMethod) + getBucketWritersMethodId, err := hex.DecodeString(bucketSetResourceCapMethod) if err != nil { - log.WithError(err).WithField("method", bucketSetResourceCapMethod).Fatal("Can't decode method bucketSetResourceCapMethodId") + log.WithError(err).WithField("method", getBucketWritersMethodId).Fatal("Can't decode method getBucketWritersMethodId") } - betBucketReadersMethodId, err := hex.DecodeString(betBucketReadersMethod) + getBucketReadersMethodId, err := hex.DecodeString(getBucketReadersMethod) if err != nil { - log.WithError(err).WithField("method", betBucketReadersMethod).Fatal("Can't decode method betBucketReadersMethodId") + log.WithError(err).WithField("method", getBucketReadersMethod).Fatal("Can't decode method getBucketReadersMethodId") } bucketSetWriterPermMethodId, err := hex.DecodeString(bucketSetWriterPermMethod) @@ -536,10 +538,10 @@ func CreateDdcBucketContract(client pkg.BlockchainClient, contractAddressSS58 st eventDispatcher := make(map[types.Hash]pkg.ContractEventDispatchEntry) for k, v := range eventDispatchTable { - if key, err := types.NewHashFromHexString(k); err != nil { + if nodeKey, 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[nodeKey] = pkg.ContractEventDispatchEntry{ArgumentType: v} } } @@ -596,8 +598,8 @@ func CreateDdcBucketContract(client pkg.BlockchainClient, contractAddressSS58 st bucketListForAccountMethodId: bucketListForAccountMethodId, bucketSetAvailabilityMethodId: bucketSetAvailabilityMethodId, bucketSetResourceCapMethodId: bucketSetResourceCapMethodId, - betBucketWritersMethodId: betBucketWritersMethodId, - betBucketReadersMethodId: betBucketReadersMethodId, + getBucketWritersMethodId: getBucketWritersMethodId, + getBucketReadersMethodId: getBucketReadersMethodId, bucketSetWriterPermMethodId: bucketSetWriterPermMethodId, bucketRevokeWriterPermMethodId: bucketRevokeWriterPermMethodId, bucketSetReaderPermMethodId: bucketSetReaderPermMethodId, @@ -605,35 +607,35 @@ func CreateDdcBucketContract(client pkg.BlockchainClient, contractAddressSS58 st } } -func (d *ddcBucketContract) BucketGet(bucketId uint32) (*BucketInfo, error) { +func (d *ddcBucketContract) BucketGet(bucketId BucketId) (*BucketInfo, error) { res := &BucketInfo{} err := d.callToRead(res, d.bucketGetMethodId, types.U32(bucketId)) return res, err } -func (d *ddcBucketContract) ClusterGet(clusterId uint32) (*ClusterInfo, error) { +func (d *ddcBucketContract) ClusterGet(clusterId ClusterId) (*ClusterInfo, error) { res := &ClusterInfo{} err := d.callToRead(res, d.clusterGetMethodId, types.U32(clusterId)) return res, err } -func (d *ddcBucketContract) NodeGet(nodeKey string) (*NodeInfo, error) { +func (d *ddcBucketContract) NodeGet(nodeKey NodeKey) (*NodeInfo, error) { res := &NodeInfo{} err := d.callToRead(res, d.nodeGetMethodId, nodeKey) return res, err } -func (d *ddcBucketContract) CDNNodeGet(nodeKey string) (*CDNNodeInfo, error) { - res := &CDNNodeInfo{} +func (d *ddcBucketContract) CdnNodeGet(nodeKey CdnNodeKey) (*CdnNodeInfo, error) { + res := &CdnNodeInfo{} err := d.callToRead(res, d.cdnNodeGetMethodId, nodeKey) return res, err } -func (d *ddcBucketContract) AccountGet(account types.AccountID) (*Account, error) { +func (d *ddcBucketContract) AccountGet(account AccountId) (*Account, error) { res := &Account{} if err := d.callToRead(res, d.accountGetMethodId, account); err != nil { return nil, err @@ -658,12 +660,23 @@ func (d *ddcBucketContract) callToRead(result interface{}, method []byte, args . return res.err } +func (d *ddcBucketContract) callToReadNoResult(res interface{}, method []byte, args ...interface{}) error { + data, err := d.contract.CallToReadEncoded(d.contractAddressSS58, d.contractAddressSS58, method, args...) + if err != nil { + return err + } + + d.lastAccessTime = time.Now() + + return codec.DecodeFromHex(data, res) +} + func (d *ddcBucketContract) AddContractEventHandler(event string, handler func(interface{})) error { - key, err := types.NewHashFromHexString(event) + nodeKey, err := types.NewHashFromHexString(event) if err != nil { return err } - entry, found := d.eventDispatcher[key] + entry, found := d.eventDispatcher[nodeKey] if !found { return errors.New("Event not found") } @@ -671,7 +684,7 @@ func (d *ddcBucketContract) AddContractEventHandler(event string, handler func(i return errors.New("Contract event handler already set for " + event) } entry.Handler = handler - d.eventDispatcher[key] = entry + d.eventDispatcher[nodeKey] = entry return nil } @@ -687,138 +700,141 @@ func (d *ddcBucketContract) GetEventDispatcher() map[types.Hash]pkg.ContractEven return d.eventDispatcher } -func (d *ddcBucketContract) ClusterCreate(cluster *NewCluster) (clusterId uint32, err error) { +func (d *ddcBucketContract) ClusterCreate(cluster *NewCluster) (clusterId ClusterId, err error) { err = d.callToRead(clusterId, d.clusterCreateMethodId, cluster) return clusterId, err } -func (d *ddcBucketContract) ClusterAddNode(clusterId uint32, nodeKey string, vNodes [][]Token) error { +func (d *ddcBucketContract) ClusterAddNode(clusterId ClusterId, nodeKey NodeKey, vNodes [][]Token) error { err := d.callToRead(clusterId, d.clusterAddNodeMethodId, clusterId, nodeKey, vNodes) return err } -func (d *ddcBucketContract) ClusterRemoveNode(clusterId uint32, nodeKey string) error { +func (d *ddcBucketContract) ClusterRemoveNode(clusterId ClusterId, nodeKey NodeKey) error { err := d.callToRead(clusterId, d.clusterRemoveNodeMethodId, clusterId, nodeKey) return err } -func (d *ddcBucketContract) ClusterResetNode(clusterId uint32, nodeKey string, vNodes [][]Token) error { +func (d *ddcBucketContract) ClusterResetNode(clusterId ClusterId, nodeKey NodeKey, vNodes [][]Token) error { err := d.callToRead(clusterId, d.clusterResetNodeMethodId, clusterId, nodeKey, vNodes) return err } -func (d *ddcBucketContract) ClusterReplaceNode(clusterId uint32, vNodes [][]Token, newNodeKey string) error { +func (d *ddcBucketContract) ClusterReplaceNode(clusterId ClusterId, vNodes [][]Token, newNodeKey NodeKey) error { err := d.callToRead(clusterId, d.clusterReplaceNodeMethodId, clusterId, vNodes, newNodeKey) return err } -func (d *ddcBucketContract) ClusterAddCdnNode(clusterId uint32, cdnNodeKey string) error { - err := d.callToRead(clusterId, d.clusterAddCdnNodeMethodId, clusterId, cdnNodeKey) +func (d *ddcBucketContract) ClusterAddCdnNode(clusterId ClusterId, nodeKey CdnNodeKey) error { + err := d.callToRead(clusterId, d.clusterAddCdnNodeMethodId, clusterId, nodeKey) return err } -func (d *ddcBucketContract) ClusterRemoveCdnNode(clusterId uint32, cdnNodeKey string) error { - err := d.callToRead(clusterId, d.clusterRemoveCdnNodeMethodId, clusterId, cdnNodeKey) +func (d *ddcBucketContract) ClusterRemoveCdnNode(clusterId ClusterId, nodeKey CdnNodeKey) error { + err := d.callToRead(clusterId, d.clusterRemoveCdnNodeMethodId, clusterId, nodeKey) return err } -func (d *ddcBucketContract) ClusterSetParams(clusterId uint32, params Params) error { +func (d *ddcBucketContract) ClusterSetParams(clusterId ClusterId, params Params) error { err := d.callToRead(clusterId, d.clusterSetParamsMethodId, clusterId, params) return err } -func (d *ddcBucketContract) ClusterRemove(clusterId uint32) error { +func (d *ddcBucketContract) ClusterRemove(clusterId ClusterId) error { err := d.callToRead(clusterId, d.clusterRemoveMethodId, clusterId) return err } -func (d *ddcBucketContract) ClusterSetNodeStatus(clusterId uint32, nodeKey string, statusInCluster string) error { +func (d *ddcBucketContract) ClusterSetNodeStatus(clusterId ClusterId, nodeKey NodeKey, statusInCluster string) error { err := d.callToRead(clusterId, d.clusterSetNodeStatusMethodId, clusterId, nodeKey, statusInCluster) return err } -func (d *ddcBucketContract) ClusterSetCdnNodeStatus(clusterId uint32, cdnNodeKey string, statusInCluster string) error { - err := d.callToRead(clusterId, d.clusterSetCdnNodeStatusMethodId, clusterId, cdnNodeKey, statusInCluster) +func (d *ddcBucketContract) ClusterSetCdnNodeStatus(clusterId ClusterId, nodeKey CdnNodeKey, statusInCluster string) error { + err := d.callToRead(clusterId, d.clusterSetCdnNodeStatusMethodId, clusterId, nodeKey, statusInCluster) return err } -func (d *ddcBucketContract) ClusterList(offset uint32, limit uint32, filterManagerId string) (clusters []*ClusterInfo) { - _ = d.callToRead(clusters, d.clusterListMethodId, offset, limit, filterManagerId) - return clusters +func (d *ddcBucketContract) ClusterList(offset uint32, limit uint32, filterManagerId types.OptionAccountID) (*ClusterListInfo, error) { + res := ClusterListInfo{} + err := d.callToReadNoResult(&res, d.clusterListMethodId, offset, limit, filterManagerId) + return &res, err } -func (d *ddcBucketContract) NodeCreate(nodeKey string, params Params, capacity Resource) (key string, err error) { - err = d.callToRead(key, d.nodeCreateMethodId, nodeKey, params, capacity) +func (d *ddcBucketContract) NodeCreate(nodeKey NodeKey, params Params, capacity Resource) (key NodeKey, err error) { + err = d.callToRead(nodeKey, d.nodeCreateMethodId, nodeKey, params, capacity) return key, err } -func (d *ddcBucketContract) NodeRemove(nodeKey string) error { +func (d *ddcBucketContract) NodeRemove(nodeKey NodeKey) error { err := d.callToRead(nodeKey, d.nodeRemoveMethodId, nodeKey) return err } -func (d *ddcBucketContract) NodeSetParams(nodeKey string, params Params) error { +func (d *ddcBucketContract) NodeSetParams(nodeKey NodeKey, params Params) error { err := d.callToRead(nodeKey, d.nodeSetParamsMethodId, nodeKey, params) return err } -func (d *ddcBucketContract) NodeList(offset uint32, limit uint32, filterManagerId string) (nodes []*NodeInfo, err error) { - err = d.callToRead(nodes, d.nodeListMethodId, offset, limit, filterManagerId) - return nodes, err +func (d *ddcBucketContract) NodeList(offset uint32, limit uint32, filterProviderId types.OptionAccountID) (*NodeListInfo, error) { + res := NodeListInfo{} + err := d.callToReadNoResult(&res, d.nodeListMethodId, offset, limit, filterProviderId) + return &res, err } -func (d *ddcBucketContract) CDNNodeCreate(nodeKey string, params CDNNodeParams) error { +func (d *ddcBucketContract) CdnNodeCreate(nodeKey CdnNodeKey, params CDNNodeParams) error { err := d.callToRead(nodeKey, d.cdnNodeCreateMethodId, nodeKey, params) return err } -func (d *ddcBucketContract) CDNNodeRemove(nodeKey string) error { +func (d *ddcBucketContract) CdnNodeRemove(nodeKey CdnNodeKey) error { err := d.callToRead(nodeKey, d.cdnNodeRemoveMethodId, nodeKey) return err } -func (d *ddcBucketContract) CDNNodeSetParams(nodeKey string, params CDNNodeParams) error { +func (d *ddcBucketContract) CdnNodeSetParams(nodeKey CdnNodeKey, params CDNNodeParams) error { err := d.callToRead(nodeKey, d.cdnNodeSetParamsMethodId, nodeKey, params) return err } -func (d *ddcBucketContract) CDNNodeList(offset uint32, limit uint32, filterManagerId string) (nodes []*CDNNodeInfo, err error) { - err = d.callToRead(nodes, d.cdnNodeListMethodId, offset, limit, filterManagerId) - return nodes, err +func (d *ddcBucketContract) CdnNodeList(offset uint32, limit uint32, filterProviderId types.OptionAccountID) (*CdnNodeListInfo, error) { + res := CdnNodeListInfo{} + err := d.callToReadNoResult(&res, d.cdnNodeListMethodId, offset, limit, filterProviderId) + return &res, err } -func (d *ddcBucketContract) HasPermission(account types.AccountID, permission string) (has bool, err error) { +func (d *ddcBucketContract) HasPermission(account AccountId, permission string) (has bool, err error) { err = d.callToRead(has, d.hasPermissionMethodId, account, permission) return has, err } -func (d *ddcBucketContract) GrantTrustedManagerPermission(managerId types.AccountID) error { +func (d *ddcBucketContract) GrantTrustedManagerPermission(managerId AccountId) error { err := d.callToRead(managerId, d.grantTrustedManagerPermissionMethodId, managerId) return err } -func (d *ddcBucketContract) RevokeTrustedManagerPermission(managerId types.AccountID) error { +func (d *ddcBucketContract) RevokeTrustedManagerPermission(managerId AccountId) error { err := d.callToRead(managerId, d.revokeTrustedManagerPermissionMethodId, managerId) return err } -func (d *ddcBucketContract) AdminGrantPermission(grantee types.AccountID, permission string) error { +func (d *ddcBucketContract) AdminGrantPermission(grantee AccountId, permission string) error { err := d.callToRead(grantee, d.adminGrantPermissionMethodId, grantee, permission) return err } -func (d *ddcBucketContract) AdminRevokePermission(grantee types.AccountID, permission string) error { +func (d *ddcBucketContract) AdminRevokePermission(grantee AccountId, permission string) error { err := d.callToRead(grantee, d.adminRevokePermissionMethodId, grantee, permission) return err } -func (d *ddcBucketContract) AdminTransferNodeOwnership(nodeKey string, newOwner types.AccountID) error { +func (d *ddcBucketContract) AdminTransferNodeOwnership(nodeKey NodeKey, newOwner AccountId) error { err := d.callToRead(newOwner, d.adminTransferNodeOwnershipMethodId, nodeKey, newOwner) return err } -func (d *ddcBucketContract) AdminTransferCdnNodeOwnership(cdnNodeKey string, newOwner types.AccountID) error { - err := d.callToRead(newOwner, d.adminTransferCdnNodeOwnershipMethodId, cdnNodeKey, newOwner) +func (d *ddcBucketContract) AdminTransferCdnNodeOwnership(nodeKey CdnNodeKey, newOwner AccountId) error { + err := d.callToRead(newOwner, d.adminTransferCdnNodeOwnershipMethodId, nodeKey, newOwner) return err } @@ -857,34 +873,35 @@ func (d *ddcBucketContract) GetAccounts() (accounts []types.AccountID, err error return accounts, err } -func (d *ddcBucketContract) BucketCreate(bucketParams BucketParams, clusterId uint32, ownerId types.AccountID) (bucketId uint32, err error) { +func (d *ddcBucketContract) BucketCreate(bucketParams BucketParams, clusterId ClusterId, ownerId types.OptionAccountID) (bucketId BucketId, err error) { err = d.callToRead(bucketId, d.bucketCreateMethodId, bucketParams, clusterId, ownerId) return bucketId, err } -func (d *ddcBucketContract) BucketChangeOwner(bucketId uint32, newOwnerId types.AccountID) error { +func (d *ddcBucketContract) BucketChangeOwner(bucketId BucketId, newOwnerId types.AccountID) error { err := d.callToRead(newOwnerId, d.bucketChangeOwnerMethodId, bucketId, newOwnerId) return err } -func (d *ddcBucketContract) BucketAllocIntoCluster(bucketId uint32, resource Resource) error { +func (d *ddcBucketContract) BucketAllocIntoCluster(bucketId BucketId, resource Resource) error { // TODO Implement BucketAllocIntoCluster logic return nil } -func (d *ddcBucketContract) BucketSettlePayment(bucketId uint32) error { +func (d *ddcBucketContract) BucketSettlePayment(bucketId BucketId) error { // TODO Implement BucketSettlePayment logic return nil } -func (d *ddcBucketContract) BucketChangeParams(bucketId uint32, bucketParams BucketParams) error { +func (d *ddcBucketContract) BucketChangeParams(bucketId BucketId, bucketParams BucketParams) error { // TODO Implement BucketChangeParams logic return nil } -func (d *ddcBucketContract) BucketList(offset uint32, limit uint32, ownerId string) []*BucketInfo { - // TODO Implement BucketList logic - return nil +func (d *ddcBucketContract) BucketList(offset uint32, limit uint32, filterOwnerId types.OptionAccountID) (*BucketListInfo, error) { + res := BucketListInfo{} + err := d.callToReadNoResult(&res, d.cdnNodeListMethodId, offset, limit, filterOwnerId) + return &res, err } func (d *ddcBucketContract) BucketListForAccount(ownerId types.AccountID) ([]*Bucket, error) { @@ -892,42 +909,42 @@ func (d *ddcBucketContract) BucketListForAccount(ownerId types.AccountID) ([]*Bu return nil, nil } -func (d *ddcBucketContract) BucketSetAvailability(bucketId uint32, publicAvailability bool) error { +func (d *ddcBucketContract) BucketSetAvailability(bucketId BucketId, publicAvailability bool) error { // TODO Implement BucketSetAvailability logic return nil } -func (d *ddcBucketContract) BucketSetResourceCap(bucketId uint32, newResourceCap Resource) error { +func (d *ddcBucketContract) BucketSetResourceCap(bucketId BucketId, newResourceCap Resource) error { // TODO Implement BucketSetResourceCap logic return nil } -func (d *ddcBucketContract) GetBucketWriters(bucketId uint32) ([]types.AccountID, error) { +func (d *ddcBucketContract) GetBucketWriters(bucketId BucketId) ([]AccountId, error) { // TODO Implement GetBucketWriters logic return nil, nil } -func (d *ddcBucketContract) GetBucketReaders(bucketId uint32) ([]types.AccountID, error) { +func (d *ddcBucketContract) GetBucketReaders(bucketId BucketId) ([]AccountId, error) { // TODO Implement GetBucketReaders logic return nil, nil } -func (d *ddcBucketContract) BucketSetWriterPerm(bucketId uint32, writer types.AccountID) error { +func (d *ddcBucketContract) BucketSetWriterPerm(bucketId BucketId, writer AccountId) error { // Implement BucketSetWriterPerm logic return nil } -func (d *ddcBucketContract) BucketRevokeWriterPerm(bucketId uint32, writer types.AccountID) error { +func (d *ddcBucketContract) BucketRevokeWriterPerm(bucketId BucketId, writer AccountId) error { // TODO Implement BucketRevokeWriterPerm logic return nil } -func (d *ddcBucketContract) BucketSetReaderPerm(bucketId uint32, reader types.AccountID) error { +func (d *ddcBucketContract) BucketSetReaderPerm(bucketId BucketId, reader AccountId) error { // TODO Implement BucketSetReaderPerm logic return nil } -func (d *ddcBucketContract) BucketRevokeReaderPerm(bucketId uint32, reader types.AccountID) error { +func (d *ddcBucketContract) BucketRevokeReaderPerm(bucketId BucketId, reader AccountId) error { // TODO Implement BucketRevokeReaderPerm logic return nil } diff --git a/contract/pkg/bucket/messages.go b/contract/pkg/bucket/messages.go index 36a953b..0a794a1 100644 --- a/contract/pkg/bucket/messages.go +++ b/contract/pkg/bucket/messages.go @@ -12,17 +12,19 @@ import ( type ( Balance = types.U128 Cash = Balance - Resource = uint32 - Token = uint64 - ClusterId = uint32 + Resource = types.U32 + Token = types.U64 + ClusterId = types.U32 AccountId = types.AccountID ProviderId = AccountId - BucketId = uint32 + BucketId = types.U32 Params = string BucketParams = Params + NodeParams = Params CdnNodeParams = Params - NodeStatusInCluster = byte - NodeKey = string + NodeStatusInCluster = uint8 + NodeKey = AccountId + CdnNodeKey = AccountId ) const ( @@ -49,7 +51,7 @@ type Cluster struct { ResourceUsed Resource Revenues Cash TotalRent Balance - CdnNodesKeys []NodeKey + CdnNodesKeys []CdnNodeKey CdnRevenues Cash CdnUsdPerGb Balance } @@ -65,6 +67,11 @@ type ClusterInfo struct { NodesVNodes []NodeVNodesInfo } +type ClusterListInfo struct { + Clusters []ClusterInfo + Total types.U32 +} + type NewCluster struct { Params Params ResourcePerVNode Resource @@ -74,28 +81,38 @@ type Node struct { ProviderId ProviderId RentPerMonth Balance FreeResources Resource - Params string + Params NodeParams ClusterId types.OptionU32 - StatusInCluster types.OptionBytes + StatusInCluster types.OptionU8 } type NodeInfo struct { - Key string + Key NodeKey Node Node VNodes []Token } -type CDNNode struct { +type NodeListInfo struct { + Nodes []NodeInfo + Total types.U32 +} + +type CdnNode struct { ProviderId ProviderId UndistributedPayment Balance - Params string + Params CdnNodeParams ClusterId types.OptionU32 - StatusInCluster types.OptionBytes + StatusInCluster types.OptionU8 } -type CDNNodeInfo struct { - Key string - Node CDNNode +type CdnNodeInfo struct { + Key CdnNodeKey + Node CdnNode +} + +type CdnNodeListInfo struct { + Nodes []CdnNodeInfo + Total types.U32 } type Bucket struct { @@ -120,6 +137,11 @@ type BucketInfo struct { RentCoveredUntilMs uint64 } +type BucketListInfo struct { + Buckets []BucketListInfo + Total types.U32 +} + type Account struct { Deposit Cash Bonded Cash @@ -370,20 +392,20 @@ func (b *BucketInfo) isReader(address types.Address) bool { } func (n *Node) GetStatusInCluster() (NodeStatusInCluster, error) { - hasValue, status := n.StatusInCluster.Unwrap() + hasValue, val := n.StatusInCluster.Unwrap() if !hasValue { return UNKNOWN_NODE_STATUS_IN_CLUSTER, fmt.Errorf("unknown storage node status in cluster") } else { - return status[0], nil + return uint8(val), nil } } -func (n *CDNNode) GetStatusInCluster() (NodeStatusInCluster, error) { - hasValue, status := n.StatusInCluster.Unwrap() +func (n *CdnNode) GetStatusInCluster() (NodeStatusInCluster, error) { + hasValue, val := n.StatusInCluster.Unwrap() if !hasValue { return UNKNOWN_NODE_STATUS_IN_CLUSTER, fmt.Errorf("unknown cdn node status in cluster") } else { - return status[0], nil + return uint8(val), nil } } @@ -391,6 +413,6 @@ func (n *NodeInfo) GetStatusInCluster() (NodeStatusInCluster, error) { return n.Node.GetStatusInCluster() } -func (n *CDNNodeInfo) GetStatusInCluster() (NodeStatusInCluster, error) { +func (n *CdnNodeInfo) GetStatusInCluster() (NodeStatusInCluster, error) { return n.Node.GetStatusInCluster() } diff --git a/contract/pkg/bucket/result.go b/contract/pkg/bucket/result.go index 0dfef85..dddb8f2 100644 --- a/contract/pkg/bucket/result.go +++ b/contract/pkg/bucket/result.go @@ -19,6 +19,7 @@ type Result struct { } func (result *Result) decodeDdcBucketContract(encodedData string) error { + if strings.HasPrefix(encodedData, okPrefix) { encodedData = strings.TrimPrefix(encodedData, okPrefix) if err := codec.DecodeFromHex(encodedData, result.data); err != nil { diff --git a/contract/pkg/cache/ddc_bucket_contract_cache.go b/contract/pkg/cache/ddc_bucket_contract_cache.go index 4c89c30..8af5f0d 100644 --- a/contract/pkg/cache/ddc_bucket_contract_cache.go +++ b/contract/pkg/cache/ddc_bucket_contract_cache.go @@ -98,14 +98,14 @@ func (d *ddcBucketContractCached) HookContractEvents() error { return nil } -func (d *ddcBucketContractCached) ClusterGet(clusterId uint32) (*bucket.ClusterInfo, error) { +func (d *ddcBucketContractCached) ClusterGet(clusterId bucket.ClusterId) (*bucket.ClusterInfo, error) { return d.ddcBucketContract.ClusterGet(clusterId) } -func (d *ddcBucketContractCached) NodeGet(nodeKey string) (*bucket.NodeInfo, error) { +func (d *ddcBucketContractCached) NodeGet(nodeKey bucket.NodeKey) (*bucket.NodeInfo, error) { - result, err := d.nodeSingleFlight.Do(nodeKey, func() (interface{}, error) { - if cached, ok := d.nodeCache.Get(nodeKey); ok { + result, err := d.nodeSingleFlight.Do(nodeKey.ToHexString(), func() (interface{}, error) { + if cached, ok := d.nodeCache.Get(nodeKey.ToHexString()); ok { return cached, nil } @@ -114,7 +114,7 @@ func (d *ddcBucketContractCached) NodeGet(nodeKey string) (*bucket.NodeInfo, err return nil, err } - d.nodeCache.SetDefault(nodeKey, value) + d.nodeCache.SetDefault(nodeKey.ToHexString(), value) return value, nil }) @@ -122,11 +122,11 @@ func (d *ddcBucketContractCached) NodeGet(nodeKey string) (*bucket.NodeInfo, err return resp, err } -func (d *ddcBucketContractCached) CDNNodeGet(nodeKey string) (*bucket.CDNNodeInfo, error) { - return d.ddcBucketContract.CDNNodeGet(nodeKey) +func (d *ddcBucketContractCached) CdnNodeGet(nodeKey bucket.CdnNodeKey) (*bucket.CdnNodeInfo, error) { + return d.ddcBucketContract.CdnNodeGet(nodeKey) } -func (d *ddcBucketContractCached) BucketGet(bucketId uint32) (*bucket.BucketInfo, error) { +func (d *ddcBucketContractCached) BucketGet(bucketId bucket.BucketId) (*bucket.BucketInfo, error) { key := toString(bucketId) result, err := d.bucketSingleFlight.Do(key, func() (interface{}, error) { if cached, ok := d.bucketCache.Get(key); ok { @@ -201,11 +201,11 @@ func (d *ddcBucketContractCached) ClearAccounts() { } func (d *ddcBucketContractCached) ClearNodeById(key bucket.NodeKey) { //nolint:golint,unused - d.nodeCache.Delete(key) + d.nodeCache.Delete(key.ToHexString()) } -func (d *ddcBucketContractCached) ClearNodeByKey(nodeKey string) { - d.nodeCache.Delete(nodeKey) +func (d *ddcBucketContractCached) ClearNodeByKey(nodeKey bucket.NodeKey) { + d.nodeCache.Delete(nodeKey.ToHexString()) } func (d *ddcBucketContractCached) ClearBucketById(id bucket.BucketId) { @@ -224,7 +224,7 @@ func cacheDurationOrDefault(duration time.Duration, defaultDuration time.Duratio return defaultDuration } -func isNodeKeyPresent(nodeKeys []string, nodeKey string) bool { +func isNodeKeyPresent(nodeKeys []bucket.NodeKey, nodeKey bucket.NodeKey) bool { for _, key := range nodeKeys { if key == nodeKey { return true @@ -233,7 +233,7 @@ func isNodeKeyPresent(nodeKeys []string, nodeKey string) bool { return false } -func toString(value uint32) string { +func toString(value bucket.BucketId) string { return strconv.FormatUint(uint64(value), 10) } @@ -251,7 +251,7 @@ func validateCDNNodeParams(params bucket.CDNNodeParams) error { return nil } -func (d *ddcBucketContractCached) ClusterCreate(cluster *bucket.NewCluster) (clusterId uint32, err error) { +func (d *ddcBucketContractCached) ClusterCreate(cluster *bucket.NewCluster) (clusterId bucket.ClusterId, err error) { clusterId, err = d.ddcBucketContract.ClusterCreate(cluster) if err != nil { @@ -264,10 +264,7 @@ func (d *ddcBucketContractCached) ClusterCreate(cluster *bucket.NewCluster) (clu return clusterId, nil } -func (d *ddcBucketContractCached) ClusterAddNode(clusterId uint32, nodeKey string, vNodes [][]bucket.Token) error { - if nodeKey == "" { - return errors.New("Empty node key.") - } +func (d *ddcBucketContractCached) ClusterAddNode(clusterId bucket.ClusterId, nodeKey bucket.NodeKey, vNodes [][]bucket.Token) error { if len(vNodes) == 0 { return errors.New("No vNodes provided.") } @@ -292,7 +289,7 @@ func (d *ddcBucketContractCached) ClusterAddNode(clusterId uint32, nodeKey strin return nil } -func (d *ddcBucketContractCached) ClusterRemoveNode(clusterId uint32, nodeKey string) error { +func (d *ddcBucketContractCached) ClusterRemoveNode(clusterId bucket.ClusterId, nodeKey bucket.NodeKey) error { err := d.ddcBucketContract.ClusterRemoveNode(clusterId, nodeKey) if err != nil { return err @@ -304,7 +301,7 @@ func (d *ddcBucketContractCached) ClusterRemoveNode(clusterId uint32, nodeKey st return nil } -func (d *ddcBucketContractCached) ClusterResetNode(clusterId uint32, nodeKey string, vNodes [][]bucket.Token) error { +func (d *ddcBucketContractCached) ClusterResetNode(clusterId bucket.ClusterId, nodeKey bucket.NodeKey, vNodes [][]bucket.Token) error { clusterStatus, err := d.ClusterGet(clusterId) if err != nil { return bucket.ErrClusterDoesNotExist @@ -325,10 +322,7 @@ func (d *ddcBucketContractCached) ClusterResetNode(clusterId uint32, nodeKey str return nil } -func (d *ddcBucketContractCached) ClusterReplaceNode(clusterId uint32, vNodes [][]bucket.Token, newNodeKey string) error { - if newNodeKey == "" { - return errors.New("Empty new node key.") - } +func (d *ddcBucketContractCached) ClusterReplaceNode(clusterId bucket.ClusterId, vNodes [][]bucket.Token, newNodeKey bucket.NodeKey) error { if len(vNodes) == 0 { return errors.New("No vNodes provided.") } @@ -353,11 +347,7 @@ func (d *ddcBucketContractCached) ClusterReplaceNode(clusterId uint32, vNodes [] return nil } -func (d *ddcBucketContractCached) ClusterAddCdnNode(clusterId uint32, cdnNodeKey string) error { - if cdnNodeKey == "" { - return errors.New("Empty CDN node key.") - } - +func (d *ddcBucketContractCached) ClusterAddCdnNode(clusterId bucket.ClusterId, cdnNodeKey bucket.CdnNodeKey) error { clusterStatus, responseError := d.ClusterGet(clusterId) if responseError != nil { return bucket.ErrClusterDoesNotExist @@ -378,10 +368,7 @@ func (d *ddcBucketContractCached) ClusterAddCdnNode(clusterId uint32, cdnNodeKey return nil } -func (d *ddcBucketContractCached) ClusterRemoveCdnNode(clusterId uint32, cdnNodeKey string) error { - if cdnNodeKey == "" { - return errors.New("Empty CDN node key.") - } +func (d *ddcBucketContractCached) ClusterRemoveCdnNode(clusterId bucket.ClusterId, cdnNodeKey bucket.CdnNodeKey) error { clusterStatus, clusterError := d.ClusterGet(clusterId) if clusterError != nil { @@ -402,7 +389,7 @@ func (d *ddcBucketContractCached) ClusterRemoveCdnNode(clusterId uint32, cdnNode return nil } -func (d *ddcBucketContractCached) ClusterSetParams(clusterId uint32, params bucket.Params) error { +func (d *ddcBucketContractCached) ClusterSetParams(clusterId bucket.ClusterId, params bucket.Params) error { if params == "" { return errors.New("Empty cluster parameters.") } @@ -423,7 +410,7 @@ func (d *ddcBucketContractCached) ClusterSetParams(clusterId uint32, params buck return nil } -func (d *ddcBucketContractCached) ClusterRemove(clusterId uint32) error { +func (d *ddcBucketContractCached) ClusterRemove(clusterId bucket.ClusterId) error { _, clusterError := d.ClusterGet(clusterId) if clusterError != nil { return bucket.ErrClusterDoesNotExist @@ -440,11 +427,7 @@ func (d *ddcBucketContractCached) ClusterRemove(clusterId uint32) error { return nil } -func (d *ddcBucketContractCached) ClusterSetNodeStatus(clusterId uint32, nodeKey string, statusInCluster string) error { - if nodeKey == "" { - return errors.New("Empty node key.") - } - +func (d *ddcBucketContractCached) ClusterSetNodeStatus(clusterId bucket.ClusterId, nodeKey bucket.NodeKey, statusInCluster string) error { if statusInCluster == "" { return errors.New("Empty status in cluster.") } @@ -469,11 +452,7 @@ func (d *ddcBucketContractCached) ClusterSetNodeStatus(clusterId uint32, nodeKey return nil } -func (d *ddcBucketContractCached) ClusterSetCdnNodeStatus(clusterId uint32, cdnNodeKey string, statusInCluster string) error { - if cdnNodeKey == "" { - return errors.New("Empty CDN node key.") - } - +func (d *ddcBucketContractCached) ClusterSetCdnNodeStatus(clusterId bucket.ClusterId, cdnNodeKey bucket.CdnNodeKey, statusInCluster string) error { if statusInCluster == "" { return errors.New("Empty status in cluster.") } @@ -497,20 +476,24 @@ func (d *ddcBucketContractCached) ClusterSetCdnNodeStatus(clusterId uint32, cdnN return nil } -func (d *ddcBucketContractCached) ClusterList(offset uint32, limit uint32, filterManagerId string) []*bucket.ClusterInfo { - clusters := d.ddcBucketContract.ClusterList(offset, limit, filterManagerId) +func (d *ddcBucketContractCached) ClusterList(offset uint32, limit uint32, filterManagerId types.OptionAccountID) (*bucket.ClusterListInfo, error) { + if limit == 0 { + return nil, errors.New("Invalid limit. Limit must be greater than zero.") + } + + clusters, err := d.ddcBucketContract.ClusterList(offset, limit, filterManagerId) - if clusters == nil { - return []*bucket.ClusterInfo{} + if err != nil { + return nil, err } d.ClearBuckets() d.ClearNodes() - return clusters + return clusters, nil } -func (d *ddcBucketContractCached) NodeCreate(nodeKey string, params bucket.Params, capacity bucket.Resource) (key string, err error) { +func (d *ddcBucketContractCached) NodeCreate(nodeKey bucket.NodeKey, params bucket.Params, capacity bucket.Resource) (key bucket.NodeKey, err error) { key, err = d.ddcBucketContract.NodeCreate(nodeKey, params, capacity) d.ClearNodes() @@ -518,11 +501,7 @@ func (d *ddcBucketContractCached) NodeCreate(nodeKey string, params bucket.Param return key, err } -func (d *ddcBucketContractCached) NodeRemove(nodeKey string) error { - if nodeKey == "" { - return errors.New("Empty node key.") - } - +func (d *ddcBucketContractCached) NodeRemove(nodeKey bucket.NodeKey) error { err := d.ddcBucketContract.NodeRemove(nodeKey) if err != nil { @@ -535,11 +514,7 @@ func (d *ddcBucketContractCached) NodeRemove(nodeKey string) error { return nil } -func (d *ddcBucketContractCached) NodeSetParams(nodeKey string, params bucket.Params) error { - if nodeKey == "" { - return errors.New("Empty node key.") - } - +func (d *ddcBucketContractCached) NodeSetParams(nodeKey bucket.NodeKey, params bucket.Params) error { err := d.ddcBucketContract.NodeSetParams(nodeKey, params) if err != nil { @@ -551,12 +526,12 @@ func (d *ddcBucketContractCached) NodeSetParams(nodeKey string, params bucket.Pa return nil } -func (d *ddcBucketContractCached) NodeList(offset uint32, limit uint32, filterManagerId string) ([]*bucket.NodeInfo, error) { +func (d *ddcBucketContractCached) NodeList(offset uint32, limit uint32, filterProviderId types.OptionAccountID) (*bucket.NodeListInfo, error) { if limit == 0 { return nil, errors.New("Invalid limit. Limit must be greater than zero.") } - nodes, err := d.ddcBucketContract.NodeList(offset, limit, filterManagerId) + nodes, err := d.ddcBucketContract.NodeList(offset, limit, filterProviderId) if err != nil { return nil, err } @@ -564,12 +539,8 @@ func (d *ddcBucketContractCached) NodeList(offset uint32, limit uint32, filterMa return nodes, nil } -func (d *ddcBucketContractCached) CDNNodeCreate(nodeKey string, params bucket.CDNNodeParams) error { - if nodeKey == "" { - return errors.New("Empty node key.") - } - - err := d.ddcBucketContract.CDNNodeCreate(nodeKey, params) +func (d *ddcBucketContractCached) CdnNodeCreate(nodeKey bucket.CdnNodeKey, params bucket.CDNNodeParams) error { + err := d.ddcBucketContract.CdnNodeCreate(nodeKey, params) if err != nil { return err @@ -580,12 +551,8 @@ func (d *ddcBucketContractCached) CDNNodeCreate(nodeKey string, params bucket.CD return err } -func (d *ddcBucketContractCached) CDNNodeRemove(nodeKey string) error { - if nodeKey == "" { - return errors.New("Empty CDN node key.") - } - - err := d.ddcBucketContract.CDNNodeRemove(nodeKey) +func (d *ddcBucketContractCached) CdnNodeRemove(nodeKey bucket.CdnNodeKey) error { + err := d.ddcBucketContract.CdnNodeRemove(nodeKey) if err != nil { return err } @@ -597,16 +564,12 @@ func (d *ddcBucketContractCached) CDNNodeRemove(nodeKey string) error { return nil } -func (d *ddcBucketContractCached) CDNNodeSetParams(nodeKey string, params bucket.CDNNodeParams) error { - if nodeKey == "" { - return errors.New("Empty node key.") - } - +func (d *ddcBucketContractCached) CdnNodeSetParams(nodeKey bucket.CdnNodeKey, params bucket.CDNNodeParams) error { if err := validateCDNNodeParams(params); err != nil { return errors.Wrap(err, "Invalid CDN node params.") } - err := d.ddcBucketContract.CDNNodeSetParams(nodeKey, params) + err := d.ddcBucketContract.CdnNodeSetParams(nodeKey, params) if err != nil { return err } @@ -616,16 +579,12 @@ func (d *ddcBucketContractCached) CDNNodeSetParams(nodeKey string, params bucket return nil } -func (d *ddcBucketContractCached) CDNNodeList(offset uint32, limit uint32, filterManagerId string) ([]*bucket.CDNNodeInfo, error) { +func (d *ddcBucketContractCached) CdnNodeList(offset uint32, limit uint32, filterManagerId types.OptionAccountID) (*bucket.CdnNodeListInfo, error) { if limit == 0 { return nil, errors.New("Invalid limit. Limit must be greater than zero.") } - if filterManagerId == "" { - return nil, errors.New("Filter manager id is empty.") - } - - nodes, err := d.ddcBucketContract.CDNNodeList(offset, limit, filterManagerId) + nodes, err := d.ddcBucketContract.CdnNodeList(offset, limit, filterManagerId) if err != nil { return nil, err } @@ -641,7 +600,7 @@ func (d *ddcBucketContractCached) HasPermission(account types.AccountID, permiss return d.ddcBucketContract.HasPermission(account, permission) } -func (d *ddcBucketContractCached) GrantTrustedManagerPermission(managerId types.AccountID) error { +func (d *ddcBucketContractCached) GrantTrustedManagerPermission(managerId bucket.AccountId) error { err := d.ddcBucketContract.GrantTrustedManagerPermission(managerId) d.ClearBuckets() @@ -650,7 +609,7 @@ func (d *ddcBucketContractCached) GrantTrustedManagerPermission(managerId types. return err } -func (d *ddcBucketContractCached) RevokeTrustedManagerPermission(managerId types.AccountID) error { +func (d *ddcBucketContractCached) RevokeTrustedManagerPermission(managerId bucket.AccountId) error { err := d.ddcBucketContract.RevokeTrustedManagerPermission(managerId) d.ClearBuckets() @@ -659,7 +618,7 @@ func (d *ddcBucketContractCached) RevokeTrustedManagerPermission(managerId types return err } -func (d *ddcBucketContractCached) AdminGrantPermission(grantee types.AccountID, permission string) error { +func (d *ddcBucketContractCached) AdminGrantPermission(grantee bucket.AccountId, permission string) error { if permission == "" { return errors.New("Empty permission string.") } @@ -671,7 +630,7 @@ func (d *ddcBucketContractCached) AdminGrantPermission(grantee types.AccountID, return err } -func (d *ddcBucketContractCached) AdminRevokePermission(grantee types.AccountID, permission string) error { +func (d *ddcBucketContractCached) AdminRevokePermission(grantee bucket.AccountId, permission string) error { if permission == "" { return errors.New("Empty permission string.") } @@ -683,10 +642,7 @@ func (d *ddcBucketContractCached) AdminRevokePermission(grantee types.AccountID, return err } -func (d *ddcBucketContractCached) AdminTransferNodeOwnership(nodeKey string, newOwner types.AccountID) error { - if nodeKey == "" { - return errors.New("Empty nodeKey string.") - } +func (d *ddcBucketContractCached) AdminTransferNodeOwnership(nodeKey bucket.NodeKey, newOwner bucket.AccountId) error { err := d.ddcBucketContract.AdminTransferNodeOwnership(nodeKey, newOwner) d.ClearBuckets() @@ -695,10 +651,7 @@ func (d *ddcBucketContractCached) AdminTransferNodeOwnership(nodeKey string, new return err } -func (d *ddcBucketContractCached) AdminTransferCdnNodeOwnership(cdnNodeKey string, newOwner types.AccountID) error { - if cdnNodeKey == "" { - return errors.New("Empty cdnNodeKey string.") - } +func (d *ddcBucketContractCached) AdminTransferCdnNodeOwnership(cdnNodeKey bucket.CdnNodeKey, newOwner bucket.AccountId) error { err := d.ddcBucketContract.AdminTransferCdnNodeOwnership(cdnNodeKey, newOwner) d.ClearBuckets() @@ -706,3 +659,92 @@ func (d *ddcBucketContractCached) AdminTransferCdnNodeOwnership(cdnNodeKey strin return err } + +// TODO implement caching for underlying methods +func (d *ddcBucketContractCached) AccountDeposit() error { + return d.ddcBucketContract.AccountDeposit() +} + +func (d *ddcBucketContractCached) AccountBond(bondAmount bucket.Balance) error { + return d.ddcBucketContract.AccountBond(bondAmount) +} + +func (d *ddcBucketContractCached) AccountUnbond(bondAmount bucket.Balance) error { + return d.ddcBucketContract.AccountUnbond(bondAmount) +} + +func (d *ddcBucketContractCached) AccountGetUsdPerCere() (balance bucket.Balance, err error) { + return d.ddcBucketContract.AccountGetUsdPerCere() +} + +func (d *ddcBucketContractCached) AccountSetUsdPerCere(usdPerCere bucket.Balance) error { + return d.ddcBucketContract.AccountSetUsdPerCere(usdPerCere) +} + +func (d *ddcBucketContractCached) AccountWithdrawUnbonded() error { + return d.ddcBucketContract.AccountWithdrawUnbonded() +} + +func (d *ddcBucketContractCached) GetAccounts() ([]types.AccountID, error) { + return d.ddcBucketContract.GetAccounts() +} + +func (d *ddcBucketContractCached) BucketCreate(bucketParams bucket.BucketParams, clusterId bucket.ClusterId, ownerId types.OptionAccountID) (bucketId bucket.BucketId, err error) { + return d.ddcBucketContract.BucketCreate(bucketParams, clusterId, ownerId) +} + +func (d *ddcBucketContractCached) BucketChangeOwner(bucketId bucket.BucketId, ownerId types.AccountID) error { + return d.ddcBucketContract.BucketChangeOwner(bucketId, ownerId) +} + +func (d *ddcBucketContractCached) BucketAllocIntoCluster(bucketId bucket.BucketId, resource bucket.Resource) error { + return d.ddcBucketContract.BucketAllocIntoCluster(bucketId, resource) +} + +func (d *ddcBucketContractCached) BucketSettlePayment(bucketId bucket.BucketId) error { + return d.ddcBucketContract.BucketSettlePayment(bucketId) +} + +func (d *ddcBucketContractCached) BucketChangeParams(bucketId bucket.BucketId, bucketParams bucket.BucketParams) error { + return d.ddcBucketContract.BucketChangeParams(bucketId, bucketParams) +} + +func (d *ddcBucketContractCached) BucketList(offset uint32, limit uint32, filterOwnerId types.OptionAccountID) (*bucket.BucketListInfo, error) { + return d.ddcBucketContract.BucketList(offset, limit, filterOwnerId) +} + +func (d *ddcBucketContractCached) BucketListForAccount(ownerId types.AccountID) ([]*bucket.Bucket, error) { + return d.ddcBucketContract.BucketListForAccount(ownerId) +} + +func (d *ddcBucketContractCached) BucketSetAvailability(bucketId bucket.BucketId, publicAvailability bool) error { + return d.ddcBucketContract.BucketSetAvailability(bucketId, publicAvailability) +} + +func (d *ddcBucketContractCached) BucketSetResourceCap(bucketId bucket.BucketId, newResourceCap bucket.Resource) error { + return d.ddcBucketContract.BucketSetResourceCap(bucketId, newResourceCap) +} + +func (d *ddcBucketContractCached) GetBucketWriters(bucketId bucket.BucketId) ([]types.AccountID, error) { + return d.ddcBucketContract.GetBucketWriters(bucketId) +} + +func (d *ddcBucketContractCached) GetBucketReaders(bucketId bucket.BucketId) ([]types.AccountID, error) { + return d.ddcBucketContract.GetBucketReaders(bucketId) +} + +func (d *ddcBucketContractCached) BucketSetWriterPerm(bucketId bucket.BucketId, writer types.AccountID) error { + return d.ddcBucketContract.BucketSetWriterPerm(bucketId, writer) +} + +func (d *ddcBucketContractCached) BucketRevokeWriterPerm(bucketId bucket.BucketId, writer types.AccountID) error { + return d.ddcBucketContract.BucketRevokeWriterPerm(bucketId, writer) +} + +func (d *ddcBucketContractCached) BucketSetReaderPerm(bucketId bucket.BucketId, reader types.AccountID) error { + return d.ddcBucketContract.BucketSetReaderPerm(bucketId, reader) +} + +func (d *ddcBucketContractCached) BucketRevokeReaderPerm(bucketId bucket.BucketId, reader types.AccountID) error { + return d.ddcBucketContract.BucketRevokeReaderPerm(bucketId, reader) +} diff --git a/contract/pkg/cache/ddc_bucket_contract_cache_test.go b/contract/pkg/cache/ddc_bucket_contract_cache_test.go index 5f6df0e..92dba55 100644 --- a/contract/pkg/cache/ddc_bucket_contract_cache_test.go +++ b/contract/pkg/cache/ddc_bucket_contract_cache_test.go @@ -1,6 +1,7 @@ package cache import ( + "log" "testing" "time" @@ -26,42 +27,42 @@ func (m *mockedDdcBucketContract) GetLastAccessTime() time.Time { return args.Get(0).(time.Time) } -func (m *mockedDdcBucketContract) ClusterGet(clusterId uint32) (*bucket.ClusterInfo, error) { +func (m *mockedDdcBucketContract) ClusterGet(clusterId bucket.ClusterId) (*bucket.ClusterInfo, error) { args := m.Called(clusterId) return args.Get(0).(*bucket.ClusterInfo), args.Error(1) } -func (m *mockedDdcBucketContract) NodeGet(nodeKey string) (*bucket.NodeInfo, error) { +func (m *mockedDdcBucketContract) NodeGet(nodeKey bucket.NodeKey) (*bucket.NodeInfo, error) { args := m.Called(nodeKey) return args.Get(0).(*bucket.NodeInfo), args.Error(1) } -func (m *mockedDdcBucketContract) CDNNodeGet(nodeKey string) (*bucket.CDNNodeInfo, error) { +func (m *mockedDdcBucketContract) CdnNodeGet(nodeKey bucket.CdnNodeKey) (*bucket.CdnNodeInfo, error) { args := m.Called(nodeKey) - return args.Get(0).(*bucket.CDNNodeInfo), args.Error(1) + return args.Get(0).(*bucket.CdnNodeInfo), args.Error(1) } -func (m *mockedDdcBucketContract) BucketGet(bucketId uint32) (*bucket.BucketInfo, error) { +func (m *mockedDdcBucketContract) BucketGet(bucketId bucket.BucketId) (*bucket.BucketInfo, error) { args := m.Called(bucketId) return args.Get(0).(*bucket.BucketInfo), args.Error(1) } -func (m *mockedDdcBucketContract) AccountGet(account types.AccountID) (*bucket.Account, error) { +func (m *mockedDdcBucketContract) AccountGet(account bucket.AccountId) (*bucket.Account, error) { args := m.Called(account) return args.Get(0).(*bucket.Account), args.Error(1) } -func (m *mockedDdcBucketContract) CDNNodeList(offset uint32, limit uint32, filterManagerId string) ([]*bucket.CDNNodeInfo, error) { - args := m.Called(offset, limit, filterManagerId) - return args.Get(0).([]*bucket.CDNNodeInfo), args.Error(1) +func (m *mockedDdcBucketContract) CdnNodeList(offset uint32, limit uint32, filterProviderId types.OptionAccountID) (*bucket.CdnNodeListInfo, error) { + args := m.Called(offset, limit, filterProviderId) + return args.Get(0).(*bucket.CdnNodeListInfo), args.Error(1) } -func (m *mockedDdcBucketContract) NodeList(offset uint32, limit uint32, filterManagerId string) ([]*bucket.NodeInfo, error) { - args := m.Called(offset, limit, filterManagerId) - return args.Get(0).([]*bucket.NodeInfo), args.Error(1) +func (m *mockedDdcBucketContract) NodeList(offset uint32, limit uint32, filterProviderId types.OptionAccountID) (*bucket.NodeListInfo, error) { + args := m.Called(offset, limit, filterProviderId) + return args.Get(0).(*bucket.NodeListInfo), args.Error(1) } -func (m *mockedDdcBucketContract) ClusterCreate(cluster *bucket.NewCluster) (clusterId uint32, err error) { +func (m *mockedDdcBucketContract) ClusterCreate(cluster *bucket.NewCluster) (clusterId bucket.ClusterId, err error) { return 0, nil } @@ -73,130 +74,224 @@ func (d *mockedDdcBucketContract) GetEventDispatcher() map[types.Hash]pkg.Contra return nil } -func (m *mockedDdcBucketContract) AdminGrantPermission(grantee types.AccountID, permission string) error { +func (m *mockedDdcBucketContract) AdminGrantPermission(grantee bucket.AccountId, permission string) error { return nil } -func (m *mockedDdcBucketContract) AdminRevokePermission(grantee types.AccountID, permission string) error { +func (m *mockedDdcBucketContract) AdminRevokePermission(grantee bucket.AccountId, permission string) error { return nil } -func (m *mockedDdcBucketContract) AdminTransferCdnNodeOwnership(cdnNodeKey string, newOwner types.AccountID) error { +func (m *mockedDdcBucketContract) AdminTransferCdnNodeOwnership(cdnNodeKey bucket.CdnNodeKey, newOwner bucket.AccountId) error { return nil } -func (m *mockedDdcBucketContract) AdminTransferNodeOwnership(nodeKey string, newOwner types.AccountID) error { +func (m *mockedDdcBucketContract) AdminTransferNodeOwnership(nodeKey bucket.NodeKey, newOwner bucket.AccountId) error { return nil } -func (m *mockedDdcBucketContract) CDNNodeCreate(nodeKey string, params bucket.CDNNodeParams) error { +func (m *mockedDdcBucketContract) CdnNodeCreate(nodeKey bucket.NodeKey, params bucket.CDNNodeParams) error { return nil } -func (m *mockedDdcBucketContract) NodeCreate(nodeKey string, params bucket.Params, capacity bucket.Resource) (key string, err error) { +func (m *mockedDdcBucketContract) NodeCreate(nodeKey bucket.NodeKey, params bucket.Params, capacity bucket.Resource) (key bucket.NodeKey, err error) { args := m.Called(nodeKey, params, capacity) - return args.Get(0).(string), args.Error(1) + accountId, err := types.NewAccountIDFromHexString(args.Get(0).(string)) + if err != nil { + log.Fatalf("Failed to create AccountID from hex string: %v", err) + return types.AccountID{}, err + } + key = *accountId + return key, args.Error(1) } -func (m *mockedDdcBucketContract) CDNNodeRemove(nodeKey string) error { +func (m *mockedDdcBucketContract) CdnNodeRemove(nodeKey bucket.CdnNodeKey) error { args := m.Called(nodeKey) return args.Error(1) } -func (m *mockedDdcBucketContract) NodeRemove(nodeKey string) error { +func (m *mockedDdcBucketContract) NodeRemove(nodeKey bucket.NodeKey) error { args := m.Called(nodeKey) return args.Error(1) } -func (m *mockedDdcBucketContract) CDNNodeSetParams(nodeKey string, params bucket.CDNNodeParams) error { +func (m *mockedDdcBucketContract) CdnNodeSetParams(nodeKey bucket.CdnNodeKey, params bucket.CDNNodeParams) error { args := m.Called(nodeKey, params) return args.Error(1) } -func (m *mockedDdcBucketContract) ClusterAddCdnNode(clusterId uint32, cdnNodeKey string) error { +func (m *mockedDdcBucketContract) ClusterAddCdnNode(clusterId bucket.ClusterId, cdnNodeKey bucket.CdnNodeKey) error { args := m.Called(clusterId, cdnNodeKey) return args.Error(1) } -func (m *mockedDdcBucketContract) ClusterAddNode(clusterId uint32, nodeKey string, vNodes [][]bucket.Token) error { +func (m *mockedDdcBucketContract) ClusterAddNode(clusterId bucket.ClusterId, nodeKey bucket.NodeKey, vNodes [][]bucket.Token) error { args := m.Called(clusterId, nodeKey, vNodes) return args.Error(1) } -func (m *mockedDdcBucketContract) ClusterList(offset uint32, limit uint32, filterManagerId string) []*bucket.ClusterInfo { +func (m *mockedDdcBucketContract) ClusterList(offset uint32, limit uint32, filterManagerId types.OptionAccountID) (*bucket.ClusterListInfo, error) { args := m.Called(offset, limit, filterManagerId) - return args.Get(0).([]*bucket.ClusterInfo) + return args.Get(0).(*bucket.ClusterListInfo), args.Error(1) } -func (m *mockedDdcBucketContract) ClusterRemoveNode(clusterId uint32, nodeKey string) error { +func (m *mockedDdcBucketContract) ClusterRemoveNode(clusterId bucket.ClusterId, nodeKey bucket.NodeKey) error { args := m.Called(clusterId, nodeKey) return args.Error(1) } -func (m *mockedDdcBucketContract) ClusterRemove(clusterId uint32) error { +func (m *mockedDdcBucketContract) ClusterRemove(clusterId bucket.ClusterId) error { args := m.Called(clusterId) return args.Error(1) } -func (m *mockedDdcBucketContract) ClusterRemoveCdnNode(clusterId uint32, cdnNodeKey string) error { +func (m *mockedDdcBucketContract) ClusterRemoveCdnNode(clusterId bucket.ClusterId, cdnNodeKey bucket.CdnNodeKey) error { args := m.Called(clusterId, cdnNodeKey) return args.Error(1) } -func (m *mockedDdcBucketContract) ClusterReplaceNode(clusterId uint32, vNodes [][]bucket.Token, newNodeKey string) error { +func (m *mockedDdcBucketContract) ClusterReplaceNode(clusterId bucket.ClusterId, vNodes [][]bucket.Token, newNodeKey bucket.NodeKey) error { args := m.Called(clusterId, vNodes, newNodeKey) return args.Error(1) } -func (m *mockedDdcBucketContract) ClusterResetNode(clusterId uint32, nodeKey string, vNodes [][]bucket.Token) error { +func (m *mockedDdcBucketContract) ClusterResetNode(clusterId bucket.ClusterId, nodeKey bucket.NodeKey, vNodes [][]bucket.Token) error { args := m.Called(clusterId, nodeKey, vNodes) return args.Error(1) } -func (m *mockedDdcBucketContract) ClusterSetCdnNodeStatus(clusterId uint32, cdnNodeKey string, statusInCluster string) error { +func (m *mockedDdcBucketContract) ClusterSetCdnNodeStatus(clusterId bucket.ClusterId, cdnNodeKey bucket.CdnNodeKey, statusInCluster string) error { args := m.Called(clusterId, cdnNodeKey, statusInCluster) return args.Error(1) } -func (m *mockedDdcBucketContract) ClusterSetNodeStatus(clusterId uint32, nodeKey string, statusInCluster string) error { +func (m *mockedDdcBucketContract) ClusterSetNodeStatus(clusterId bucket.ClusterId, nodeKey bucket.NodeKey, statusInCluster string) error { args := m.Called(clusterId, nodeKey, statusInCluster) return args.Error(1) } -func (m *mockedDdcBucketContract) ClusterSetParams(clusterId uint32, params bucket.Params) error { +func (m *mockedDdcBucketContract) ClusterSetParams(clusterId bucket.ClusterId, params bucket.Params) error { args := m.Called(clusterId, params) return args.Error(1) } -func (m *mockedDdcBucketContract) GrantTrustedManagerPermission(managerId types.AccountID) error { +func (m *mockedDdcBucketContract) GrantTrustedManagerPermission(managerId bucket.AccountId) error { args := m.Called(managerId) return args.Error(1) } -func (m *mockedDdcBucketContract) HasPermission(account types.AccountID, permission string) (bool, error) { +func (m *mockedDdcBucketContract) HasPermission(account bucket.AccountId, permission string) (bool, error) { args := m.Called(account, permission) return true, args.Error(1) } -func (m *mockedDdcBucketContract) NodeSetParams(nodeKey string, params bucket.Params) error { +func (m *mockedDdcBucketContract) NodeSetParams(nodeKey bucket.NodeKey, params bucket.Params) error { args := m.Called(nodeKey, params) return args.Error(1) } -func (m *mockedDdcBucketContract) RevokeTrustedManagerPermission(managerId types.AccountID) error { +func (m *mockedDdcBucketContract) RevokeTrustedManagerPermission(managerId bucket.AccountId) error { args := m.Called(managerId) return args.Error(1) } +// TODO: implement yhe underlying methods +func (m *mockedDdcBucketContract) AccountDeposit() error { + panic("implement me") +} + +func (m *mockedDdcBucketContract) AccountBond(bondAmount bucket.Balance) error { + panic("implement me") +} + +func (m *mockedDdcBucketContract) AccountUnbond(bondAmount bucket.Balance) error { + panic("implement me") +} +func (m *mockedDdcBucketContract) AccountGetUsdPerCere() (balance bucket.Balance, err error) { + panic("implement me") +} + +func (m *mockedDdcBucketContract) AccountSetUsdPerCere(usdPerCere bucket.Balance) error { + panic("implement me") +} + +func (m *mockedDdcBucketContract) AccountWithdrawUnbonded() error { + panic("implement me") +} + +func (m *mockedDdcBucketContract) GetAccounts() ([]types.AccountID, error) { + panic("implement me") +} + +func (m *mockedDdcBucketContract) BucketCreate(bucketParams bucket.BucketParams, clusterId bucket.ClusterId, ownerId types.OptionAccountID) (bucketId bucket.BucketId, err error) { + panic("implement me") +} + +func (m *mockedDdcBucketContract) BucketChangeOwner(bucketId bucket.BucketId, ownerId types.AccountID) error { + panic("implement me") +} + +func (m *mockedDdcBucketContract) BucketAllocIntoCluster(bucketId bucket.BucketId, resource bucket.Resource) error { + panic("implement me") +} + +func (m *mockedDdcBucketContract) BucketSetResourceCap(bucketId bucket.BucketId, newResourceCap bucket.Resource) error { + panic("implement me") +} + +func (m *mockedDdcBucketContract) BucketSettlePayment(bucketId bucket.BucketId) error { + panic("implement me") +} + +func (m *mockedDdcBucketContract) BucketChangeParams(bucketId bucket.BucketId, bucketParams bucket.BucketParams) error { + panic("implement me") +} + +func (m *mockedDdcBucketContract) BucketList(offset uint32, limit uint32, ownerId types.OptionAccountID) (*bucket.BucketListInfo, error) { + panic("implement me") +} + +func (m *mockedDdcBucketContract) BucketListForAccount(ownerId types.AccountID) ([]*bucket.Bucket, error) { + panic("implement me") +} + +func (m *mockedDdcBucketContract) BucketSetAvailability(bucketId bucket.BucketId, publicAvailability bool) error { + panic("implement me") +} + +func (m *mockedDdcBucketContract) GetBucketWriters(bucketId bucket.BucketId) ([]types.AccountID, error) { + panic("implement me") +} + +func (m *mockedDdcBucketContract) GetBucketReaders(bucketId bucket.BucketId) ([]types.AccountID, error) { + panic("implement me") +} + +func (m *mockedDdcBucketContract) BucketSetWriterPerm(bucketId bucket.BucketId, writer types.AccountID) error { + panic("implement me") +} + +func (m *mockedDdcBucketContract) BucketRevokeWriterPerm(bucketId bucket.BucketId, writer types.AccountID) error { + panic("implement me") +} + +func (m *mockedDdcBucketContract) BucketSetReaderPerm(bucketId bucket.BucketId, reader types.AccountID) error { + panic("implement me") +} + +func (m *mockedDdcBucketContract) BucketRevokeReaderPerm(bucketId bucket.BucketId, reader types.AccountID) error { + panic("implement me") +} + func TestBucketGet(t *testing.T) { //given ddcBucketContract := &mockedDdcBucketContract{} testSubject := &ddcBucketContractCached{bucketCache: cache.New(defaultExpiration, cleanupInterval), ddcBucketContract: ddcBucketContract} result := &bucket.BucketInfo{} - ddcBucketContract.On("BucketGet", uint32(1)).Return(result, nil).Once() + ddcBucketContract.On("BucketGet", types.NewU32(1)).Return(result, nil).Once() //when - bucket, err := testSubject.BucketGet(uint32(1)) + bucket, err := testSubject.BucketGet(types.NewU32(1)) //then assert.NoError(t, err) @@ -208,12 +303,12 @@ func TestBucketGetCached(t *testing.T) { //given ddcBucketContract := &mockedDdcBucketContract{} testSubject := &ddcBucketContractCached{bucketCache: cache.New(defaultExpiration, cleanupInterval), ddcBucketContract: ddcBucketContract} - result := &bucket.BucketInfo{BucketId: uint32(1)} - ddcBucketContract.On("BucketGet", uint32(1)).Return(result, nil).Once() - _, _ = testSubject.BucketGet(uint32(1)) + result := &bucket.BucketInfo{BucketId: types.NewU32(1)} + ddcBucketContract.On("BucketGet", types.NewU32(1)).Return(result, nil).Once() + _, _ = testSubject.BucketGet(types.NewU32(1)) //when - bucket, err := testSubject.BucketGet(uint32(1)) + bucket, err := testSubject.BucketGet(types.NewU32(1)) //then assert.NoError(t, err) @@ -226,11 +321,11 @@ func TestBucketGetCached(t *testing.T) { // //given // ddcBucketContract := &mockedDdcBucketContract{} // testSubject := &ddcBucketContractCached{bucketCache: cache.New(defaultExpiration, cleanupInterval), ddcBucketContract: ddcBucketContract} -// result := []*bucket.CDNNodeInfo{} -// ddcBucketContract.On("CDNNodeGet", 0, 1, "filterManagerId").Return(result, nil).Once() +// result := []*bucket.CdnNodeInfo{} +// ddcBucketContract.On("CdnNodeGet", 0, 1, "filterManagerId").Return(result, nil).Once() // //when -// nodes, err := testSubject.CDNNodeList(0, 1, "filterManagerId") +// nodes, err := testSubject.CdnNodeList(0, 1, "filterManagerId") // //then // assert.NoError(t, err) diff --git a/contract/pkg/client.go b/contract/pkg/client.go index 490155c..da30065 100644 --- a/contract/pkg/client.go +++ b/contract/pkg/client.go @@ -231,6 +231,7 @@ func (b *blockchainClient) CallToReadEncoded(contractAddressSS58 string, fromAdd } func (b *blockchainClient) callToRead(contractAddressSS58 string, fromAddress string, data []byte) (Response, error) { + params := Request{ Origin: fromAddress, Dest: contractAddressSS58, diff --git a/contract/pkg/mock/ddc_bucket_contract_mock.go b/contract/pkg/mock/ddc_bucket_contract_mock.go index 8c9b802..f8e3aae 100644 --- a/contract/pkg/mock/ddc_bucket_contract_mock.go +++ b/contract/pkg/mock/ddc_bucket_contract_mock.go @@ -6,6 +6,7 @@ import ( "fmt" "math" "math/big" + "strings" "time" "github.com/centrifuge/go-substrate-rpc-client/v4/types" @@ -46,7 +47,7 @@ type ( VNodes []uint64 } - CDNNode struct { + CdnNode struct { Key string `json:"key"` Params CDNNodeParams `json:"params"` } @@ -63,23 +64,35 @@ type ( lastAccessTime time.Time nodes []Node clusters []Cluster - cdnNodes []CDNNode + cdnNodes []CdnNode } ) func MapTestNodesVNodes(nodes []NodeVNodes) []bucket.NodeVNodesInfo { var nodesVNodes []bucket.NodeVNodesInfo for _, node := range nodes { + nodeKey, err := types.NewAccountIDFromHexString(node.NodeKey) + if err != nil { + log.Printf("ERROR DECODING THE ACCOUNT ID err: %v", err) + } nodeVNodes := bucket.NodeVNodesInfo{ - NodeKey: node.NodeKey, - VNodes: node.VNodes, + NodeKey: *nodeKey, + VNodes: MapTokens(node.VNodes), } nodesVNodes = append(nodesVNodes, nodeVNodes) } return nodesVNodes } -func CreateDdcBucketContractMock(apiUrl string, accountId string, nodes []Node, clusters []Cluster, cdnNodes []CDNNode) bucket.DdcBucketContract { +func MapTokens(tokens []uint64) []bucket.Token { + var result []bucket.Token + for _, token := range tokens { + result = append(result, types.NewU64(token)) + } + return result +} + +func CreateDdcBucketContractMock(apiUrl string, accountId string, nodes []Node, clusters []Cluster, cdnNodes []CdnNode) bucket.DdcBucketContract { log.Info("DDC Bucket contract configured [MOCK]") return &ddcBucketContractMock{ accountId: accountId, @@ -91,12 +104,12 @@ func CreateDdcBucketContractMock(apiUrl string, accountId string, nodes []Node, } } -func (d *ddcBucketContractMock) BucketGet(bucketId uint32) (*bucket.BucketInfo, error) { +func (d *ddcBucketContractMock) BucketGet(bucketId bucket.BucketId) (*bucket.BucketInfo, error) { if bucketId == 0 || len(d.clusters)*2 < int(bucketId) { return nil, errors.New("unknown bucket") } - clusterId := bucketId + clusterId := uint32(bucketId) if int(bucketId) > len(d.clusters) { clusterId -= uint32(len(d.clusters)) } @@ -104,9 +117,9 @@ func (d *ddcBucketContractMock) BucketGet(bucketId uint32) (*bucket.BucketInfo, return CreateBucket(bucketId, clusterId, "", writerIds), nil } -func (d *ddcBucketContractMock) ClusterGet(clusterId uint32) (*bucket.ClusterInfo, error) { +func (d *ddcBucketContractMock) ClusterGet(clusterId bucket.ClusterId) (*bucket.ClusterInfo, error) { for _, cluster := range d.clusters { - if cluster.Id == clusterId { + if cluster.Id == uint32(clusterId) { return &bucket.ClusterInfo{ ClusterId: clusterId, Cluster: bucket.Cluster{ @@ -130,16 +143,16 @@ func (d *ddcBucketContractMock) ClusterGet(clusterId uint32) (*bucket.ClusterInf return nil, fmt.Errorf("unknown cluster with id %v | available clusters are: %v", clusterId, available) } -func (d *ddcBucketContractMock) NodeGet(nodeKey string) (*bucket.NodeInfo, error) { +func (d *ddcBucketContractMock) NodeGet(nodeKey bucket.NodeKey) (*bucket.NodeInfo, error) { for _, node := range d.nodes { - if node.Key == nodeKey { + if strings.TrimPrefix(node.Key, "0x") == strings.TrimPrefix(nodeKey.ToHexString(), "0x") { return &bucket.NodeInfo{ Key: nodeKey, Node: bucket.Node{ ProviderId: types.AccountID{}, RentPerMonth: types.NewU128(*big.NewInt(1)), Params: `{"url":"` + node.Url + `"}`, - StatusInCluster: types.NewOptionBytes([]byte{bucket.NodeStatusesInClusterMap[node.StatusInCluster]}), + StatusInCluster: types.NewOptionU8(types.NewU8((bucket.NodeStatusesInClusterMap[node.StatusInCluster]))), //types.NewOptionBytes([]byte{bucket.NodeStatusesInClusterMap[node.StatusInCluster]}), FreeResources: 100, }, }, nil @@ -154,16 +167,16 @@ func (d *ddcBucketContractMock) NodeGet(nodeKey string) (*bucket.NodeInfo, error return nil, fmt.Errorf("unknown node with key %v | available nodes are: %v", nodeKey, available) } -func (d *ddcBucketContractMock) CDNNodeGet(nodeKey string) (*bucket.CDNNodeInfo, error) { +func (d *ddcBucketContractMock) CdnNodeGet(nodeKey bucket.CdnNodeKey) (*bucket.CdnNodeInfo, error) { for _, node := range d.cdnNodes { - if node.Key == nodeKey { + if strings.TrimPrefix(node.Key, "0x") == strings.TrimPrefix(nodeKey.ToHexString(), "0x") { params, err := json.Marshal(node.Params) if err != nil { return nil, err } - return &bucket.CDNNodeInfo{ + return &bucket.CdnNodeInfo{ Key: nodeKey, - Node: bucket.CDNNode{ + Node: bucket.CdnNode{ ProviderId: types.AccountID{}, UndistributedPayment: types.NewU128(*big.NewInt(1)), Params: string(params), @@ -208,14 +221,14 @@ func (d *ddcBucketContractMock) AddContractEventHandler(event string, handler fu return nil } -func CreateBucket(bucketId uint32, clusterId uint32, bucketParams string, writerIds []types.AccountID) *bucket.BucketInfo { +func CreateBucket(bucketId bucket.BucketId, clusterId uint32, bucketParams string, writerIds []types.AccountID) *bucket.BucketInfo { return &bucket.BucketInfo{ BucketId: bucketId, Bucket: bucket.Bucket{ OwnerId: writerIds[0], - ClusterId: clusterId, + ClusterId: types.NewU32(clusterId), ResourceReserved: 32, - PublicAvailability: clusterId != bucketId, + PublicAvailability: types.NewU32(clusterId) != bucketId, GasConsumptionCap: math.MaxUint32, }, Params: bucketParams, @@ -241,137 +254,247 @@ func (d *ddcBucketContractMock) GetEventDispatcher() map[types.Hash]pkg.Contract return nil } -func (d *ddcBucketContractMock) ClusterCreate(cluster *bucket.NewCluster) (clusterId uint32, err error) { +func (d *ddcBucketContractMock) ClusterCreate(cluster *bucket.NewCluster) (clusterId bucket.ClusterId, err error) { + //TODO implement me + panic("implement me") +} + +func (d *ddcBucketContractMock) ClusterAddNode(clusterId bucket.ClusterId, nodeKey bucket.NodeKey, vNodes [][]bucket.Token) error { + //TODO implement me + panic("implement me") +} + +func (d *ddcBucketContractMock) ClusterRemoveNode(clusterId bucket.ClusterId, nodeKey bucket.NodeKey) error { + //TODO implement me + panic("implement me") +} + +func (d *ddcBucketContractMock) ClusterResetNode(clusterId bucket.ClusterId, nodeKey bucket.NodeKey, vNodes [][]bucket.Token) error { + //TODO implement me + panic("implement me") +} + +func (d *ddcBucketContractMock) ClusterReplaceNode(clusterId bucket.ClusterId, vNodes [][]bucket.Token, newNodeKey bucket.NodeKey) error { + //TODO implement me + panic("implement me") +} + +func (d *ddcBucketContractMock) ClusterAddCdnNode(clusterId bucket.ClusterId, cdnNodeKey bucket.CdnNodeKey) error { + //TODO implement me + panic("implement me") +} + +func (d *ddcBucketContractMock) ClusterRemoveCdnNode(clusterId bucket.ClusterId, cdnNodeKey bucket.CdnNodeKey) error { + //TODO implement me + panic("implement me") +} + +func (d *ddcBucketContractMock) ClusterSetParams(clusterId bucket.ClusterId, params bucket.Params) error { + //TODO implement me + panic("implement me") +} + +func (d *ddcBucketContractMock) ClusterRemove(clusterId bucket.ClusterId) error { + //TODO implement me + panic("implement me") +} + +func (d *ddcBucketContractMock) ClusterSetNodeStatus(clusterId bucket.ClusterId, nodeKey bucket.NodeKey, statusInCluster string) error { + //TODO implement me + panic("implement me") +} + +func (d *ddcBucketContractMock) ClusterSetCdnNodeStatus(clusterId bucket.ClusterId, cdnNodeKey bucket.CdnNodeKey, statusInCluster string) error { + //TODO implement me + panic("implement me") +} + +func (d *ddcBucketContractMock) ClusterList(offset uint32, limit uint32, filterManagerId types.OptionAccountID) (*bucket.ClusterListInfo, error) { + //TODO implement me + panic("implement me") +} + +func (d *ddcBucketContractMock) NodeCreate(nodeKey bucket.NodeKey, params bucket.Params, capacity bucket.Resource) (key bucket.NodeKey, err error) { + //TODO implement me + panic("implement me") +} + +func (d *ddcBucketContractMock) NodeRemove(nodeKey bucket.NodeKey) error { + //TODO implement me + panic("implement me") +} + +func (d *ddcBucketContractMock) NodeSetParams(nodeKey bucket.NodeKey, params bucket.Params) error { + //TODO implement me + panic("implement me") +} + +func (d *ddcBucketContractMock) NodeList(offset uint32, limit uint32, filterProviderId types.OptionAccountID) (*bucket.NodeListInfo, error) { + //TODO implement me + panic("implement me") +} + +func (d *ddcBucketContractMock) CdnNodeCreate(nodeKey bucket.CdnNodeKey, params bucket.CDNNodeParams) error { + //TODO implement me + panic("implement me") +} + +func (d *ddcBucketContractMock) CdnNodeRemove(nodeKey bucket.CdnNodeKey) error { + //TODO implement me + panic("implement me") +} + +func (d *ddcBucketContractMock) CdnNodeSetParams(nodeKey bucket.CdnNodeKey, params bucket.CDNNodeParams) error { + //TODO implement me + panic("implement me") +} + +func (d *ddcBucketContractMock) CdnNodeList(offset uint32, limit uint32, filterProviderId types.OptionAccountID) (*bucket.CdnNodeListInfo, error) { + //TODO implement me + panic("implement me") +} + +func (d *ddcBucketContractMock) HasPermission(account bucket.AccountId, permission string) (bool, error) { + //TODO implement me + panic("implement me") +} + +func (d *ddcBucketContractMock) GrantTrustedManagerPermission(managerId bucket.AccountId) error { + //TODO implement me + panic("implement me") +} + +func (d *ddcBucketContractMock) RevokeTrustedManagerPermission(managerId bucket.AccountId) error { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) ClusterAddNode(clusterId uint32, nodeKey string, vNodes [][]bucket.Token) error { +func (d *ddcBucketContractMock) AdminGrantPermission(grantee bucket.AccountId, permission string) error { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) ClusterRemoveNode(clusterId uint32, nodeKey string) error { +func (d *ddcBucketContractMock) AdminRevokePermission(grantee bucket.AccountId, permission string) error { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) ClusterResetNode(clusterId uint32, nodeKey string, vNodes [][]bucket.Token) error { +func (d *ddcBucketContractMock) AdminTransferNodeOwnership(nodeKey bucket.NodeKey, newOwner bucket.AccountId) error { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) ClusterReplaceNode(clusterId uint32, vNodes [][]bucket.Token, newNodeKey string) error { +func (d *ddcBucketContractMock) AdminTransferCdnNodeOwnership(cdnNodeKey bucket.CdnNodeKey, newOwner bucket.AccountId) error { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) ClusterAddCdnNode(clusterId uint32, cdnNodeKey string) error { +func (d *ddcBucketContractMock) AccountDeposit() error { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) ClusterRemoveCdnNode(clusterId uint32, cdnNodeKey string) error { +func (d *ddcBucketContractMock) AccountBond(balance bucket.Balance) error { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) ClusterSetParams(clusterId uint32, params bucket.Params) error { +func (d *ddcBucketContractMock) AccountUnbond(bondAmount bucket.Balance) error { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) ClusterRemove(clusterId uint32) error { +func (d *ddcBucketContractMock) AccountGetUsdPerCere() (bucket.Balance, error) { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) ClusterSetNodeStatus(clusterId uint32, nodeKey string, statusInCluster string) error { +func (d *ddcBucketContractMock) AccountSetUsdPerCere(balance bucket.Balance) error { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) ClusterSetCdnNodeStatus(clusterId uint32, cdnNodeKey string, statusInCluster string) error { +func (d *ddcBucketContractMock) AccountWithdrawUnbonded() error { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) ClusterList(offset uint32, limit uint32, filterManagerId string) []*bucket.ClusterInfo { +func (d *ddcBucketContractMock) GetAccounts() ([]bucket.AccountId, error) { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) NodeCreate(nodeKey string, params bucket.Params, capacity bucket.Resource) (key string, err error) { +func (d *ddcBucketContractMock) BucketCreate(bucketParams bucket.BucketParams, clusterId bucket.ClusterId, ownerId types.OptionAccountID) (bucketId bucket.BucketId, err error) { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) NodeRemove(nodeKey string) error { +func (d *ddcBucketContractMock) BucketChangeOwner(bucketId bucket.BucketId, ownerId types.AccountID) error { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) NodeSetParams(nodeKey string, params bucket.Params) error { +func (d *ddcBucketContractMock) BucketAllocIntoCluster(bucketId bucket.BucketId, resource bucket.Resource) error { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) NodeList(offset uint32, limit uint32, filterManagerId string) ([]*bucket.NodeInfo, error) { +func (d *ddcBucketContractMock) BucketSettlePayment(bucketId bucket.BucketId) error { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) CDNNodeCreate(nodeKey string, params bucket.CDNNodeParams) error { +func (d *ddcBucketContractMock) BucketChangeParams(bucketId bucket.BucketId, bucketParams bucket.BucketParams) error { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) CDNNodeRemove(nodeKey string) error { +func (d *ddcBucketContractMock) BucketList(offset uint32, limit uint32, filterOnwerId types.OptionAccountID) (*bucket.BucketListInfo, error) { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) CDNNodeSetParams(nodeKey string, params bucket.CDNNodeParams) error { +func (d *ddcBucketContractMock) BucketListForAccount(ownerId types.AccountID) ([]*bucket.Bucket, error) { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) CDNNodeList(offset uint32, limit uint32, filterManagerId string) ([]*bucket.CDNNodeInfo, error) { +func (d *ddcBucketContractMock) BucketSetAvailability(bucketId bucket.BucketId, publicAvailability bool) error { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) HasPermission(account types.AccountID, permission string) (bool, error) { +func (d *ddcBucketContractMock) BucketSetResourceCap(bucketId bucket.BucketId, newResourceCap bucket.Resource) error { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) GrantTrustedManagerPermission(managerId types.AccountID) error { +func (d *ddcBucketContractMock) GetBucketWriters(bucketId bucket.BucketId) ([]bucket.AccountId, error) { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) RevokeTrustedManagerPermission(managerId types.AccountID) error { +func (d *ddcBucketContractMock) GetBucketReaders(bucketId bucket.BucketId) ([]bucket.AccountId, error) { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) AdminGrantPermission(grantee types.AccountID, permission string) error { +func (d *ddcBucketContractMock) BucketSetWriterPerm(bucketId bucket.BucketId, writer bucket.AccountId) error { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) AdminRevokePermission(grantee types.AccountID, permission string) error { +func (d *ddcBucketContractMock) BucketRevokeWriterPerm(bucketId bucket.BucketId, writer bucket.AccountId) error { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) AdminTransferNodeOwnership(nodeKey string, newOwner types.AccountID) error { +func (d *ddcBucketContractMock) BucketSetReaderPerm(bucketId bucket.BucketId, reader bucket.AccountId) error { //TODO implement me panic("implement me") } -func (d *ddcBucketContractMock) AdminTransferCdnNodeOwnership(cdnNodeKey string, newOwner types.AccountID) error { +func (d *ddcBucketContractMock) BucketRevokeReaderPerm(bucketId bucket.BucketId, reader bucket.AccountId) error { //TODO implement me panic("implement me") } diff --git a/contract/pkg/utils.go b/contract/pkg/utils.go index ff544a5..5238c75 100644 --- a/contract/pkg/utils.go +++ b/contract/pkg/utils.go @@ -4,11 +4,12 @@ import ( "bytes" "errors" "fmt" + "strings" + "github.com/centrifuge/go-substrate-rpc-client/v4/scale" "github.com/centrifuge/go-substrate-rpc-client/v4/types" "github.com/decred/base58" "golang.org/x/crypto/blake2b" - "strings" ) const addressLength = 32 + 1 + 2