diff --git a/core/constants.go b/core/constants.go index 829b8841d..492ffadc5 100644 --- a/core/constants.go +++ b/core/constants.go @@ -230,3 +230,48 @@ 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 +) + +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 +) 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) + } +} diff --git a/core/interface.go b/core/interface.go index cb9f4b9a4..01294e031 100644 --- a/core/interface.go +++ b/core/interface.go @@ -2,6 +2,8 @@ package core import ( "time" + + "github.com/ElrondNetwork/elrond-go-core/data" ) // AppStatusHandler interface will handle different implementations of monitoring tools, such as term-ui or status metrics @@ -126,3 +128,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() 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 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 +} 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 400093a38..0b4e773ec 100644 --- a/data/api/options.go +++ b/data/api/options.go @@ -1,9 +1,15 @@ package api +import "github.com/ElrondNetwork/elrond-go-core/core" + // AccountQueryOptions holds options for account queries type AccountQueryOptions struct { OnFinalBlock bool - OnStartOfEpoch uint32 + OnStartOfEpoch core.OptionalUint32 + BlockNonce core.OptionalUint64 + BlockHash []byte + BlockRootHash []byte + HintEpoch core.OptionalUint32 } // BlockQueryOptions holds options for block queries