Skip to content

Commit

Permalink
export methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Faulty Tolly committed Sep 20, 2024
1 parent 835dcae commit f88b299
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 64 deletions.
87 changes: 63 additions & 24 deletions types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"errors"
"fmt"
"time"

"github.com/dymensionxyz/gerr-cosmos/gerrc"
)
Expand All @@ -18,80 +19,118 @@ var (
ErrEmptyProposerAddress = errors.New("no proposer address")
)

// TimeFraudMaxDrift is the maximum allowed time drift between the block time and the local time.
var TimeFraudMaxDrift = 10 * time.Minute

type ErrFraudHeightMismatch struct {
expected uint64
actual uint64
Expected uint64
Actual uint64

headerHeight uint64
headerHash [32]byte
proposer []byte
HeaderHeight uint64
HeaderHash [32]byte
Proposer []byte
}

// NewErrFraudHeightMismatch creates a new ErrFraudHeightMismatch error.
func NewErrFraudHeightMismatch(expected uint64, actual uint64, block *Block) error {
return &ErrFraudHeightMismatch{
expected: expected, actual: actual,
headerHeight: block.Header.Height, headerHash: block.Header.Hash(), proposer: block.Header.ProposerAddress,
Expected: expected, Actual: actual,
HeaderHeight: block.Header.Height, HeaderHash: block.Header.Hash(), Proposer: block.Header.ProposerAddress,
}
}

func (e ErrFraudHeightMismatch) Error() string {
return fmt.Sprintf("possible fraud detected on height %d, with header hash %X, emitted by sequencer %X:"+
" height mismatch: state expected %d, got %d", e.headerHeight, e.headerHash, e.proposer, e.expected, e.actual)
" height mismatch: state expected %d, got %d", e.HeaderHeight, e.HeaderHash, e.Proposer, e.Expected, e.Actual)
}

func (e ErrFraudHeightMismatch) Unwrap() error {
return gerrc.ErrFault
}

type ErrFraudAppHashMismatch struct {
expected [32]byte
Expected [32]byte

headerHeight uint64
headerHash [32]byte
proposer []byte
HeaderHeight uint64
HeaderHash [32]byte
Proposer []byte
}

// NewErrFraudAppHashMismatch creates a new ErrFraudAppHashMismatch error.
func NewErrFraudAppHashMismatch(expected [32]byte, actual [32]byte, block *Block) error {
return &ErrFraudAppHashMismatch{
expected: expected,
headerHeight: block.Header.Height, headerHash: block.Header.Hash(), proposer: block.Header.ProposerAddress,
Expected: expected,
HeaderHeight: block.Header.Height, HeaderHash: block.Header.Hash(), Proposer: block.Header.ProposerAddress,
}
}

func (e ErrFraudAppHashMismatch) Error() string {
return fmt.Sprintf("possible fraud detected on height %d, with header hash %X, emitted by sequencer %X:"+
" AppHash mismatch: state expected %X, got %X", e.headerHeight, e.headerHash, e.proposer, e.expected, e.headerHash)
" AppHash mismatch: state expected %X, got %X", e.HeaderHeight, e.HeaderHash, e.Proposer, e.Expected, e.HeaderHash)
}

func (e ErrFraudAppHashMismatch) Unwrap() error {
return gerrc.ErrFault
}

type ErrLastResultsHashMismatch struct {
expected [32]byte
Expected [32]byte

headerHeight uint64
headerHash [32]byte
proposer []byte
lastResultHash [32]byte
HeaderHeight uint64
HeaderHash [32]byte
Proposer []byte
LastResultHash [32]byte
}

// NewErrLastResultsHashMismatch creates a new ErrLastResultsHashMismatch error.
func NewErrLastResultsHashMismatch(expected [32]byte, block *Block) error {
return &ErrLastResultsHashMismatch{
expected: expected,
headerHeight: block.Header.Height, headerHash: block.Header.Hash(), proposer: block.Header.ProposerAddress,
lastResultHash: block.Header.LastResultsHash,
Expected: expected,
HeaderHeight: block.Header.Height, HeaderHash: block.Header.Hash(), Proposer: block.Header.ProposerAddress,
LastResultHash: block.Header.LastResultsHash,
}
}

