Skip to content

Commit

Permalink
Fix: operations count for account
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Oct 1, 2023
1 parent 590135c commit 11d1dc6
Show file tree
Hide file tree
Showing 19 changed files with 76 additions and 50 deletions.
2 changes: 1 addition & 1 deletion internal/parsers/contract/alpha.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion internal/parsers/contract/babylon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion internal/parsers/contract/hangzhou.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion internal/parsers/contract/jakarta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion internal/parsers/migrations/implicit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/parsers/migrations/vesting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion internal/parsers/operations/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
19 changes: 18 additions & 1 deletion internal/parsers/operations/operation_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions internal/parsers/operations/origination.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/parsers/operations/register_global_constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(&registerGlobalConstant.Source)
store.AddAccounts(registerGlobalConstant.Source)
return nil
}
4 changes: 2 additions & 2 deletions internal/parsers/operations/sr_execute_outbox_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions internal/parsers/operations/sr_originate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions internal/parsers/operations/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions internal/parsers/operations/ticket_updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(),
Expand Down
6 changes: 3 additions & 3 deletions internal/parsers/operations/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions internal/parsers/operations/transfer_ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions internal/parsers/operations/tx_rollup_origination.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 21 additions & 16 deletions internal/parsers/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -90,24 +90,38 @@ 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
}
}
}

// 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)
}
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions internal/postgres/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -132,23 +132,23 @@ 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
}
}
}

// 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)
}
Expand Down

0 comments on commit 11d1dc6

Please sign in to comment.