-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into query-pending-nonces-by-chainID
- Loading branch information
Showing
48 changed files
with
1,710 additions
and
1,264 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package common | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"errors" | ||
"fmt" | ||
|
||
ethtypes "github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/rlp" | ||
) | ||
|
||
// NewEthereumHeader returns a new HeaderData containing an Ethereum header | ||
func NewEthereumHeader(header []byte) HeaderData { | ||
return HeaderData{ | ||
Data: &HeaderData_EthereumHeader{ | ||
EthereumHeader: header, | ||
}, | ||
} | ||
} | ||
|
||
// ParentHash extracts the parent hash from the header | ||
func (h HeaderData) ParentHash() ([]byte, error) { | ||
switch data := h.Data.(type) { | ||
case *HeaderData_EthereumHeader: | ||
var header ethtypes.Header | ||
if err := rlp.DecodeBytes(data.EthereumHeader, &header); err != nil { | ||
return nil, err | ||
} | ||
return header.ParentHash.Bytes(), nil | ||
default: | ||
return nil, errors.New("unrecognized header type") | ||
} | ||
} | ||
|
||
// Validate performs a basic validation of the HeaderData | ||
func (h HeaderData) Validate(blockHash []byte, height int64) error { | ||
switch data := h.Data.(type) { | ||
case *HeaderData_EthereumHeader: | ||
return validateEthereumHeader(data.EthereumHeader, blockHash, height) | ||
default: | ||
return errors.New("unrecognized header type") | ||
} | ||
} | ||
|
||
// validateEthereumHeader performs a basic validation of the Ethereum header | ||
func validateEthereumHeader(headerBytes []byte, blockHash []byte, height int64) error { | ||
// on ethereum the block header is ~538 bytes in RLP encoding | ||
if len(headerBytes) > 1024 { | ||
return fmt.Errorf("header too long (%d)", len(headerBytes)) | ||
} | ||
|
||
// RLP encoded block header | ||
var header ethtypes.Header | ||
if err := rlp.DecodeBytes(headerBytes, &header); err != nil { | ||
return fmt.Errorf("cannot decode RLP (%s)", err) | ||
} | ||
if err := header.SanityCheck(); err != nil { | ||
return fmt.Errorf("sanity check failed (%s)", err) | ||
} | ||
if bytes.Compare(blockHash, header.Hash().Bytes()) != 0 { | ||
return fmt.Errorf("tx hash mismatch (%s) vs (%s)", hex.EncodeToString(blockHash), header.Hash().Hex()) | ||
} | ||
if height != header.Number.Int64() { | ||
return fmt.Errorf("height mismatch (%d) vs (%d)", height, header.Number.Int64()) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package common | ||
|
||
import ( | ||
"errors" | ||
|
||
ethtypes "github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/rlp" | ||
"github.com/zeta-chain/zetacore/common/ethereum" | ||
) | ||
|
||
// ErrInvalidProof is a error type for invalid proofs embedding the underlying error | ||
type ErrInvalidProof struct { | ||
Err error | ||
} | ||
|
||
func NewErrInvalidProof(err error) ErrInvalidProof { | ||
return ErrInvalidProof{ | ||
Err: err, | ||
} | ||
} | ||
|
||
func (e ErrInvalidProof) Error() string { | ||
return e.Err.Error() | ||
} | ||
|
||
// IsErrorInvalidProof returns true if the error is an ErrInvalidProof | ||
func IsErrorInvalidProof(err error) bool { | ||
return errors.As(err, &ErrInvalidProof{}) | ||
} | ||
|
||
// NewEthereumProof returns a new Proof containing an Ethereum proof | ||
func NewEthereumProof(proof *ethereum.Proof) *Proof { | ||
return &Proof{ | ||
Proof: &Proof_EthereumProof{ | ||
EthereumProof: proof, | ||
}, | ||
} | ||
} | ||
|
||
// Verify verifies the proof against the header | ||
func (p Proof) Verify(headerData HeaderData, txIndex int) ([]byte, error) { | ||
switch proof := p.Proof.(type) { | ||
case *Proof_EthereumProof: | ||
ethHeaderBytes := headerData.GetEthereumHeader() | ||
if ethHeaderBytes == nil { | ||
return nil, errors.New("can't verify ethereum proof against non-ethereum header") | ||
} | ||
var ethHeader ethtypes.Header | ||
err := rlp.DecodeBytes(ethHeaderBytes, ðHeader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
val, err := proof.EthereumProof.Verify(ethHeader.TxHash, txIndex) | ||
if err != nil { | ||
return nil, NewErrInvalidProof(err) | ||
} | ||
return val, nil | ||
default: | ||
return nil, errors.New("unrecognized proof type") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package common_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/zeta-chain/zetacore/common" | ||
) | ||
|
||
func Test_IsErrorInvalidProof(t *testing.T) { | ||
require.False(t, common.IsErrorInvalidProof(nil)) | ||
require.False(t, common.IsErrorInvalidProof(errors.New("foo"))) | ||
require.True(t, common.IsErrorInvalidProof(common.NewErrInvalidProof(errors.New("foo")))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.