2022-01-25 18:47:39 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import "github.com/pkg/errors"
|
|
|
|
|
|
|
|
var (
|
2022-05-22 17:48:54 +00:00
|
|
|
// ErrInvalidPayload is returned when the payload is invalid
|
2022-07-09 20:51:03 +00:00
|
|
|
ErrInvalidPayload = invalidBlock{error: errors.New("received an INVALID payload from execution engine")}
|
2022-07-06 00:22:12 +00:00
|
|
|
// ErrInvalidBlockHashPayloadStatus is returned when the payload has invalid block hash.
|
2022-07-09 20:51:03 +00:00
|
|
|
ErrInvalidBlockHashPayloadStatus = invalidBlock{error: errors.New("received an INVALID_BLOCK_HASH payload from execution engine")}
|
2022-05-22 17:48:54 +00:00
|
|
|
// ErrUndefinedExecutionEngineError is returned when the execution engine returns an error that is not defined
|
2022-12-15 07:46:17 +00:00
|
|
|
ErrUndefinedExecutionEngineError = errors.New("received an undefined execution engine error")
|
2022-01-25 18:47:39 +00:00
|
|
|
// errNilFinalizedInStore is returned when a nil finalized checkpt is returned from store.
|
|
|
|
errNilFinalizedInStore = errors.New("nil finalized checkpoint returned from store")
|
2022-05-22 18:37:01 +00:00
|
|
|
// errNilFinalizedCheckpoint is returned when a nil finalized checkpt is returned from a state.
|
|
|
|
errNilFinalizedCheckpoint = errors.New("nil finalized checkpoint returned from state")
|
|
|
|
// errNilJustifiedCheckpoint is returned when a nil justified checkpt is returned from a state.
|
2022-10-26 20:44:24 +00:00
|
|
|
errNilJustifiedCheckpoint = errors.New("nil justified checkpoint returned from state")
|
2023-03-30 14:09:36 +00:00
|
|
|
// errBlockDoesNotExist is returned when a block does not exist for a particular state summary.
|
|
|
|
errBlockDoesNotExist = errors.New("could not find block in DB")
|
2022-05-22 17:48:54 +00:00
|
|
|
// errBlockNotFoundInCacheOrDB is returned when a block is not found in the cache or DB.
|
|
|
|
errBlockNotFoundInCacheOrDB = errors.New("block not found in cache or db")
|
|
|
|
// errWSBlockNotFound is returned when a block is not found in the WS cache or DB.
|
|
|
|
errWSBlockNotFound = errors.New("weak subjectivity root not found in db")
|
|
|
|
// errWSBlockNotFoundInEpoch is returned when a block is not found in the WS cache or DB within epoch.
|
|
|
|
errWSBlockNotFoundInEpoch = errors.New("weak subjectivity root not found in db within epoch")
|
2023-05-12 16:55:33 +00:00
|
|
|
// ErrNotDescendantOfFinalized is returned when a block is not a descendant of the finalized checkpoint
|
2023-02-25 16:39:13 +00:00
|
|
|
ErrNotDescendantOfFinalized = invalidBlock{error: errors.New("not descendant of finalized checkpoint")}
|
2023-05-12 16:55:33 +00:00
|
|
|
// ErrNotCheckpoint is returned when a given checkpoint is not a
|
|
|
|
// checkpoint in any chain known to forkchoice
|
|
|
|
ErrNotCheckpoint = errors.New("not a checkpoint in forkchoice")
|
2022-01-25 18:47:39 +00:00
|
|
|
)
|
2022-05-20 17:05:39 +00:00
|
|
|
|
2023-11-21 18:44:38 +00:00
|
|
|
var errMaxBlobsExceeded = errors.New("Expected commitments in block exceeds MAX_BLOBS_PER_BLOCK")
|
|
|
|
|
2022-05-20 17:05:39 +00:00
|
|
|
// An invalid block is the block that fails state transition based on the core protocol rules.
|
|
|
|
// The beacon node shall not be accepting nor building blocks that branch off from an invalid block.
|
|
|
|
// Some examples of invalid blocks are:
|
|
|
|
// The block violates state transition rules.
|
|
|
|
// The block is deemed invalid according to execution layer client.
|
|
|
|
// The block violates certain fork choice rules (before finalized slot, not finalized ancestor)
|
|
|
|
type invalidBlock struct {
|
2022-07-25 13:45:03 +00:00
|
|
|
invalidAncestorRoots [][32]byte
|
2022-05-20 17:05:39 +00:00
|
|
|
error
|
2023-06-28 13:38:24 +00:00
|
|
|
root [32]byte
|
|
|
|
lastValidHash [32]byte
|
2022-05-20 17:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type invalidBlockError interface {
|
|
|
|
Error() string
|
2022-07-25 13:45:03 +00:00
|
|
|
InvalidAncestorRoots() [][32]byte
|
2022-07-09 20:51:03 +00:00
|
|
|
BlockRoot() [32]byte
|
2023-06-28 13:38:24 +00:00
|
|
|
LastValidHash() [32]byte
|
2022-05-20 17:05:39 +00:00
|
|
|
}
|
|
|
|
|
2022-07-09 20:51:03 +00:00
|
|
|
// BlockRoot returns the invalid block root.
|
|
|
|
func (e invalidBlock) BlockRoot() [32]byte {
|
|
|
|
return e.root
|
2022-05-20 17:05:39 +00:00
|
|
|
}
|
|
|
|
|
2023-06-28 13:38:24 +00:00
|
|
|
// LastValidHash returns the last valid hash root.
|
|
|
|
func (e invalidBlock) LastValidHash() [32]byte {
|
|
|
|
return e.lastValidHash
|
|
|
|
}
|
|
|
|
|
2022-07-25 13:45:03 +00:00
|
|
|
// InvalidAncestorRoots returns an optional list of invalid roots of the invalid block which leads up last valid root.
|
|
|
|
func (e invalidBlock) InvalidAncestorRoots() [][32]byte {
|
|
|
|
return e.invalidAncestorRoots
|
|
|
|
}
|
|
|
|
|
2022-05-20 17:05:39 +00:00
|
|
|
// IsInvalidBlock returns true if the error has `invalidBlock`.
|
|
|
|
func IsInvalidBlock(e error) bool {
|
|
|
|
if e == nil {
|
|
|
|
return false
|
|
|
|
}
|
2023-10-04 18:16:35 +00:00
|
|
|
var d invalidBlockError
|
|
|
|
return errors.As(e, &d)
|
2022-07-09 20:51:03 +00:00
|
|
|
}
|
|
|
|
|
2023-06-28 13:38:24 +00:00
|
|
|
// InvalidBlockLVH returns the invalid block last valid hash root. If the error
|
|
|
|
// doesn't have a last valid hash, [32]byte{} is returned.
|
|
|
|
func InvalidBlockLVH(e error) [32]byte {
|
|
|
|
if e == nil {
|
|
|
|
return [32]byte{}
|
|
|
|
}
|
2023-10-04 18:16:35 +00:00
|
|
|
var d invalidBlockError
|
|
|
|
ok := errors.As(e, &d)
|
2023-06-28 13:38:24 +00:00
|
|
|
if !ok {
|
|
|
|
return [32]byte{}
|
|
|
|
}
|
|
|
|
return d.LastValidHash()
|
|
|
|
}
|
|
|
|
|
2022-07-09 20:51:03 +00:00
|
|
|
// InvalidBlockRoot returns the invalid block root. If the error
|
|
|
|
// doesn't have an invalid blockroot. [32]byte{} is returned.
|
|
|
|
func InvalidBlockRoot(e error) [32]byte {
|
|
|
|
if e == nil {
|
|
|
|
return [32]byte{}
|
|
|
|
}
|
2023-10-04 18:16:35 +00:00
|
|
|
var d invalidBlockError
|
|
|
|
ok := errors.As(e, &d)
|
2022-07-09 20:51:03 +00:00
|
|
|
if !ok {
|
|
|
|
return [32]byte{}
|
|
|
|
}
|
|
|
|
return d.BlockRoot()
|
2022-05-20 17:05:39 +00:00
|
|
|
}
|
2022-07-25 13:45:03 +00:00
|
|
|
|
|
|
|
// InvalidAncestorRoots returns a list of invalid roots up to last valid root.
|
|
|
|
func InvalidAncestorRoots(e error) [][32]byte {
|
|
|
|
if e == nil {
|
|
|
|
return [][32]byte{}
|
|
|
|
}
|
2023-10-04 18:16:35 +00:00
|
|
|
var d invalidBlockError
|
|
|
|
ok := errors.As(e, &d)
|
2022-07-25 13:45:03 +00:00
|
|
|
if !ok {
|
|
|
|
return [][32]byte{}
|
|
|
|
}
|
|
|
|
return d.InvalidAncestorRoots()
|
|
|
|
}
|