From 11d1dc6fec0a902be3f5868bde338b8a8c0c1b6e Mon Sep 17 00:00:00 2001 From: Artem Date: Sun, 1 Oct 2023 21:41:56 +0200 Subject: [PATCH] Fix: operations count for account --- internal/parsers/contract/alpha.go | 2 +- internal/parsers/contract/babylon.go | 2 +- internal/parsers/contract/hangzhou.go | 2 +- internal/parsers/contract/jakarta.go | 2 +- internal/parsers/migrations/implicit.go | 2 +- internal/parsers/migrations/vesting.go | 6 +-- internal/parsers/operations/event.go | 2 +- .../operations/operation_group_test.go | 19 +++++++++- internal/parsers/operations/origination.go | 5 +-- .../operations/register_global_constant.go | 2 +- .../operations/sr_execute_outbox_message.go | 4 +- internal/parsers/operations/sr_originate.go | 4 +- internal/parsers/operations/test_common.go | 5 +++ internal/parsers/operations/ticket_updates.go | 6 +-- internal/parsers/operations/transaction.go | 6 +-- .../parsers/operations/transfer_ticket.go | 4 +- .../operations/tx_rollup_origination.go | 4 +- internal/parsers/store.go | 37 +++++++++++-------- internal/postgres/store/store.go | 12 +++--- 19 files changed, 76 insertions(+), 50 deletions(-) diff --git a/internal/parsers/contract/alpha.go b/internal/parsers/contract/alpha.go index 6ee3b41a8..9dd21f8cb 100644 --- a/internal/parsers/contract/alpha.go +++ b/internal/parsers/contract/alpha.go @@ -43,7 +43,7 @@ func (p *Alpha) Parse(ctx context.Context, operation *operation.Operation, store } store.AddContracts(&contract) - store.AddAccounts(&contract.Account) + store.AddAccounts(contract.Account) return nil } diff --git a/internal/parsers/contract/babylon.go b/internal/parsers/contract/babylon.go index d31b9ea57..99a9acc2d 100644 --- a/internal/parsers/contract/babylon.go +++ b/internal/parsers/contract/babylon.go @@ -43,7 +43,7 @@ func (p *Babylon) Parse(ctx context.Context, operation *operation.Operation, sto } store.AddContracts(&contract) - store.AddAccounts(&contract.Account) + store.AddAccounts(contract.Account) return nil } diff --git a/internal/parsers/contract/hangzhou.go b/internal/parsers/contract/hangzhou.go index b8645187c..e07f0766e 100644 --- a/internal/parsers/contract/hangzhou.go +++ b/internal/parsers/contract/hangzhou.go @@ -43,7 +43,7 @@ func (p *Hangzhou) Parse(ctx context.Context, operation *operation.Operation, st } store.AddContracts(&contract) - store.AddAccounts(&contract.Account) + store.AddAccounts(contract.Account) return nil } diff --git a/internal/parsers/contract/jakarta.go b/internal/parsers/contract/jakarta.go index 0b3bc6984..b35155794 100644 --- a/internal/parsers/contract/jakarta.go +++ b/internal/parsers/contract/jakarta.go @@ -47,7 +47,7 @@ func (p *Jakarta) Parse(ctx context.Context, operation *operation.Operation, sto } store.AddContracts(&contract) - store.AddAccounts(&contract.Account) + store.AddAccounts(contract.Account) return nil } diff --git a/internal/parsers/migrations/implicit.go b/internal/parsers/migrations/implicit.go index 3ffc4aa96..e27666706 100644 --- a/internal/parsers/migrations/implicit.go +++ b/internal/parsers/migrations/implicit.go @@ -139,7 +139,7 @@ func (p *ImplicitParser) transaction(implicit noderpc.ImplicitOperationsResult, OperationsCount: 1, LastAction: head.Timestamp, } - store.AddAccounts(&tx.Destination) + store.AddAccounts(tx.Destination) break } } diff --git a/internal/parsers/migrations/vesting.go b/internal/parsers/migrations/vesting.go index e20fb1ba7..aa435cdc5 100644 --- a/internal/parsers/migrations/vesting.go +++ b/internal/parsers/migrations/vesting.go @@ -64,9 +64,9 @@ func (p *VestingParser) Parse(ctx context.Context, data noderpc.ContractData, he } store.AddAccounts( - &vestingOperation.Source, - &vestingOperation.Destination, - &vestingOperation.Delegate, + vestingOperation.Source, + vestingOperation.Destination, + vestingOperation.Delegate, ) contracts := store.ListContracts() diff --git a/internal/parsers/operations/event.go b/internal/parsers/operations/event.go index e458e1b6b..857e7f668 100644 --- a/internal/parsers/operations/event.go +++ b/internal/parsers/operations/event.go @@ -58,7 +58,7 @@ func (p Event) Parse(ctx context.Context, data noderpc.Operation, store parsers. p.stackTrace.Add(event) store.AddOperations(&event) - store.AddAccounts(&event.Source) + store.AddAccounts(event.Source) return nil } diff --git a/internal/parsers/operations/operation_group_test.go b/internal/parsers/operations/operation_group_test.go index 901ba759e..74ff8dd78 100644 --- a/internal/parsers/operations/operation_group_test.go +++ b/internal/parsers/operations/operation_group_test.go @@ -927,6 +927,15 @@ func TestGroup_Parse(t *testing.T) { DeffatedStorage: []byte("{\"int\":\"0\"}\n"), }, }, + Accounts: map[string]*account.Account{ + "KT1NppzrgyLZD3aku7fssfhYPm5QqZwyabvR": { + Address: "KT1NppzrgyLZD3aku7fssfhYPm5QqZwyabvR", + Type: types.AccountTypeContract, + Level: 86142, + OperationsCount: 1, + LastAction: timestamp, + }, + }, Contracts: []*modelContract.Contract{ { Level: 86142, @@ -2093,7 +2102,15 @@ func TestGroup_Parse(t *testing.T) { Contract: "KT1BM1SyQnTzNU1J8TZv5Mdj4ScuTgNKH5uj", }, }, - + Accounts: map[string]*account.Account{ + "KT1Jk8LRDoj6LkopYZwRq5ZEWBhYv8nVc6e6": { + Address: "KT1Jk8LRDoj6LkopYZwRq5ZEWBhYv8nVc6e6", + Type: types.AccountTypeContract, + Level: 381735, + OperationsCount: 4, + LastAction: timestamp, + }, + }, Contracts: []*modelContract.Contract{ { Timestamp: timestamp, diff --git a/internal/parsers/operations/origination.go b/internal/parsers/operations/origination.go index cd98878e0..2e5057f2b 100644 --- a/internal/parsers/operations/origination.go +++ b/internal/parsers/operations/origination.go @@ -77,9 +77,8 @@ func (p Origination) Parse(ctx context.Context, data noderpc.Operation, store pa store.AddOperations(&origination) store.AddAccounts( - &origination.Source, - &origination.Destination, - &origination.Delegate, + origination.Source, + origination.Delegate, ) return nil diff --git a/internal/parsers/operations/register_global_constant.go b/internal/parsers/operations/register_global_constant.go index 7025d63cb..a8540e88d 100644 --- a/internal/parsers/operations/register_global_constant.go +++ b/internal/parsers/operations/register_global_constant.go @@ -52,6 +52,6 @@ func (p RegisterGlobalConstant) Parse(ctx context.Context, data noderpc.Operatio if registerGlobalConstant.IsApplied() { store.AddGlobalConstants(NewGlobalConstant().Parse(data, registerGlobalConstant)) } - store.AddAccounts(®isterGlobalConstant.Source) + store.AddAccounts(registerGlobalConstant.Source) return nil } diff --git a/internal/parsers/operations/sr_execute_outbox_message.go b/internal/parsers/operations/sr_execute_outbox_message.go index 9003216c3..98db83b81 100644 --- a/internal/parsers/operations/sr_execute_outbox_message.go +++ b/internal/parsers/operations/sr_execute_outbox_message.go @@ -56,7 +56,7 @@ func (p SrExecuteOutboxMessage) Parse(ctx context.Context, data noderpc.Operatio OperationsCount: 1, LastAction: p.head.Timestamp, } - store.AddAccounts(&operation.Destination) + store.AddAccounts(operation.Destination) } p.fillInternal(&operation) operation.SetBurned(*p.protocol.Constants) @@ -78,7 +78,7 @@ func (p SrExecuteOutboxMessage) Parse(ctx context.Context, data noderpc.Operatio } store.AddOperations(&operation) - store.AddAccounts(&operation.Source) + store.AddAccounts(operation.Source) return nil } diff --git a/internal/parsers/operations/sr_originate.go b/internal/parsers/operations/sr_originate.go index c04bf48d8..8c80630ca 100644 --- a/internal/parsers/operations/sr_originate.go +++ b/internal/parsers/operations/sr_originate.go @@ -58,11 +58,11 @@ func (p SrOriginate) Parse(ctx context.Context, data noderpc.Operation, store pa return err } store.AddSmartRollups(&smartRollup) - store.AddAccounts(&smartRollup.Address) + store.AddAccounts(smartRollup.Address) operation.Destination = smartRollup.Address } - store.AddAccounts(&operation.Destination, &operation.Source) + store.AddAccounts(operation.Destination, operation.Source) return nil } diff --git a/internal/parsers/operations/test_common.go b/internal/parsers/operations/test_common.go index 467163980..1a5646fa7 100644 --- a/internal/parsers/operations/test_common.go +++ b/internal/parsers/operations/test_common.go @@ -146,6 +146,11 @@ func compareParserResponse(t *testing.T, got, want *parsers.TestStore) { for hash := range got.Tickets { require.Equal(t, want.Tickets[hash], got.Tickets[hash]) } + for key, wantAddress := range want.Accounts { + gotAddress, ok := got.Accounts[key] + require.True(t, ok) + require.Equal(t, wantAddress, gotAddress) + } } func compareAddress(t *testing.T, want, got account.Account) { diff --git a/internal/parsers/operations/ticket_updates.go b/internal/parsers/operations/ticket_updates.go index cbfc201ba..a49a9ad24 100644 --- a/internal/parsers/operations/ticket_updates.go +++ b/internal/parsers/operations/ticket_updates.go @@ -45,7 +45,7 @@ func (p *TicketUpdateParser) toModel(data noderpc.TicketUpdate, operation *opera UpdatesCount: 1, Level: operation.Level, } - store.AddTickets(&tckt) + store.AddTickets(tckt) updates := make([]*ticket.TicketUpdate, 0) for i := range data.Updates { @@ -62,8 +62,8 @@ func (p *TicketUpdateParser) toModel(data noderpc.TicketUpdate, operation *opera Amount: decimal.RequireFromString(data.Updates[i].Amount), } updates = append(updates, &update) - store.AddAccounts(&update.Account, &tckt.Ticketer) - store.AddTicketBalances(&ticket.Balance{ + store.AddAccounts(update.Account, tckt.Ticketer) + store.AddTicketBalances(ticket.Balance{ Account: update.Account, Ticket: tckt, Amount: update.Amount.Copy(), diff --git a/internal/parsers/operations/transaction.go b/internal/parsers/operations/transaction.go index 51c7c10d7..8a2dfc617 100644 --- a/internal/parsers/operations/transaction.go +++ b/internal/parsers/operations/transaction.go @@ -78,9 +78,9 @@ func (p Transaction) Parse(ctx context.Context, data noderpc.Operation, store pa store.AddOperations(&tx) store.AddAccounts( - &tx.Source, - &tx.Delegate, - &tx.Destination, + tx.Source, + tx.Delegate, + tx.Destination, ) switch tx.Destination.Type { diff --git a/internal/parsers/operations/transfer_ticket.go b/internal/parsers/operations/transfer_ticket.go index 856b321f4..d7c007f59 100644 --- a/internal/parsers/operations/transfer_ticket.go +++ b/internal/parsers/operations/transfer_ticket.go @@ -56,7 +56,7 @@ func (p TransferTicket) Parse(ctx context.Context, data noderpc.Operation, store OperationsCount: 1, LastAction: p.head.Timestamp, } - store.AddAccounts(&transferTicket.Destination) + store.AddAccounts(transferTicket.Destination) } p.fillInternal(&transferTicket) @@ -65,7 +65,7 @@ func (p TransferTicket) Parse(ctx context.Context, data noderpc.Operation, store p.stackTrace.Add(transferTicket) store.AddOperations(&transferTicket) - store.AddAccounts(&transferTicket.Source) + store.AddAccounts(transferTicket.Source) return nil } diff --git a/internal/parsers/operations/tx_rollup_origination.go b/internal/parsers/operations/tx_rollup_origination.go index b96c44b8e..8c90c0648 100644 --- a/internal/parsers/operations/tx_rollup_origination.go +++ b/internal/parsers/operations/tx_rollup_origination.go @@ -55,10 +55,10 @@ func (p TxRollupOrigination) Parse(ctx context.Context, data noderpc.Operation, p.stackTrace.Add(txRollupOrigination) store.AddOperations(&txRollupOrigination) - store.AddAccounts(&txRollupOrigination.Source) + store.AddAccounts(txRollupOrigination.Source) if txRollupOrigination.IsApplied() { - store.AddAccounts(&txRollupOrigination.Destination) + store.AddAccounts(txRollupOrigination.Destination) } return nil diff --git a/internal/parsers/store.go b/internal/parsers/store.go index 6bee34663..a074ef488 100644 --- a/internal/parsers/store.go +++ b/internal/parsers/store.go @@ -22,11 +22,11 @@ type Store interface { AddOperations(operations ...*operation.Operation) AddGlobalConstants(constants ...*contract.GlobalConstant) AddSmartRollups(rollups ...*smartrollup.SmartRollup) - AddTickets(tickets ...*ticket.Ticket) - AddTicketBalances(balances ...*ticket.Balance) + AddTickets(tickets ...ticket.Ticket) + AddTicketBalances(balances ...ticket.Balance) ListContracts() []*contract.Contract ListOperations() []*operation.Operation - AddAccounts(accounts ...*account.Account) + AddAccounts(accounts ...account.Account) Save(ctx context.Context) error SetBlock(block *block.Block) } @@ -90,12 +90,26 @@ func (store *TestStore) AddSmartRollups(rollups ...*smartrollup.SmartRollup) { store.SmartRollups = append(store.SmartRollups, rollups...) } +// AddAccounts - +func (store *TestStore) AddAccounts(accounts ...account.Account) { + for i := range accounts { + if account, ok := store.Accounts[accounts[i].Address]; !ok { + store.Accounts[accounts[i].Address] = &accounts[i] + } else { + account.OperationsCount += accounts[i].OperationsCount + account.EventsCount += accounts[i].EventsCount + account.MigrationsCount += accounts[i].MigrationsCount + account.TicketUpdatesCount += accounts[i].TicketUpdatesCount + } + } +} + // AddTickets - -func (store *TestStore) AddTickets(tickets ...*ticket.Ticket) { +func (store *TestStore) AddTickets(tickets ...ticket.Ticket) { for i := range tickets { hash := tickets[i].Hash() if t, ok := store.Tickets[hash]; !ok { - store.Tickets[hash] = tickets[i] + store.Tickets[hash] = &tickets[i] } else { t.UpdatesCount += tickets[i].UpdatesCount } @@ -103,11 +117,11 @@ func (store *TestStore) AddTickets(tickets ...*ticket.Ticket) { } // AddTicketBalances - -func (store *TestStore) AddTicketBalances(balance ...*ticket.Balance) { +func (store *TestStore) AddTicketBalances(balance ...ticket.Balance) { for i := range balance { key := fmt.Sprintf("%s_%s", balance[i].Ticket.Hash(), balance[i].Account.Address) if t, ok := store.TicketBalances[key]; !ok { - store.TicketBalances[key] = balance[i] + store.TicketBalances[key] = &balance[i] } else { t.Amount = t.Amount.Add(balance[i].Amount) } @@ -124,15 +138,6 @@ func (store *TestStore) ListOperations() []*operation.Operation { return store.Operations } -// AddAccounts - -func (store *TestStore) AddAccounts(accounts ...*account.Account) { - for i := range accounts { - if _, ok := store.Accounts[accounts[i].Address]; !ok { - store.Accounts[accounts[i].Address] = accounts[i] - } - } -} - // Save - func (store *TestStore) Save(ctx context.Context) error { return nil diff --git a/internal/postgres/store/store.go b/internal/postgres/store/store.go index 445a13c33..6a2918b78 100644 --- a/internal/postgres/store/store.go +++ b/internal/postgres/store/store.go @@ -118,10 +118,10 @@ func (store *Store) AddSmartRollups(rollups ...*smartrollup.SmartRollup) { } // AddAccounts - -func (store *Store) AddAccounts(accounts ...*account.Account) { +func (store *Store) AddAccounts(accounts ...account.Account) { for i := range accounts { if account, ok := store.Accounts[accounts[i].Address]; !ok { - store.Accounts[accounts[i].Address] = accounts[i] + store.Accounts[accounts[i].Address] = &accounts[i] } else { account.OperationsCount += accounts[i].OperationsCount account.EventsCount += accounts[i].EventsCount @@ -132,11 +132,11 @@ func (store *Store) AddAccounts(accounts ...*account.Account) { } // AddTickets - -func (store *Store) AddTickets(tickets ...*ticket.Ticket) { +func (store *Store) AddTickets(tickets ...ticket.Ticket) { for i := range tickets { hash := tickets[i].Hash() if t, ok := store.Tickets[hash]; !ok { - store.Tickets[hash] = tickets[i] + store.Tickets[hash] = &tickets[i] } else { t.UpdatesCount += tickets[i].UpdatesCount } @@ -144,11 +144,11 @@ func (store *Store) AddTickets(tickets ...*ticket.Ticket) { } // AddTicketBalances - -func (store *Store) AddTicketBalances(balance ...*ticket.Balance) { +func (store *Store) AddTicketBalances(balance ...ticket.Balance) { for i := range balance { key := fmt.Sprintf("%s_%s", balance[i].Ticket.Hash(), balance[i].Account.Address) if t, ok := store.TicketBalances[key]; !ok { - store.TicketBalances[key] = balance[i] + store.TicketBalances[key] = &balance[i] } else { t.Amount = t.Amount.Add(balance[i].Amount) }