diff --git a/internal/models/ticket/ticket.go b/internal/models/ticket/ticket.go index 9c56cb885..52433fda8 100644 --- a/internal/models/ticket/ticket.go +++ b/internal/models/ticket/ticket.go @@ -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"` } @@ -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...) diff --git a/internal/models/ticket/ticket_test.go b/internal/models/ticket/ticket_test.go index 28b036ade..88c9a77aa 100644 --- a/internal/models/ticket/ticket_test.go +++ b/internal/models/ticket/ticket_test.go @@ -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) }) } diff --git a/internal/parsers/operations/operation_group_test.go b/internal/parsers/operations/operation_group_test.go index 74ff8dd78..cca2cfe2f 100644 --- a/internal/parsers/operations/operation_group_test.go +++ b/internal/parsers/operations/operation_group_test.go @@ -2484,6 +2484,7 @@ func TestGroup_Parse(t *testing.T) { ContentType: []byte(`{"prim":"string"}`), Content: []byte(`{"string":"Ticket"}`), UpdatesCount: 1, + Hash: "44f09c8c9d1135c11c71f31ab4126d464cad6067010e1d440e4749331450d860", }, }, }, @@ -2551,6 +2552,7 @@ func TestGroup_Parse(t *testing.T) { ContentType: []byte(`{"prim":"string"}`), Content: []byte(`{"string":"Ticket"}`), UpdatesCount: 1, + Hash: "44f09c8c9d1135c11c71f31ab4126d464cad6067010e1d440e4749331450d860", }, }, }, @@ -2569,6 +2571,7 @@ func TestGroup_Parse(t *testing.T) { ContentType: []byte(`{"prim":"string"}`), Content: []byte(`{"string":"Ticket"}`), UpdatesCount: 2, + Hash: "44f09c8c9d1135c11c71f31ab4126d464cad6067010e1d440e4749331450d860", }, }, }, diff --git a/internal/parsers/operations/ticket_updates.go b/internal/parsers/operations/ticket_updates.go index a49a9ad24..c93fe1e35 100644 --- a/internal/parsers/operations/ticket_updates.go +++ b/internal/parsers/operations/ticket_updates.go @@ -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) diff --git a/internal/parsers/store.go b/internal/parsers/store.go index a074ef488..2e5444c99 100644 --- a/internal/parsers/store.go +++ b/internal/parsers/store.go @@ -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 { @@ -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 { diff --git a/internal/postgres/core/transaction.go b/internal/postgres/core/transaction.go index 9de7d712e..513c77b47 100644 --- a/internal/postgres/core/transaction.go +++ b/internal/postgres/core/transaction.go @@ -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"). diff --git a/internal/postgres/store/save.go b/internal/postgres/store/save.go index 22a5a3aa7..6800dcffd 100644 --- a/internal/postgres/store/save.go +++ b/internal/postgres/store/save.go @@ -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 @@ -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) @@ -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 { diff --git a/internal/postgres/store/store.go b/internal/postgres/store/store.go index 6a2918b78..039875d36 100644 --- a/internal/postgres/store/store.go +++ b/internal/postgres/store/store.go @@ -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 { @@ -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 { diff --git a/internal/postgres/tests/fixtures/tickets.yml b/internal/postgres/tests/fixtures/tickets.yml index 6bb3d4bbf..d953671cc 100644 --- a/internal/postgres/tests/fixtures/tickets.yml +++ b/internal/postgres/tests/fixtures/tickets.yml @@ -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"}' \ No newline at end of file + content: '{"int":"1"}' + hash: c342d400f1ad59ba909b6f308af1c312f43c88f7200877398d9c5677da43830d \ No newline at end of file diff --git a/internal/postgres/tests/transaction_test.go b/internal/postgres/tests/transaction_test.go index bc39d03b0..8c04f3406 100644 --- a/internal/postgres/tests/transaction_test.go +++ b/internal/postgres/tests/transaction_test.go @@ -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() {