Skip to content

Commit

Permalink
Fix: ticket hash
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Dec 29, 2023
1 parent cab5642 commit 41d167e
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 28 deletions.
9 changes: 5 additions & 4 deletions internal/models/ticket/ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ type Ticket struct {

ID int64 `bun:"id,pk,notnull,autoincrement"`
Level int64 `bun:"level"`
TicketerID int64 `bun:"ticketer_id,unique:ticket_key"`
ContentType []byte `bun:"content_type,type:bytea,unique:ticket_key"`
Content []byte `bun:"content,type:bytea,unique:ticket_key"`
TicketerID int64 `bun:"ticketer_id"`
ContentType []byte `bun:"content_type,type:bytea"`
Content []byte `bun:"content,type:bytea"`
UpdatesCount int `bun:"updates_count"`
Hash string `bun:"hash,unique:ticket_key"`

Ticketer account.Account `bun:"rel:belongs-to"`
}
Expand All @@ -29,7 +30,7 @@ func (Ticket) TableName() string {
return "tickets"
}

func (t Ticket) Hash() string {
func (t Ticket) GetHash() string {
data := make([]byte, len(t.ContentType))
copy(data, t.ContentType)
data = append(data, t.Content...)
Expand Down
2 changes: 1 addition & 1 deletion internal/models/ticket/ticket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestTicket_Hash(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.t.Hash()
got := tt.t.GetHash()
require.Equal(t, tt.want, got)
})
}
Expand Down
3 changes: 3 additions & 0 deletions internal/parsers/operations/operation_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2484,6 +2484,7 @@ func TestGroup_Parse(t *testing.T) {
ContentType: []byte(`{"prim":"string"}`),
Content: []byte(`{"string":"Ticket"}`),
UpdatesCount: 1,
Hash: "44f09c8c9d1135c11c71f31ab4126d464cad6067010e1d440e4749331450d860",
},
},
},
Expand Down Expand Up @@ -2551,6 +2552,7 @@ func TestGroup_Parse(t *testing.T) {
ContentType: []byte(`{"prim":"string"}`),
Content: []byte(`{"string":"Ticket"}`),
UpdatesCount: 1,
Hash: "44f09c8c9d1135c11c71f31ab4126d464cad6067010e1d440e4749331450d860",
},
},
},
Expand All @@ -2569,6 +2571,7 @@ func TestGroup_Parse(t *testing.T) {
ContentType: []byte(`{"prim":"string"}`),
Content: []byte(`{"string":"Ticket"}`),
UpdatesCount: 2,
Hash: "44f09c8c9d1135c11c71f31ab4126d464cad6067010e1d440e4749331450d860",
},
},
},
Expand Down
1 change: 1 addition & 0 deletions internal/parsers/operations/ticket_updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (p *TicketUpdateParser) toModel(data noderpc.TicketUpdate, operation *opera
UpdatesCount: 1,
Level: operation.Level,
}
tckt.Hash = tckt.GetHash()
store.AddTickets(tckt)

