Use numeric prefixes

This commit is contained in:
Alfred Kamanda 2024-12-04 20:42:47 +03:00
parent 4ecfc9de38
commit 93c44861e0
Signed by untrusted user: Alfred-mk
GPG Key ID: 7EA3D01708908703
4 changed files with 60 additions and 40 deletions

View File

@ -32,7 +32,18 @@ const (
DATA_PUBLIC_KEY_REVERSE DATA_PUBLIC_KEY_REVERSE
DATA_ACTIVE_DECIMAL DATA_ACTIVE_DECIMAL
DATA_ACTIVE_ADDRESS 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 ( var (
@ -69,3 +80,10 @@ func StringToDataTyp(str string) (DataTyp, error) {
return 0, errors.New("invalid DataTyp string") 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
}

View File

@ -57,11 +57,11 @@ func ProcessTransfers(transfers []dataserviceapi.Last10TxResponse) TransferMetad
// GetTransferData retrieves and matches transfer data // GetTransferData retrieves and matches transfer data
// returns a formatted string of the full transaction/statement // returns a formatted string of the full transaction/statement
func GetTransferData(ctx context.Context, db storage.PrefixDb, publicKey string, index int) (string, error) { func GetTransferData(ctx context.Context, db storage.PrefixDb, publicKey string, index int) (string, error) {
keys := []string{"txfrom", "txto", "txval", "txaddr", "txhash", "txdate", "txsym"} 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[string]string) data := make(map[DataTyp]string)
for _, key := range keys { for _, key := range keys {
value, err := db.Get(ctx, []byte(key)) value, err := db.Get(ctx, key.ToBytes())
if err != nil { if err != nil {
return "", fmt.Errorf("failed to get %s: %v", key, err) 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 // Split the data
senders := strings.Split(string(data["txfrom"]), "\n") senders := strings.Split(string(data[DATA_PREFIX_TX_SENDERS]), "\n")
recipients := strings.Split(string(data["txto"]), "\n") recipients := strings.Split(string(data[DATA_PREFIX_TX_RECIPIENTS]), "\n")
values := strings.Split(string(data["txval"]), "\n") values := strings.Split(string(data[DATA_PREFIX_TX_VALUES]), "\n")
addresses := strings.Split(string(data["txaddr"]), "\n") addresses := strings.Split(string(data[DATA_PREFIX_TX_ADDRESSES]), "\n")
hashes := strings.Split(string(data["txhash"]), "\n") hashes := strings.Split(string(data[DATA_PREFIX_TX_HASHES]), "\n")
dates := strings.Split(string(data["txdate"]), "\n") dates := strings.Split(string(data[DATA_PREFIX_TX_DATES]), "\n")
syms := strings.Split(string(data["txsym"]), "\n") syms := strings.Split(string(data[DATA_PREFIX_TX_SYMBOLS]), "\n")
// Check if index is within range // Check if index is within range
if index < 1 || index > len(senders) { if index < 1 || index > len(senders) {

View File

@ -64,22 +64,24 @@ func ScaleDownBalance(balance, decimals string) string {
// GetVoucherData retrieves and matches voucher data // GetVoucherData retrieves and matches voucher data
func GetVoucherData(ctx context.Context, db storage.PrefixDb, input string) (*dataserviceapi.TokenHoldings, error) { func GetVoucherData(ctx context.Context, db storage.PrefixDb, input string) (*dataserviceapi.TokenHoldings, error) {
keys := []string{"sym", "bal", "deci", "addr"} keys := []DataTyp{DATA_PREFIX_SYMBOLS, DATA_PREFIX_BALANCES, DATA_PREFIX_DECIMALS, DATA_PREFIX_ADDRESSES}
data := make(map[string]string) data := make(map[DataTyp]string)
for _, key := range keys { for _, key := range keys {
value, err := db.Get(ctx, []byte(key)) value, err := db.Get(ctx, key.ToBytes())
if err != nil { 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) data[key] = string(value)
} }
symbol, balance, decimal, address := MatchVoucher(input, symbol, balance, decimal, address := MatchVoucher(input,
data["sym"], data[DATA_PREFIX_SYMBOLS],
data["bal"], data[DATA_PREFIX_BALANCES],
data["deci"], data[DATA_PREFIX_DECIMALS],
data["addr"]) data[DATA_PREFIX_ADDRESSES],
)
if symbol == "" { if symbol == "" {
return nil, nil return nil, nil

View File

@ -1569,15 +1569,15 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte)
data := common.ProcessVouchers(vouchersResp) data := common.ProcessVouchers(vouchersResp)
// Store all voucher data // Store all voucher data
dataMap := map[string]string{ dataMap := map[common.DataTyp]string{
"sym": data.Symbols, common.DATA_PREFIX_SYMBOLS: data.Symbols,
"bal": data.Balances, common.DATA_PREFIX_BALANCES: data.Balances,
"deci": data.Decimals, common.DATA_PREFIX_DECIMALS: data.Decimals,
"addr": data.Addresses, common.DATA_PREFIX_ADDRESSES: data.Addresses,
} }
for key, value := range dataMap { 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 return res, nil
} }
} }
@ -1590,7 +1590,7 @@ func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte)
var res resource.Result var res resource.Result
// Read vouchers from the store // 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 { if err != nil {
logg.ErrorCtxf(ctx, "Failed to read the voucherData from prefixDb", "error", err) logg.ErrorCtxf(ctx, "Failed to read the voucherData from prefixDb", "error", err)
return res, err return res, err
@ -1732,19 +1732,19 @@ func (h *Handlers) CheckTransactions(ctx context.Context, sym string, input []by
data := common.ProcessTransfers(transactionsResp) data := common.ProcessTransfers(transactionsResp)
// Store all transaction data // Store all transaction data
dataMap := map[string]string{ dataMap := map[common.DataTyp]string{
"txfrom": data.Senders, common.DATA_PREFIX_TX_SENDERS: data.Senders,
"txto": data.Recipients, common.DATA_PREFIX_TX_RECIPIENTS: data.Recipients,
"txval": data.TransferValues, common.DATA_PREFIX_TX_VALUES: data.TransferValues,
"txaddr": data.Addresses, common.DATA_PREFIX_TX_ADDRESSES: data.Addresses,
"txhash": data.TxHashes, common.DATA_PREFIX_TX_HASHES: data.TxHashes,
"txdate": data.Dates, common.DATA_PREFIX_TX_DATES: data.Dates,
"txsym": data.Symbols, common.DATA_PREFIX_TX_SYMBOLS: data.Symbols,
"txdeci": data.Decimals, common.DATA_PREFIX_TX_DECIMALS: data.Decimals,
} }
for key, value := range dataMap { 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) logg.ErrorCtxf(ctx, "failed to write to prefixDb", "error", err)
return res, 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 // 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 { if err != nil {
logg.ErrorCtxf(ctx, "Failed to read the TransactionSenders from prefixDb", "error", err) logg.ErrorCtxf(ctx, "Failed to read the TransactionSenders from prefixDb", "error", err)
return res, 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 { if err != nil {
logg.ErrorCtxf(ctx, "Failed to read the TransactionSyms from prefixDb", "error", err) logg.ErrorCtxf(ctx, "Failed to read the TransactionSyms from prefixDb", "error", err)
return res, 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 { if err != nil {
logg.ErrorCtxf(ctx, "Failed to read the TransactionValues from prefixDb", "error", err) logg.ErrorCtxf(ctx, "Failed to read the TransactionValues from prefixDb", "error", err)
return res, 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 { if err != nil {
logg.ErrorCtxf(ctx, "Failed to read the TransactionDates from prefixDb", "error", err) logg.ErrorCtxf(ctx, "Failed to read the TransactionDates from prefixDb", "error", err)
return res, err return res, err