Commit ae71aee0 authored by Yury's avatar Yury
Browse files

Merge branch 'feature/rub_usd' into 'dev'

Feature/rub usd

See merge request !45
Showing with 169 additions and 4 deletions
+169 -4
......@@ -10,6 +10,10 @@ type PingRecord struct {
Value string `json:"value,omitempty"`
}
func (this PingRecord) GetObjectType() string {
return "PingRecord";
}
func (this PingRecord) CheckBeforeAdd(manager IManager) error {
return nil
}
......
......@@ -7,6 +7,10 @@ import (
type IManager interface {
GetTime() int64
GetStateByPartialCompositeKey(objectType string, keys []string) (shim.StateQueryIteratorInterface, error)
CreateCompositeKey(objectType string, attributes []string) (string, error)
GetObjectListByPartialCompositeKey(expectedValidObjectType ValidObject, keys []string) ([]ValidObject, error)
SplitCompositeKey(compositeKey string) (string, []string, error)
SetAdapter(new StubInterfaceAdapter)
SetTimeGetter(new TimeGetter)
GetAdapter() StubInterfaceAdapter
......
package chaincode
import (
"encoding/json"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
"encoding/json"
"github.com/pkg/errors"
"reflect"
"gitlab.smartblocklab.com/smartcontracts/common/security/actions"
"gitlab.smartblocklab.com/smartcontracts/common/model/requests"
"gitlab.smartblocklab.com/smartcontracts/common/invoke"
"gitlab.smartblocklab.com/smartcontracts/common/model/requests"
"gitlab.smartblocklab.com/smartcontracts/common/security/actions"
"reflect"
)
// Пример наследования данной структуры
......@@ -68,6 +68,54 @@ func (this ManagerImpl) PutState(key string, value []byte) error {
return this.Adapter.PutState(key, value)
}
func (this ManagerImpl) CreateCompositeKey(objectType string, attributes []string) (string, error) {
GlobalLogger.Infof("managerImpl.CreateCompositeKey by objectType %s and attributes %v", objectType, attributes)
return this.Adapter.CreateCompositeKey(objectType, attributes)
}
func (this ManagerImpl) GetStateByPartialCompositeKey(objectType string, keys []string) (shim.StateQueryIteratorInterface, error) {
GlobalLogger.Infof("managerImpl.CreateCompositeKey by objectType %s and attributes %v", objectType, keys)
return this.Adapter.GetStateByPartialCompositeKey(objectType, keys)
}
func (this ManagerImpl) GetObjectListByPartialCompositeKey(expectedValidObjectType ValidObject, keys []string) ([]ValidObject, error) {
objectType := expectedValidObjectType.GetObjectType()
GlobalLogger.Infof("managerImpl.CreateCompositeKey by objectType %s and attributes %v", objectType, keys)
stateQueryIteratorInterface, err := this.GetStateByPartialCompositeKey(objectType, keys)
if err != nil {
return nil, err
}
defer stateQueryIteratorInterface.Close()
validObjectArray := []ValidObject{}
// Iterate through result set and for each marble found, transfer to newOwner
var i int
for i = 0; stateQueryIteratorInterface.HasNext(); i++ {
// Note that we don't get the value (2nd return variable), we'll just get the marble name from the composite key
responseRange, err := stateQueryIteratorInterface.Next()
if err != nil {
return nil, err
}
validObject := expectedValidObjectType.New()
err = json.Unmarshal(responseRange.Value, validObject)
if err != nil {
return nil, GlobalLogger.Error(err.Error())
}
validObjectArray = append(validObjectArray, validObject)
}
return validObjectArray, nil
}
func (this ManagerImpl) SplitCompositeKey(compositeKey string) (string, []string, error) {
GlobalLogger.Infof("managerImpl.SplitCompositeKey by key %s", compositeKey)
return this.Adapter.SplitCompositeKey(compositeKey)
}
func (this ManagerImpl) DelState(key string) error {
GlobalLogger.Infof("managerImpl.DelState %s", key)
return this.Adapter.DelState(key)
......
......@@ -68,6 +68,26 @@ func (mock ManagerImplMock) GetQueryResult(query string) (shim.StateQueryIterato
return mockArgs.Get(0).(shim.StateQueryIteratorInterface), mockArgs.Error(1)
}
func (mock ManagerImplMock) GetStateByPartialCompositeKey(objectType string, keys []string) (shim.StateQueryIteratorInterface, error) {
mockArgs := mock.Called(objectType, keys)
return mockArgs.Get(0).(shim.StateQueryIteratorInterface), mockArgs.Error(1)
}
func (mock ManagerImplMock) GetObjectListByPartialCompositeKey(expectedValidObjectType ValidObject, keys []string) ([]ValidObject, error) {
mockArgs := mock.Called(expectedValidObjectType, keys)
return mockArgs.Get(0).([]ValidObject), mockArgs.Error(1)
}
func (mock ManagerImplMock) CreateCompositeKey(objectType string, attributes []string) (string, error) {
mockArgs := mock.Called(objectType, attributes)
return mockArgs.Get(0).(string), mockArgs.Error(1)
}
func (mock ManagerImplMock) SplitCompositeKey(compositeKey string) (string, []string, error) {
mockArgs := mock.Called(compositeKey)
return mockArgs.Get(0).(string), mockArgs.Get(1).([]string), mockArgs.Error(2)
}
func (mock ManagerImplMock) InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.Response {
mockArgs := mock.Called(chaincodeName, args, channel)
if mockArgs.Get(0) == nil {
......
......@@ -55,6 +55,31 @@ type StubInterfaceAdapter interface {
InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.Response
GetTxID() string
// CreateCompositeKey combines the given `attributes` to form a composite
// key. The objectType and attributes are expected to have only valid utf8
// strings and should not contain U+0000 (nil byte) and U+10FFFF
// (biggest and unallocated code point).
// The resulting composite key can be used as the key in PutState().
CreateCompositeKey(objectType string, attributes []string) (string, error)
// SplitCompositeKey splits the specified key into attributes on which the
// composite key was formed. Composite keys found during range queries
// or partial composite key queries can therefore be split into their
// composite parts.
SplitCompositeKey(compositeKey string) (string, []string, error)
// GetStateByPartialCompositeKey queries the state in the ledger based on
// a given partial composite key. This function returns an iterator
// which can be used to iterate over all composite keys whose prefix matches
// the given partial composite key. The `objectType` and attributes are
// expected to have only valid utf8 strings and should not contain
// U+0000 (nil byte) and U+10FFFF (biggest and unallocated code point).
// See related functions SplitCompositeKey and CreateCompositeKey.
// Call Close() on the returned StateQueryIteratorInterface object when done.
// The query is re-executed during validation phase to ensure result set
// has not changed since transaction endorsement (phantom reads detected).
GetStateByPartialCompositeKey(objectType string, keys []string) (shim.StateQueryIteratorInterface, error)
}
type ChaincodeParameter struct {
......
......@@ -6,4 +6,5 @@ type ValidObject interface {
CheckBeforeAdd(manager IManager) error
CheckBeforeUpdate(updatedObject ValidObject, manager IManager) error
New() ValidObject
GetObjectType() string
}
......@@ -18,4 +18,6 @@ const (
AddKeyValueArrayFunction = "addKeyValueArray"
GetKeyValueArrayFunction = "getKeyValueArray"
GetKeyValueArrayByPartOfKeyFunction = "getKeyValueArrayByPartOfKey"
)
\ No newline at end of file
......@@ -17,6 +17,10 @@ type Account struct {
CurrencyType string `json:"currency_type,omitempty"`
}
func (this Account) GetObjectType() string {
return "Account";
}
func (this Account) ToUserAccountInfo() *UserAccountInfo {
return &UserAccountInfo{
UserID: this.UserID,
......
......@@ -15,6 +15,10 @@ type Wallet struct {
Status AccountStatus `json:"status,omitempty"`
}
func (this Wallet) GetObjectType() string {
return "Wallet";
}
func (this Wallet) CheckBeforeAdd(manager chaincode.IManager) error {
// TODO check user exists
// TODO when add check AccountStatus_ACTIVE : wallet.Status = model.AccountStatus_ACTIVE
......
......@@ -18,6 +18,10 @@ type Commission struct {
CommissionPercent float64 `json:"commission_percent,omitempty"`
}
func (this Commission) GetObjectType() string {
return "Commission";
}
func (this Commission) New() chaincode.ValidObject {
return &Commission{}
}
......
......@@ -19,6 +19,10 @@ type CryptocurrencyStore struct {
Amount nums.PlainCurrencyNumber `json:"amount"`
}
func (this CryptocurrencyStore) GetObjectType() string {
return "CryptocurrencyStore";
}
func (this CryptocurrencyStore) New() chaincode.ValidObject {
return &CryptocurrencyStore{}
}
......
......@@ -12,6 +12,10 @@ type MarketTransaction struct {
*transactionCC.TransactionRecord
}
func (this MarketTransaction) GetObjectType() string {
return "MarketTransaction";
}
func (this MarketTransaction) New() chaincode.ValidObject {
return &MarketTransaction{}
}
......
......@@ -18,6 +18,10 @@ type OfferRequest struct {
Properties chaincode.Fields `json:"properties,omitempty"`
}
func (this OfferRequest) GetObjectType() string {
return "OfferRequest";
}
const ExternalTransactionID = "external_transaction_id"
const ExternalAccountID = "external_account_id"
const ReplayedID = "replayed_id"
......
......@@ -16,6 +16,10 @@ type Organization struct {
Website string `json:"website,omitempty"`
}
func (this Organization) GetObjectType() string {
return "Organization";
}
func (this Organization) CheckBeforeAdd(manager chaincode.IManager) error {
return nil
}
......
......@@ -12,6 +12,10 @@ type Preference struct {
Fields chaincode.Fields `json:"fields,omitempty"`
}
func (this Preference) GetObjectType() string {
return "Preference";
}
func (this Preference) CheckBeforeAdd(manager chaincode.IManager) error {
return nil
}
......
......@@ -14,12 +14,22 @@ type ArrayId struct {
ArrayId []Id `json:"array_id,omitempty"`
}
type PartKey struct {
Keys []string `json:"keys,omitempty"`
}
func UnmarshalArrayId(data []byte) (*ArrayId, error) {
var obj ArrayId
err := json.Unmarshal(data, &obj)
return &obj, err;
}
func UnmarshalPartKey(data []byte) (*PartKey, error) {
var obj PartKey
err := json.Unmarshal(data, &obj)
return &obj, err;
}
func (this Id) Validate() error {
if len(this.Id) == 0 {
return errors.New("Id is empty")
......
......@@ -31,6 +31,10 @@ type TransactionRecord struct {
InOutType InputOutputType `json:"input_output_type"`
}
func (this TransactionRecord) GetObjectType() string {
return "TransactionRecord";
}
func (this TransactionRecord) CheckBeforeAdd(manager chaincode.IManager) error {
// TODO добавить проверку в регуляторном канале передав this
// проверка статических переменных
......
......@@ -37,6 +37,10 @@ type User struct {
Roles []actions.Roles `json:"roles"` // TODO уйдет в link_user
}
func (this User) GetObjectType() string {
return "User";
}
func (this User) CheckBeforeAdd(manager chaincode.IManager) error {
err := VerifyPublicKey(manager, this)
if err != nil {
......
......@@ -28,6 +28,10 @@ func (this UserPreference) CheckBeforeAdd(manager chaincode.IManager) error {
return nil
}
func (this UserPreference) GetObjectType() string {
return "UserPreference";
}
func (this UserPreference) CheckBeforeUpdate(updatedObject chaincode.ValidObject, manager chaincode.IManager) error {
return this.CheckBeforeAdd(manager)
}
......
......@@ -22,6 +22,9 @@ var btg, _ = NewAsset("BTG", 8, "21000000", "0.00000001")
var doge, _ = NewAsset("DOGE", 8, "21000000", "0.00000001")
var emc2, _ = NewAsset("EMC2", 8, "21000000", "0.00000001")
var rub, _ = NewAsset("RUB", 2, "2777530283277760", "0.01")
var usd, _ = NewAsset("USD", 2, "2777530283277760", "0.01")
// TODO use channel for this
func CurrencyTypeFrom(currencyType string) (*Asset, error) {
if len(currencyType) == 0 {
......@@ -67,6 +70,10 @@ func CurrencyTypeFrom(currencyType string) (*Asset, error) {
return doge, nil
case "EMC2":
return emc2, nil
case "RUB":
return rub, nil
case "USD":
return usd, nil
default:
return nil, logging.Get().Errorf("failed. Bad currencyType %s", currencyType)
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment