Skip to content

Commit

Permalink
Fix and optimize babylon migration
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Sep 29, 2023
1 parent ba37f66 commit 3c37305
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 76 deletions.
1 change: 0 additions & 1 deletion internal/models/bigmapdiff/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ type Repository interface {
GetForAddress(ctx context.Context, address string) ([]BigMapState, error)
Count(ctx context.Context, ptr int64) (int, error)
Current(ctx context.Context, keyHash string, ptr int64) (BigMapState, error)
CurrentByContract(ctx context.Context, contract string) ([]BigMapState, error)
Previous(ctx context.Context, diffs []BigMapDiff) ([]BigMapDiff, error)
GetStats(ctx context.Context, ptr int64) (Stats, error)
Keys(ctx context.Context, reqCtx GetContext) (states []BigMapState, err error)
Expand Down
2 changes: 1 addition & 1 deletion internal/models/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type Transaction interface {
ToJakarta(ctx context.Context) error
JakartaVesting(ctx context.Context, contract *contract.Contract) error
JakartaUpdateNonDelegator(ctx context.Context, contract *contract.Contract) error
BabylonBigMapStates(ctx context.Context, state *bigmapdiff.BigMapState) error
DeleteBigMapStatesByContract(ctx context.Context, contract string) ([]bigmapdiff.BigMapState, error)

Commit() error
Rollback() error
Expand Down
39 changes: 0 additions & 39 deletions internal/models/mock/bigmapdiff/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions internal/parsers/migrations/babylon.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (p *Babylon) getUpdates(ctx context.Context, script noderpc.Script, contrac
}
}

keys, err := p.bmdRepo.CurrentByContract(ctx, contract.Account.Address)
keys, err := tx.DeleteBigMapStatesByContract(ctx, contract.Account.Address)
if err != nil {
return err
}
Expand All @@ -128,7 +128,9 @@ func (p *Babylon) getUpdates(ctx context.Context, script noderpc.Script, contrac

for i := range keys {
keys[i].Ptr = newPtr
if err := tx.BabylonBigMapStates(ctx, &keys[i]); err != nil {
keys[i].ID = 0

if err := tx.BigMapStates(ctx, &keys[i]); err != nil {
return err
}
}
Expand Down
8 changes: 0 additions & 8 deletions internal/postgres/bigmapdiff/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,6 @@ func (storage *Storage) GetStats(ctx context.Context, ptr int64) (stats bigmapdi
return
}

// CurrentByContract -
func (storage *Storage) CurrentByContract(ctx context.Context, contract string) (keys []bigmapdiff.BigMapState, err error) {
err = storage.DB.NewSelect().Model(&keys).
Where("contract = ?", contract).
Scan(ctx)
return
}

// Keys -
func (storage *Storage) Keys(ctx context.Context, req bigmapdiff.GetContext) (states []bigmapdiff.BigMapState, err error) {
err = storage.buildGetContextForState(req).Scan(ctx, &states)
Expand Down
14 changes: 9 additions & 5 deletions internal/postgres/core/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,6 @@ func (t Transaction) ToJakarta(ctx context.Context) error {
return err
}

func (t Transaction) BabylonBigMapStates(ctx context.Context, state *bigmapdiff.BigMapState) error {
_, err := t.tx.NewUpdate().Model(state).WherePK().Column("ptr").Exec(ctx)
return err
}

func (t Transaction) UpdateStats(ctx context.Context, stats stats.Stats) error {
_, err := t.tx.NewInsert().
Model(&stats).
Expand Down Expand Up @@ -269,3 +264,12 @@ func (t Transaction) TicketBalances(ctx context.Context, balances ...*ticket.Bal
Exec(ctx)
return err
}

func (t Transaction) DeleteBigMapStatesByContract(ctx context.Context, contract string) (states []bigmapdiff.BigMapState, err error) {
_, err = t.tx.NewDelete().
Model((*bigmapdiff.BigMapState)(nil)).
Where("contract = ?", contract).
Returning("*").
Exec(ctx, &states)
return
}
9 changes: 0 additions & 9 deletions internal/postgres/tests/big_map_diffs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,6 @@ func (s *StorageTestSuite) TestBigMapDiffsGetStats() {
s.Require().EqualValues("KT1NSpRTVR4MUwx64XCADXDUmpMGQw5yVNK1", stats.Contract)
}

func (s *StorageTestSuite) TestBigMapDiffsCurrentByContract() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

diff, err := s.bigMapDiffs.CurrentByContract(ctx, "KT1NSpRTVR4MUwx64XCADXDUmpMGQw5yVNK1")
s.Require().NoError(err)
s.Require().Len(diff, 4)
}

func (s *StorageTestSuite) TestBigMapDiffsKeys() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
Expand Down
15 changes: 4 additions & 11 deletions internal/postgres/tests/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,28 +319,21 @@ func (s *StorageTestSuite) TestToJakarta() {
s.Require().EqualValues(14, newContract.JakartaID)
}

func (s *StorageTestSuite) TestBabylonBigMapStates() {
func (s *StorageTestSuite) TestDeleteBigMapStates() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

tx, err := core.NewTransaction(ctx, s.storage.DB)
s.Require().NoError(err)

err = tx.BabylonBigMapStates(ctx, &bigmapdiff.BigMapState{
ID: 3,
Ptr: 1000,
KeyHash: "expruDuAZnFKqmLoisJqUGqrNzXTvw7PJM2rYk97JErM5FHCerQqgn",
Contract: "KT1Pz65ssbPF7Zv9Dh7ggqUkgAYNSuJ9iia7",
})
states, err := tx.DeleteBigMapStatesByContract(ctx, "KT1Pz65ssbPF7Zv9Dh7ggqUkgAYNSuJ9iia7")
s.Require().NoError(err)

err = tx.Commit()
s.Require().NoError(err)
s.Require().Len(states, 9)

var state bigmapdiff.BigMapState
err = s.storage.DB.NewSelect().Model(&state).Where("id = 3").Scan(ctx)
s.Require().NoError(err)
s.Require().EqualValues(1000, state.Ptr)
s.Require().Equal("KT1Pz65ssbPF7Zv9Dh7ggqUkgAYNSuJ9iia7", states[0].Contract)
}

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

0 comments on commit 3c37305

Please sign in to comment.