updates := make([]*ticket.TicketUpdate, 0)
Expand Down
4 changes: 2 additions & 2 deletions internal/parsers/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (store *TestStore) AddAccounts(accounts ...account.Account) {
// AddTickets -
func (store *TestStore) AddTickets(tickets ...ticket.Ticket) {
for i := range tickets {
hash := tickets[i].Hash()
hash := tickets[i].GetHash()
if t, ok := store.Tickets[hash]; !ok {
store.Tickets[hash] = &tickets[i]
} else {
Expand All @@ -119,7 +119,7 @@ func (store *TestStore) AddTickets(tickets ...ticket.Ticket) {
// AddTicketBalances -
func (store *TestStore) AddTicketBalances(balance ...ticket.Balance) {
for i := range balance {
key := fmt.Sprintf("%s_%s", balance[i].Ticket.Hash(), balance[i].Account.Address)
key := fmt.Sprintf("%s_%s", balance[i].Ticket.GetHash(), balance[i].Account.Address)
if t, ok := store.TicketBalances[key]; !ok {
store.TicketBalances[key] = &balance[i]
} else {
Expand Down
2 changes: 1 addition & 1 deletion internal/postgres/core/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func (t Transaction) Tickets(ctx context.Context, tickets ...*ticket.Ticket) err
}

_, err := t.tx.NewInsert().Model(&tickets).
Column("ticketer_id", "content", "content_type", "updates_count", "level").
Column("ticketer_id", "content", "content_type", "updates_count", "level", "hash").
On("CONFLICT ON CONSTRAINT ticket_key DO UPDATE").
Set("updates_count = ticket.updates_count + EXCLUDED.updates_count").
Returning("id").
Expand Down
6 changes: 3 additions & 3 deletions internal/postgres/store/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (store *Store) saveTickets(ctx context.Context, tx models.Transaction) erro
}

for i := range arr {
store.ticketIds[arr[i].Hash()] = arr[i].ID
store.ticketIds[arr[i].GetHash()] = arr[i].ID
}

return nil
Expand All @@ -142,7 +142,7 @@ func (store *Store) saveTicketBalances(ctx context.Context, tx models.Transactio
return errors.Errorf("unknown ticket balance ticketer: %s", balance.Ticket.Ticketer.Address)
}

if id, ok := store.ticketIds[balance.Ticket.Hash()]; ok {
if id, ok := store.ticketIds[balance.Ticket.GetHash()]; ok {
balance.TicketId = id
} else {
return errors.Errorf("unknown ticket of balance: %s", balance.Ticket.Ticketer.Address)
Expand Down Expand Up @@ -230,7 +230,7 @@ func (store *Store) saveOperations(ctx context.Context, tx models.Transaction) e

operation.TicketUpdates[j].OperationId = operation.ID

hash := operation.TicketUpdates[j].Ticket.Hash()
hash := operation.TicketUpdates[j].Ticket.GetHash()
if id, ok := store.ticketIds[hash]; ok {
operation.TicketUpdates[j].TicketId = id
} else {
Expand Down
4 changes: 2 additions & 2 deletions internal/postgres/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (store *Store) AddAccounts(accounts ...account.Account) {
// AddTickets -
func (store *Store) AddTickets(tickets ...ticket.Ticket) {
for i := range tickets {
hash := tickets[i].Hash()
hash := tickets[i].GetHash()
if t, ok := store.Tickets[hash]; !ok {
store.Tickets[hash] = &tickets[i]
} else {
Expand All @@ -146,7 +146,7 @@ func (store *Store) AddTickets(tickets ...ticket.Ticket) {
// AddTicketBalances -
func (store *Store) AddTicketBalances(balance ...ticket.Balance) {
for i := range balance {
key := fmt.Sprintf("%s_%s", balance[i].Ticket.Hash(), balance[i].Account.Address)
key := fmt.Sprintf("%s_%s", balance[i].Ticket.GetHash(), balance[i].Account.Address)
if t, ok := store.TicketBalances[key]; !ok {
store.TicketBalances[key] = &balance[i]
} else {
Expand Down
4 changes: 3 additions & 1 deletion internal/postgres/tests/fixtures/tickets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
content: '{"string":"abc"}'
updates_count: 2
level: 40
hash: 1e2be4e81235c6c25e245b098cb9dd9ac32c5316ee9f2b3bc4289811cf8e0b67
- id: 2
updates_count: 1
level: 40
ticketer_id: 251
content_type: '{"prim":"int"}'
content: '{"int":"1"}'
content: '{"int":"1"}'
hash: c342d400f1ad59ba909b6f308af1c312f43c88f7200877398d9c5677da43830d
40 changes: 26 additions & 14 deletions internal/postgres/tests/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,26 +375,38 @@ func (s *StorageTestSuite) TestTickets() {
tx, err := core.NewTransaction(ctx, s.storage.DB)
s.Require().NoError(err)

err = tx.Tickets(ctx, &ticket.Ticket{
ContentType: testsuite.MustHexDecode("7b227072696d223a22737472696e67227d"),
Content: testsuite.MustHexDecode("7b22737472696e67223a22616263227d"),
TicketerID: 133,
UpdatesCount: 1,
}, &ticket.Ticket{
ContentType: testsuite.MustHexDecode("7b227072696d223a22737472696e67227d"),
Content: testsuite.MustHexDecode("7b22737472696e67223a22616263227d"),
TicketerID: 132,
UpdatesCount: 2,
})
tickets := []*ticket.Ticket{
{
ContentType: testsuite.MustHexDecode("7b227072696d223a22737472696e67227d"),
Content: testsuite.MustHexDecode("7b22737472696e67223a22616263227d"),
TicketerID: 133,
UpdatesCount: 1,
Ticketer: account.Account{
Address: "KT1SM849krq9FFxGWCZyc7X5GvAz8XnRmXnf",
},
Hash: "1e2be4e81235c6c25e245b098cb9dd9ac32c5316ee9f2b3bc4289811cf8e0b67",
}, {
ContentType: testsuite.MustHexDecode("7b227072696d223a22737472696e67227d"),
Content: testsuite.MustHexDecode("7b22737472696e67223a22616263227d"),
TicketerID: 132,
UpdatesCount: 2,
Ticketer: account.Account{
Address: "KT1BrG24EYvtYGeGgd615Yzhvoa14BNPLZzu",
},
Hash: "87b52da988c2bb5b6fd8bc53d816fae03faf51dcdc6c99d90203fdcecc8ee23c",
},
}

err = tx.Tickets(ctx, tickets...)
s.Require().NoError(err)

err = tx.Commit()
s.Require().NoError(err)

var tickets []ticket.Ticket
err = s.storage.DB.NewSelect().Model(&tickets).Scan(ctx)
var received []ticket.Ticket
err = s.storage.DB.NewSelect().Model(&received).Relation("Ticketer").Scan(ctx)
s.Require().NoError(err)
s.Require().Len(tickets, 3)
s.Require().Len(received, 3)
}

func (s *StorageTestSuite) TestTicketBalances() {
Expand Down

0 comments on commit 41d167e

Please sign in to comment.