Skip to content

Commit

Permalink
Add previous validators hash to ConsensusState (#17)
Browse files Browse the repository at this point in the history
Add previous validators hash to ConsensusState 

Signed-off-by: Naohiro Yoshida <[email protected]>
  • Loading branch information
yoshidan authored Oct 19, 2023
1 parent 740ed58 commit 4ba9b53
Show file tree
Hide file tree
Showing 13 changed files with 378 additions and 160 deletions.
54 changes: 45 additions & 9 deletions e2e/contracts/contracts/ibc/lightclients/parlia/v1/parlia.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,8 @@ library IbcLightclientsParliaV1ConsensusState {
struct Data {
bytes state_root;
uint64 timestamp;
bytes validators_hash;
bytes current_validators_hash;
bytes previous_validators_hash;
}

// Decoder section
Expand Down Expand Up @@ -1286,7 +1287,10 @@ library IbcLightclientsParliaV1ConsensusState {
pointer += _read_timestamp(pointer, bs, r);
} else
if (fieldId == 3) {
pointer += _read_validators_hash(pointer, bs, r);
pointer += _read_current_validators_hash(pointer, bs, r);
} else
if (fieldId == 4) {
pointer += _read_previous_validators_hash(pointer, bs, r);
} else
{
pointer += ProtoBufRuntime._skip_field_decode(wireType, pointer, bs);
Expand Down Expand Up @@ -1339,13 +1343,30 @@ library IbcLightclientsParliaV1ConsensusState {
* @param r The in-memory struct
* @return The number of bytes decoded
*/
function _read_validators_hash(
function _read_current_validators_hash(
uint256 p,
bytes memory bs,
Data memory r
) internal pure returns (uint) {
(bytes memory x, uint256 sz) = ProtoBufRuntime._decode_bytes(p, bs);
r.validators_hash = x;
r.current_validators_hash = 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_previous_validators_hash(
uint256 p,
bytes memory bs,
Data memory r
) internal pure returns (uint) {
(bytes memory x, uint256 sz) = ProtoBufRuntime._decode_bytes(p, bs);
r.previous_validators_hash = x;
return sz;
}

Expand Down Expand Up @@ -1400,14 +1421,23 @@ library IbcLightclientsParliaV1ConsensusState {
);
pointer += ProtoBufRuntime._encode_uint64(r.timestamp, pointer, bs);
}
if (r.validators_hash.length != 0) {
if (r.current_validators_hash.length != 0) {
pointer += ProtoBufRuntime._encode_key(
3,
ProtoBufRuntime.WireType.LengthDelim,
pointer,
bs
);
pointer += ProtoBufRuntime._encode_bytes(r.validators_hash, pointer, bs);
pointer += ProtoBufRuntime._encode_bytes(r.current_validators_hash, pointer, bs);
}
if (r.previous_validators_hash.length != 0) {
pointer += ProtoBufRuntime._encode_key(
4,
ProtoBufRuntime.WireType.LengthDelim,
pointer,
bs
);
pointer += ProtoBufRuntime._encode_bytes(r.previous_validators_hash, pointer, bs);
}
return pointer - offset;
}
Expand Down Expand Up @@ -1454,7 +1484,8 @@ library IbcLightclientsParliaV1ConsensusState {
uint256 e;
e += 1 + ProtoBufRuntime._sz_lendelim(r.state_root.length);
e += 1 + ProtoBufRuntime._sz_uint64(r.timestamp);
e += 1 + ProtoBufRuntime._sz_lendelim(r.validators_hash.length);
e += 1 + ProtoBufRuntime._sz_lendelim(r.current_validators_hash.length);
e += 1 + ProtoBufRuntime._sz_lendelim(r.previous_validators_hash.length);
return e;
}
// empty checker
Expand All @@ -1471,7 +1502,11 @@ library IbcLightclientsParliaV1ConsensusState {
return false;
}

if (r.validators_hash.length != 0) {
if (r.current_validators_hash.length != 0) {
return false;
}

if (r.previous_validators_hash.length != 0) {
return false;
}

Expand All @@ -1488,7 +1523,8 @@ library IbcLightclientsParliaV1ConsensusState {
function store(Data memory input, Data storage output) internal {
output.state_root = input.state_root;
output.timestamp = input.timestamp;
output.validators_hash = input.validators_hash;
output.current_validators_hash = input.current_validators_hash;
output.previous_validators_hash = input.previous_validators_hash;

}

Expand Down
159 changes: 103 additions & 56 deletions module/parlia.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions module/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ func (pr *Prover) getStateCommitmentProof(path []byte, height exported.Height) (
return stateProof.StorageProofRLP[0], nil
}

func (pr *Prover) getStateRootOrEmpty(header *types.Header) common.Hash {
func (pr *Prover) GetStorageRoot(header *types.Header) (common.Hash, error) {
rlpAccountProof, _, err := pr.getAccountProof(header.Number.Int64())
if err != nil {
return common.Hash{}
return common.Hash{}, err
}
stateAccount, err := verifyAccount(header, rlpAccountProof, pr.chain.IBCAddress())
if err != nil {
return common.Hash{}
return common.Hash{}, err
}
return stateAccount.Root
return stateAccount.Root, nil
}

type proofList struct {
Expand Down
Loading

0 comments on commit 4ba9b53

Please sign in to comment.