From dbb014994962a6d40a95d16afe531e7d6d6be103 Mon Sep 17 00:00:00 2001 From: bogdan-rosianu Date: Tue, 21 Dec 2021 11:54:45 +0200 Subject: [PATCH 1/7] added log data struct --- data/indexer/dtos.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/data/indexer/dtos.go b/data/indexer/dtos.go index 38388378f..19c721b33 100644 --- a/data/indexer/dtos.go +++ b/data/indexer/dtos.go @@ -50,3 +50,9 @@ type RoundInfo struct { Epoch uint32 Timestamp time.Duration } + +// LogData holds the data needed for indexing logs and events +type LogData struct { + data.LogHandler + TxHash string +} From be885e9df5642223979c8c609db98b0dc2ef6eaf Mon Sep 17 00:00:00 2001 From: bogdan-rosianu Date: Tue, 21 Dec 2021 12:27:09 +0200 Subject: [PATCH 2/7] changed pool's logs type --- data/indexer/dtos.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/indexer/dtos.go b/data/indexer/dtos.go index 19c721b33..6c5d30b78 100644 --- a/data/indexer/dtos.go +++ b/data/indexer/dtos.go @@ -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 []LogData } // ValidatorRatingInfo is a structure containing validator rating information From 48ed29656e9e850cba597215c7fc96d54ffa5781 Mon Sep 17 00:00:00 2001 From: jules00 Date: Wed, 22 Dec 2021 08:48:59 +0200 Subject: [PATCH 3/7] - renamed 2 functions in the Flag implementation --- core/atomic/flag.go | 12 ++++++------ core/atomic/flag_test.go | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/core/atomic/flag.go b/core/atomic/flag.go index f31ec4c19..d8903f5a6 100644 --- a/core/atomic/flag.go +++ b/core/atomic/flag.go @@ -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) } @@ -27,8 +27,8 @@ func (flag *Flag) IsSet() bool { // Toggle toggles the flag func (flag *Flag) Toggle(set bool) { if set { - flag.Set() + _ = flag.SetReturningPrevious() } else { - flag.Unset() + flag.Reset() } } diff --git a/core/atomic/flag_test.go b/core/atomic/flag_test.go index ec4a4212c..276066444 100644 --- a/core/atomic/flag_test.go +++ b/core/atomic/flag_test.go @@ -16,12 +16,12 @@ 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() }() @@ -29,22 +29,22 @@ func TestFlag_Set(t *testing.T) { require.True(t, flag.IsSet()) } -func TestFlag_Unset(t *testing.T) { +func TestFlag_Reset(t *testing.T) { 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() }() From 7a7b23c5a5f1487a0c7c52e5ea5ec759569a9cf7 Mon Sep 17 00:00:00 2001 From: jules00 Date: Wed, 22 Dec 2021 10:33:25 +0200 Subject: [PATCH 4/7] - more refactoring work --- core/atomic/flag.go | 42 +++++++++++++++++++++++++++------------- core/atomic/flag_test.go | 39 ++++++++++++++++++++++++++++++++----- 2 files changed, 63 insertions(+), 18 deletions(-) diff --git a/core/atomic/flag.go b/core/atomic/flag.go index d8903f5a6..5d05449ca 100644 --- a/core/atomic/flag.go +++ b/core/atomic/flag.go @@ -1,34 +1,50 @@ package atomic -import "sync/atomic" +import ( + "sync" +) // Flag is an atomic flag type Flag struct { - value uint32 + mut sync.RWMutex + value bool } // SetReturningPrevious sets flag and returns its previous value func (flag *Flag) SetReturningPrevious() bool { - previousValue := atomic.SwapUint32(&flag.value, 1) - return previousValue == 1 + flag.mut.Lock() + previousValue := flag.value + flag.value = true + flag.mut.Unlock() + + return previousValue } // Reset resets the flag, putting it in off position func (flag *Flag) Reset() { - atomic.StoreUint32(&flag.value, 0) + flag.mut.Lock() + flag.value = false + flag.mut.Unlock() } // IsSet checks whether flag is set func (flag *Flag) IsSet() bool { - value := atomic.LoadUint32(&flag.value) - return value == 1 + flag.mut.RLock() + defer flag.mut.RUnlock() + + return flag.value } // Toggle toggles the flag -func (flag *Flag) Toggle(set bool) { - if set { - _ = flag.SetReturningPrevious() - } else { - flag.Reset() - } +func (flag *Flag) Toggle() { + flag.mut.Lock() + flag.value = !flag.value + flag.mut.Unlock() +} + +// SetValue sets the new value in the flag +func (flag *Flag) SetValue(newValue bool) { + flag.mut.Lock() + flag.value = newValue + flag.mut.Unlock() } diff --git a/core/atomic/flag_test.go b/core/atomic/flag_test.go index 276066444..6573c89e9 100644 --- a/core/atomic/flag_test.go +++ b/core/atomic/flag_test.go @@ -1,6 +1,7 @@ package atomic import ( + "github.com/stretchr/testify/assert" "sync" "testing" @@ -8,6 +9,8 @@ import ( ) func TestFlag_Set(t *testing.T) { + t.Parallel() + var flag Flag var wg sync.WaitGroup @@ -30,6 +33,8 @@ func TestFlag_Set(t *testing.T) { } func TestFlag_Reset(t *testing.T) { + t.Parallel() + var flag Flag var wg sync.WaitGroup @@ -52,7 +57,9 @@ func TestFlag_Reset(t *testing.T) { 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 @@ -60,12 +67,12 @@ func TestFlag_Toggle(t *testing.T) { wg.Add(2) go func() { - flag.Toggle(true) + flag.SetValue(true) wg.Done() }() go func() { - flag.Toggle(true) + flag.SetValue(true) wg.Done() }() @@ -76,15 +83,37 @@ 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() }() wg.Wait() require.False(t, flag.IsSet()) } + +func TestFlag_Toggle(t *testing.T) { + t.Parallel() + + f := Flag{} + f.SetValue(true) + + numToggles := 51 + wg := sync.WaitGroup{} + wg.Add(numToggles) + for i := 0; i < numToggles; i++ { + go func() { + f.Toggle() + + wg.Done() + }() + } + + wg.Wait() + + assert.False(t, f.IsSet()) +} From ad4e72f6a2efd86c4691cca6786e83ae9740c965 Mon Sep 17 00:00:00 2001 From: bogdan-rosianu Date: Wed, 22 Dec 2021 10:44:47 +0200 Subject: [PATCH 5/7] switch to pointer slice --- data/indexer/dtos.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/indexer/dtos.go b/data/indexer/dtos.go index d57de0509..88c95e059 100644 --- a/data/indexer/dtos.go +++ b/data/indexer/dtos.go @@ -32,7 +32,7 @@ type Pool struct { Rewards map[string]data.TransactionHandler Invalid map[string]data.TransactionHandler Receipts map[string]data.TransactionHandler - Logs []LogData + Logs []*LogData } // ValidatorRatingInfo is a structure containing validator rating information From 577add3bc0ccc218f6ba254bfdc78a2de641e644 Mon Sep 17 00:00:00 2001 From: jules00 Date: Wed, 22 Dec 2021 13:00:21 +0200 Subject: [PATCH 6/7] - removed mutex operation, removed Toggle function as it is un-implementable using atomic operations on an uint32 value --- core/atomic/flag.go | 40 ++++++++++++---------------------------- core/atomic/flag_test.go | 23 ----------------------- 2 files changed, 12 insertions(+), 51 deletions(-) diff --git a/core/atomic/flag.go b/core/atomic/flag.go index 5d05449ca..daca6d4fe 100644 --- a/core/atomic/flag.go +++ b/core/atomic/flag.go @@ -1,50 +1,34 @@ package atomic -import ( - "sync" -) +import "sync/atomic" // Flag is an atomic flag type Flag struct { - mut sync.RWMutex - value bool + value uint32 } // SetReturningPrevious sets flag and returns its previous value func (flag *Flag) SetReturningPrevious() bool { - flag.mut.Lock() - previousValue := flag.value - flag.value = true - flag.mut.Unlock() - - return previousValue + previousValue := atomic.SwapUint32(&flag.value, 1) + return previousValue == 1 } // Reset resets the flag, putting it in off position func (flag *Flag) Reset() { - flag.mut.Lock() - flag.value = false - flag.mut.Unlock() + atomic.StoreUint32(&flag.value, 0) } // IsSet checks whether flag is set func (flag *Flag) IsSet() bool { - flag.mut.RLock() - defer flag.mut.RUnlock() - - return flag.value -} - -// Toggle toggles the flag -func (flag *Flag) Toggle() { - flag.mut.Lock() - flag.value = !flag.value - flag.mut.Unlock() + value := atomic.LoadUint32(&flag.value) + return value == 1 } // SetValue sets the new value in the flag func (flag *Flag) SetValue(newValue bool) { - flag.mut.Lock() - flag.value = newValue - flag.mut.Unlock() + if newValue { + _ = flag.SetReturningPrevious() + } else { + flag.Reset() + } } diff --git a/core/atomic/flag_test.go b/core/atomic/flag_test.go index 6573c89e9..1b216e5df 100644 --- a/core/atomic/flag_test.go +++ b/core/atomic/flag_test.go @@ -1,7 +1,6 @@ package atomic import ( - "github.com/stretchr/testify/assert" "sync" "testing" @@ -95,25 +94,3 @@ func TestFlag_SetValue(t *testing.T) { wg.Wait() require.False(t, flag.IsSet()) } - -func TestFlag_Toggle(t *testing.T) { - t.Parallel() - - f := Flag{} - f.SetValue(true) - - numToggles := 51 - wg := sync.WaitGroup{} - wg.Add(numToggles) - for i := 0; i < numToggles; i++ { - go func() { - f.Toggle() - - wg.Done() - }() - } - - wg.Wait() - - assert.False(t, f.IsSet()) -} From 7f3f1ad307cdd9a66ae51caedd3e619215e66731 Mon Sep 17 00:00:00 2001 From: bogdan-rosianu Date: Wed, 22 Dec 2021 13:32:09 +0200 Subject: [PATCH 7/7] move LogData struct --- data/indexer/dtos.go | 8 +------- data/types.go | 7 +++++++ 2 files changed, 8 insertions(+), 7 deletions(-) create mode 100644 data/types.go diff --git a/data/indexer/dtos.go b/data/indexer/dtos.go index 88c95e059..a9337fc60 100644 --- a/data/indexer/dtos.go +++ b/data/indexer/dtos.go @@ -32,7 +32,7 @@ type Pool struct { Rewards map[string]data.TransactionHandler Invalid map[string]data.TransactionHandler Receipts map[string]data.TransactionHandler - Logs []*LogData + Logs []*data.LogData } // ValidatorRatingInfo is a structure containing validator rating information @@ -50,9 +50,3 @@ type RoundInfo struct { Epoch uint32 Timestamp time.Duration } - -// LogData holds the data needed for indexing logs and events -type LogData struct { - data.LogHandler - TxHash string -} diff --git a/data/types.go b/data/types.go new file mode 100644 index 000000000..52a6b0d7e --- /dev/null +++ b/data/types.go @@ -0,0 +1,7 @@ +package data + +// LogData holds the data needed for indexing logs and events +type LogData struct { + LogHandler + TxHash string +}