Compare commits
2 Commits
589a94216b
...
14737b5f12
Author | SHA1 | Date | |
---|---|---|---|
14737b5f12 | |||
caff27b43d |
27
common/db.go
27
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 (
|
||||
@ -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
|
||||
|
@ -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 := []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 {
|
||||
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)
|
||||
}
|
||||
|
||||
// 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) {
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
@ -1746,18 +1746,18 @@ 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 {
|
||||
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_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_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_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_TX_DATES))
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "Failed to read the TransactionDates from prefixDb", "error", err)
|
||||
return res, err
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user