Skip to content

Commit

Permalink
Merge branch 'main' into merge-main-feat-scheduled-dec27
Browse files Browse the repository at this point in the history
  • Loading branch information
AdoAdoAdo committed Dec 27, 2021
2 parents d996401 + 9733e61 commit d2871da
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
18 changes: 9 additions & 9 deletions core/atomic/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ type Flag struct {
value uint32
}

// Set sets flag and returns its previous value
func (flag *Flag) Set() bool {
// SetReturningPrevious sets flag and returns its previous value
func (flag *Flag) SetReturningPrevious() bool {
previousValue := atomic.SwapUint32(&flag.value, 1)
return previousValue == 1
}

// Unset sets flag
func (flag *Flag) Unset() {
// Reset resets the flag, putting it in off position
func (flag *Flag) Reset() {
atomic.StoreUint32(&flag.value, 0)
}

Expand All @@ -24,11 +24,11 @@ func (flag *Flag) IsSet() bool {
return value == 1
}

// Toggle toggles the flag
func (flag *Flag) Toggle(set bool) {
if set {
flag.Set()
// SetValue sets the new value in the flag
func (flag *Flag) SetValue(newValue bool) {
if newValue {
_ = flag.SetReturningPrevious()
} else {
flag.Unset()
flag.Reset()
}
}
28 changes: 17 additions & 11 deletions core/atomic/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

func TestFlag_Set(t *testing.T) {
t.Parallel()

var flag Flag
var wg sync.WaitGroup

Expand All @@ -16,56 +18,60 @@ func TestFlag_Set(t *testing.T) {
wg.Add(2)

go func() {
flag.Set()
_ = flag.SetReturningPrevious()
wg.Done()
}()

go func() {
flag.Set()
_ = flag.SetReturningPrevious()
wg.Done()
}()

wg.Wait()
require.True(t, flag.IsSet())
}

func TestFlag_Unset(t *testing.T) {
func TestFlag_Reset(t *testing.T) {
t.Parallel()

var flag Flag
var wg sync.WaitGroup

flag.Set()
_ = flag.SetReturningPrevious()
require.True(t, flag.IsSet())

wg.Add(2)

go func() {
flag.Unset()
flag.Reset()
wg.Done()
}()

go func() {
flag.Unset()
flag.Reset()
wg.Done()
}()

wg.Wait()
require.False(t, flag.IsSet())
}

func TestFlag_Toggle(t *testing.T) {
func TestFlag_SetValue(t *testing.T) {
t.Parallel()

var flag Flag
var wg sync.WaitGroup

// First, Toggle(true)
wg.Add(2)

go func() {
flag.Toggle(true)
flag.SetValue(true)
wg.Done()
}()

go func() {
flag.Toggle(true)
flag.SetValue(true)
wg.Done()
}()

Expand All @@ -76,12 +82,12 @@ func TestFlag_Toggle(t *testing.T) {
wg.Add(2)

go func() {
flag.Toggle(false)
flag.SetValue(false)
wg.Done()
}()

go func() {
flag.Toggle(false)
flag.SetValue(false)
wg.Done()
}()

Expand Down
2 changes: 1 addition & 1 deletion data/indexer/dtos.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Pool struct {
Rewards map[string]data.TransactionHandler
Invalid map[string]data.TransactionHandler
Receipts map[string]data.TransactionHandler
Logs map[string]data.LogHandler
Logs []*data.LogData
}

// ValidatorRatingInfo is a structure containing validator rating information
Expand Down
7 changes: 7 additions & 0 deletions data/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package data

// LogData holds the data needed for indexing logs and events
type LogData struct {
LogHandler
TxHash string
}

0 comments on commit d2871da

Please sign in to comment.