Skip to content

Commit

Permalink
Merge pull request #510 from anandrgitnirman/gettoken
Browse files Browse the repository at this point in the history
#509 : Bug fix on key generation
  • Loading branch information
anandrgitnirman authored Aug 10, 2020
2 parents 4174a74 + 1d7d701 commit ee0dbc2
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 73 deletions.
73 changes: 24 additions & 49 deletions escrow/atomic_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ type TypedKeyValueData struct {
type TypedAtomicStorageImpl struct {
atomicStorage AtomicStorage
keySerializer func(key interface{}) (serialized string, err error)
keyDeserializer func(serialized string, key interface{}) (err error)
keyType reflect.Type
valueSerializer func(value interface{}) (serialized string, err error)
valueDeserializer func(serialized string, value interface{}) (err error)
Expand Down Expand Up @@ -237,15 +236,6 @@ func (storage *TypedAtomicStorageImpl) Get(key interface{}) (value interface{},
return value, true, nil
}

func (storage *TypedAtomicStorageImpl) deserializeKey(keyString string) (key interface{}, err error) {
key = reflect.New(storage.keyType).Interface()
err = storage.keyDeserializer(keyString, key)
if err != nil {
return nil, err
}
return key, err
}

func (storage *TypedAtomicStorageImpl) deserializeValue(valueString string) (value interface{}, err error) {
value = reflect.New(storage.valueType).Interface()
err = storage.valueDeserializer(valueString, value)
Expand Down Expand Up @@ -336,29 +326,23 @@ func (storage *TypedAtomicStorageImpl) Delete(key interface{}) (err error) {
return storage.atomicStorage.Delete(keyString)
}

type typedTransactionImpl struct {
transactionString Transaction
storage *TypedAtomicStorageImpl
}

func (transaction *typedTransactionImpl) GetConditionValues() ([]TypedKeyValueData, error) {
keyValueDataString, err := transaction.transactionString.GetConditionValues()
if err != nil {
return nil, err
}
return transaction.storage.convertKeyValueDataToTyped(keyValueDataString)
}

func (storage *TypedAtomicStorageImpl) convertKeyValueDataToTyped(keyValueData []KeyValueData) (result []TypedKeyValueData, err error) {
result = make([]TypedKeyValueData, len(keyValueData))
for i, keyValueString := range keyValueData {
result[i] = TypedKeyValueData{
Present: keyValueString.Present,
}
result[i].Key, err = storage.deserializeKey(keyValueString.Key)
func (storage *TypedAtomicStorageImpl) convertKeyValueDataToTyped(conditionKeys []interface{}, keyValueData []KeyValueData) (result []TypedKeyValueData, err error) {
result = make([]TypedKeyValueData, len(conditionKeys))
for i, conditionKey := range conditionKeys {
conditionKeyString, err := storage.keySerializer(conditionKey)
if err != nil {
return nil, err
}
result[i] = TypedKeyValueData{
Key: conditionKey,
Present: false,
}
keyValueString, ok := findKeyValueByKey(keyValueData, conditionKeyString)
if ok {
result[i].Present = keyValueString.Present
} else {
result[i].Present = false
}
if !keyValueString.Present {
continue
}
Expand All @@ -370,10 +354,19 @@ func (storage *TypedAtomicStorageImpl) convertKeyValueDataToTyped(keyValueData [
return result, nil
}

func findKeyValueByKey(keyValueData []KeyValueData, key string) (keyValueString *KeyValueData, ok bool) {
for _, keyValueString := range keyValueData {
if keyValueString.Key == key {
return &keyValueString, true
}
}
return nil, false
}

func (storage *TypedAtomicStorageImpl) ExecuteTransaction(request TypedCASRequest) (ok bool, err error) {

updateFunction := func(conditionValues []KeyValueData) (update []KeyValueData, ok bool, err error) {
typedValues, err := storage.convertKeyValueDataToTyped(conditionValues)
typedValues, err := storage.convertKeyValueDataToTyped(request.ConditionKeys, conditionValues)
if err != nil {
return nil, false, err
}
Expand Down Expand Up @@ -406,17 +399,6 @@ func (storage *TypedAtomicStorageImpl) convertTypedKeyToString(typedKeys []inter
}
return stringKeys, nil
}
func (storage *TypedAtomicStorageImpl) StartTransaction(conditionKeys []string) (transaction TypedTransaction, err error) {
transactionString, err := storage.atomicStorage.StartTransaction(conditionKeys)
if err != nil {
return
}

return &typedTransactionImpl{
transactionString: transactionString,
storage: storage,
}, nil
}

func (storage *TypedAtomicStorageImpl) convertTypedKeyValueDataToString(
update []TypedKeyValueData) (data []KeyValueData, err error) {
Expand All @@ -442,10 +424,3 @@ func (storage *TypedAtomicStorageImpl) convertTypedKeyValueDataToString(
}
return updateString, nil
}
func (storage *TypedAtomicStorageImpl) CompleteTransaction(transaction TypedTransaction, update []TypedKeyValueData) (ok bool, err error) {
updateString, err := storage.convertTypedKeyValueDataToString(update)
if err != nil {
return false, err
}
return storage.atomicStorage.CompleteTransaction(transaction.(*typedTransactionImpl).transactionString, updateString)
}
7 changes: 5 additions & 2 deletions escrow/free_call_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ func NewFreeCallUserStorage(atomicStorage AtomicStorage) *FreeCallUserStorage {
delegate: atomicStorage,
keyPrefix: "/free-call-user/storage",
},
keySerializer: serialize,
keyDeserializer: deserialize,
keySerializer: serializeFreeCallKey,
keyType: reflect.TypeOf(FreeCallUserKey{}),
valueSerializer: serialize,
valueDeserializer: deserialize,
Expand All @@ -25,6 +24,10 @@ func NewFreeCallUserStorage(atomicStorage AtomicStorage) *FreeCallUserStorage {
}
}

func serializeFreeCallKey(key interface{}) (serialized string, err error) {
myKey := key.(*FreeCallUserKey)
return myKey.String(), nil
}
func (storage *FreeCallUserStorage) Get(key *FreeCallUserKey) (state *FreeCallUserData, ok bool, err error) {
value, ok, err := storage.delegate.Get(key)
if err != nil || !ok {
Expand Down
6 changes: 4 additions & 2 deletions escrow/payment_channel_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ func NewPaymentChannelStorage(atomicStorage AtomicStorage) *PaymentChannelStorag
return &PaymentChannelStorage{
delegate: &TypedAtomicStorageImpl{
atomicStorage: NewPrefixedAtomicStorage(atomicStorage, "/payment-channel/storage"),
keySerializer: serialize,
keyDeserializer: deserialize,
keySerializer: serializeKey,
keyType: reflect.TypeOf(PaymentChannelKey{}),
valueSerializer: serialize,
valueDeserializer: deserialize,
Expand All @@ -35,6 +34,9 @@ func NewPaymentChannelStorage(atomicStorage AtomicStorage) *PaymentChannelStorag
}
}

func serializeKey(key interface{}) (slice string, err error) {
return fmt.Sprintf("%v", key), nil
}
func serialize(value interface{}) (slice string, err error) {
var b bytes.Buffer
e := gob.NewEncoder(&b)
Expand Down
3 changes: 1 addition & 2 deletions escrow/payment_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ func NewPaymentStorage(atomicStorage AtomicStorage) *PaymentStorage {
delegate: &TypedAtomicStorageImpl{

atomicStorage: NewPrefixedAtomicStorage(atomicStorage, "/payment/storage"),
keySerializer: serialize,
keyDeserializer: deserialize,
keySerializer: serializeKey,
keyType: reflect.TypeOf(""),
valueSerializer: serialize,
valueDeserializer: deserialize,
Expand Down
12 changes: 6 additions & 6 deletions escrow/prepaid_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func convertTypedDataToPrePaidUsage(data []TypedKeyValueData) (new *PrePaidUsage
usageData := &PrePaidUsageData{PlannedAmount: big.NewInt(0),
UsedAmount: big.NewInt(0), RefundAmount: big.NewInt(0)}
for _, usageType := range data {
key := usageType.Key.(*PrePaidDataKey)
key := usageType.Key.(PrePaidDataKey)
usageData.ChannelID = key.ChannelID
if !usageType.Present {
continue
Expand All @@ -112,7 +112,7 @@ func convertTypedDataToPrePaidUsage(data []TypedKeyValueData) (new *PrePaidUsage

func BuildOldAndNewValuesForCAS(data *PrePaidUsageData) (newValues []TypedKeyValueData, err error) {
updateUsageData := &PrePaidData{}
updateUsageKey := &PrePaidDataKey{ChannelID: data.ChannelID, UsageType: data.UpdateUsageType}
updateUsageKey := PrePaidDataKey{ChannelID: data.ChannelID, UsageType: data.UpdateUsageType}
if amt, err := data.GetAmountForUsageType(); err != nil {
return nil, err
} else {
Expand All @@ -133,7 +133,7 @@ var (
}
oldState.ChannelID = channelId
newState := oldState.Clone()
usageKey := &PrePaidDataKey{UsageType: USED_AMOUNT, ChannelID: oldState.ChannelID}
usageKey := PrePaidDataKey{UsageType: USED_AMOUNT, ChannelID: oldState.ChannelID}
updateDetails(newState, usageKey, revisedAmount)
if newState.UsedAmount.Cmp(oldState.PlannedAmount.Add(oldState.PlannedAmount, oldState.RefundAmount)) > 0 {
return nil, fmt.Errorf("Usage Exceeded on channel Id %v", oldState.ChannelID)
Expand All @@ -151,7 +151,7 @@ var (
//function and pick it from there
oldState.ChannelID = channelId
newState := oldState.Clone()
usageKey := &PrePaidDataKey{UsageType: PLANNED_AMOUNT, ChannelID: oldState.ChannelID}
usageKey := PrePaidDataKey{UsageType: PLANNED_AMOUNT, ChannelID: oldState.ChannelID}
updateDetails(newState, usageKey, revisedAmount)
return BuildOldAndNewValuesForCAS(newState)

Expand All @@ -163,14 +163,14 @@ var (
return nil, err
}
newState.ChannelID = channelId
usageKey := &PrePaidDataKey{UsageType: REFUND_AMOUNT, ChannelID: newState.ChannelID}
usageKey := PrePaidDataKey{UsageType: REFUND_AMOUNT, ChannelID: newState.ChannelID}
updateDetails(newState, usageKey, revisedAmount)
return BuildOldAndNewValuesForCAS(newState)

}
)

func updateDetails(usageData *PrePaidUsageData, key *PrePaidDataKey, usage *big.Int) {
func updateDetails(usageData *PrePaidUsageData, key PrePaidDataKey, usage *big.Int) {
usageData.ChannelID = key.ChannelID
usageData.UpdateUsageType = key.UsageType
switch key.UsageType {
Expand Down
26 changes: 17 additions & 9 deletions escrow/prepaid_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ func Test_getAllKeys(t *testing.T) {
func Test_convertTypedDataToPrePaidUsage(t *testing.T) {
typedArray := make([]TypedKeyValueData, 3)
typedArray[0] = TypedKeyValueData{
Key: &PrePaidDataKey{ChannelID: big.NewInt(10), UsageType: USED_AMOUNT},
Key: PrePaidDataKey{ChannelID: big.NewInt(10), UsageType: USED_AMOUNT},
Value: &PrePaidData{Amount: big.NewInt(3)},
Present: true,
}
typedArray[1] = TypedKeyValueData{
Key: &PrePaidDataKey{ChannelID: big.NewInt(10), UsageType: PLANNED_AMOUNT},
Key: PrePaidDataKey{ChannelID: big.NewInt(10), UsageType: PLANNED_AMOUNT},
Value: &PrePaidData{Amount: big.NewInt(10)},
Present: true,
}

typedArray[2] = TypedKeyValueData{
Key: &PrePaidDataKey{ChannelID: big.NewInt(10), UsageType: REFUND_AMOUNT},
Key: PrePaidDataKey{ChannelID: big.NewInt(10), UsageType: REFUND_AMOUNT},
Value: &PrePaidData{Amount: big.NewInt(4)},
Present: true,
}
Expand All @@ -53,7 +53,7 @@ func Test_convertTypedDataToPrePaidUsage(t *testing.T) {
assert.Equal(t, newState.RefundAmount, big.NewInt(4))

typedArray[0] = TypedKeyValueData{
Key: &PrePaidDataKey{ChannelID: big.NewInt(10), UsageType: "BAD"},
Key: PrePaidDataKey{ChannelID: big.NewInt(10), UsageType: "BAD"},
Value: &PrePaidData{Amount: big.NewInt(3)},
Present: true,
}
Expand All @@ -78,21 +78,29 @@ func TestBuildOldAndNewValuesForCAS(t *testing.T) {
func Test_updateDetails(t *testing.T) {
usage := &PrePaidUsageData{PlannedAmount: big.NewInt(10),
UpdateUsageType: PLANNED_AMOUNT}
updateDetails(usage, &PrePaidDataKey{ChannelID: big.NewInt(11), UsageType: PLANNED_AMOUNT}, big.NewInt(10))
updateDetails(usage, PrePaidDataKey{ChannelID: big.NewInt(11), UsageType: PLANNED_AMOUNT}, big.NewInt(10))
assert.Equal(t, usage.PlannedAmount, big.NewInt(20))

}

func Test_KeySerializeAndDeserialize(t *testing.T) {
key := PrePaidDataKey{ChannelID: big.NewInt(11), UsageType: PLANNED_AMOUNT}
newkey, err := serializePrePaidKey(key)
assert.Equal(t, "{ID:11/P}", newkey)
assert.Nil(t, err)

}

func TestFuncUsedAmount(t *testing.T) {
channelId := big.NewInt(10)
typedArray := make([]TypedKeyValueData, 2)
typedArray[0] = TypedKeyValueData{
Key: &PrePaidDataKey{ChannelID: channelId, UsageType: USED_AMOUNT},
Key: PrePaidDataKey{ChannelID: channelId, UsageType: USED_AMOUNT},
Value: &PrePaidData{Amount: big.NewInt(3)},
Present: true,
}
typedArray[1] = TypedKeyValueData{
Key: &PrePaidDataKey{ChannelID: channelId, UsageType: PLANNED_AMOUNT},
Key: PrePaidDataKey{ChannelID: channelId, UsageType: PLANNED_AMOUNT},
Value: &PrePaidData{Amount: big.NewInt(30)},
Present: true,
}
Expand All @@ -108,7 +116,7 @@ func TestFuncPlannedAmount(t *testing.T) {
channelId := big.NewInt(10)
typedArray := make([]TypedKeyValueData, 1)
typedArray[0] = TypedKeyValueData{
Key: &PrePaidDataKey{ChannelID: channelId, UsageType: PLANNED_AMOUNT},
Key: PrePaidDataKey{ChannelID: channelId, UsageType: PLANNED_AMOUNT},
Value: &PrePaidData{Amount: big.NewInt(3)},
Present: true,
}
Expand All @@ -122,7 +130,7 @@ func TestFuncRefundAmount(t *testing.T) {
channelId := big.NewInt(10)
typedArray := make([]TypedKeyValueData, 1)
typedArray[0] = TypedKeyValueData{
Key: &PrePaidDataKey{ChannelID: channelId, UsageType: REFUND_AMOUNT},
Key: PrePaidDataKey{ChannelID: channelId, UsageType: REFUND_AMOUNT},
Value: &PrePaidData{Amount: big.NewInt(3)},
Present: true,
}
Expand Down
10 changes: 7 additions & 3 deletions escrow/prepaid_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (data *PrePaidData) String() string {
return fmt.Sprintf("{Amount:%v}", data.Amount)
}
func (key *PrePaidDataKey) String() string {
return fmt.Sprintf("{ChannelID:%v,UsageType:%v}", key.ChannelID, key.UsageType)
return fmt.Sprintf("{ID:%v/%v}", key.ChannelID, key.UsageType)
}

const (
Expand Down Expand Up @@ -79,12 +79,16 @@ func (data PrePaidUsageData) Clone() *PrePaidUsageData {
}
}

func serializePrePaidKey(key interface{}) (serialized string, err error) {
myKey := key.(PrePaidDataKey)
return myKey.String(), nil
}

// NewPrepaidStorage returns new instance of TypedAtomicStorage
func NewPrepaidStorage(atomicStorage AtomicStorage) TypedAtomicStorage {
return &TypedAtomicStorageImpl{
atomicStorage: NewPrefixedAtomicStorage(atomicStorage, "/PrePaid/storage"),
keySerializer: serialize,
keyDeserializer: deserialize,
keySerializer: serializePrePaidKey,
keyType: reflect.TypeOf(PrePaidDataKey{}),
valueSerializer: serialize,
valueDeserializer: deserialize,
Expand Down

0 comments on commit ee0dbc2

Please sign in to comment.