diff --git a/common/db.go b/common/db.go index e90fd47..5532dc9 100644 --- a/common/db.go +++ b/common/db.go @@ -32,7 +32,19 @@ const ( DATA_PUBLIC_KEY_REVERSE DATA_ACTIVE_DECIMAL DATA_ACTIVE_ADDRESS - DATA_TRANSACTIONS + // Start the sub prefix data at 256 (0x0100) + DATA_VOUCHER_SYMBOLS DataTyp = 256 + iota + DATA_VOUCHER_BALANCES + DATA_VOUCHER_DECIMALS + DATA_VOUCHER_ADDRESSES + DATA_TX_SENDERS + DATA_TX_RECIPIENTS + DATA_TX_VALUES + DATA_TX_ADDRESSES + DATA_TX_HASHES + DATA_TX_DATES + DATA_TX_SYMBOLS + DATA_TX_DECIMALS ) var ( @@ -69,3 +81,10 @@ func StringToDataTyp(str string) (DataTyp, error) { return 0, errors.New("invalid DataTyp string") } } + +// ToBytes converts DataTyp or int to a byte slice +func ToBytes[T ~uint16 | int](value T) []byte { + bytes := make([]byte, 2) + binary.BigEndian.PutUint16(bytes, uint16(value)) + return bytes +} diff --git a/common/transfer_statements.go b/common/transfer_statements.go index 4dd409c..4e6f66b 100644 --- a/common/transfer_statements.go +++ b/common/transfer_statements.go @@ -57,25 +57,25 @@ func ProcessTransfers(transfers []dataserviceapi.Last10TxResponse) TransferMetad // GetTransferData retrieves and matches transfer data // returns a formatted string of the full transaction/statement func GetTransferData(ctx context.Context, db storage.PrefixDb, publicKey string, index int) (string, error) { - keys := []string{"txfrom", "txto", "txval", "txaddr", "txhash", "txdate", "txsym"} - data := make(map[string]string) + keys := []DataTyp{DATA_TX_SENDERS, DATA_TX_RECIPIENTS, DATA_TX_VALUES, DATA_TX_ADDRESSES, DATA_TX_HASHES, DATA_TX_DATES, DATA_TX_SYMBOLS} + data := make(map[DataTyp]string) for _, key := range keys { - value, err := db.Get(ctx, []byte(key)) + value, err := db.Get(ctx, ToBytes(key)) if err != nil { - return "", fmt.Errorf("failed to get %s: %v", key, err) + return "", fmt.Errorf("failed to get %s: %v", ToBytes(key), err) } data[key] = string(value) } // Split the data - senders := strings.Split(string(data["txfrom"]), "\n") - recipients := strings.Split(string(data["txto"]), "\n") - values := strings.Split(string(data["txval"]), "\n") - addresses := strings.Split(string(data["txaddr"]), "\n") - hashes := strings.Split(string(data["txhash"]), "\n") - dates := strings.Split(string(data["txdate"]), "\n") - syms := strings.Split(string(data["txsym"]), "\n") + senders := strings.Split(string(data[DATA_TX_SENDERS]), "\n") + recipients := strings.Split(string(data[DATA_TX_RECIPIENTS]), "\n") + values := strings.Split(string(data[DATA_TX_VALUES]), "\n") + addresses := strings.Split(string(data[DATA_TX_ADDRESSES]), "\n") + hashes := strings.Split(string(data[DATA_TX_HASHES]), "\n") + dates := strings.Split(string(data[DATA_TX_DATES]), "\n") + syms := strings.Split(string(data[DATA_TX_SYMBOLS]), "\n") // Check if index is within range if index < 1 || index > len(senders) { diff --git a/common/vouchers.go b/common/vouchers.go index c542084..6cff91d 100644 --- a/common/vouchers.go +++ b/common/vouchers.go @@ -64,22 +64,23 @@ func ScaleDownBalance(balance, decimals string) string { // GetVoucherData retrieves and matches voucher data func GetVoucherData(ctx context.Context, db storage.PrefixDb, input string) (*dataserviceapi.TokenHoldings, error) { - keys := []string{"sym", "bal", "deci", "addr"} - data := make(map[string]string) + keys := []DataTyp{DATA_VOUCHER_SYMBOLS, DATA_VOUCHER_BALANCES, DATA_VOUCHER_DECIMALS, DATA_VOUCHER_ADDRESSES} + data := make(map[DataTyp]string) for _, key := range keys { - value, err := db.Get(ctx, []byte(key)) + value, err := db.Get(ctx, ToBytes(key)) if err != nil { - return nil, fmt.Errorf("failed to get %s: %v", key, err) + return nil, fmt.Errorf("failed to get %s: %v", ToBytes(key), err) } data[key] = string(value) } symbol, balance, decimal, address := MatchVoucher(input, - data["sym"], - data["bal"], - data["deci"], - data["addr"]) + data[DATA_VOUCHER_SYMBOLS], + data[DATA_VOUCHER_BALANCES], + data[DATA_VOUCHER_DECIMALS], + data[DATA_VOUCHER_ADDRESSES], + ) if symbol == "" { return nil, nil diff --git a/common/vouchers_test.go b/common/vouchers_test.go index 9f5bf85..ba6cd60 100644 --- a/common/vouchers_test.go +++ b/common/vouchers_test.go @@ -8,8 +8,9 @@ import ( "github.com/alecthomas/assert/v2" "github.com/stretchr/testify/require" - "git.grassecon.net/urdt/ussd/internal/storage" + visedb "git.defalsify.org/vise.git/db" memdb "git.defalsify.org/vise.git/db/mem" + "git.grassecon.net/urdt/ussd/internal/storage" dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api" ) @@ -83,19 +84,21 @@ func TestGetVoucherData(t *testing.T) { if err != nil { t.Fatal(err) } - spdb := storage.NewSubPrefixDb(db, []byte("vouchers")) + + prefix := ToBytes(visedb.DATATYPE_USERDATA) + spdb := storage.NewSubPrefixDb(db, prefix) // Test voucher data - mockData := map[string][]byte{ - "sym": []byte("1:SRF\n2:MILO"), - "bal": []byte("1:100\n2:200"), - "deci": []byte("1:6\n2:4"), - "addr": []byte("1:0xd4c288865Ce\n2:0x41c188d63Qa"), + mockData := map[DataTyp][]byte{ + DATA_VOUCHER_SYMBOLS: []byte("1:SRF\n2:MILO"), + DATA_VOUCHER_BALANCES: []byte("1:100\n2:200"), + DATA_VOUCHER_DECIMALS: []byte("1:6\n2:4"), + DATA_VOUCHER_ADDRESSES: []byte("1:0xd4c288865Ce\n2:0x41c188d63Qa"), } // Put the data for key, value := range mockData { - err = spdb.Put(ctx, []byte(key), []byte(value)) + err = spdb.Put(ctx, []byte(ToBytes(key)), []byte(value)) if err != nil { t.Fatal(err) } diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index ba82b47..b47e31f 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -85,8 +85,10 @@ func NewHandlers(appFlags *asm.FlagParser, userdataStore db.Db, adminstore *util userDb := &common.UserDataStore{ Db: userdataStore, } - // Instantiate the SubPrefixDb with "vouchers" prefix - prefixDb := storage.NewSubPrefixDb(userdataStore, []byte("vouchers")) + + // Instantiate the SubPrefixDb with "DATATYPE_USERDATA" prefix + prefix := common.ToBytes(db.DATATYPE_USERDATA) + prefixDb := storage.NewSubPrefixDb(userdataStore, prefix) h := &Handlers{ userdataStore: userDb, @@ -1576,15 +1578,15 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte) data := common.ProcessVouchers(vouchersResp) // Store all voucher data - dataMap := map[string]string{ - "sym": data.Symbols, - "bal": data.Balances, - "deci": data.Decimals, - "addr": data.Addresses, + dataMap := map[common.DataTyp]string{ + common.DATA_VOUCHER_SYMBOLS: data.Symbols, + common.DATA_VOUCHER_BALANCES: data.Balances, + common.DATA_VOUCHER_DECIMALS: data.Decimals, + common.DATA_VOUCHER_ADDRESSES: data.Addresses, } for key, value := range dataMap { - if err := h.prefixDb.Put(ctx, []byte(key), []byte(value)); err != nil { + if err := h.prefixDb.Put(ctx, []byte(common.ToBytes(key)), []byte(value)); err != nil { return res, nil } } @@ -1597,7 +1599,7 @@ func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte) var res resource.Result // Read vouchers from the store - voucherData, err := h.prefixDb.Get(ctx, []byte("sym")) + voucherData, err := h.prefixDb.Get(ctx, common.ToBytes(common.DATA_VOUCHER_SYMBOLS)) if err != nil { logg.ErrorCtxf(ctx, "Failed to read the voucherData from prefixDb", "error", err) return res, err @@ -1743,19 +1745,19 @@ func (h *Handlers) CheckTransactions(ctx context.Context, sym string, input []by data := common.ProcessTransfers(transactionsResp) // Store all transaction data - dataMap := map[string]string{ - "txfrom": data.Senders, - "txto": data.Recipients, - "txval": data.TransferValues, - "txaddr": data.Addresses, - "txhash": data.TxHashes, - "txdate": data.Dates, - "txsym": data.Symbols, - "txdeci": data.Decimals, + dataMap := map[common.DataTyp]string{ + common.DATA_TX_SENDERS: data.Senders, + common.DATA_TX_RECIPIENTS: data.Recipients, + common.DATA_TX_VALUES: data.TransferValues, + common.DATA_TX_ADDRESSES: data.Addresses, + common.DATA_TX_HASHES: data.TxHashes, + common.DATA_TX_DATES: data.Dates, + common.DATA_TX_SYMBOLS: data.Symbols, + common.DATA_TX_DECIMALS: data.Decimals, } for key, value := range dataMap { - if err := h.prefixDb.Put(ctx, []byte(key), []byte(value)); err != nil { + if err := h.prefixDb.Put(ctx, []byte(common.ToBytes(key)), []byte(value)); err != nil { logg.ErrorCtxf(ctx, "failed to write to prefixDb", "error", err) return res, err } @@ -1781,22 +1783,22 @@ func (h *Handlers) GetTransactionsList(ctx context.Context, sym string, input [] } // Read transactions from the store and format them - TransactionSenders, err := h.prefixDb.Get(ctx, []byte("txfrom")) + TransactionSenders, err := h.prefixDb.Get(ctx, common.ToBytes(common.DATA_TX_SENDERS)) if err != nil { logg.ErrorCtxf(ctx, "Failed to read the TransactionSenders from prefixDb", "error", err) return res, err } - TransactionSyms, err := h.prefixDb.Get(ctx, []byte("txsym")) + TransactionSyms, err := h.prefixDb.Get(ctx, common.ToBytes(common.DATA_TX_SYMBOLS)) if err != nil { logg.ErrorCtxf(ctx, "Failed to read the TransactionSyms from prefixDb", "error", err) return res, err } - TransactionValues, err := h.prefixDb.Get(ctx, []byte("txval")) + TransactionValues, err := h.prefixDb.Get(ctx, common.ToBytes(common.DATA_TX_VALUES)) if err != nil { logg.ErrorCtxf(ctx, "Failed to read the TransactionValues from prefixDb", "error", err) return res, err } - TransactionDates, err := h.prefixDb.Get(ctx, []byte("txdate")) + TransactionDates, err := h.prefixDb.Get(ctx, common.ToBytes(common.DATA_TX_DATES)) if err != nil { logg.ErrorCtxf(ctx, "Failed to read the TransactionDates from prefixDb", "error", err) return res, err diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index d7cd855..fac95a1 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -22,6 +22,7 @@ import ( testdataloader "github.com/peteole/testdata-loader" "github.com/stretchr/testify/require" + visedb "git.defalsify.org/vise.git/db" memdb "git.defalsify.org/vise.git/db/mem" dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api" ) @@ -56,7 +57,8 @@ func InitializeTestSubPrefixDb(t *testing.T, ctx context.Context) *storage.SubPr if err != nil { t.Fatal(err) } - spdb := storage.NewSubPrefixDb(db, []byte("vouchers")) + prefix := common.ToBytes(visedb.DATATYPE_USERDATA) + spdb := storage.NewSubPrefixDb(db, prefix) return spdb } @@ -1937,7 +1939,7 @@ func TestCheckVouchers(t *testing.T) { assert.NoError(t, err) // Read voucher sym data from the store - voucherData, err := spdb.Get(ctx, []byte("sym")) + voucherData, err := spdb.Get(ctx, common.ToBytes(common.DATA_VOUCHER_SYMBOLS)) if err != nil { t.Fatal(err) } @@ -1961,7 +1963,7 @@ func TestGetVoucherList(t *testing.T) { expectedSym := []byte("1:SRF\n2:MILO") // Put voucher sym data from the store - err := spdb.Put(ctx, []byte("sym"), expectedSym) + err := spdb.Put(ctx, common.ToBytes(common.DATA_VOUCHER_SYMBOLS), expectedSym) if err != nil { t.Fatal(err) } @@ -1991,16 +1993,16 @@ func TestViewVoucher(t *testing.T) { } // Define mock voucher data - mockData := map[string][]byte{ - "sym": []byte("1:SRF\n2:MILO"), - "bal": []byte("1:100\n2:200"), - "deci": []byte("1:6\n2:4"), - "addr": []byte("1:0xd4c288865Ce\n2:0x41c188d63Qa"), + mockData := map[common.DataTyp][]byte{ + common.DATA_VOUCHER_SYMBOLS: []byte("1:SRF\n2:MILO"), + common.DATA_VOUCHER_BALANCES: []byte("1:100\n2:200"), + common.DATA_VOUCHER_DECIMALS: []byte("1:6\n2:4"), + common.DATA_VOUCHER_ADDRESSES: []byte("1:0xd4c288865Ce\n2:0x41c188d63Qa"), } // Put the data for key, value := range mockData { - err = spdb.Put(ctx, []byte(key), []byte(value)) + err = spdb.Put(ctx, []byte(common.ToBytes(key)), []byte(value)) if err != nil { t.Fatal(err) } diff --git a/internal/storage/db.go b/internal/storage/sub_prefix_db.go similarity index 88% rename from internal/storage/db.go rename to internal/storage/sub_prefix_db.go index 8c9ff35..457af78 100644 --- a/internal/storage/db.go +++ b/internal/storage/sub_prefix_db.go @@ -6,10 +6,6 @@ import ( "git.defalsify.org/vise.git/db" ) -const ( - DATATYPE_USERSUB = 64 -) - // PrefixDb interface abstracts the database operations. type PrefixDb interface { Get(ctx context.Context, key []byte) ([]byte, error) @@ -35,13 +31,13 @@ func (s *SubPrefixDb) toKey(k []byte) []byte { } func (s *SubPrefixDb) Get(ctx context.Context, key []byte) ([]byte, error) { - s.store.SetPrefix(DATATYPE_USERSUB) + s.store.SetPrefix(db.DATATYPE_USERDATA) key = s.toKey(key) return s.store.Get(ctx, key) } func (s *SubPrefixDb) Put(ctx context.Context, key []byte, val []byte) error { - s.store.SetPrefix(DATATYPE_USERSUB) + s.store.SetPrefix(db.DATATYPE_USERDATA) key = s.toKey(key) return s.store.Put(ctx, key, val) } diff --git a/internal/storage/db_test.go b/internal/storage/sub_prefix_db_test.go similarity index 100% rename from internal/storage/db_test.go rename to internal/storage/sub_prefix_db_test.go