From 4ecfc9de38ac602a632eaab4b95010ea1f72d47d Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 4 Dec 2024 20:38:52 +0300 Subject: [PATCH 01/11] removed DATATYPE_USERSUB and replaced with DATATYPE_USERDATA --- internal/storage/db.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/internal/storage/db.go b/internal/storage/db.go index 8c9ff35..457af78 100644 --- a/internal/storage/db.go +++ b/internal/storage/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) } From 93c44861e04c426be2069bc8993b4b2d4b084692 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 4 Dec 2024 20:42:47 +0300 Subject: [PATCH 02/11] Use numeric prefixes --- common/db.go | 20 ++++++++++++- common/transfer_statements.go | 20 ++++++------- common/vouchers.go | 18 +++++++----- internal/handlers/ussd/menuhandler.go | 42 +++++++++++++-------------- 4 files changed, 60 insertions(+), 40 deletions(-) diff --git a/common/db.go b/common/db.go index e90fd47..1454a84 100644 --- a/common/db.go +++ b/common/db.go @@ -32,7 +32,18 @@ const ( DATA_PUBLIC_KEY_REVERSE DATA_ACTIVE_DECIMAL DATA_ACTIVE_ADDRESS - DATA_TRANSACTIONS + DATA_PREFIX_SYMBOLS + DATA_PREFIX_BALANCES + DATA_PREFIX_DECIMALS + DATA_PREFIX_ADDRESSES + DATA_PREFIX_TX_SENDERS + DATA_PREFIX_TX_RECIPIENTS + DATA_PREFIX_TX_VALUES + DATA_PREFIX_TX_ADDRESSES + DATA_PREFIX_TX_HASHES + DATA_PREFIX_TX_DATES + DATA_PREFIX_TX_SYMBOLS + DATA_PREFIX_TX_DECIMALS ) var ( @@ -69,3 +80,10 @@ func StringToDataTyp(str string) (DataTyp, error) { return 0, errors.New("invalid DataTyp string") } } + +// Convert DataTyp to []byte +func (d DataTyp) ToBytes() []byte { + bytes := make([]byte, 2) + binary.BigEndian.PutUint16(bytes, uint16(d)) + return bytes +} diff --git a/common/transfer_statements.go b/common/transfer_statements.go index 4dd409c..adbdeaf 100644 --- a/common/transfer_statements.go +++ b/common/transfer_statements.go @@ -57,11 +57,11 @@ 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_PREFIX_TX_SENDERS, DATA_PREFIX_TX_RECIPIENTS, DATA_PREFIX_TX_VALUES, DATA_PREFIX_TX_ADDRESSES, DATA_PREFIX_TX_HASHES, DATA_PREFIX_TX_DATES, DATA_PREFIX_TX_SYMBOLS} + data := make(map[DataTyp]string) for _, key := range keys { - value, err := db.Get(ctx, []byte(key)) + value, err := db.Get(ctx, key.ToBytes()) if err != nil { return "", fmt.Errorf("failed to get %s: %v", key, err) } @@ -69,13 +69,13 @@ func GetTransferData(ctx context.Context, db storage.PrefixDb, publicKey string, } // 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_PREFIX_TX_SENDERS]), "\n") + recipients := strings.Split(string(data[DATA_PREFIX_TX_RECIPIENTS]), "\n") + values := strings.Split(string(data[DATA_PREFIX_TX_VALUES]), "\n") + addresses := strings.Split(string(data[DATA_PREFIX_TX_ADDRESSES]), "\n") + hashes := strings.Split(string(data[DATA_PREFIX_TX_HASHES]), "\n") + dates := strings.Split(string(data[DATA_PREFIX_TX_DATES]), "\n") + syms := strings.Split(string(data[DATA_PREFIX_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..5863c46 100644 --- a/common/vouchers.go +++ b/common/vouchers.go @@ -64,22 +64,24 @@ 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_PREFIX_SYMBOLS, DATA_PREFIX_BALANCES, DATA_PREFIX_DECIMALS, DATA_PREFIX_ADDRESSES} + data := make(map[DataTyp]string) for _, key := range keys { - value, err := db.Get(ctx, []byte(key)) + value, err := db.Get(ctx, key.ToBytes()) if err != nil { - return nil, fmt.Errorf("failed to get %s: %v", key, err) + fmt.Printf("failed to get %v: %v\n", key, err) + continue } data[key] = string(value) } symbol, balance, decimal, address := MatchVoucher(input, - data["sym"], - data["bal"], - data["deci"], - data["addr"]) + data[DATA_PREFIX_SYMBOLS], + data[DATA_PREFIX_BALANCES], + data[DATA_PREFIX_DECIMALS], + data[DATA_PREFIX_ADDRESSES], + ) if symbol == "" { return nil, nil diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index a79d054..2d5691f 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -1569,15 +1569,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_PREFIX_SYMBOLS: data.Symbols, + common.DATA_PREFIX_BALANCES: data.Balances, + common.DATA_PREFIX_DECIMALS: data.Decimals, + common.DATA_PREFIX_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(key.ToBytes()), []byte(value)); err != nil { return res, nil } } @@ -1590,7 +1590,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.DATA_PREFIX_SYMBOLS.ToBytes()) if err != nil { logg.ErrorCtxf(ctx, "Failed to read the voucherData from prefixDb", "error", err) return res, err @@ -1732,19 +1732,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_PREFIX_TX_SENDERS: data.Senders, + common.DATA_PREFIX_TX_RECIPIENTS: data.Recipients, + common.DATA_PREFIX_TX_VALUES: data.TransferValues, + common.DATA_PREFIX_TX_ADDRESSES: data.Addresses, + common.DATA_PREFIX_TX_HASHES: data.TxHashes, + common.DATA_PREFIX_TX_DATES: data.Dates, + common.DATA_PREFIX_TX_SYMBOLS: data.Symbols, + common.DATA_PREFIX_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(key.ToBytes()), []byte(value)); err != nil { logg.ErrorCtxf(ctx, "failed to write to prefixDb", "error", err) return res, err } @@ -1770,22 +1770,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.DATA_PREFIX_TX_SENDERS.ToBytes()) 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.DATA_PREFIX_TX_SYMBOLS.ToBytes()) 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.DATA_PREFIX_TX_VALUES.ToBytes()) 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.DATA_PREFIX_TX_DATES.ToBytes()) if err != nil { logg.ErrorCtxf(ctx, "Failed to read the TransactionDates from prefixDb", "error", err) return res, err From bc0e536d3da41c0cac3212bc9816be16c390aa44 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 4 Dec 2024 20:55:03 +0300 Subject: [PATCH 03/11] updated failing tests --- common/transfer_statements.go | 2 +- common/vouchers_test.go | 12 ++++++------ internal/handlers/ussd/menuhandler_test.go | 16 ++++++++-------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/common/transfer_statements.go b/common/transfer_statements.go index adbdeaf..dd8324a 100644 --- a/common/transfer_statements.go +++ b/common/transfer_statements.go @@ -63,7 +63,7 @@ func GetTransferData(ctx context.Context, db storage.PrefixDb, publicKey string, for _, key := range keys { value, err := db.Get(ctx, key.ToBytes()) if err != nil { - return "", fmt.Errorf("failed to get %s: %v", key, err) + return "", fmt.Errorf("failed to get %s: %v", key.ToBytes(), err) } data[key] = string(value) } diff --git a/common/vouchers_test.go b/common/vouchers_test.go index 9f5bf85..9ba7cbc 100644 --- a/common/vouchers_test.go +++ b/common/vouchers_test.go @@ -86,16 +86,16 @@ func TestGetVoucherData(t *testing.T) { spdb := storage.NewSubPrefixDb(db, []byte("vouchers")) // 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_PREFIX_SYMBOLS: []byte("1:SRF\n2:MILO"), + DATA_PREFIX_BALANCES: []byte("1:100\n2:200"), + DATA_PREFIX_DECIMALS: []byte("1:6\n2:4"), + DATA_PREFIX_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(key.ToBytes()), []byte(value)) if err != nil { t.Fatal(err) } diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index d7cd855..4e53228 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -1937,7 +1937,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.DATA_PREFIX_SYMBOLS.ToBytes()) if err != nil { t.Fatal(err) } @@ -1961,7 +1961,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.DATA_PREFIX_SYMBOLS.ToBytes(), expectedSym) if err != nil { t.Fatal(err) } @@ -1991,16 +1991,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_PREFIX_SYMBOLS: []byte("1:SRF\n2:MILO"), + common.DATA_PREFIX_BALANCES: []byte("1:100\n2:200"), + common.DATA_PREFIX_DECIMALS: []byte("1:6\n2:4"), + common.DATA_PREFIX_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(key.ToBytes()), []byte(value)) if err != nil { t.Fatal(err) } From 862830e9dedf20d2d9cf25c3ff738eb1428c3203 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 4 Dec 2024 20:59:46 +0300 Subject: [PATCH 04/11] renamed internal/storage/db.go -> internal/storage/sub_prefix_db.go for clarity --- internal/storage/{db.go => sub_prefix_db.go} | 0 internal/storage/{db_test.go => sub_prefix_db_test.go} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename internal/storage/{db.go => sub_prefix_db.go} (100%) rename internal/storage/{db_test.go => sub_prefix_db_test.go} (100%) diff --git a/internal/storage/db.go b/internal/storage/sub_prefix_db.go similarity index 100% rename from internal/storage/db.go rename to internal/storage/sub_prefix_db.go 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 From 7a9de79aae67ee37369195899770a475f5cd8d55 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 4 Dec 2024 21:07:11 +0300 Subject: [PATCH 05/11] return nil with error --- common/vouchers.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/common/vouchers.go b/common/vouchers.go index 5863c46..e3cc134 100644 --- a/common/vouchers.go +++ b/common/vouchers.go @@ -70,8 +70,7 @@ func GetVoucherData(ctx context.Context, db storage.PrefixDb, input string) (*da for _, key := range keys { value, err := db.Get(ctx, key.ToBytes()) if err != nil { - fmt.Printf("failed to get %v: %v\n", key, err) - continue + return nil, fmt.Errorf("failed to get %s: %v", key.ToBytes(), err) } data[key] = string(value) } From 18423fcd9c91a392f54d7c8ec5fdc0dc7d966d60 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 5 Dec 2024 16:26:56 +0300 Subject: [PATCH 06/11] updated the name of the voucher related data --- common/db.go | 8 ++++---- common/vouchers.go | 12 ++++++------ common/vouchers_test.go | 10 +++++----- internal/handlers/ussd/menuhandler.go | 10 +++++----- internal/handlers/ussd/menuhandler_test.go | 12 ++++++------ 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/common/db.go b/common/db.go index 1454a84..6abfcb5 100644 --- a/common/db.go +++ b/common/db.go @@ -32,10 +32,10 @@ const ( DATA_PUBLIC_KEY_REVERSE DATA_ACTIVE_DECIMAL DATA_ACTIVE_ADDRESS - DATA_PREFIX_SYMBOLS - DATA_PREFIX_BALANCES - DATA_PREFIX_DECIMALS - DATA_PREFIX_ADDRESSES + DATA_VOUCHER_SYMBOLS + DATA_VOUCHER_BALANCES + DATA_VOUCHER_DECIMALS + DATA_VOUCHER_ADDRESSES DATA_PREFIX_TX_SENDERS DATA_PREFIX_TX_RECIPIENTS DATA_PREFIX_TX_VALUES diff --git a/common/vouchers.go b/common/vouchers.go index e3cc134..ab0e099 100644 --- a/common/vouchers.go +++ b/common/vouchers.go @@ -64,22 +64,22 @@ 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 := []DataTyp{DATA_PREFIX_SYMBOLS, DATA_PREFIX_BALANCES, DATA_PREFIX_DECIMALS, DATA_PREFIX_ADDRESSES} + 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, key.ToBytes()) if err != nil { - return nil, fmt.Errorf("failed to get %s: %v", key.ToBytes(), err) + return nil, fmt.Errorf("failed to get %s: %v", key.ToBytes(), err) } data[key] = string(value) } symbol, balance, decimal, address := MatchVoucher(input, - data[DATA_PREFIX_SYMBOLS], - data[DATA_PREFIX_BALANCES], - data[DATA_PREFIX_DECIMALS], - data[DATA_PREFIX_ADDRESSES], + data[DATA_VOUCHER_SYMBOLS], + data[DATA_VOUCHER_BALANCES], + data[DATA_VOUCHER_DECIMALS], + data[DATA_VOUCHER_ADDRESSES], ) if symbol == "" { diff --git a/common/vouchers_test.go b/common/vouchers_test.go index 9ba7cbc..e6f449d 100644 --- a/common/vouchers_test.go +++ b/common/vouchers_test.go @@ -8,8 +8,8 @@ import ( "github.com/alecthomas/assert/v2" "github.com/stretchr/testify/require" - "git.grassecon.net/urdt/ussd/internal/storage" memdb "git.defalsify.org/vise.git/db/mem" + "git.grassecon.net/urdt/ussd/internal/storage" dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api" ) @@ -87,10 +87,10 @@ func TestGetVoucherData(t *testing.T) { // Test voucher data mockData := map[DataTyp][]byte{ - DATA_PREFIX_SYMBOLS: []byte("1:SRF\n2:MILO"), - DATA_PREFIX_BALANCES: []byte("1:100\n2:200"), - DATA_PREFIX_DECIMALS: []byte("1:6\n2:4"), - DATA_PREFIX_ADDRESSES: []byte("1:0xd4c288865Ce\n2:0x41c188d63Qa"), + 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 diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index df3c9ae..14cfc50 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -1577,10 +1577,10 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte) // Store all voucher data dataMap := map[common.DataTyp]string{ - common.DATA_PREFIX_SYMBOLS: data.Symbols, - common.DATA_PREFIX_BALANCES: data.Balances, - common.DATA_PREFIX_DECIMALS: data.Decimals, - common.DATA_PREFIX_ADDRESSES: data.Addresses, + 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 { @@ -1597,7 +1597,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, common.DATA_PREFIX_SYMBOLS.ToBytes()) + voucherData, err := h.prefixDb.Get(ctx, common.DATA_VOUCHER_SYMBOLS.ToBytes()) if err != nil { logg.ErrorCtxf(ctx, "Failed to read the voucherData from prefixDb", "error", err) return res, err diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index 4e53228..9fabe97 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -1937,7 +1937,7 @@ func TestCheckVouchers(t *testing.T) { assert.NoError(t, err) // Read voucher sym data from the store - voucherData, err := spdb.Get(ctx, common.DATA_PREFIX_SYMBOLS.ToBytes()) + voucherData, err := spdb.Get(ctx, common.DATA_VOUCHER_SYMBOLS.ToBytes()) if err != nil { t.Fatal(err) } @@ -1961,7 +1961,7 @@ func TestGetVoucherList(t *testing.T) { expectedSym := []byte("1:SRF\n2:MILO") // Put voucher sym data from the store - err := spdb.Put(ctx, common.DATA_PREFIX_SYMBOLS.ToBytes(), expectedSym) + err := spdb.Put(ctx, common.DATA_VOUCHER_SYMBOLS.ToBytes(), expectedSym) if err != nil { t.Fatal(err) } @@ -1992,10 +1992,10 @@ func TestViewVoucher(t *testing.T) { // Define mock voucher data mockData := map[common.DataTyp][]byte{ - common.DATA_PREFIX_SYMBOLS: []byte("1:SRF\n2:MILO"), - common.DATA_PREFIX_BALANCES: []byte("1:100\n2:200"), - common.DATA_PREFIX_DECIMALS: []byte("1:6\n2:4"), - common.DATA_PREFIX_ADDRESSES: []byte("1:0xd4c288865Ce\n2:0x41c188d63Qa"), + 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 From f733fe563601b6fd8bb19e297147664a69938d31 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 5 Dec 2024 16:31:47 +0300 Subject: [PATCH 07/11] Start the sub prefix data at 256 (0x0100) --- common/db.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/common/db.go b/common/db.go index 6abfcb5..b1d67fd 100644 --- a/common/db.go +++ b/common/db.go @@ -32,7 +32,8 @@ const ( DATA_PUBLIC_KEY_REVERSE DATA_ACTIVE_DECIMAL DATA_ACTIVE_ADDRESS - DATA_VOUCHER_SYMBOLS + // Start the sub prefix data at 256 (0x0100) + DATA_VOUCHER_SYMBOLS DataTyp = 256 + iota DATA_VOUCHER_BALANCES DATA_VOUCHER_DECIMALS DATA_VOUCHER_ADDRESSES From a659fb06fa463c42dfd290ee32d1ba8693972dd4 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 5 Dec 2024 16:58:03 +0300 Subject: [PATCH 08/11] Added a function to convert int to []byte --- common/db.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/common/db.go b/common/db.go index b1d67fd..80757e9 100644 --- a/common/db.go +++ b/common/db.go @@ -88,3 +88,10 @@ func (d DataTyp) ToBytes() []byte { binary.BigEndian.PutUint16(bytes, uint16(d)) return bytes } + +// Convert int to []byte +func IntToBytes(value int) []byte { + bytes := make([]byte, 2) + binary.BigEndian.PutUint16(bytes, uint16(value)) + return bytes +} From 589a94216b93dd8592645953c89c6af3a80ab549 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 5 Dec 2024 17:02:26 +0300 Subject: [PATCH 09/11] Use the DATATYPE_USERDATA as the prefix for the NewSubPrefixDb func --- common/vouchers_test.go | 5 ++++- internal/handlers/ussd/menuhandler.go | 6 ++++-- internal/handlers/ussd/menuhandler_test.go | 4 +++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/common/vouchers_test.go b/common/vouchers_test.go index e6f449d..560fb5b 100644 --- a/common/vouchers_test.go +++ b/common/vouchers_test.go @@ -8,6 +8,7 @@ import ( "github.com/alecthomas/assert/v2" "github.com/stretchr/testify/require" + 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,7 +84,9 @@ func TestGetVoucherData(t *testing.T) { if err != nil { t.Fatal(err) } - spdb := storage.NewSubPrefixDb(db, []byte("vouchers")) + + prefix := IntToBytes(visedb.DATATYPE_USERDATA) + spdb := storage.NewSubPrefixDb(db, prefix) // Test voucher data mockData := map[DataTyp][]byte{ diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 14cfc50..812c897 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.IntToBytes(db.DATATYPE_USERDATA) + prefixDb := storage.NewSubPrefixDb(userdataStore, prefix) h := &Handlers{ userdataStore: userDb, diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index 9fabe97..1aa7c28 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.IntToBytes(visedb.DATATYPE_USERDATA) + spdb := storage.NewSubPrefixDb(db, prefix) return spdb } From caff27b43da33c015eb13ef0fe10483bd88078e6 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 5 Dec 2024 17:50:40 +0300 Subject: [PATCH 10/11] Replace IntToBytes(value int) and ToBytes() with a single ToBytes() function --- common/db.go | 11 ++--------- common/transfer_statements.go | 4 ++-- common/vouchers.go | 4 ++-- common/vouchers_test.go | 4 ++-- internal/handlers/ussd/menuhandler.go | 16 ++++++++-------- internal/handlers/ussd/menuhandler_test.go | 8 ++++---- 6 files changed, 20 insertions(+), 27 deletions(-) diff --git a/common/db.go b/common/db.go index 80757e9..b8fdb51 100644 --- a/common/db.go +++ b/common/db.go @@ -82,15 +82,8 @@ func StringToDataTyp(str string) (DataTyp, error) { } } -// Convert DataTyp to []byte -func (d DataTyp) ToBytes() []byte { - bytes := make([]byte, 2) - binary.BigEndian.PutUint16(bytes, uint16(d)) - return bytes -} - -// Convert int to []byte -func IntToBytes(value int) []byte { +// 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 dd8324a..8567557 100644 --- a/common/transfer_statements.go +++ b/common/transfer_statements.go @@ -61,9 +61,9 @@ func GetTransferData(ctx context.Context, db storage.PrefixDb, publicKey string, data := make(map[DataTyp]string) for _, key := range keys { - value, err := db.Get(ctx, key.ToBytes()) + value, err := db.Get(ctx, ToBytes(key)) if err != nil { - return "", fmt.Errorf("failed to get %s: %v", key.ToBytes(), err) + return "", fmt.Errorf("failed to get %s: %v", ToBytes(key), err) } data[key] = string(value) } diff --git a/common/vouchers.go b/common/vouchers.go index ab0e099..6cff91d 100644 --- a/common/vouchers.go +++ b/common/vouchers.go @@ -68,9 +68,9 @@ func GetVoucherData(ctx context.Context, db storage.PrefixDb, input string) (*da data := make(map[DataTyp]string) for _, key := range keys { - value, err := db.Get(ctx, key.ToBytes()) + value, err := db.Get(ctx, ToBytes(key)) if err != nil { - return nil, fmt.Errorf("failed to get %s: %v", key.ToBytes(), err) + return nil, fmt.Errorf("failed to get %s: %v", ToBytes(key), err) } data[key] = string(value) } diff --git a/common/vouchers_test.go b/common/vouchers_test.go index 560fb5b..ba6cd60 100644 --- a/common/vouchers_test.go +++ b/common/vouchers_test.go @@ -85,7 +85,7 @@ func TestGetVoucherData(t *testing.T) { t.Fatal(err) } - prefix := IntToBytes(visedb.DATATYPE_USERDATA) + prefix := ToBytes(visedb.DATATYPE_USERDATA) spdb := storage.NewSubPrefixDb(db, prefix) // Test voucher data @@ -98,7 +98,7 @@ func TestGetVoucherData(t *testing.T) { // Put the data for key, value := range mockData { - err = spdb.Put(ctx, []byte(key.ToBytes()), []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 812c897..c860771 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -87,7 +87,7 @@ func NewHandlers(appFlags *asm.FlagParser, userdataStore db.Db, adminstore *util } // Instantiate the SubPrefixDb with "DATATYPE_USERDATA" prefix - prefix := common.IntToBytes(db.DATATYPE_USERDATA) + prefix := common.ToBytes(db.DATATYPE_USERDATA) prefixDb := storage.NewSubPrefixDb(userdataStore, prefix) h := &Handlers{ @@ -1586,7 +1586,7 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte) } for key, value := range dataMap { - if err := h.prefixDb.Put(ctx, []byte(key.ToBytes()), []byte(value)); err != nil { + if err := h.prefixDb.Put(ctx, []byte(common.ToBytes(key)), []byte(value)); err != nil { return res, nil } } @@ -1599,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, common.DATA_VOUCHER_SYMBOLS.ToBytes()) + 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 @@ -1757,7 +1757,7 @@ func (h *Handlers) CheckTransactions(ctx context.Context, sym string, input []by } for key, value := range dataMap { - if err := h.prefixDb.Put(ctx, []byte(key.ToBytes()), []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 } @@ -1783,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, common.DATA_PREFIX_TX_SENDERS.ToBytes()) + TransactionSenders, err := h.prefixDb.Get(ctx, common.ToBytes(common.DATA_PREFIX_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, common.DATA_PREFIX_TX_SYMBOLS.ToBytes()) + TransactionSyms, err := h.prefixDb.Get(ctx, common.ToBytes(common.DATA_PREFIX_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, common.DATA_PREFIX_TX_VALUES.ToBytes()) + TransactionValues, err := h.prefixDb.Get(ctx, common.ToBytes(common.DATA_PREFIX_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, common.DATA_PREFIX_TX_DATES.ToBytes()) + TransactionDates, err := h.prefixDb.Get(ctx, common.ToBytes(common.DATA_PREFIX_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 1aa7c28..fac95a1 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -57,7 +57,7 @@ func InitializeTestSubPrefixDb(t *testing.T, ctx context.Context) *storage.SubPr if err != nil { t.Fatal(err) } - prefix := common.IntToBytes(visedb.DATATYPE_USERDATA) + prefix := common.ToBytes(visedb.DATATYPE_USERDATA) spdb := storage.NewSubPrefixDb(db, prefix) return spdb @@ -1939,7 +1939,7 @@ func TestCheckVouchers(t *testing.T) { assert.NoError(t, err) // Read voucher sym data from the store - voucherData, err := spdb.Get(ctx, common.DATA_VOUCHER_SYMBOLS.ToBytes()) + voucherData, err := spdb.Get(ctx, common.ToBytes(common.DATA_VOUCHER_SYMBOLS)) if err != nil { t.Fatal(err) } @@ -1963,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, common.DATA_VOUCHER_SYMBOLS.ToBytes(), expectedSym) + err := spdb.Put(ctx, common.ToBytes(common.DATA_VOUCHER_SYMBOLS), expectedSym) if err != nil { t.Fatal(err) } @@ -2002,7 +2002,7 @@ func TestViewVoucher(t *testing.T) { // Put the data for key, value := range mockData { - err = spdb.Put(ctx, []byte(key.ToBytes()), []byte(value)) + err = spdb.Put(ctx, []byte(common.ToBytes(key)), []byte(value)) if err != nil { t.Fatal(err) } From 14737b5f1254fc5ee9ada948ab6e6811baf9d0f9 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 5 Dec 2024 17:58:51 +0300 Subject: [PATCH 11/11] Removed redundant naming of transfer related data --- common/db.go | 16 ++++++++-------- common/transfer_statements.go | 16 ++++++++-------- internal/handlers/ussd/menuhandler.go | 24 ++++++++++++------------ 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/common/db.go b/common/db.go index b8fdb51..5532dc9 100644 --- a/common/db.go +++ b/common/db.go @@ -37,14 +37,14 @@ const ( DATA_VOUCHER_BALANCES DATA_VOUCHER_DECIMALS DATA_VOUCHER_ADDRESSES - DATA_PREFIX_TX_SENDERS - DATA_PREFIX_TX_RECIPIENTS - DATA_PREFIX_TX_VALUES - DATA_PREFIX_TX_ADDRESSES - DATA_PREFIX_TX_HASHES - DATA_PREFIX_TX_DATES - DATA_PREFIX_TX_SYMBOLS - DATA_PREFIX_TX_DECIMALS + 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 ( diff --git a/common/transfer_statements.go b/common/transfer_statements.go index 8567557..4e6f66b 100644 --- a/common/transfer_statements.go +++ b/common/transfer_statements.go @@ -57,7 +57,7 @@ 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 := []DataTyp{DATA_PREFIX_TX_SENDERS, DATA_PREFIX_TX_RECIPIENTS, DATA_PREFIX_TX_VALUES, DATA_PREFIX_TX_ADDRESSES, DATA_PREFIX_TX_HASHES, DATA_PREFIX_TX_DATES, DATA_PREFIX_TX_SYMBOLS} + 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 { @@ -69,13 +69,13 @@ func GetTransferData(ctx context.Context, db storage.PrefixDb, publicKey string, } // Split the data - senders := strings.Split(string(data[DATA_PREFIX_TX_SENDERS]), "\n") - recipients := strings.Split(string(data[DATA_PREFIX_TX_RECIPIENTS]), "\n") - values := strings.Split(string(data[DATA_PREFIX_TX_VALUES]), "\n") - addresses := strings.Split(string(data[DATA_PREFIX_TX_ADDRESSES]), "\n") - hashes := strings.Split(string(data[DATA_PREFIX_TX_HASHES]), "\n") - dates := strings.Split(string(data[DATA_PREFIX_TX_DATES]), "\n") - syms := strings.Split(string(data[DATA_PREFIX_TX_SYMBOLS]), "\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/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index c860771..b47e31f 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -1746,14 +1746,14 @@ func (h *Handlers) CheckTransactions(ctx context.Context, sym string, input []by // Store all transaction data dataMap := map[common.DataTyp]string{ - common.DATA_PREFIX_TX_SENDERS: data.Senders, - common.DATA_PREFIX_TX_RECIPIENTS: data.Recipients, - common.DATA_PREFIX_TX_VALUES: data.TransferValues, - common.DATA_PREFIX_TX_ADDRESSES: data.Addresses, - common.DATA_PREFIX_TX_HASHES: data.TxHashes, - common.DATA_PREFIX_TX_DATES: data.Dates, - common.DATA_PREFIX_TX_SYMBOLS: data.Symbols, - common.DATA_PREFIX_TX_DECIMALS: data.Decimals, + 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 { @@ -1783,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, common.ToBytes(common.DATA_PREFIX_TX_SENDERS)) + 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, common.ToBytes(common.DATA_PREFIX_TX_SYMBOLS)) + 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, common.ToBytes(common.DATA_PREFIX_TX_VALUES)) + 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, common.ToBytes(common.DATA_PREFIX_TX_DATES)) + 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