From c463adcdc4a6b843ed2cb56e5f410862937c6960 Mon Sep 17 00:00:00 2001 From: ssd04 Date: Wed, 10 Aug 2022 08:19:06 +0300 Subject: [PATCH 01/10] - added interfaces for p2p.Message, validator, epochStart ActionHandler - constants for block processing state --- core/constants.go | 16 +++++++++++++++ core/interface.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/core/constants.go b/core/constants.go index 829b8841d..925cdf95f 100644 --- a/core/constants.go +++ b/core/constants.go @@ -230,3 +230,19 @@ const SCDeployIdentifier = "SCDeploy" // SCUpgradeIdentifier is the identifier for a smart contract upgrade const SCUpgradeIdentifier = "SCUpgrade" + +// BlockHeaderState specifies which is the state of the block header received +type BlockHeaderState int + +const ( + // BHReceived defines ID of a received block header + BHReceived BlockHeaderState = iota + // BHReceivedTooLate defines ID of a late received block header + BHReceivedTooLate + // BHProcessed defines ID of a processed block header + BHProcessed + // BHProposed defines ID of a proposed block header + BHProposed + // BHNotarized defines ID of a notarized block header + BHNotarized +) diff --git a/core/interface.go b/core/interface.go index cb9f4b9a4..2dbf9c031 100644 --- a/core/interface.go +++ b/core/interface.go @@ -2,6 +2,9 @@ package core import ( "time" + + "github.com/ElrondNetwork/elrond-go-core/data" + core "github.com/libp2p/go-libp2p-core" ) // AppStatusHandler interface will handle different implementations of monitoring tools, such as term-ui or status metrics @@ -126,3 +129,50 @@ type Logger interface { LogIfError(err error, args ...interface{}) IsInterfaceNil() bool } + +// MessageP2P defines what a p2p message can do (should return) +type MessageP2P interface { + From() []byte + Data() []byte + Payload() []byte + SeqNo() []byte + Topic() string + Signature() []byte + Key() []byte + Peer() core.PeerID + Timestamp() int64 + IsInterfaceNil() bool +} + +// InterceptedDebugger defines an interface for debugging the intercepted data +type InterceptedDebugger interface { + LogReceivedHashes(topic string, hashes [][]byte) + LogProcessedHashes(topic string, hashes [][]byte, err error) + IsInterfaceNil() bool +} + +// Interceptor defines what a data interceptor should do +// It should also adhere to the p2p.MessageProcessor interface so it can wire to a p2p.Messenger +type Interceptor interface { + ProcessReceivedMessage(message MessageP2P, fromConnectedPeer core.PeerID) error + SetInterceptedDebugHandler(handler InterceptedDebugger) error + RegisterHandler(handler func(topic string, hash []byte, data interface{})) + Close() error + IsInterfaceNil() bool +} + +// Validator defines a node that can be allocated to a shard for participation in a consensus group as validator +// or block proposer +type Validator interface { + PubKey() []byte + Chances() uint32 + Index() uint32 + Size() int +} + +// EpochStartActionHandler defines the action taken on epoch start event +type EpochStartActionHandler interface { + EpochStartAction(hdr data.HeaderHandler) + EpochStartPrepare(metaHdr data.HeaderHandler, body data.BodyHandler) + NotifyOrder() uint32 +} From 3432869044ab6fafb77d8ba5fc3790ed1b48b4bb Mon Sep 17 00:00:00 2001 From: ssd04 Date: Wed, 10 Aug 2022 08:31:45 +0300 Subject: [PATCH 02/10] fix core peer id reference --- core/interface.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/core/interface.go b/core/interface.go index 2dbf9c031..01294e031 100644 --- a/core/interface.go +++ b/core/interface.go @@ -4,7 +4,6 @@ import ( "time" "github.com/ElrondNetwork/elrond-go-core/data" - core "github.com/libp2p/go-libp2p-core" ) // AppStatusHandler interface will handle different implementations of monitoring tools, such as term-ui or status metrics @@ -139,7 +138,7 @@ type MessageP2P interface { Topic() string Signature() []byte Key() []byte - Peer() core.PeerID + Peer() PeerID Timestamp() int64 IsInterfaceNil() bool } @@ -154,7 +153,7 @@ type InterceptedDebugger interface { // Interceptor defines what a data interceptor should do // It should also adhere to the p2p.MessageProcessor interface so it can wire to a p2p.Messenger type Interceptor interface { - ProcessReceivedMessage(message MessageP2P, fromConnectedPeer core.PeerID) error + ProcessReceivedMessage(message MessageP2P, fromConnectedPeer PeerID) error SetInterceptedDebugHandler(handler InterceptedDebugger) error RegisterHandler(handler func(topic string, hash []byte, data interface{})) Close() error From d2813ba870c34949478efcc4d638018e9573ccd9 Mon Sep 17 00:00:00 2001 From: ssd04 Date: Thu, 11 Aug 2022 14:27:19 +0300 Subject: [PATCH 03/10] added constants: - start of epoch notify orders - node state --- core/constants.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/core/constants.go b/core/constants.go index 925cdf95f..492ffadc5 100644 --- a/core/constants.go +++ b/core/constants.go @@ -246,3 +246,32 @@ const ( // BHNotarized defines ID of a notarized block header BHNotarized ) + +const ( + // StorerOrder defines the order of storers to be notified of a start of epoch event + StorerOrder = iota + // NodesCoordinatorOrder defines the order in which NodesCoordinator is notified of a start of epoch event + NodesCoordinatorOrder + // ConsensusOrder defines the order in which Consensus is notified of a start of epoch event + ConsensusOrder + // NetworkShardingOrder defines the order in which the network sharding subsystem is notified of a start of epoch event + NetworkShardingOrder + // IndexerOrder defines the order in which indexer is notified of a start of epoch event + IndexerOrder + // NetStatisticsOrder defines the order in which netStatistic component is notified of a start of epoch event + NetStatisticsOrder + // OldDatabaseCleanOrder defines the order in which oldDatabaseCleaner component is notified of a start of epoch event + OldDatabaseCleanOrder +) + +// NodeState specifies what type of state a node could have +type NodeState int + +const ( + // NsSynchronized defines ID of a state of synchronized + NsSynchronized NodeState = iota + // NsNotSynchronized defines ID of a state of not synchronized + NsNotSynchronized + // NsNotCalculated defines ID of a state which is not calculated + NsNotCalculated +) From a3b12c31affc5dbce10b18fc3d48ea06523b35af Mon Sep 17 00:00:00 2001 From: ssd04 Date: Thu, 11 Aug 2022 15:16:59 +0300 Subject: [PATCH 04/10] move IsContextDone from elrond-go to core --- core/context.go | 17 ++++++++++++++++ core/context_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 core/context.go create mode 100644 core/context_test.go diff --git a/core/context.go b/core/context.go new file mode 100644 index 000000000..2eb3c6447 --- /dev/null +++ b/core/context.go @@ -0,0 +1,17 @@ +package core + +import "context" + +// IsContextDone will return true if the provided context signals it is done +func IsContextDone(ctx context.Context) bool { + if ctx == nil { + return true + } + + select { + case <-ctx.Done(): + return true + default: + return false + } +} diff --git a/core/context_test.go b/core/context_test.go new file mode 100644 index 000000000..723b0554e --- /dev/null +++ b/core/context_test.go @@ -0,0 +1,46 @@ +package core + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" +) + +// nolint +func TestIsContextDone(t *testing.T) { + t.Parallel() + + t.Run("valid context tests", func(t *testing.T) { + numTests := 10 + ctx, cancel := context.WithCancel(context.Background()) + for i := 0; i < numTests; i++ { + assert.False(t, IsContextDone(ctx)) + } + cancel() + for i := 0; i < numTests; i++ { + assert.True(t, IsContextDone(ctx)) + } + }) + t.Run("nil context", func(t *testing.T) { + assert.True(t, IsContextDone(nil)) + }) +} + +func BenchmarkIsContextDone_WhenDone(b *testing.B) { + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + for i := 0; i < b.N; i++ { + _ = IsContextDone(ctx) + } +} + +func BenchmarkIsContextDone_WhenNotDone(b *testing.B) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + for i := 0; i < b.N; i++ { + _ = IsContextDone(ctx) + } +} From eedf4fa888a5b4bf8e275d97f43a2ae194f17dc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Thu, 11 Aug 2022 18:34:59 +0300 Subject: [PATCH 05/10] New fields on AccountQueryOptions. --- data/api/options.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/data/api/options.go b/data/api/options.go index 400093a38..f18c6a55a 100644 --- a/data/api/options.go +++ b/data/api/options.go @@ -4,6 +4,8 @@ package api type AccountQueryOptions struct { OnFinalBlock bool OnStartOfEpoch uint32 + OnBlockNonce uint64 + OnBlockHash string } // BlockQueryOptions holds options for block queries From 3ab58f631112d35732e867eb5d6f2ef561ba5ffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Fri, 12 Aug 2022 13:37:43 +0300 Subject: [PATCH 06/10] Adjust AccountQueryOptions. --- data/api/options.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/data/api/options.go b/data/api/options.go index f18c6a55a..626220f18 100644 --- a/data/api/options.go +++ b/data/api/options.go @@ -4,8 +4,9 @@ package api type AccountQueryOptions struct { OnFinalBlock bool OnStartOfEpoch uint32 - OnBlockNonce uint64 - OnBlockHash string + BlockNonce uint64 + BlockHash string + BlockRootHash string } // BlockQueryOptions holds options for block queries From be24d296caaa03d333641a3d7166835a29d6ab16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Sat, 13 Aug 2022 17:16:39 +0300 Subject: [PATCH 07/10] Handle zero-values, as well. --- data/api/options.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data/api/options.go b/data/api/options.go index 626220f18..0316d52cf 100644 --- a/data/api/options.go +++ b/data/api/options.go @@ -3,8 +3,8 @@ package api // AccountQueryOptions holds options for account queries type AccountQueryOptions struct { OnFinalBlock bool - OnStartOfEpoch uint32 - BlockNonce uint64 + OnStartOfEpoch *uint32 + BlockNonce *uint64 BlockHash string BlockRootHash string } From d498e11dd0399bebd998a88720d180fba0fff3be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Sat, 13 Aug 2022 17:27:50 +0300 Subject: [PATCH 08/10] Define optional values as structs. --- core/optionals.go | 13 +++++++++++++ data/api/options.go | 6 ++++-- 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 core/optionals.go diff --git a/core/optionals.go b/core/optionals.go new file mode 100644 index 000000000..d2af955e9 --- /dev/null +++ b/core/optionals.go @@ -0,0 +1,13 @@ +package core + +// OptionalUint32 holds an optional uint32 value +type OptionalUint32 struct { + Value uint32 + HasValue bool +} + +// OptionalUint64 holds an optional uint64 value +type OptionalUint64 struct { + Value uint64 + HasValue bool +} diff --git a/data/api/options.go b/data/api/options.go index 0316d52cf..d69001e85 100644 --- a/data/api/options.go +++ b/data/api/options.go @@ -1,10 +1,12 @@ package api +import "github.com/ElrondNetwork/elrond-go-core/core" + // AccountQueryOptions holds options for account queries type AccountQueryOptions struct { OnFinalBlock bool - OnStartOfEpoch *uint32 - BlockNonce *uint64 + OnStartOfEpoch core.OptionalUint32 + BlockNonce core.OptionalUint64 BlockHash string BlockRootHash string } From a7d8c4aeaa8797eecf41a3939e81af23c15758c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Sat, 13 Aug 2022 22:37:23 +0300 Subject: [PATCH 09/10] Fix type of fields. --- data/api/options.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data/api/options.go b/data/api/options.go index d69001e85..a38f13485 100644 --- a/data/api/options.go +++ b/data/api/options.go @@ -7,8 +7,8 @@ type AccountQueryOptions struct { OnFinalBlock bool OnStartOfEpoch core.OptionalUint32 BlockNonce core.OptionalUint64 - BlockHash string - BlockRootHash string + BlockHash []byte + BlockRootHash []byte } // BlockQueryOptions holds options for block queries From 375b415dcf7a9f02587716e56c57967673018d80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Sun, 21 Aug 2022 20:57:42 +0300 Subject: [PATCH 10/10] On AccountQueryOptions, add epoch hint. --- data/api/options.go | 1 + 1 file changed, 1 insertion(+) diff --git a/data/api/options.go b/data/api/options.go index a38f13485..0b4e773ec 100644 --- a/data/api/options.go +++ b/data/api/options.go @@ -9,6 +9,7 @@ type AccountQueryOptions struct { BlockNonce core.OptionalUint64 BlockHash []byte BlockRootHash []byte + HintEpoch core.OptionalUint32 } // BlockQueryOptions holds options for block queries