Skip to content

Commit

Permalink
add testdata maker
Browse files Browse the repository at this point in the history
Signed-off-by: Naohiro Yoshida <[email protected]>
  • Loading branch information
Naohiro Yoshida committed Sep 6, 2023
1 parent 9436390 commit 4f7f8a9
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 184 deletions.
2 changes: 1 addition & 1 deletion module/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (h *Header) Account(path common.Address) (*types.StateAccount, error) {
return verifyAccount(target, h.AccountProof, path)
}

func extractValidatorSet(h *types.Header) ([][]byte, error) {
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())
Expand Down
4 changes: 2 additions & 2 deletions module/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (ts *HeaderTestSuite) TestNewHeaderSuccess() {
target, err := header.DecodedTarget()
ts.Require().NoError(err)
ts.Require().Equal(target.Number, rawHeader.Number)
validator, err := extractValidatorSet(target)
validator, err := ExtractValidatorSet(target)
ts.Require().NoError(err)
ts.Require().Equal(len(validator), 21)
ts.Require().NoError(header.ValidateBasic())
Expand All @@ -65,7 +65,7 @@ func (ts *HeaderTestSuite) TestExtractValidatorSet() {
Number: big.NewInt(29835600),
Extra: common.Hex2Bytes("d883010202846765746888676f312e31392e39856c696e7578000000110bea95071284214b9b9c85549ab3d2b972df0deef66ac2c9ab1757500d6f4fdee439b17cf8e43267f94bc759162fb68de676d2fe10cc4cde26dd06be7e345e9cbf4b1dbf86b262bc35552c16704d214347f29fa77f77da6d75d7c752b742ad4855bae330426b823e742da31f816cc83bc16d69a9134be0cfb4a1d17ec34f1b5b32d5c20440b8536b1e88f0f296c5d20b2a975c050e4220be276ace4892f4b41a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000980a75ecd1309ea12fa2ed87a8744fbfc9b863d589037a9ace3b590165ea1c0c5ac72bf600b7c88c1e435f41932c1132aae1bfa0bb68e46b96ccb12c3415e4d82af717d8a2959d3f95eae5dc7d70144ce1b73b403b7eb6e0b973c2d38487e58fd6e145491b110080fb14ac915a0411fc78f19e09a399ddee0d20c63a75d8f930f1694544ad2dc01bb71b214cb885500844365e95cd9942c7276e7fd8a2750ec6dded3dcdc2f351782310b0eadc077db59abca0f0cd26776e2e7acb9f3bce40b1fa5221fd1561226c6263cc5ff474cf03cceff28abc65c9cbae594f725c80e12d96c9b86c3400e529bfe184056e257c07940bb664636f689e8d2027c834681f8f878b73445261034e946bb2d901b4b878f8b27bb860a140cc9c8cc07d4ddf366440d9784efc88743d26af40f8956dd1c3501e560f745910bb14a5ec392f53cf78ddc2d2d69a146af287f7e079c3cbbfd3d446836d9b9397aa9a803b6c6b4f1cfc50baddbe2378cf194da35b9f4a1a32850114f1c5d9f84c8401c7414ea049d2e0876f51ce4693892331f8344a102aad88eb9e9bcfaa247cc9f898d1f8008401c7414fa0cf8d34727ff1d895bb49ca4be60c3b24d98d8afa9ce78644924e4b9aa39df8548022dc981e8703d3ca8b23fc032089667cb631cb28c32731762813bbf9fdb7e7a56b3945d65f2d72402a2abb9fbaf4bf094a3e5a542e175ecc54b426ee366b2ba200"),
}
validators, err := extractValidatorSet(testnetHeader)
validators, err := ExtractValidatorSet(testnetHeader)
ts.Require().NoError(err)
ts.Require().Len(validators, 7)
ts.Require().Equal(common.Bytes2Hex(validators[0]), "1284214b9b9c85549ab3d2b972df0deef66ac2c9ab1757500d6f4fdee439b17cf8e43267f94bc759162fb68de676d2fe10cc4cde26dd06be7e345e9cbf4b1dbf86b262bc")
Expand Down
9 changes: 3 additions & 6 deletions module/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (pr *Prover) CreateMsgCreateClient(_ string, dstHeader core.Header, _ sdk.A
if err != nil {
return nil, err
}
previousValidators, err := extractValidatorSet(previousEpochHeader)
previousValidators, err := ExtractValidatorSet(previousEpochHeader)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -134,9 +134,6 @@ func (pr *Prover) CreateMsgCreateClient(_ string, dstHeader core.Header, _ sdk.A
// Since ibc handler may not be deployed at the target epoch when create_client is used, state_root is not obtained.
StateRoot: pr.getStateRootOrEmpty(previousEpochHeader).Bytes(),
}
if pr.config.Debug {
log.Printf("initialConsensusState timestamp=%d, validatorHash=%s, validatorSize=%d", previousEpochHeader.Time, common.Bytes2Hex(consensusState.ValidatorsHash), len(previousValidators))
}
anyConsensusState, err := codectypes.NewAnyWithValue(&consensusState)
if err != nil {
return nil, err
Expand Down Expand Up @@ -204,7 +201,7 @@ func (pr *Prover) SetupHeadersForUpdateByLatestHeight(clientStateLatestHeight ex
parentValidatorsHash := common.Bytes2Hex(crypto.Keccak256(h.(*Header).ParentValidators...))
target, _ := h.(*Header).DecodedTarget()
if target.Number.Uint64()%constant.BlocksPerEpoch == 0 {
newValidators, _ := extractValidatorSet(target)
newValidators, _ := ExtractValidatorSet(target)
newValidatorsHash := common.Bytes2Hex(crypto.Keccak256(newValidators...))
log.Printf("SetupHeadersForUpdate: targetHeight=%v, trustedHeight=%v targetValidatorsHash=%s, parentValidatorsHash=%s, newValidatorsHash=%s\n", h.GetHeight(), trustedHeight, targetValidatorsHash, parentValidatorsHash, newValidatorsHash)
} else {
Expand Down Expand Up @@ -304,7 +301,7 @@ func (pr *Prover) queryValidatorSet(epochBlockNumber uint64) ([][]byte, error) {
if err != nil {
return nil, err
}
return extractValidatorSet(header)
return ExtractValidatorSet(header)
}

// newETHHeader returns the new ETHHeader
Expand Down
122 changes: 0 additions & 122 deletions module/prover_mainnet_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion module/prover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (ts *ProverTestSuite) TestCreateMsgCreateClient() {
ts.Require().NoError(proto.Unmarshal(msg.ConsensusState.Value, &cs2))
target, err := previousEpochHeader.DecodedTarget()
ts.Require().NoError(err)
validatorSet, err := extractValidatorSet(target)
validatorSet, err := ExtractValidatorSet(target)
ts.Require().NoError(err)
ts.Require().Equal(cs2.ValidatorsHash, crypto.Keccak256(validatorSet...))
ts.Require().Equal(cs2.Timestamp, target.Time)
Expand Down
6 changes: 1 addition & 5 deletions tool/testdata/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@
go run main.go misbehavior success

# src/client.rs test_error_submit_misbehavior
# src/misbehavior.rs test_error_try_from_any
go run main.go misbehavior error
```

### Header
```sh
# src/client.rs test_success_update_client_epoch
# src/header/mod.rs test_success_verify_epoch
go run main.go header success epoch

# src/client.rs test_success_update_client_non_epoch
# src/header/mod.rs test_success_verify_non_epoch
go run main.go header success non_epoch
go run main.go header success latest

# src/client.rs test_error_update_client
# src/header/mod.rs test_error_verify
go run main.go header error
```
95 changes: 48 additions & 47 deletions tool/testdata/internal/create_misbehavior.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,30 @@ const (
IbcAddress = "0x702E40245797c5a2108A566b3CE2Bf14Bc6aF841"
LocalNetValidatorSize = 3
MainNetValidatorSize = 21
MainNetIbcAddress = "0x151f3951FA218cac426edFe078fA9e5C6dceA500"
)

func CreateMisbehavior() *cobra.Command {
cmd := &cobra.Command{
Use: "misbehavior",
Short: "Create testdata for misbehavior. ",
}
cmd.AddCommand(successCmd())
cmd.AddCommand(errorCmd())
cmd.AddCommand(misbehaviorSuccessCmd())
cmd.AddCommand(misbehaviorErrorCmd())
return cmd
}

func successCmd() *cobra.Command {
func misbehaviorSuccessCmd() *cobra.Command {
return &cobra.Command{
Use: "success",
Short: "create misbehavior testdata for success",
RunE: func(cmd *cobra.Command, args []string) error {
chainID := int64(9999)
targetHeight, header1, err := getHeader(chainID, 8645, 0)
targetHeight, header1, err := getLocalHeader(chainID, 8645, 0)
if err != nil {
log.Panic(err)
}
_, header2, err := getHeader(chainID, 8545, targetHeight)
_, header2, err := getLocalHeader(chainID, 8545, targetHeight)
if err != nil {
log.Panic(err)
}
Expand Down Expand Up @@ -70,47 +71,7 @@ func successCmd() *cobra.Command {
}
}

func getHeader(chainID int64, port int64, targetHeight uint64) (uint64, *module.Header, error) {
chain, err := ethereum.NewChain(ethereum.ChainConfig{
EthChainId: chainID,
RpcAddr: fmt.Sprintf("http://localhost:%d", port),
HdwMnemonic: hdwMnemonic,
HdwPath: hdwPath,
IbcAddress: IbcAddress,
})
if err != nil {
return targetHeight, nil, err
}
if targetHeight == 0 {
latest, err := chain.LatestHeight()
if err != nil {
return targetHeight, nil, err
}
targetHeight = latest.GetRevisionHeight()
}
config := module.ProverConfig{
Debug: true,
}
prover := module.NewProver(module.NewChain(chain), &config).(*module.Prover)

// Get Finalized header
latestHeight := types.NewHeight(0, targetHeight)
latest := latestHeight.GetRevisionHeight()
iHeader, err := prover.GetLatestFinalizedHeaderByLatestHeight(latest)
if err != nil {
return latest, nil, err
}
header := iHeader.(*module.Header)
target, err := header.DecodedTarget()
if err != nil {
return latest, nil, err
}
trustedHeight := types.NewHeight(0, target.Number.Uint64()-5)
header.TrustedHeight = &trustedHeight
return latest, header, nil
}

func errorCmd() *cobra.Command {
func misbehaviorErrorCmd() *cobra.Command {
return &cobra.Command{
Use: "error",
Short: "create misbehavior testdata for error",
Expand All @@ -120,7 +81,7 @@ func errorCmd() *cobra.Command {
RpcAddr: "https://bsc-mainnet-rpc.allthatnode.com",
HdwMnemonic: hdwMnemonic,
HdwPath: hdwPath,
IbcAddress: "0x151f3951FA218cac426edFe078fA9e5C6dceA500",
IbcAddress: MainNetIbcAddress,
})
if err != nil {
return err
Expand Down Expand Up @@ -184,3 +145,43 @@ func errorCmd() *cobra.Command {
},
}
}

func getLocalHeader(chainID int64, port int64, targetHeight uint64) (uint64, *module.Header, error) {
chain, err := ethereum.NewChain(ethereum.ChainConfig{
EthChainId: chainID,
RpcAddr: fmt.Sprintf("http://localhost:%d", port),
HdwMnemonic: hdwMnemonic,
HdwPath: hdwPath,
IbcAddress: IbcAddress,
})
if err != nil {
return targetHeight, nil, err
}
if targetHeight == 0 {
latest, err := chain.LatestHeight()
if err != nil {
return targetHeight, nil, err
}
targetHeight = latest.GetRevisionHeight()
}
config := module.ProverConfig{
Debug: true,
}
prover := module.NewProver(module.NewChain(chain), &config).(*module.Prover)

// Get Finalized header
latestHeight := types.NewHeight(0, targetHeight)
latest := latestHeight.GetRevisionHeight()
iHeader, err := prover.GetLatestFinalizedHeaderByLatestHeight(latest)
if err != nil {
return latest, nil, err
}
header := iHeader.(*module.Header)
target, err := header.DecodedTarget()
if err != nil {
return latest, nil, err
}
trustedHeight := types.NewHeight(0, target.Number.Uint64()-5)
header.TrustedHeight = &trustedHeight
return latest, header, nil
}
Loading

0 comments on commit 4f7f8a9

Please sign in to comment.