Consolidate temp data storage #150

Merged
lash merged 5 commits from consolidate-temp-data-storage into master 2024-10-31 23:47:05 +01:00
5 changed files with 34 additions and 66 deletions
Showing only changes of commit f4ca4454ea - Show all commits

View File

@ -222,7 +222,7 @@ func (h *Handlers) VerifyNewPin(ctx context.Context, sym string, input []byte) (
return res, nil return res, nil
} }
// SaveTemporaryPin saves the valid PIN input to the DATA_TEMPORARY_PIN // SaveTemporaryPin saves the valid PIN input to the DATA_TEMPORARY_VALUE
// during the account creation process // during the account creation process
// and during the change PIN process // and during the change PIN process
func (h *Handlers) SaveTemporaryPin(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) SaveTemporaryPin(ctx context.Context, sym string, input []byte) (resource.Result, error) {
@ -247,7 +247,7 @@ func (h *Handlers) SaveTemporaryPin(ctx context.Context, sym string, input []byt
res.FlagReset = append(res.FlagReset, flag_incorrect_pin) res.FlagReset = append(res.FlagReset, flag_incorrect_pin)
store := h.userdataStore store := h.userdataStore
err = store.WriteEntry(ctx, sessionId, utils.DATA_TEMPORARY_PIN, []byte(accountPIN)) err = store.WriteEntry(ctx, sessionId, utils.DATA_TEMPORARY_VALUE, []byte(accountPIN))
if err != nil { if err != nil {
return res, err return res, err
} }
@ -264,7 +264,7 @@ func (h *Handlers) ConfirmPinChange(ctx context.Context, sym string, input []byt
flag_pin_mismatch, _ := h.flagManager.GetFlag("flag_pin_mismatch") flag_pin_mismatch, _ := h.flagManager.GetFlag("flag_pin_mismatch")
store := h.userdataStore store := h.userdataStore
temporaryPin, err := store.ReadEntry(ctx, sessionId, utils.DATA_TEMPORARY_PIN) temporaryPin, err := store.ReadEntry(ctx, sessionId, utils.DATA_TEMPORARY_VALUE)
if err != nil { if err != nil {
return res, err return res, err
} }
@ -295,7 +295,7 @@ func (h *Handlers) VerifyCreatePin(ctx context.Context, sym string, input []byte
return res, fmt.Errorf("missing session") return res, fmt.Errorf("missing session")
} }
store := h.userdataStore store := h.userdataStore
temporaryPin, err := store.ReadEntry(ctx, sessionId, utils.DATA_TEMPORARY_PIN) temporaryPin, err := store.ReadEntry(ctx, sessionId, utils.DATA_TEMPORARY_VALUE)
if err != nil { if err != nil {
return res, err return res, err
} }

View File

@ -19,8 +19,8 @@ import (
"git.grassecon.net/urdt/ussd/internal/testutil/mocks" "git.grassecon.net/urdt/ussd/internal/testutil/mocks"
"git.grassecon.net/urdt/ussd/internal/testutil/testservice" "git.grassecon.net/urdt/ussd/internal/testutil/testservice"
"git.grassecon.net/urdt/ussd/internal/utils"
"git.grassecon.net/urdt/ussd/common" "git.grassecon.net/urdt/ussd/common"
"git.grassecon.net/urdt/ussd/internal/utils"
"github.com/alecthomas/assert/v2" "github.com/alecthomas/assert/v2"
"github.com/grassrootseconomics/eth-custodial/pkg/api" "github.com/grassrootseconomics/eth-custodial/pkg/api"
testdataloader "github.com/peteole/testdata-loader" testdataloader "github.com/peteole/testdata-loader"
@ -271,7 +271,7 @@ func TestSaveTemporaryPin(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
// Set up the expected behavior of the mock // Set up the expected behavior of the mock
mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_TEMPORARY_PIN, []byte(tt.input)).Return(nil) mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_TEMPORARY_VALUE, []byte(tt.input)).Return(nil)
// Call the method // Call the method
res, err := h.SaveTemporaryPin(ctx, "save_pin", tt.input) res, err := h.SaveTemporaryPin(ctx, "save_pin", tt.input)
@ -1013,7 +1013,7 @@ func TestVerifyCreatePin(t *testing.T) {
}, },
} }
typ := utils.DATA_TEMPORARY_PIN typ := utils.DATA_TEMPORARY_VALUE
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@ -1806,7 +1806,7 @@ func TestConfirmPin(t *testing.T) {
// Set up the expected behavior of the mock // Set up the expected behavior of the mock
mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_ACCOUNT_PIN, []byte(tt.temporarypin)).Return(nil) mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_ACCOUNT_PIN, []byte(tt.temporarypin)).Return(nil)
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TEMPORARY_PIN).Return(tt.temporarypin, nil) mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TEMPORARY_VALUE).Return(tt.temporarypin, nil)
//Call the function under test //Call the function under test
res, _ := h.ConfirmPinChange(ctx, "confirm_pin_change", tt.temporarypin) res, _ := h.ConfirmPinChange(ctx, "confirm_pin_change", tt.temporarypin)
@ -2078,16 +2078,9 @@ func TestViewVoucher(t *testing.T) {
} }
// Set up expectations for mockDataStore // Set up expectations for mockDataStore
expectedData := map[utils.DataTyp]string{ expectedData := fmt.Sprintf("%s,%s,%s,%s", "SRF", "100", "6", "0xd4c288865Ce")
utils.DATA_TEMPORARY_SYM: "SRF",
utils.DATA_TEMPORARY_BAL: "100",
utils.DATA_TEMPORARY_DECIMAL: "6",
utils.DATA_TEMPORARY_ADDRESS: "0xd4c288865Ce",
}
for dataType, dataValue := range expectedData { mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_TEMPORARY_VALUE, []byte(expectedData)).Return(nil)
mockDataStore.On("WriteEntry", ctx, sessionId, dataType, []byte(dataValue)).Return(nil)
}
res, err := h.ViewVoucher(ctx, "view_voucher", []byte("1")) res, err := h.ViewVoucher(ctx, "view_voucher", []byte("1"))
assert.NoError(t, err) assert.NoError(t, err)
@ -2111,6 +2104,8 @@ func TestSetVoucher(t *testing.T) {
ContractAddress: "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9", ContractAddress: "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9",
} }
expectedData := fmt.Sprintf("%s,%s,%s,%s", "SRF", "200", "6", "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9")
// Define the expected active entries // Define the expected active entries
activeEntries := map[utils.DataTyp][]byte{ activeEntries := map[utils.DataTyp][]byte{
utils.DATA_ACTIVE_SYM: []byte(tempData.TokenSymbol), utils.DATA_ACTIVE_SYM: []byte(tempData.TokenSymbol),
@ -2120,10 +2115,7 @@ func TestSetVoucher(t *testing.T) {
} }
// Mocking ReadEntry calls for temporary data retrieval // Mocking ReadEntry calls for temporary data retrieval
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TEMPORARY_SYM).Return([]byte(tempData.TokenSymbol), nil) mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TEMPORARY_VALUE).Return([]byte(expectedData), nil)
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TEMPORARY_BAL).Return([]byte(tempData.Balance), nil)
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TEMPORARY_DECIMAL).Return([]byte(tempData.TokenDecimals), nil)
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TEMPORARY_ADDRESS).Return([]byte(tempData.ContractAddress), nil)
// Mocking WriteEntry calls for setting active data // Mocking WriteEntry calls for setting active data
for key, value := range activeEntries { for key, value := range activeEntries {

View File

@ -22,16 +22,12 @@ const (
DATA_OFFERINGS DATA_OFFERINGS
DATA_RECIPIENT DATA_RECIPIENT
DATA_AMOUNT DATA_AMOUNT
DATA_TEMPORARY_PIN DATA_TEMPORARY_VALUE
DATA_VOUCHER_LIST DATA_VOUCHER_LIST
DATA_TEMPORARY_SYM
DATA_ACTIVE_SYM DATA_ACTIVE_SYM
DATA_TEMPORARY_BAL
DATA_ACTIVE_BAL DATA_ACTIVE_BAL
DATA_PUBLIC_KEY_REVERSE DATA_PUBLIC_KEY_REVERSE
DATA_TEMPORARY_DECIMAL
DATA_ACTIVE_DECIMAL DATA_ACTIVE_DECIMAL
DATA_TEMPORARY_ADDRESS
DATA_ACTIVE_ADDRESS DATA_ACTIVE_ADDRESS
) )

View File

@ -97,45 +97,30 @@ func MatchVoucher(input, symbols, balances, decimals, addresses string) (symbol,
// StoreTemporaryVoucher saves voucher metadata as temporary entries in the DataStore. // StoreTemporaryVoucher saves voucher metadata as temporary entries in the DataStore.
func StoreTemporaryVoucher(ctx context.Context, store DataStore, sessionId string, data *dataserviceapi.TokenHoldings) error { func StoreTemporaryVoucher(ctx context.Context, store DataStore, sessionId string, data *dataserviceapi.TokenHoldings) error {
entries := map[DataTyp][]byte{ tempData := fmt.Sprintf("%s,%s,%s,%s", data.TokenSymbol, data.Balance, data.TokenDecimals, data.ContractAddress)
DATA_TEMPORARY_SYM: []byte(data.TokenSymbol),
DATA_TEMPORARY_BAL: []byte(data.Balance),
DATA_TEMPORARY_DECIMAL: []byte(data.TokenDecimals),
DATA_TEMPORARY_ADDRESS: []byte(data.ContractAddress),
}
for key, value := range entries { if err := store.WriteEntry(ctx, sessionId, DATA_TEMPORARY_VALUE, []byte(tempData)); err != nil {
if err := store.WriteEntry(ctx, sessionId, key, value); err != nil {
return err return err
} }
}
return nil return nil
} }
// GetTemporaryVoucherData retrieves temporary voucher metadata from the DataStore. // GetTemporaryVoucherData retrieves temporary voucher metadata from the DataStore.
func GetTemporaryVoucherData(ctx context.Context, store DataStore, sessionId string) (*dataserviceapi.TokenHoldings, error) { func GetTemporaryVoucherData(ctx context.Context, store DataStore, sessionId string) (*dataserviceapi.TokenHoldings, error) {
keys := []DataTyp{ temp_data, err := store.ReadEntry(ctx, sessionId, DATA_TEMPORARY_VALUE)
DATA_TEMPORARY_SYM,
DATA_TEMPORARY_BAL,
DATA_TEMPORARY_DECIMAL,
DATA_TEMPORARY_ADDRESS,
}
data := &dataserviceapi.TokenHoldings{}
values := make([][]byte, len(keys))
for i, key := range keys {
value, err := store.ReadEntry(ctx, sessionId, key)
if err != nil { if err != nil {
return nil, err return nil, err
} }
values[i] = value
}
data.TokenSymbol = string(values[0]) values := strings.SplitN(string(temp_data), ",", 4)
data.Balance = string(values[1])
data.TokenDecimals = string(values[2]) data := &dataserviceapi.TokenHoldings{}
data.ContractAddress = string(values[3])
data.TokenSymbol = values[0]
data.Balance = values[1]
data.TokenDecimals = values[2]
data.ContractAddress = values[3]
return data, nil return data, nil
} }

View File

@ -2,6 +2,7 @@ package utils
import ( import (
"context" "context"
"fmt"
"testing" "testing"
"git.grassecon.net/urdt/ussd/internal/storage" "git.grassecon.net/urdt/ussd/internal/storage"
@ -126,18 +127,12 @@ func TestStoreTemporaryVoucher(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
// Verify stored data // Verify stored data
expectedEntries := map[DataTyp][]byte{ expectedData := fmt.Sprintf("%s,%s,%s,%s", "SRF", "200", "6", "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9")
DATA_TEMPORARY_SYM: []byte("SRF"),
DATA_TEMPORARY_BAL: []byte("200"),
DATA_TEMPORARY_DECIMAL: []byte("6"),
DATA_TEMPORARY_ADDRESS: []byte("0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9"),
}
for key, expectedValue := range expectedEntries { storedValue, err := store.ReadEntry(ctx, sessionId, DATA_TEMPORARY_VALUE)
storedValue, err := store.ReadEntry(ctx, sessionId, key)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, expectedValue, storedValue, "Mismatch for key %v", key) require.Equal(t, expectedData, string(storedValue), "Mismatch for key %v", DATA_TEMPORARY_VALUE)
}
} }
func TestGetTemporaryVoucherData(t *testing.T) { func TestGetTemporaryVoucherData(t *testing.T) {