-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split func to make test easy to add / add test Signed-off-by: Naohiro Yoshida <[email protected]>
- Loading branch information
Showing
13 changed files
with
523 additions
and
186 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package module | ||
|
||
import ( | ||
"context" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/rlp" | ||
"github.com/hyperledger-labs/yui-relayer/log" | ||
) | ||
|
||
type getHeaderFn func(context.Context, uint64) (*types.Header, error) | ||
|
||
func QueryVerifyingEthHeaders(fn getHeaderFn, height uint64, limit uint64) ([]*ETHHeader, error) { | ||
var ethHeaders []*ETHHeader | ||
for i := height; i+2 <= limit; i++ { | ||
targetBlock, targetETHHeader, _, err := queryETHHeader(fn, i) | ||
if err != nil { | ||
return nil, err | ||
} | ||
childBlock, childETHHeader, childVote, err := queryETHHeader(fn, i+1) | ||
if err != nil { | ||
return nil, err | ||
} | ||
_, grandChildETHHeader, grandChildVote, err := queryETHHeader(fn, i+2) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if childVote == nil || grandChildVote == nil || | ||
grandChildVote.Data.SourceNumber != targetBlock.Number.Uint64() || | ||
grandChildVote.Data.SourceNumber != childVote.Data.TargetNumber || | ||
grandChildVote.Data.TargetNumber != childBlock.Number.Uint64() { | ||
// Append to verify header sequence | ||
ethHeaders = append(ethHeaders, targetETHHeader) | ||
continue | ||
} | ||
return append(ethHeaders, targetETHHeader, childETHHeader, grandChildETHHeader), nil | ||
} | ||
log.GetLogger().Debug("Insufficient verifying headers to finalize", "height", height, "limit", limit) | ||
return nil, nil | ||
} | ||
|
||
func queryETHHeader(fn getHeaderFn, height uint64) (*types.Header, *ETHHeader, *VoteAttestation, error) { | ||
block, err := fn(context.TODO(), height) | ||
if err != nil { | ||
return nil, nil, nil, err | ||
} | ||
ethHeader, err := newETHHeader(block) | ||
if err != nil { | ||
return nil, nil, nil, err | ||
} | ||
vote, err := getVoteAttestationFromHeader(block) | ||
if err != nil { | ||
return nil, nil, nil, err | ||
} | ||
return block, ethHeader, vote, err | ||
} | ||
|
||
func newETHHeader(header *types.Header) (*ETHHeader, error) { | ||
rlpHeader, err := rlp.EncodeToBytes(header) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return ÐHeader{Header: rlpHeader}, 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,70 @@ | ||
package module | ||
|
||
import ( | ||
"context" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/hyperledger-labs/yui-relayer/log" | ||
"github.com/stretchr/testify/suite" | ||
"math/big" | ||
"testing" | ||
) | ||
|
||
type HeaderQueryTestSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func TestHeaderQueryTestSuite(t *testing.T) { | ||
suite.Run(t, new(HeaderQueryTestSuite)) | ||
} | ||
|
||
func (ts *HeaderQueryTestSuite) SetupTest() { | ||
} | ||
|
||
func (ts *HeaderQueryTestSuite) TestErrorQueryVerifyingEthHeaders() { | ||
ts.Require().NoError(log.InitLogger("INFO", "json", "stdout")) | ||
fn := func(ctx context.Context, height uint64) (*types.Header, error) { | ||
return &types.Header{ | ||
Number: big.NewInt(int64(height)), | ||
}, nil | ||
} | ||
|
||
// No finalized header found | ||
headers, err := QueryVerifyingEthHeaders(fn, 1, 10) | ||
ts.Require().NoError(err) | ||
ts.Require().Nil(headers) | ||
|
||
fn = func(ctx context.Context, height uint64) (*types.Header, error) { | ||
header := &types.Header{ | ||
Number: big.NewInt(int64(height)), | ||
} | ||
if height == 31835601 || height == 31835602 { | ||
header.Extra = common.Hex2Bytes("d88301020b846765746888676f312e32302e35856c696e7578000000b19df4a2f8b5831defffb860a44482b16993815ff4903016ce83ef788b455e2c80ba9976e8e55ac6591b9f9965234a0a2c579269bc5e09577977322d07d17bb8d657ac621a1abfadcb35b9c9d4713dbdd3d47fd3cc6dc2475c989aa224fecd083101049ef1adea2718b00e37f84c8401e5c5cfa0be938dfeafe5b932c2dcef0e2bebb1a05f31104a59b49d78b0b7746a483c14648401e5c5d0a03658f0bb6692995a9dd3b72a69ec6e8e1b9af4361718d8a275c2b92d26eeffc28027cb6d065d5a6d8749ca45a185add61b9ce470136898643170f8072513ca45f35d826f02cb2494f857beebdac9ec04196c8b30a65352ef155a28ac6a0057ff1601") | ||
} | ||
return header, nil | ||
} | ||
|
||
// No finalized header found ( invalid relation ) | ||
headers, err = QueryVerifyingEthHeaders(fn, 31835592, 31835602) | ||
ts.Require().NoError(err) | ||
ts.Require().Nil(headers) | ||
} | ||
|
||
func (ts *HeaderQueryTestSuite) TestSuccessQueryVerifyingEthHeaders() { | ||
ts.Require().NoError(log.InitLogger("INFO", "json", "stdout")) | ||
fn := func(ctx context.Context, height uint64) (*types.Header, error) { | ||
header := &types.Header{ | ||
Number: big.NewInt(int64(height)), | ||
} | ||
if height == 31835601 { | ||
header.Extra = common.Hex2Bytes("d88301020b846765746888676f312e32302e35856c696e7578000000b19df4a2f8b5831defffb860a44482b16993815ff4903016ce83ef788b455e2c80ba9976e8e55ac6591b9f9965234a0a2c579269bc5e09577977322d07d17bb8d657ac621a1abfadcb35b9c9d4713dbdd3d47fd3cc6dc2475c989aa224fecd083101049ef1adea2718b00e37f84c8401e5c5cfa0be938dfeafe5b932c2dcef0e2bebb1a05f31104a59b49d78b0b7746a483c14648401e5c5d0a03658f0bb6692995a9dd3b72a69ec6e8e1b9af4361718d8a275c2b92d26eeffc28027cb6d065d5a6d8749ca45a185add61b9ce470136898643170f8072513ca45f35d826f02cb2494f857beebdac9ec04196c8b30a65352ef155a28ac6a0057ff1601") | ||
} else if height == 31835602 { | ||
header.Extra = common.Hex2Bytes("d88301020b846765746888676f312e31392e38856c696e7578000000b19df4a2f8b5831defffb860a244628caa7b3002a245b677c419c5991d9ba62e7d298e96565b72f8ccc6587510f8827c00783d0a13326bfc72bbcbb90e6bdf988ef662b286158296e0f270f21568fdb75210f631d53b81e74f0fa9a5c591dc46cbeceb28952264d8863b7812f84c8401e5c5d0a03658f0bb6692995a9dd3b72a69ec6e8e1b9af4361718d8a275c2b92d26eeffc28401e5c5d1a06b3b459206a5b6b1963e686318b0261b9c0888e1a253f77d109c60c6734c84c28031c42276b8ebf15bb5b843865147ea9435be29a83afeae646fc156b45832e0016bb3fa7119db6fe5dfe5d99733b6f7dd38ac4d7aeb7882cd4b6c576faf6951a901") | ||
} | ||
return header, nil | ||
} | ||
|
||
headers, err := QueryVerifyingEthHeaders(fn, 31835592, 31835602) | ||
ts.Require().NoError(err) | ||
ts.Require().Len(headers, 11) | ||
} |
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
Oops, something went wrong.