Skip to content

Commit

Permalink
stores: improve perf in UpdateStateElements
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjan committed Apr 19, 2024
1 parent 4305ba2 commit 9820426
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
4 changes: 2 additions & 2 deletions chain/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ func (s *Subscriber) Run() (func(), error) {
s.logger.Errorf("failed to get chain index: %v", err)
continue
}
err = s.sync(ci)
if err != nil && !errors.Is(err, errClosed) {

if err := s.sync(ci); err != nil && !errors.Is(err, errClosed) {
s.logger.Errorf("failed to sync: %v", err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/test/e2e/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,9 +742,9 @@ func (c *TestCluster) AddHost(h *Host) {
c.tt.Retry(10, time.Second, func() error {
c.tt.Helper()

c.MineBlocks(1)
_, err = c.Bus.Host(context.Background(), h.PublicKey())
if err != nil {
c.MineBlocks(1)
return err
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/test/e2e/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
// TestNewTestCluster is a test for creating a cluster of Nodes for testing,
// making sure that it forms contracts, renews contracts and shuts down.
func TestNewTestCluster(t *testing.T) {
cluster := newTestCluster(t, testClusterOptions{logger: newTestLoggerCustom(zapcore.WarnLevel)})
cluster := newTestCluster(t, testClusterOptions{logger: newTestLoggerCustom(zapcore.ErrorLevel)})
defer cluster.Shutdown()
b := cluster.Bus
tt := cluster.tt
Expand Down
29 changes: 20 additions & 9 deletions stores/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,18 +329,29 @@ func (u *chainUpdateTx) UpdateHost(hk types.PublicKey, ha chain.HostAnnouncement
// UpdateStateElements updates the proofs of all state elements affected by the
// update.
func (u *chainUpdateTx) UpdateStateElements(elements []types.StateElement) error {
if len(elements) == 0 {
return nil
}

var entities []dbWalletOutput
if err := u.tx.Model(&dbWalletOutput{}).Find(&entities).Error; err != nil {
return err
} else if len(entities) == 0 {
return nil
}

indices := make(map[types.Hash256]int)
for i, e := range entities {
indices[types.Hash256(e.OutputID)] = i
}

for _, se := range elements {
if err := u.tx.
Model(&dbWalletOutput{}).
Where("output_id", hash256(se.ID)).
Updates(map[string]interface{}{
"merkle_proof": merkleProof{proof: se.MerkleProof},
"leaf_index": se.LeafIndex,
}).Error; err != nil {
return err
if index, ok := indices[se.ID]; ok {
entities[index].LeafIndex = se.LeafIndex
entities[index].MerkleProof = merkleProof{proof: se.MerkleProof}
}
}
return nil
return u.tx.Save(&entities).Error
}

// WalletStateElements implements the ChainStore interface and returns all state
Expand Down

0 comments on commit 9820426

Please sign in to comment.