Skip to content
Draft
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
45 changes: 45 additions & 0 deletions core/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
17 changes: 17 additions & 0 deletions core/context.go
Original file line number Diff line number Diff line change
@@ -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
}
}
46 changes: 46 additions & 0 deletions core/context_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
49 changes: 49 additions & 0 deletions core/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mixed feelings about declaring this interface here. Also, for the Iterceptor and EpochStartActionHandler as they do not quite define the core business logic of elrond-go.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will remain as it is, for now

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
}
13 changes: 13 additions & 0 deletions core/optionals.go
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 7 additions & 1 deletion data/api/options.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down