Skip to content

Commit

Permalink
Add unit test (#22)
Browse files Browse the repository at this point in the history
split func to make test easy to add / add test

Signed-off-by: Naohiro Yoshida <[email protected]>
  • Loading branch information
yoshidan authored Nov 25, 2023
1 parent 962d9b7 commit 128c68f
Show file tree
Hide file tree
Showing 13 changed files with 523 additions and 186 deletions.
24 changes: 0 additions & 24 deletions module/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,3 @@ func (h *Header) Account(path common.Address) (*types.StateAccount, error) {
}
return verifyAccount(target, h.AccountProof, path)
}

func ExtractValidatorSet(h *types.Header) ([][]byte, error) {
extra := h.Extra
if len(extra) < extraVanity+extraSeal {
return nil, fmt.Errorf("invalid extra length : %d", h.Number.Uint64())
}
num := int(extra[extraVanity])
if num == 0 || len(extra) <= extraVanity+extraSeal+num*validatorBytesLength {
return nil, fmt.Errorf("invalid validator bytes length: %d", h.Number.Uint64())
}
start := extraVanity + validatorNumberSize
end := start + num*validatorBytesLength
validators := extra[start:end]

var validatorSet [][]byte
for i := 0; i < num; i++ {
s := validatorBytesLength * i
e := s + validatorBytesLength
validatorWithBLS := validators[s:e]
validatorSet = append(validatorSet, validatorWithBLS)
}

return validatorSet, nil
}
64 changes: 64 additions & 0 deletions module/header_query.go
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 &ETHHeader{Header: rlpHeader}, nil
}
70 changes: 70 additions & 0 deletions module/header_query_test.go
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)
}
18 changes: 0 additions & 18 deletions module/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,6 @@ func (ts *HeaderTestSuite) TestNewHeaderError() {
ts.Require().Error(header.ValidateBasic())
}

func (ts *HeaderTestSuite) TestExtractValidatorSet() {
testnetHeader := &types.Header{
// after luban in testnet:
Number: big.NewInt(29835600),
Extra: common.Hex2Bytes("d883010202846765746888676f312e31392e39856c696e7578000000110bea95071284214b9b9c85549ab3d2b972df0deef66ac2c9ab1757500d6f4fdee439b17cf8e43267f94bc759162fb68de676d2fe10cc4cde26dd06be7e345e9cbf4b1dbf86b262bc35552c16704d214347f29fa77f77da6d75d7c752b742ad4855bae330426b823e742da31f816cc83bc16d69a9134be0cfb4a1d17ec34f1b5b32d5c20440b8536b1e88f0f296c5d20b2a975c050e4220be276ace4892f4b41a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000980a75ecd1309ea12fa2ed87a8744fbfc9b863d589037a9ace3b590165ea1c0c5ac72bf600b7c88c1e435f41932c1132aae1bfa0bb68e46b96ccb12c3415e4d82af717d8a2959d3f95eae5dc7d70144ce1b73b403b7eb6e0b973c2d38487e58fd6e145491b110080fb14ac915a0411fc78f19e09a399ddee0d20c63a75d8f930f1694544ad2dc01bb71b214cb885500844365e95cd9942c7276e7fd8a2750ec6dded3dcdc2f351782310b0eadc077db59abca0f0cd26776e2e7acb9f3bce40b1fa5221fd1561226c6263cc5ff474cf03cceff28abc65c9cbae594f725c80e12d96c9b86c3400e529bfe184056e257c07940bb664636f689e8d2027c834681f8f878b73445261034e946bb2d901b4b878f8b27bb860a140cc9c8cc07d4ddf366440d9784efc88743d26af40f8956dd1c3501e560f745910bb14a5ec392f53cf78ddc2d2d69a146af287f7e079c3cbbfd3d446836d9b9397aa9a803b6c6b4f1cfc50baddbe2378cf194da35b9f4a1a32850114f1c5d9f84c8401c7414ea049d2e0876f51ce4693892331f8344a102aad88eb9e9bcfaa247cc9f898d1f8008401c7414fa0cf8d34727ff1d895bb49ca4be60c3b24d98d8afa9ce78644924e4b9aa39df8548022dc981e8703d3ca8b23fc032089667cb631cb28c32731762813bbf9fdb7e7a56b3945d65f2d72402a2abb9fbaf4bf094a3e5a542e175ecc54b426ee366b2ba200"),
}
validators, err := ExtractValidatorSet(testnetHeader)
ts.Require().NoError(err)
ts.Require().Len(validators, 7)
ts.Require().Equal(common.Bytes2Hex(validators[0]), "1284214b9b9c85549ab3d2b972df0deef66ac2c9ab1757500d6f4fdee439b17cf8e43267f94bc759162fb68de676d2fe10cc4cde26dd06be7e345e9cbf4b1dbf86b262bc")
ts.Require().Equal(common.Bytes2Hex(validators[1]), "35552c16704d214347f29fa77f77da6d75d7c752b742ad4855bae330426b823e742da31f816cc83bc16d69a9134be0cfb4a1d17ec34f1b5b32d5c20440b8536b1e88f0f2")
ts.Require().Equal(common.Bytes2Hex(validators[2]), "96c5d20b2a975c050e4220be276ace4892f4b41a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
ts.Require().Equal(common.Bytes2Hex(validators[3]), "980a75ecd1309ea12fa2ed87a8744fbfc9b863d589037a9ace3b590165ea1c0c5ac72bf600b7c88c1e435f41932c1132aae1bfa0bb68e46b96ccb12c3415e4d82af717d8")
ts.Require().Equal(common.Bytes2Hex(validators[4]), "a2959d3f95eae5dc7d70144ce1b73b403b7eb6e0b973c2d38487e58fd6e145491b110080fb14ac915a0411fc78f19e09a399ddee0d20c63a75d8f930f1694544ad2dc01b")
ts.Require().Equal(common.Bytes2Hex(validators[5]), "b71b214cb885500844365e95cd9942c7276e7fd8a2750ec6dded3dcdc2f351782310b0eadc077db59abca0f0cd26776e2e7acb9f3bce40b1fa5221fd1561226c6263cc5f")
ts.Require().Equal(common.Bytes2Hex(validators[6]), "f474cf03cceff28abc65c9cbae594f725c80e12d96c9b86c3400e529bfe184056e257c07940bb664636f689e8d2027c834681f8f878b73445261034e946bb2d901b4b878")
}

// see yui-ibc-solidity
func encodeRLP(proof []string) ([]byte, error) {
var target [][][]byte
Expand Down
Loading

0 comments on commit 128c68f

Please sign in to comment.