diff --git a/README.md b/README.md index 619dc5f..cc57c51 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# ibc-parlia-relay +# ibc-parlia-relay ![CI](https://github.com/datachainlab/ibc-parlia-relay/workflows/CI/badge.svg?branch=main) diff --git a/e2e/chains/bsc/docker-compose.simple.yml b/e2e/chains/bsc/docker-compose.simple.yml index 0552e74..65d8f54 100644 --- a/e2e/chains/bsc/docker-compose.simple.yml +++ b/e2e/chains/bsc/docker-compose.simple.yml @@ -34,6 +34,7 @@ services: - ./scripts:/root/scripts - ./config:/root/config - ./init-holders:/root/init-holders + # - ./validators/keystore:/root/validators/keystore command: /root/scripts/bootstrap.sh bootstrap-simple2: @@ -55,6 +56,7 @@ services: - ./scripts:/root/scripts - ./config:/root/config - ./init-holders:/root/init-holders + # - ./validators/keystore:/root/validators/keystore command: /root/scripts/bootstrap.sh bsc-rpc: # This is the bootstrap node diff --git a/e2e/chains/bsc/scripts/bootstrap.sh b/e2e/chains/bsc/scripts/bootstrap.sh index 20ef681..8e784ce 100755 --- a/e2e/chains/bsc/scripts/bootstrap.sh +++ b/e2e/chains/bsc/scripts/bootstrap.sh @@ -20,6 +20,9 @@ function init_validator() { node_id=$1 geth --datadir ${workspace}/storage/${node_id} account new --password /dev/null >${workspace}/storage/${node_id}Info validatorAddr=$(cat ${workspace}/storage/${node_id}Info | grep 'Public address of the key' | awk '{print $6}') + #mkdir -p ${workspace}/storage/${node_id}/keystore + #cp ${workspace}/validators/keystore/${node_id} ${workspace}/storage/${node_id}/keystore/${node_id} + #validatorAddr="0xa7876ea32e7a748c697d01345145485561305b24" echo "${validatorAddr},${validatorAddr},${validatorAddr},0x0000000010000000" >>${workspace}/genesis/validators.conf echo ${validatorAddr} >${workspace}/storage/${node_id}/address } diff --git a/e2e/chains/bsc/validators/keystore/bsc-validator1 b/e2e/chains/bsc/validators/keystore/bsc-validator1 new file mode 100644 index 0000000..66d44de --- /dev/null +++ b/e2e/chains/bsc/validators/keystore/bsc-validator1 @@ -0,0 +1 @@ +{"address":"a7876ea32e7a748c697d01345145485561305b24","crypto":{"cipher":"aes-128-ctr","ciphertext":"1cfb8bc7a6b6ddf56e54bf8aae6bd88279a64be887596b1e5a33156473062336","cipherparams":{"iv":"84c5d72baf65bf30dc4125e9e4142e67"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"f16e1f5d9821e74f33901deb22b7935621f8346e9d82f7588c1463de2b6b70fb"},"mac":"f498815391cc13b4d0d3026184fecdbf49ddd264eff1aa0f5591822f37feb9cb"},"id":"4980ec3a-6e91-4929-a21d-d3d231453029","version":3} \ No newline at end of file diff --git a/e2e/contracts/contracts/ibc/lightclients/parlia/v1/parlia.sol b/e2e/contracts/contracts/ibc/lightclients/parlia/v1/parlia.sol index 73e4f13..7b6c303 100644 --- a/e2e/contracts/contracts/ibc/lightclients/parlia/v1/parlia.sol +++ b/e2e/contracts/contracts/ibc/lightclients/parlia/v1/parlia.sol @@ -1777,4 +1777,310 @@ library IbcLightclientsParliaV1ConsensusState { } } } -//library IbcLightclientsParliaV1ConsensusState \ No newline at end of file +//library IbcLightclientsParliaV1ConsensusState + +library IbcLightclientsParliaV1Misbehaviour { + + + //struct definition + struct Data { + string client_id; + IbcLightclientsParliaV1Header.Data header_1; + IbcLightclientsParliaV1Header.Data header_2; + } + + // Decoder section + + /** + * @dev The main decoder for memory + * @param bs The bytes array to be decoded + * @return The decoded struct + */ + function decode(bytes memory bs) internal pure returns (Data memory) { + (Data memory x, ) = _decode(32, bs, bs.length); + return x; + } + + /** + * @dev The main decoder for storage + * @param self The in-storage struct + * @param bs The bytes array to be decoded + */ + function decode(Data storage self, bytes memory bs) internal { + (Data memory x, ) = _decode(32, bs, bs.length); + store(x, self); + } + // inner decoder + + /** + * @dev The decoder for internal usage + * @param p The offset of bytes array to start decode + * @param bs The bytes array to be decoded + * @param sz The number of bytes expected + * @return The decoded struct + * @return The number of bytes decoded + */ + function _decode(uint256 p, bytes memory bs, uint256 sz) + internal + pure + returns (Data memory, uint) + { + Data memory r; + uint256 fieldId; + ProtoBufRuntime.WireType wireType; + uint256 bytesRead; + uint256 offset = p; + uint256 pointer = p; + while (pointer < offset + sz) { + (fieldId, wireType, bytesRead) = ProtoBufRuntime._decode_key(pointer, bs); + pointer += bytesRead; + if (fieldId == 1) { + pointer += _read_client_id(pointer, bs, r); + } else + if (fieldId == 2) { + pointer += _read_header_1(pointer, bs, r); + } else + if (fieldId == 3) { + pointer += _read_header_2(pointer, bs, r); + } else + { + pointer += ProtoBufRuntime._skip_field_decode(wireType, pointer, bs); + } + + } + return (r, sz); + } + + // field readers + + /** + * @dev The decoder for reading a field + * @param p The offset of bytes array to start decode + * @param bs The bytes array to be decoded + * @param r The in-memory struct + * @return The number of bytes decoded + */ + function _read_client_id( + uint256 p, + bytes memory bs, + Data memory r + ) internal pure returns (uint) { + (string memory x, uint256 sz) = ProtoBufRuntime._decode_string(p, bs); + r.client_id = x; + return sz; + } + + /** + * @dev The decoder for reading a field + * @param p The offset of bytes array to start decode + * @param bs The bytes array to be decoded + * @param r The in-memory struct + * @return The number of bytes decoded + */ + function _read_header_1( + uint256 p, + bytes memory bs, + Data memory r + ) internal pure returns (uint) { + (IbcLightclientsParliaV1Header.Data memory x, uint256 sz) = _decode_IbcLightclientsParliaV1Header(p, bs); + r.header_1 = x; + return sz; + } + + /** + * @dev The decoder for reading a field + * @param p The offset of bytes array to start decode + * @param bs The bytes array to be decoded + * @param r The in-memory struct + * @return The number of bytes decoded + */ + function _read_header_2( + uint256 p, + bytes memory bs, + Data memory r + ) internal pure returns (uint) { + (IbcLightclientsParliaV1Header.Data memory x, uint256 sz) = _decode_IbcLightclientsParliaV1Header(p, bs); + r.header_2 = x; + return sz; + } + + // struct decoder + /** + * @dev The decoder for reading a inner struct field + * @param p The offset of bytes array to start decode + * @param bs The bytes array to be decoded + * @return The decoded inner-struct + * @return The number of bytes used to decode + */ + function _decode_IbcLightclientsParliaV1Header(uint256 p, bytes memory bs) + internal + pure + returns (IbcLightclientsParliaV1Header.Data memory, uint) + { + uint256 pointer = p; + (uint256 sz, uint256 bytesRead) = ProtoBufRuntime._decode_varint(pointer, bs); + pointer += bytesRead; + (IbcLightclientsParliaV1Header.Data memory r, ) = IbcLightclientsParliaV1Header._decode(pointer, bs, sz); + return (r, sz + bytesRead); + } + + + // Encoder section + + /** + * @dev The main encoder for memory + * @param r The struct to be encoded + * @return The encoded byte array + */ + function encode(Data memory r) internal pure returns (bytes memory) { + bytes memory bs = new bytes(_estimate(r)); + uint256 sz = _encode(r, 32, bs); + assembly { + mstore(bs, sz) + } + return bs; + } + // inner encoder + + /** + * @dev The encoder for internal usage + * @param r The struct to be encoded + * @param p The offset of bytes array to start decode + * @param bs The bytes array to be decoded + * @return The number of bytes encoded + */ + function _encode(Data memory r, uint256 p, bytes memory bs) + internal + pure + returns (uint) + { + uint256 offset = p; + uint256 pointer = p; + + if (bytes(r.client_id).length != 0) { + pointer += ProtoBufRuntime._encode_key( + 1, + ProtoBufRuntime.WireType.LengthDelim, + pointer, + bs + ); + pointer += ProtoBufRuntime._encode_string(r.client_id, pointer, bs); + } + + pointer += ProtoBufRuntime._encode_key( + 2, + ProtoBufRuntime.WireType.LengthDelim, + pointer, + bs + ); + pointer += IbcLightclientsParliaV1Header._encode_nested(r.header_1, pointer, bs); + + + pointer += ProtoBufRuntime._encode_key( + 3, + ProtoBufRuntime.WireType.LengthDelim, + pointer, + bs + ); + pointer += IbcLightclientsParliaV1Header._encode_nested(r.header_2, pointer, bs); + + return pointer - offset; + } + // nested encoder + + /** + * @dev The encoder for inner struct + * @param r The struct to be encoded + * @param p The offset of bytes array to start decode + * @param bs The bytes array to be decoded + * @return The number of bytes encoded + */ + function _encode_nested(Data memory r, uint256 p, bytes memory bs) + internal + pure + returns (uint) + { + /** + * First encoded `r` into a temporary array, and encode the actual size used. + * Then copy the temporary array into `bs`. + */ + uint256 offset = p; + uint256 pointer = p; + bytes memory tmp = new bytes(_estimate(r)); + uint256 tmpAddr = ProtoBufRuntime.getMemoryAddress(tmp); + uint256 bsAddr = ProtoBufRuntime.getMemoryAddress(bs); + uint256 size = _encode(r, 32, tmp); + pointer += ProtoBufRuntime._encode_varint(size, pointer, bs); + ProtoBufRuntime.copyBytes(tmpAddr + 32, bsAddr + pointer, size); + pointer += size; + delete tmp; + return pointer - offset; + } + // estimator + + /** + * @dev The estimator for a struct + * @param r The struct to be encoded + * @return The number of bytes encoded in estimation + */ + function _estimate( + Data memory r + ) internal pure returns (uint) { + uint256 e; + e += 1 + ProtoBufRuntime._sz_lendelim(bytes(r.client_id).length); + e += 1 + ProtoBufRuntime._sz_lendelim(IbcLightclientsParliaV1Header._estimate(r.header_1)); + e += 1 + ProtoBufRuntime._sz_lendelim(IbcLightclientsParliaV1Header._estimate(r.header_2)); + return e; + } + // empty checker + + function _empty( + Data memory r + ) internal pure returns (bool) { + + if (bytes(r.client_id).length != 0) { + return false; + } + + return true; + } + + + //store function + /** + * @dev Store in-memory struct to storage + * @param input The in-memory struct + * @param output The in-storage struct + */ + function store(Data memory input, Data storage output) internal { + output.client_id = input.client_id; + IbcLightclientsParliaV1Header.store(input.header_1, output.header_1); + IbcLightclientsParliaV1Header.store(input.header_2, output.header_2); + + } + + + + //utility functions + /** + * @dev Return an empty struct + * @return r The empty struct + */ + function nil() internal pure returns (Data memory r) { + assembly { + r := 0 + } + } + + /** + * @dev Test whether a struct is empty + * @param x The struct to be tested + * @return r True if it is empty + */ + function isNil(Data memory x) internal pure returns (bool r) { + assembly { + r := iszero(x) + } + } +} +//library IbcLightclientsParliaV1Misbehaviour \ No newline at end of file diff --git a/module/env/env.go b/module/env/env.go index 3c42ef5..b26b751 100644 --- a/module/env/env.go +++ b/module/env/env.go @@ -6,7 +6,7 @@ import ( "log" ) -var LubanFork = uint64(29295050) +var LubanFork = uint64(29020050) func init() { viper.SetEnvPrefix("bsc") diff --git a/module/misbehaviour.go b/module/misbehaviour.go new file mode 100644 index 0000000..b0c5fbd --- /dev/null +++ b/module/misbehaviour.go @@ -0,0 +1,19 @@ +package module + +func (*Misbehaviour) ClientType() string { + return Parlia +} + +func (h *Misbehaviour) GetClientID() string { + return h.ClientId +} + +func (h *Misbehaviour) ValidateBasic() error { + if err := h.Header_1.ValidateBasic(); err != nil { + return err + } + if err := h.Header_2.ValidateBasic(); err != nil { + return err + } + return nil +} diff --git a/module/parlia.pb.go b/module/parlia.pb.go index 9d15e05..0e3ae28 100644 --- a/module/parlia.pb.go +++ b/module/parlia.pb.go @@ -222,12 +222,52 @@ func (m *ConsensusState) XXX_DiscardUnknown() { var xxx_messageInfo_ConsensusState proto.InternalMessageInfo +type Misbehaviour struct { + ClientId string `protobuf:"bytes,1,opt,name=client_id,json=clientId,proto3" json:"client_id,omitempty"` + Header_1 *Header `protobuf:"bytes,2,opt,name=header_1,json=header1,proto3" json:"header_1,omitempty"` + Header_2 *Header `protobuf:"bytes,3,opt,name=header_2,json=header2,proto3" json:"header_2,omitempty"` +} + +func (m *Misbehaviour) Reset() { *m = Misbehaviour{} } +func (m *Misbehaviour) String() string { return proto.CompactTextString(m) } +func (*Misbehaviour) ProtoMessage() {} +func (*Misbehaviour) Descriptor() ([]byte, []int) { + return fileDescriptor_dc631224085c6c85, []int{5} +} +func (m *Misbehaviour) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *Misbehaviour) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_Misbehaviour.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *Misbehaviour) XXX_Merge(src proto.Message) { + xxx_messageInfo_Misbehaviour.Merge(m, src) +} +func (m *Misbehaviour) XXX_Size() int { + return m.Size() +} +func (m *Misbehaviour) XXX_DiscardUnknown() { + xxx_messageInfo_Misbehaviour.DiscardUnknown(m) +} + +var xxx_messageInfo_Misbehaviour proto.InternalMessageInfo + func init() { proto.RegisterType((*Fraction)(nil), "ibc.lightclients.parlia.v1.Fraction") proto.RegisterType((*ClientState)(nil), "ibc.lightclients.parlia.v1.ClientState") proto.RegisterType((*ETHHeader)(nil), "ibc.lightclients.parlia.v1.ETHHeader") proto.RegisterType((*Header)(nil), "ibc.lightclients.parlia.v1.Header") proto.RegisterType((*ConsensusState)(nil), "ibc.lightclients.parlia.v1.ConsensusState") + proto.RegisterType((*Misbehaviour)(nil), "ibc.lightclients.parlia.v1.Misbehaviour") } func init() { @@ -235,45 +275,49 @@ func init() { } var fileDescriptor_dc631224085c6c85 = []byte{ - // 603 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x84, 0x53, 0x4f, 0x4f, 0xd4, 0x40, - 0x14, 0xdf, 0x2e, 0xb0, 0xc0, 0xec, 0x02, 0x61, 0x24, 0xa6, 0x6e, 0xb4, 0x6e, 0x16, 0x0d, 0x1b, - 0x13, 0x5a, 0xc1, 0x0f, 0x40, 0x90, 0x60, 0x56, 0xa3, 0x09, 0x29, 0xc6, 0x83, 0x97, 0x66, 0x3a, - 0x1d, 0xb6, 0x93, 0xb4, 0xf3, 0x36, 0x33, 0xd3, 0x26, 0xfa, 0x29, 0xf4, 0x5b, 0x71, 0x93, 0xa3, - 0x47, 0x85, 0x2f, 0x62, 0x66, 0x3a, 0x4b, 0xf7, 0xa2, 0xde, 0xde, 0xfb, 0xfd, 0x79, 0x6d, 0x7f, - 0xef, 0x15, 0x1d, 0xf0, 0x94, 0x46, 0x05, 0x9f, 0xe5, 0x9a, 0x16, 0x9c, 0x09, 0xad, 0xa2, 0x39, - 0x91, 0x05, 0x27, 0x51, 0x7d, 0xe4, 0xaa, 0x70, 0x2e, 0x41, 0x03, 0x1e, 0xf2, 0x94, 0x86, 0xcb, - 0xc2, 0xd0, 0xd1, 0xf5, 0xd1, 0x70, 0x6f, 0x06, 0x33, 0xb0, 0xb2, 0xc8, 0x54, 0x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x01, 0xb4, 0x4b, 0x70, 0xd3, 0x22, 0x31, - 0x80, 0x36, 0xb7, 0xaf, 0x79, 0xc9, 0x94, 0x26, 0xe5, 0xdc, 0xdd, 0x76, 0x0b, 0x98, 0x45, 0xb6, - 0x4f, 0x4d, 0x72, 0xa2, 0x72, 0xf7, 0x55, 0xdb, 0x2d, 0x3c, 0x25, 0x2a, 0x7f, 0xfd, 0xe1, 0xfa, - 0x77, 0xd0, 0xb9, 0xbe, 0x0d, 0xbc, 0x9b, 0xdb, 0xc0, 0xfb, 0x75, 0x1b, 0x78, 0xdf, 0xee, 0x82, - 0xce, 0xcd, 0x5d, 0xd0, 0xf9, 0x79, 0x17, 0x74, 0x3e, 0x47, 0x33, 0xae, 0xf3, 0x2a, 0x0d, 0x29, - 0x94, 0x51, 0x46, 0x34, 0xb1, 0x7f, 0x43, 0x41, 0xd2, 0x88, 0xa7, 0xf4, 0xb0, 0xd9, 0xcb, 0xa1, - 0x64, 0x05, 0xf9, 0x12, 0x95, 0x90, 0x55, 0x05, 0x4b, 0x7b, 0xf6, 0x27, 0x7d, 0xf5, 0x27, 0x00, - 0x00, 0xff, 0xff, 0x3f, 0xef, 0x21, 0x0e, 0x22, 0x04, 0x00, 0x00, + // 658 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x54, 0xdd, 0x6e, 0xd3, 0x30, + 0x14, 0x6e, 0xba, 0xad, 0x6b, 0xdd, 0x6e, 0xd3, 0xcc, 0x84, 0x42, 0x81, 0x50, 0x65, 0xa0, 0x55, + 0x48, 0x4b, 0x68, 0xb9, 0x46, 0xd3, 0x98, 0x86, 0x3a, 0xc4, 0xa4, 0x29, 0x43, 0x5c, 0x70, 0x13, + 0x39, 0x8e, 0xd7, 0x58, 0x4a, 0xe2, 0xca, 0x76, 0x22, 0xc1, 0x53, 0xc0, 0x63, 0xf0, 0x26, 0xbb, + 0x63, 0x97, 0x5c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x98, 0x22, 0x89, 0xf4, 0xcb, 0xcd, 0x51, 0x12, + 0xd2, 0x04, 0xef, 0x9b, 0xa6, 0xfb, 0x9c, 0xe4, 0xe8, 0x53, 0x58, 0xb0, 0xb4, 0xca, 0x49, 0xd2, + 0xd2, 0x3f, 0x94, 0x97, 0xbf, 0x03, 0x00, 0x00, 0xff, 0xff, 0x5e, 0x03, 0x0b, 0x8f, 0xce, 0x04, + 0x00, 0x00, } func (m *Fraction) Marshal() (dAtA []byte, err error) { @@ -536,6 +580,60 @@ func (m *ConsensusState) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } +func (m *Misbehaviour) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *Misbehaviour) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *Misbehaviour) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.Header_2 != nil { + { + size, err := m.Header_2.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintParlia(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x1a + } + if m.Header_1 != nil { + { + size, err := m.Header_1.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintParlia(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + } + if len(m.ClientId) > 0 { + i -= len(m.ClientId) + copy(dAtA[i:], m.ClientId) + i = encodeVarintParlia(dAtA, i, uint64(len(m.ClientId))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + func encodeVarintParlia(dAtA []byte, offset int, v uint64) int { offset -= sovParlia(v) base := offset @@ -664,6 +762,27 @@ func (m *ConsensusState) Size() (n int) { return n } +func (m *Misbehaviour) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.ClientId) + if l > 0 { + n += 1 + l + sovParlia(uint64(l)) + } + if m.Header_1 != nil { + l = m.Header_1.Size() + n += 1 + l + sovParlia(uint64(l)) + } + if m.Header_2 != nil { + l = m.Header_2.Size() + n += 1 + l + sovParlia(uint64(l)) + } + return n +} + func sovParlia(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -1445,6 +1564,160 @@ func (m *ConsensusState) Unmarshal(dAtA []byte) error { } return nil } +func (m *Misbehaviour) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowParlia + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: Misbehaviour: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: Misbehaviour: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field ClientId", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowParlia + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthParlia + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthParlia + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.ClientId = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Header_1", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowParlia + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthParlia + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthParlia + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.Header_1 == nil { + m.Header_1 = &Header{} + } + if err := m.Header_1.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Header_2", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowParlia + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthParlia + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthParlia + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.Header_2 == nil { + m.Header_2 = &Header{} + } + if err := m.Header_2.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipParlia(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthParlia + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func skipParlia(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 diff --git a/module/prover.go b/module/prover.go index 1bc7e94..b9ee231 100644 --- a/module/prover.go +++ b/module/prover.go @@ -61,7 +61,7 @@ func (pr *Prover) GetLatestFinalizedHeader() (out core.Header, err error) { if err != nil { return nil, err } - header, err := pr.getLatestFinalizedHeader(latestHeight.GetRevisionHeight()) + header, err := pr.GetLatestFinalizedHeaderByLatestHeight(latestHeight.GetRevisionHeight()) if err != nil { return nil, err } @@ -71,8 +71,8 @@ func (pr *Prover) GetLatestFinalizedHeader() (out core.Header, err error) { return header, err } -// getLatestFinalizedHeader returns the latest finalized header from the chain -func (pr *Prover) getLatestFinalizedHeader(latestBlockNumber uint64) (out core.Header, err error) { +// GetLatestFinalizedHeaderByLatestHeight returns the latest finalized header from the chain +func (pr *Prover) GetLatestFinalizedHeaderByLatestHeight(latestBlockNumber uint64) (out core.Header, err error) { currentEpoch := getCurrentEpoch(latestBlockNumber) currentEpochValidators, err := pr.queryValidatorSet(currentEpoch) if err != nil { @@ -190,10 +190,10 @@ func (pr *Prover) SetupHeadersForUpdate(dstChain core.ChainInfoICS02Querier, lat if err = pr.chain.Codec().UnpackAny(csRes.ClientState, &cs); err != nil { return nil, err } - return pr.setupHeadersForUpdate(cs.GetLatestHeight(), header) + return pr.SetupHeadersForUpdateByLatestHeight(cs.GetLatestHeight(), header) } -func (pr *Prover) setupHeadersForUpdate(clientStateLatestHeight exported.Height, latestFinalizedHeader *Header) ([]core.Header, error) { +func (pr *Prover) SetupHeadersForUpdateByLatestHeight(clientStateLatestHeight exported.Height, latestFinalizedHeader *Header) ([]core.Header, error) { targetHeaders := make([]core.Header, 0) // Needless to update already saved state @@ -212,15 +212,15 @@ func (pr *Prover) setupHeadersForUpdate(clientStateLatestHeight exported.Height, for epochHeight := firstUnsavedEpoch; epochHeight < latestFinalizedHeight; epochHeight += constant.BlocksPerEpoch { epoch, err := pr.queryVerifyingHeader(epochHeight, requiredHeaderCountToFinalize(len(previousValidatorSet))) if err != nil { - return nil, fmt.Errorf("SetupHeadersForUpdate failed to get past epochs : height=%d : %+v", epochHeight, err) + return nil, fmt.Errorf("SetupHeadersForUpdateByLatestHeight failed to get past epochs : height=%d : %+v", epochHeight, err) } unwrap, err := epoch.(*Header).Target() if err != nil { - return nil, fmt.Errorf("SetupHeadersForUpdate failed to unwrap header : height=%d : %+v", epoch.GetHeight(), err) + return nil, fmt.Errorf("SetupHeadersForUpdateByLatestHeight failed to unwrap header : height=%d : %+v", epoch.GetHeight(), err) } previousValidatorSet, err = extractValidatorSet(unwrap) if err != nil { - return nil, fmt.Errorf("SetupHeadersForUpdate failed to extract validator : height=%d : %+v", epoch.GetHeight(), err) + return nil, fmt.Errorf("SetupHeadersForUpdateByLatestHeight failed to extract validator : height=%d : %+v", epoch.GetHeight(), err) } targetHeaders = append(targetHeaders, epoch) } @@ -237,7 +237,7 @@ func (pr *Prover) setupHeadersForUpdate(clientStateLatestHeight exported.Height, h.(*Header).TrustedHeight = &trustedHeight if pr.config.Debug { - log.Printf("SetupHeadersForUpdate: targetHeight=%v, trustedHeight=%v, headerLength=%d, \n", h.GetHeight(), trustedHeight, len(h.(*Header).Headers)) + log.Printf("SetupHeadersForUpdateByLatestHeight: targetHeight=%v, trustedHeight=%v, headerLength=%d, \n", h.GetHeight(), trustedHeight, len(h.(*Header).Headers)) } } return targetHeaders, nil diff --git a/module/prover_mainnet_test.go b/module/prover_mainnet_test.go index 4e0e9fe..1febcad 100644 --- a/module/prover_mainnet_test.go +++ b/module/prover_mainnet_test.go @@ -38,11 +38,12 @@ func (ts *ProverMainnetTestSuite) SetupTest() { } func (ts *ProverMainnetTestSuite) TestQueryLatestFinalizedHeader() { + latestHeight, err := ts.prover.chain.LatestHeight() ts.Require().NoError(err) latest := latestHeight.GetRevisionHeight() println(latest) - iHeader, err := ts.prover.getLatestFinalizedHeader(latest) + iHeader, err := ts.prover.GetLatestFinalizedHeaderByLatestHeight(latest) ts.Require().NoError(err) ts.Require().NoError(iHeader.ValidateBasic()) @@ -78,7 +79,7 @@ func (ts *ProverMainnetTestSuite) TestQueryLatestFinalizedHeader() { log.Println(account.Root) // setup - updating, err := ts.prover.setupHeadersForUpdate(types.NewHeight(header.GetHeight().GetRevisionNumber(), target.Number.Uint64()-1), header) + updating, err := ts.prover.SetupHeadersForUpdateByLatestHeight(types.NewHeight(header.GetHeight().GetRevisionNumber(), target.Number.Uint64()-1), header) ts.Require().NoError(err) ts.Require().Len(updating, 1) ts.Require().Equal(updating[0].(*Header).GetHeight(), header.GetHeight()) diff --git a/proto/ibc/lightclients/parlia/v1/parlia.proto b/proto/ibc/lightclients/parlia/v1/parlia.proto index 6f278fb..7357fbd 100644 --- a/proto/ibc/lightclients/parlia/v1/parlia.proto +++ b/proto/ibc/lightclients/parlia/v1/parlia.proto @@ -41,4 +41,10 @@ message ConsensusState { bytes state_root = 1; uint64 timestamp = 2; bytes validators_hash = 3; +} + +message Misbehaviour { + string client_id = 1; + Header header_1 = 2; + Header header_2 = 3; } \ No newline at end of file diff --git a/tool/testdata/internal/create_misbehavior.go b/tool/testdata/internal/create_misbehavior.go new file mode 100644 index 0000000..65bdb41 --- /dev/null +++ b/tool/testdata/internal/create_misbehavior.go @@ -0,0 +1,100 @@ +package internal + +import ( + "fmt" + "github.com/cosmos/ibc-go/v4/modules/core/02-client/types" + "github.com/datachainlab/ibc-parlia-relay/module" + "github.com/ethereum/go-ethereum/common" + "github.com/hyperledger-labs/yui-ibc-solidity/pkg/relay/ethereum" + "github.com/spf13/cobra" + "log" +) + +const ( + hdwMnemonic = "math razor capable expose worth grape metal sunset metal sudden usage scheme" + hdwPath = "m/44'/60'/0'/0/0" + IbcAddress = "0x702E40245797c5a2108A566b3CE2Bf14Bc6aF841" +) + +func CreateMisbehavior() *cobra.Command { + cmd := &cobra.Command{ + Use: "misbehavior", + Short: "Create testdata for misbehavior. ", + } + cmd.AddCommand(localNet()) + return cmd +} + +// Launch local net before execute. +// - make chain +// -> Change NUMS_OF_VALIDATOR to 1 in docker-compose.yml +// -> Use e2e/chains/bsc/validators/keystore/bsc-validator1 for each chain's validator +// - make contracts +func localNet() *cobra.Command { + return &cobra.Command{ + Use: "local", + Short: "create misbehavior testdata with local net", + RunE: func(cmd *cobra.Command, args []string) error { + chainID := int64(9999) + height := uint64(200) + header1, err := getHeader(chainID, 8645, height) + if err != nil { + log.Panic(err) + } + header2, err := getHeader(chainID, 8545, height) + if err != nil { + log.Panic(err) + } + clientId := "xx-parlia-1" + misbehavior := module.Misbehaviour{ + ClientId: clientId, + Header_1: header1, + Header_2: header2, + } + + // print hex for lcp-parlia test + pack, _ := types.PackMisbehaviour(&misbehavior) + marshal, _ := pack.Marshal() + log.Println(common.Bytes2Hex(marshal)) + return nil + }, + } +} + +func getHeader(chainID int64, port int64, latestBlockNumber 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 nil, err + } + + config := module.ProverConfig{ + Debug: true, + } + prover := module.NewProver(module.NewChain(chain), &config).(*module.Prover) + + // Get Finalized header + latestHeight := types.NewHeight(0, latestBlockNumber) + latest := latestHeight.GetRevisionHeight() + iHeader, err := prover.GetLatestFinalizedHeaderByLatestHeight(latest) + if err != nil { + return nil, err + } + header := iHeader.(*module.Header) + target, err := header.Target() + if err != nil { + return nil, err + } + + // Setup finalized header + updating, err := prover.SetupHeadersForUpdateByLatestHeight(types.NewHeight(header.GetHeight().GetRevisionNumber(), target.Number.Uint64()-1), header) + if err != nil { + return nil, err + } + return updating[0].(*module.Header), nil +} diff --git a/tool/testdata/main.go b/tool/testdata/main.go new file mode 100644 index 0000000..10cb96e --- /dev/null +++ b/tool/testdata/main.go @@ -0,0 +1,20 @@ +package main + +import ( + "context" + "github.com/datachainlab/ibc-parlia-relay/tool/testdata/internal" + "github.com/spf13/cobra" + "log" +) + +func main() { + cobra.EnableCommandSorting = false + + var rootCmd = &cobra.Command{} + rootCmd.AddCommand(internal.CreateMisbehavior()) + + if err := rootCmd.ExecuteContext(context.Background()); err != nil { + log.Panicf("Failed to run command : %+v", err) + return + } +}