Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Access] Add increment method for monotonous counter #5814

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions access/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1118,10 +1118,7 @@ func (h *Handler) SendAndSubscribeTransactionStatuses(
messageIndex := counters.NewMonotonousCounter(0)
return subscription.HandleSubscription(sub, func(txResults []*TransactionResult) error {
for i := range txResults {
value := messageIndex.Value()
if ok := messageIndex.Set(value + 1); !ok {
return status.Errorf(codes.Internal, "the message index has already been incremented to %d", messageIndex.Value())
}
value := messageIndex.Increment()

err = stream.Send(&access.SendAndSubscribeTransactionStatusesResponse{
TransactionResults: TransactionResultToMessage(txResults[i]),
Expand Down
10 changes: 2 additions & 8 deletions engine/access/state_stream/backend/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,7 @@ func (h *Handler) handleEventsResponse(send sendSubscribeEventsResponseFunc, hea
return status.Errorf(codes.Internal, "could not convert events to entity: %v", err)
}

index := messageIndex.Value()
if ok := messageIndex.Set(index + 1); !ok {
return status.Errorf(codes.Internal, "message index already incremented to %d", messageIndex.Value())
}
index := messageIndex.Increment()

err = send(&executiondata.SubscribeEventsResponse{
BlockHeight: resp.Height,
Expand Down Expand Up @@ -498,10 +495,7 @@ func (h *Handler) handleAccountStatusesResponse(
return err
}

index := messageIndex.Value()
if ok := messageIndex.Set(index + 1); !ok {
return status.Errorf(codes.Internal, "message index already incremented to %d", messageIndex.Value())
}
index := messageIndex.Increment()

err = send(&executiondata.SubscribeAccountStatusesResponse{
BlockId: convert.IdentifierToMessage(resp.BlockID),
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ require (
github.com/onflow/flow-core-contracts/lib/go/contracts v1.0.0
github.com/onflow/flow-core-contracts/lib/go/templates v1.0.0
github.com/onflow/flow-go-sdk v1.0.0-preview.26
github.com/onflow/flow/protobuf/go/flow v0.4.1
github.com/onflow/flow/protobuf/go/flow v0.4.3
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
github.com/pierrec/lz4 v2.6.1+incompatible
github.com/pkg/errors v0.9.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2180,8 +2180,8 @@ github.com/onflow/flow-nft/lib/go/contracts v1.2.0/go.mod h1:2gpbza+uzs1k7x31hkp
github.com/onflow/flow-nft/lib/go/templates v1.2.0 h1:JSQyh9rg0RC+D1930BiRXN8lrtMs+ubVMK6aQPon6Yc=
github.com/onflow/flow-nft/lib/go/templates v1.2.0/go.mod h1:p+2hRvtjLUR3MW1NsoJe5Gqgr2eeH49QB6+s6ze00w0=
github.com/onflow/flow/protobuf/go/flow v0.3.2-0.20231121210617-52ee94b830c2/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
github.com/onflow/flow/protobuf/go/flow v0.4.1 h1:mJ0kn5ptPvBbqiwpSz1fUs2jR3kTvVsZgLjTTqODSDo=
github.com/onflow/flow/protobuf/go/flow v0.4.1/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
github.com/onflow/flow/protobuf/go/flow v0.4.3 h1:gdY7Ftto8dtU+0wI+6ZgW4oE+z0DSDUMIDwVx8mqae8=
github.com/onflow/flow/protobuf/go/flow v0.4.3/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
github.com/onflow/go-ethereum v1.13.4 h1:iNO86fm8RbBbhZ87ZulblInqCdHnAQVY8okBrNsTevc=
github.com/onflow/go-ethereum v1.13.4/go.mod h1:cE/gEUkAffhwbVmMJYz+t1dAfVNHNwZCgc3BWtZxBGY=
github.com/onflow/sdks v0.5.1-0.20230912225508-b35402f12bba h1:rIehuhO6bj4FkwE4VzwEjX7MoAlOhUJENBJLqDqVxAo=
Expand Down
2 changes: 1 addition & 1 deletion insecure/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ require (
github.com/onflow/flow-go-sdk v1.0.0-preview.26 // indirect
github.com/onflow/flow-nft/lib/go/contracts v1.2.0 // indirect
github.com/onflow/flow-nft/lib/go/templates v1.2.0 // indirect
github.com/onflow/flow/protobuf/go/flow v0.4.1 // indirect
github.com/onflow/flow/protobuf/go/flow v0.4.3 // indirect
github.com/onflow/go-ethereum v1.13.4 // indirect
github.com/onflow/sdks v0.5.1-0.20230912225508-b35402f12bba // indirect
github.com/onflow/wal v1.0.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions insecure/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2164,8 +2164,8 @@ github.com/onflow/flow-nft/lib/go/contracts v1.2.0/go.mod h1:2gpbza+uzs1k7x31hkp
github.com/onflow/flow-nft/lib/go/templates v1.2.0 h1:JSQyh9rg0RC+D1930BiRXN8lrtMs+ubVMK6aQPon6Yc=
github.com/onflow/flow-nft/lib/go/templates v1.2.0/go.mod h1:p+2hRvtjLUR3MW1NsoJe5Gqgr2eeH49QB6+s6ze00w0=
github.com/onflow/flow/protobuf/go/flow v0.3.2-0.20231121210617-52ee94b830c2/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
github.com/onflow/flow/protobuf/go/flow v0.4.1 h1:mJ0kn5ptPvBbqiwpSz1fUs2jR3kTvVsZgLjTTqODSDo=
github.com/onflow/flow/protobuf/go/flow v0.4.1/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
github.com/onflow/flow/protobuf/go/flow v0.4.3 h1:gdY7Ftto8dtU+0wI+6ZgW4oE+z0DSDUMIDwVx8mqae8=
github.com/onflow/flow/protobuf/go/flow v0.4.3/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
github.com/onflow/go-ethereum v1.13.4 h1:iNO86fm8RbBbhZ87ZulblInqCdHnAQVY8okBrNsTevc=
github.com/onflow/go-ethereum v1.13.4/go.mod h1:cE/gEUkAffhwbVmMJYz+t1dAfVNHNwZCgc3BWtZxBGY=
github.com/onflow/sdks v0.5.1-0.20230912225508-b35402f12bba h1:rIehuhO6bj4FkwE4VzwEjX7MoAlOhUJENBJLqDqVxAo=
Expand Down
2 changes: 1 addition & 1 deletion integration/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/onflow/flow-go v0.34.0-crescendo-preview.10-staged-contracts-3.0.20240501031941-53ca114d9c37
github.com/onflow/flow-go-sdk v1.0.0-preview.26
github.com/onflow/flow-go/insecure v0.0.0-00010101000000-000000000000
github.com/onflow/flow/protobuf/go/flow v0.4.1
github.com/onflow/flow/protobuf/go/flow v0.4.3
github.com/onflow/go-ethereum v1.13.4
github.com/plus3it/gorecurcopy v0.0.1
github.com/prometheus/client_golang v1.18.0
Expand Down
4 changes: 2 additions & 2 deletions integration/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2160,8 +2160,8 @@ github.com/onflow/flow-nft/lib/go/contracts v1.2.0/go.mod h1:2gpbza+uzs1k7x31hkp
github.com/onflow/flow-nft/lib/go/templates v1.2.0 h1:JSQyh9rg0RC+D1930BiRXN8lrtMs+ubVMK6aQPon6Yc=
github.com/onflow/flow-nft/lib/go/templates v1.2.0/go.mod h1:p+2hRvtjLUR3MW1NsoJe5Gqgr2eeH49QB6+s6ze00w0=
github.com/onflow/flow/protobuf/go/flow v0.3.2-0.20231121210617-52ee94b830c2/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
github.com/onflow/flow/protobuf/go/flow v0.4.1 h1:mJ0kn5ptPvBbqiwpSz1fUs2jR3kTvVsZgLjTTqODSDo=
github.com/onflow/flow/protobuf/go/flow v0.4.1/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
github.com/onflow/flow/protobuf/go/flow v0.4.3 h1:gdY7Ftto8dtU+0wI+6ZgW4oE+z0DSDUMIDwVx8mqae8=
github.com/onflow/flow/protobuf/go/flow v0.4.3/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
github.com/onflow/go-ethereum v1.13.4 h1:iNO86fm8RbBbhZ87ZulblInqCdHnAQVY8okBrNsTevc=
github.com/onflow/go-ethereum v1.13.4/go.mod h1:cE/gEUkAffhwbVmMJYz+t1dAfVNHNwZCgc3BWtZxBGY=
github.com/onflow/sdks v0.5.1-0.20230912225508-b35402f12bba h1:rIehuhO6bj4FkwE4VzwEjX7MoAlOhUJENBJLqDqVxAo=
Expand Down
2 changes: 1 addition & 1 deletion integration/tests/access/cohort1/access_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func (s *AccessAPISuite) TestSendAndSubscribeTransactionStatuses() {
})
s.Require().NoError(err)

expectedCounter := uint64(0)
expectedCounter := uint64(1)
lastReportedTxStatus := entities.TransactionStatus_UNKNOWN
var txID sdk.Identifier

Expand Down
11 changes: 11 additions & 0 deletions module/counters/monotonous_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,14 @@ func (c *StrictMonotonousCounter) Set(newValue uint64) bool {
func (c *StrictMonotonousCounter) Value() uint64 {
return atomic.LoadUint64(&c.atomicCounter)
}

// Increment atomically increments counter and returns updated value
func (c *StrictMonotonousCounter) Increment() uint64 {
illia-malachyn marked this conversation as resolved.
Show resolved Hide resolved
for {
oldValue := c.Value()
newValue := oldValue + 1
if atomic.CompareAndSwapUint64(&c.atomicCounter, oldValue, newValue) {
return newValue
}
}
}
18 changes: 18 additions & 0 deletions module/counters/monotonous_counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ func TestSet(t *testing.T) {
require.Equal(t, uint64(4), counter.Value())
}

func TestIncrement(t *testing.T) {
illia-malachyn marked this conversation as resolved.
Show resolved Hide resolved
illia-malachyn marked this conversation as resolved.
Show resolved Hide resolved
counter := NewMonotonousCounter(1)
require.Equal(t, uint64(2), counter.Increment())
require.Equal(t, uint64(3), counter.Increment())
}

// TestIncrementConcurrently tests that the MonotonousCounter's Increment method
// works correctly when called concurrently from multiple goroutines
func TestIncrementConcurrently(t *testing.T) {
illia-malachyn marked this conversation as resolved.
Show resolved Hide resolved
counter := NewMonotonousCounter(0)

unittest.Concurrently(100, func(i int) {
counter.Increment()
})

require.Equal(t, uint64(100), counter.Value())
}

func TestFuzzy(t *testing.T) {
counter := NewMonotonousCounter(3)
require.True(t, counter.Set(4))
Expand Down
Loading