Skip to content

Commit

Permalink
use fast finalized headers
Browse files Browse the repository at this point in the history
Signed-off-by: yoshidan <[email protected]>
  • Loading branch information
yoshidan committed Aug 31, 2023
1 parent c143d97 commit 7c61f75
Show file tree
Hide file tree
Showing 10 changed files with 602 additions and 779 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ func main() {
go build -tags dev -ldflags="-X github.com/datachainlab/ibc-parlia-relay/module/constant.blocksPerEpoch=20" -o testrly .
```

## Change luban fork

* You can change block for luban fork
```
go build -o testrly .
BSC_LUBAN_FORK=1000000 ./testrly
```

## Development

Generate proto buf with protobuf definition of [lcp-parlia](https://github.com/datachainlab/lcp-parlia).
Expand Down
23 changes: 0 additions & 23 deletions module/env/env.go

This file was deleted.

57 changes: 23 additions & 34 deletions module/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v7/modules/core/exported"
"github.com/datachainlab/ibc-parlia-relay/module/env"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
Expand All @@ -25,7 +24,7 @@ func (*Header) ClientType() string {
}

func (h *Header) GetHeight() exported.Height {
target, err := h.Target()
target, err := h.DecodedTarget()
if err != nil {
log.Panicf("invalid header: %v", h)
}
Expand All @@ -34,7 +33,10 @@ func (h *Header) GetHeight() exported.Height {
}

func (h *Header) ValidateBasic() error {
if _, err := h.Target(); err != nil {
if _, err := h.DecodedTarget(); err != nil {
return err
}
if _, err := h.DecodedParent(); err != nil {
return err
}
if _, err := decodeAccountProof(h.AccountProof); err != nil {
Expand All @@ -43,31 +45,24 @@ func (h *Header) ValidateBasic() error {
return nil
}

func (h *Header) decodeEthHeaders() ([]*types.Header, error) {
ethHeaders := make([]*types.Header, len(h.Headers))
for i, e := range h.Headers {
var ethHeader types.Header
if err := rlp.DecodeBytes(e.Header, &ethHeader); err != nil {
return nil, err
}
ethHeaders[i] = &ethHeader
func (h *Header) DecodedTarget() (*types.Header, error) {
var ethHeader types.Header
if err := rlp.DecodeBytes(h.Target.Header, &ethHeader); err != nil {
return nil, err
}
return ethHeaders, nil
return &ethHeader, nil
}

func (h *Header) Target() (*types.Header, error) {
decodedHeaders, err := h.decodeEthHeaders()
if err != nil {
func (h *Header) DecodedParent() (*types.Header, error) {
var ethHeader types.Header
if err := rlp.DecodeBytes(h.Parent.Header, &ethHeader); err != nil {
return nil, err
}
if len(decodedHeaders) == 0 {
return nil, fmt.Errorf("invalid header length")
}
return decodedHeaders[0], nil
return &ethHeader, nil
}

func (h *Header) Account(path common.Address) (*types.StateAccount, error) {
target, err := h.Target()
target, err := h.DecodedTarget()
if err != nil {
return nil, err
}
Expand All @@ -81,20 +76,14 @@ func extractValidatorSet(h *types.Header) ([][]byte, error) {
}
var validatorSet [][]byte
validators := extra[extraVanity : len(extra)-extraSeal]
if h.Number.Uint64() >= env.LubanFork {
validatorCount := int(validators[0])
validatorsWithBLS := validators[1 : validatorCount*validatorBytesLength]
for i := 0; i < validatorCount; i++ {
start := validatorBytesLength * i
validatorWithBLS := validatorsWithBLS[start : start+validatorBytesLength]
validatorSet = append(validatorSet, validatorWithBLS[:validatorBytesLengthBeforeLuban])
}
} else {
validatorCount := len(validators) / validatorBytesLengthBeforeLuban
for i := 0; i < validatorCount; i++ {
start := validatorBytesLengthBeforeLuban * i
validatorSet = append(validatorSet, validators[start:start+validatorBytesLengthBeforeLuban])
}

validatorCount := int(validators[0])
validatorsWithBLS := validators[1 : validatorCount*validatorBytesLength]
for i := 0; i < validatorCount; i++ {
start := validatorBytesLength * i
validatorWithBLS := validatorsWithBLS[start : start+validatorBytesLength]
validatorSet = append(validatorSet, validatorWithBLS[:validatorBytesLengthBeforeLuban])
}

return validatorSet, nil
}
19 changes: 6 additions & 13 deletions module/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (ts *HeaderTestSuite) TestNewHeaderSuccess() {
rawHeader := types.Header{
Root: common.HexToHash("c84307dfe4ccfec4a851a77755d63228d8e0b9ba3345d1eee37ed729ee16eaa1"),
Number: big.NewInt(21400),
Extra: common.Hex2Bytes("d983010111846765746889676f312e31362e3135856c696e75780000d5efc574b794a294a7e455058789c82f15e8ce00669d689e2090e08cc7f42b0bd007728dea17979718bc99e2261bfe892a7ac9b849545ffc597338db43a9cb00564971a8f282cfe1741b0294ba45a21ac35168138e2c2e8200"),
Extra: common.Hex2Bytes("d88301020a846765746888676f312e32302e35856c696e7578000000b19df4a2150bac492386862ad3df4b666bc096b0505bb694dab0bec348681af766751cb839576e9c515a09c8bffa30a46296ccc56612490eb480d03bf948e10005bbcc0421f90b3d4e2465176c461afb316ebc773c61faee85a6515daa8a923564c6ffd37fb2fe9f118ef88092e8762c7addb526ab7eb1e772baef85181f892c731be0c1891a50e6b06262c816295e26495cef6f69dfa69911d9d8e4f3bbadb89b977cf58294f7239d515e15b24cfeb82494056cf691eaf729b165f32c9757c429dba5051155903067e56ebe3698678e9135ebb5849518aff370ca25e19e1072cc1a9fabcaa7f3e2c0b4b16ad183c473bafe30a36e39fa4a143657e229cd23c77f8fbc8e4e4e241695dd3d248d1e51521eee6619143f349bbafec1551819b8be1efea2fc46ca749aa184248a459464eec1a21e7fc7b71a053d9644e9bb8da4853b8f872cd7c1d6b324bf1922829830646ceadfb658d3de009a61dd481a114a2e761c554b641742c973867899d38a80967d39e406a0a9642d41e9007a27fc1150a267d143a9f786cd2b5eecbdcc4036273705225b956d5e2f8f5eb95d2569c77a677c40c7fbea129d4b171a39b7a8ddabfab2317f59d86abfaf690850223d90e9e7593d91a29331dfc2f84d5adecc75fc39ecab4632c1b4400a3dd1e1298835bcca70f657164e5b75689b64b7fd1fa275f334f28e1896a26afa1295da81418593bd12814463d9f6e45c36a0e47eb4cd3e5b6af29c41e2a3a5636430155a466e216585af3ba772b61c6014342d914470ec7ac2975be345796c2b81db0422a5fd08e40db1fc2368d2245e4b18b1d0b85c921aaaafd2e341760e29fc613edd39f71254614e2055c3287a517ae2f5b9e386cd1b50a4550696d957cb4900f03ab84f83ff2df44193496793b847f64e9d6db1b3953682bb95edd096eb1e69bbd357c200992ca78050d0cbe180cfaa018e8b6c8fd93d6f4cea42bbb345dbc6f0dfdb5bec73a8a257074e82b881cfa06ef3eb4efeca060c2531359abd0eab8af1e3edfa2025fca464ac9c3fd123f6c24a0d78869485a6f79b60359f141df90a0c745125b131caaffd12b772e180fbf38a051c97dabc8aaa0126a233a9e828cdafcc7422c4bb1f4030a56ba364c54103f26bad91508b5220b741b218c5d6af1f979ac42bc68d98a5a0d796c6ab01b659ad0fbd9f515893fdd740b29ba0772dbde9b4635921dd91bd2963a0fc855e31f6338f45b211c4e9dedb7f2eb09de7b4dd66d7c2c7e57f628210187192fb89d4b99dd4000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000be807dddb074639cd9fa61b47676c064fc50d62cb1f2c71577def3144fabeb75a8a1c8cb5b51d1d1b4a05eec67988b8685008baa17459ec425dbaebc852f496dc92196cdcc8e6d00c17eb431350c6c50d8b8f05176b90b11b3a3d4feb825ae9702711566df5dbf38e82add4dd1b573b95d2466fa6501ccb81e9d26a352b96150ccbf7b697fd0a419d1d6bf74282782b0b3eb1413c901d6ecf02e8e28939e8fb41b682372335be8070199ad3e8621d1743bcac4cc9d8f0f6e10f41e56461385c8eb5daac804fe3f2bca6ce739d93dbfb27e027f5e9e6da52b9e1c413ce35adc11000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ea0a6e3c511bbd10f4519ece37dc24887e11b55db2d4c6283c44a1c7bd503aaba7666e9f0c830e0ff016c1c750a5e48757a713d0836b1cabfd5c281b1de3b77d1c192183ee226379db83cffc681495730c11fdde79ba4c0cae7bc6faa3f0cc3e6093b633fd7ee4f86970926958d0b7ec80437f936acf212b78f0cd095f4565fff144fd458d233a5bef0274e31810c9df02f98fafde0f841f4e66a1cd000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f8b5830aefffb86097bc63a64e8d730014c39dcaac8f3309e37a11c06f0f5c233b55ba19c1f6c34d2d08de4b030ce825bb21fd884bc0fcb811336857419f5ca42a92ac149a4661a248de10f4ca6496069fdfd10d43bc74ccb81806b6ecd384617d1006b16dead7e4f84c8401dd8eaea0e61c6075d2ab24fcdc423764c21771cac6b241cbff89718f9cc8fc6459b4e7578401dd8eafa010c8358490a494a40c5c92aff8628fa770860a9d34e7fb7df38dfb208b0ddfc380ff15abfc44495e4d4605458bb485f0cac5a152b380a8d0208b3f9ff6216230ec4dd67a73b72b1d17a888c68e111f806ef0b255d012b5185b7420b5fb529c9b9300"),
}
ethHeader, err := newETHHeader(&rawHeader)
ts.Require().NoError(err)
Expand All @@ -39,15 +39,16 @@ func (ts *HeaderTestSuite) TestNewHeaderSuccess() {
ts.Require().NoError(err)

header := Header{
Headers: []*ETHHeader{ethHeader},
Target: ethHeader,
Parent: ethHeader,
AccountProof: accountProofRLP,
}
target, err := header.Target()
target, err := header.DecodedTarget()
ts.Require().NoError(err)
ts.Require().Equal(target.Number, rawHeader.Number)
validator, err := extractValidatorSet(target)
ts.Require().NoError(err)
ts.Require().Equal(len(validator), 1)
ts.Require().Equal(len(validator), 21)
ts.Require().NoError(header.ValidateBasic())
ts.Require().Equal(header.GetHeight().GetRevisionHeight(), target.Number.Uint64())
account, err := header.Account(common.HexToAddress(ibcHandlerAddress))
Expand All @@ -58,15 +59,7 @@ func (ts *HeaderTestSuite) TestNewHeaderSuccess() {
ts.Require().Equal(account.Balance.Uint64(), uint64(0))
}

func (ts *HeaderTestSuite) TestNewHeaderError() {
header := Header{
Headers: []*ETHHeader{},
AccountProof: []byte{},
}
ts.Require().Error(header.ValidateBasic())
}

func (ts *HeaderTestSuite) TestExtractValidatorSetLuban() {
func (ts *HeaderTestSuite) TestExtractValidatorSet() {
testnetHeader := &types.Header{
// after luban in testnet:
Number: big.NewInt(29835600),
Expand Down
Loading

0 comments on commit 7c61f75

Please sign in to comment.