Skip to content

Commit

Permalink
Fix: save big map states
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Sep 28, 2023
1 parent f2a8c45 commit d0bc5bc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
5 changes: 5 additions & 0 deletions internal/models/bigmapdiff/bigmapstate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bigmapdiff

import (
"fmt"
"time"

"github.com/baking-bad/bcdhub/internal/models/types"
Expand Down Expand Up @@ -43,6 +44,10 @@ func (b *BigMapState) LogFields() map[string]interface{} {
}
}

func (b BigMapState) String() string {
return fmt.Sprintf("%s_%s_%d", b.Contract, b.KeyHash, b.Ptr)
}

// ToDiff -
func (b *BigMapState) ToDiff() BigMapDiff {
bmd := BigMapDiff{
Expand Down
2 changes: 1 addition & 1 deletion internal/postgres/store/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (store *Store) Save(ctx context.Context) error {
return errors.Wrap(err, "saving migrations")
}

if err := tx.BigMapStates(ctx, store.BigMapState...); err != nil {
if err := tx.BigMapStates(ctx, store.bigMapStates()...); err != nil {
return errors.Wrap(err, "saving bigmap states")
}

Expand Down
23 changes: 20 additions & 3 deletions internal/postgres/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
// Store -
type Store struct {
Block *block.Block
BigMapState []*bigmapdiff.BigMapState
BigMapState map[string]*bigmapdiff.BigMapState
Contracts []*contract.Contract
Migrations []*migration.Migration
Operations []*operation.Operation
Expand All @@ -38,7 +38,7 @@ type Store struct {
// NewStore -
func NewStore(db *bun.DB, statsRepo stats.Repository) *Store {
return &Store{
BigMapState: make([]*bigmapdiff.BigMapState, 0),
BigMapState: make(map[string]*bigmapdiff.BigMapState),
Contracts: make([]*contract.Contract, 0),
Migrations: make([]*migration.Migration, 0),
Operations: make([]*operation.Operation, 0),
Expand All @@ -61,7 +61,24 @@ func (store *Store) SetBlock(block *block.Block) {

// AddBigMapStates -
func (store *Store) AddBigMapStates(states ...*bigmapdiff.BigMapState) {
store.BigMapState = append(store.BigMapState, states...)
for i := range states {
key := states[i].String()
if state, ok := store.BigMapState[key]; ok {
state.Count += 1
state.Value = states[i].Value
state.Removed = states[i].Removed
} else {
store.BigMapState[key] = states[i]
}
}
}

func (store *Store) bigMapStates() []*bigmapdiff.BigMapState {
arr := make([]*bigmapdiff.BigMapState, 0, len(store.BigMapState))
for _, state := range store.BigMapState {
arr = append(arr, state)
}
return arr
}

// AddContracts -
Expand Down

0 comments on commit d0bc5bc

Please sign in to comment.