func (e ErrLastResultsHashMismatch) Error() string {
return fmt.Sprintf("possible fraud detected on height %d, with header hash %X, emitted by sequencer %X:"+
" LastResultsHash mismatch: state expected %X, got %X", e.headerHeight, e.headerHash, e.proposer, e.expected, e.lastResultHash)
" LastResultsHash mismatch: state expected %X, got %X", e.HeaderHeight, e.HeaderHash, e.Proposer, e.Expected, e.LastResultHash)
}

func (e ErrLastResultsHashMismatch) Unwrap() error {
return gerrc.ErrFault
}

type ErrTimeFraud struct {
Drift time.Duration
ProposerAddress []byte
HeaderHash [32]byte
HeaderHeight uint64
HeaderTime time.Time
CurrentTime time.Time
}

func NewErrTimeFraud(block *Block, currentTime time.Time) error {
drift := time.Unix(int64(block.Header.Time), 0).Sub(currentTime)

return ErrTimeFraud{
Drift: drift,
ProposerAddress: block.Header.ProposerAddress,
HeaderHash: block.Header.Hash(),
HeaderHeight: block.Header.Height,
HeaderTime: time.Unix(int64(block.Header.Time), 0),
CurrentTime: currentTime,
}
}

func (e ErrTimeFraud) Error() string {
return fmt.Sprintf(
"Sequencer posted a block with invalid time. "+
"Max allowed drift exceeded. "+
"proposerAddress=%s headerHash=%s headerHeight=%d drift=%s MaxDrift=%s headerTime=%s currentTime=%s",
e.ProposerAddress, e.HeaderHash, e.HeaderHeight, e.Drift, TimeFraudMaxDrift, e.HeaderTime, e.CurrentTime,
)
}

func (e ErrTimeFraud) Unwrap() error {
return gerrc.ErrFault
}
40 changes: 0 additions & 40 deletions types/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,10 @@ import (
"time"


"github.com/dymensionxyz/gerr-cosmos/gerrc"

tmcrypto "github.com/tendermint/tendermint/crypto"
tmtypes "github.com/tendermint/tendermint/types"
)

// TimeFraudMaxDrift is the maximum allowed time drift between the block time and the local time.
var TimeFraudMaxDrift = 10 * time.Minute

type ErrTimeFraud struct {
drift time.Duration
proposerAddress []byte
headerHash [32]byte
headerHeight uint64
headerTime time.Time
currentTime time.Time
}

func NewErrTimeFraud(block *Block, currentTime time.Time) error {
drift := time.Unix(int64(block.Header.Time), 0).Sub(currentTime)

return ErrTimeFraud{
drift: drift,
proposerAddress: block.Header.ProposerAddress,
headerHash: block.Header.Hash(),
headerHeight: block.Header.Height,
headerTime: time.Unix(int64(block.Header.Time), 0),
currentTime: currentTime,
}
}

func (e ErrTimeFraud) Error() string {
return fmt.Sprintf(
"Sequencer posted a block with invalid time. "+
"Max allowed drift exceeded. "+
"proposerAddress=%s headerHash=%s headerHeight=%d drift=%s MaxDrift=%s headerTime=%s currentTime=%s",
e.proposerAddress, e.headerHash, e.headerHeight, e.drift, TimeFraudMaxDrift, e.headerTime, e.currentTime,
)
}

func (e ErrTimeFraud) Unwrap() error {
return gerrc.ErrFault
}

func ValidateProposedTransition(state *State, block *Block, commit *Commit, proposerPubKey tmcrypto.PubKey) error {
if err := block.ValidateWithState(state); err != nil {
return fmt.Errorf("block: %w", err)
Expand Down

0 comments on commit f88b299

Please sign in to comment.