Skip to content

Commit

Permalink
commitments: fix trusting period context format
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Aug 21, 2023
1 parent 83a442e commit 5bd8ce9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
24 changes: 13 additions & 11 deletions go/light-clients/lcp/types/commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ var (
})

trustingPeriodContextABI, _ = abi.NewType("tuple", "struct TrustingPeriodCommitmentContext", []abi.ArgumentMarshaling{
{Name: "params", Type: "bytes32"},
{Name: "timestamps", Type: "bytes32"},
{Name: "params", Type: "bytes32"},
})

stateCommitmentABI, _ = abi.NewType("tuple", "struct StateCommitment", []abi.ArgumentMarshaling{
Expand Down Expand Up @@ -105,26 +105,28 @@ func (NoneCommitmentContext) Validate(time.Time) error {

// TrustingPeriodCommitmentContext is the commitment context for a commitment that requires the current time to be within the trusting period.
type TrustingPeriodCommitmentContext struct {
TrustingPeriod big.Int
ClockDrift big.Int
UntrustedHeaderTimestamp time.Time
TrustedStateTimestamp time.Time
TrustingPeriod big.Int
ClockDrift big.Int
}

func DecodeTrustingPeriodCommitmentContext(params, timestamps [32]byte) *TrustingPeriodCommitmentContext {
// MSB first
func DecodeTrustingPeriodCommitmentContext(timestamps, params [32]byte) *TrustingPeriodCommitmentContext {
// 0-15: untrusted_header_timestamp
// 16-31: trusted_state_timestamp
untrustedHeaderTimestamp := timestampNanosBytesToTime(timestamps[:16])
trustedStateTimestamp := timestampNanosBytesToTime(timestamps[16:32])

// 0-15: trusting_period
// 16-31: clock_drift
trustingPeriod := uint128BytesToBigInt(params[:16])
clockDrift := uint128BytesToBigInt(params[16:32])

// 0-15: untrusted_header_timestamp
// 16-31: trusted_state_timestamp
return &TrustingPeriodCommitmentContext{
UntrustedHeaderTimestamp: untrustedHeaderTimestamp,
TrustedStateTimestamp: trustedStateTimestamp,
TrustingPeriod: trustingPeriod,
ClockDrift: clockDrift,
UntrustedHeaderTimestamp: timestampNanosBytesToTime(timestamps[:16]),
TrustedStateTimestamp: timestampNanosBytesToTime(timestamps[16:32]),
}
}

Expand Down Expand Up @@ -369,10 +371,10 @@ func EthABIDecodeTrustingPeriodCommitmentContext(bz []byte) (*TrustingPeriodComm
return nil, err
}
p := v[0].(struct {
Params [32]byte `json:"params"`
Timestamps [32]byte `json:"timestamps"`
Params [32]byte `json:"params"`
})
return DecodeTrustingPeriodCommitmentContext(p.Params, p.Timestamps), nil
return DecodeTrustingPeriodCommitmentContext(p.Timestamps, p.Params), nil
}

func EthABIDecodeStateCommitment(bz []byte) (*StateCommitment, error) {
Expand Down
28 changes: 15 additions & 13 deletions modules/commitments/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,6 @@ impl Display for TrustingPeriodContext {

impl EthABIEncoder for TrustingPeriodContext {
fn ethabi_encode(self) -> Vec<u8> {
let mut params = [0u8; 32];
params[0..=15].copy_from_slice(&self.trusting_period.as_nanos().to_be_bytes());
params[16..=31].copy_from_slice(&self.clock_drift.as_nanos().to_be_bytes());
let mut timestamps = [0u8; 32];
timestamps[0..=15].copy_from_slice(
&self
Expand All @@ -258,9 +255,12 @@ impl EthABIEncoder for TrustingPeriodContext {
.as_unix_timestamp_nanos()
.to_be_bytes(),
);
let mut params = [0u8; 32];
params[0..=15].copy_from_slice(&self.trusting_period.as_nanos().to_be_bytes());
params[16..=31].copy_from_slice(&self.clock_drift.as_nanos().to_be_bytes());
EthABITrustingPeriodContext {
params: params.to_vec(),
timestamps: timestamps.to_vec(),
params: params.to_vec(),
}
.encode()
}
Expand Down Expand Up @@ -292,22 +292,24 @@ impl From<TrustingPeriodContext> for CommitmentContext {
}

pub(crate) struct EthABITrustingPeriodContext {
// bytes32 in solidity
// MSB first
// 0-15: trusting_period
// 16-31: clock_drift
pub params: ethabi::FixedBytes,
// 0-15: untrusted_header_timestamp
// 16-31: trusted_state_timestamp
/// bytes32 in solidity
/// MSB first
/// 0-15: untrusted_header_timestamp
/// 16-31: trusted_state_timestamp
pub timestamps: ethabi::FixedBytes,
/// bytes32 in solidity
/// MSB first
/// 0-15: trusting_period
/// 16-31: clock_drift
pub params: ethabi::FixedBytes,
}

impl EthABITrustingPeriodContext {
fn encode(self) -> Vec<u8> {
use ethabi::Token;
ethabi::encode(&[Token::Tuple(vec![
Token::FixedBytes(self.params),
Token::FixedBytes(self.timestamps),
Token::FixedBytes(self.params),
])])
}
fn decode(bytes: &[u8]) -> Result<Self, Error> {
Expand All @@ -327,8 +329,8 @@ impl EthABITrustingPeriodContext {
assert!(tuple.len() == 2);
let mut values = tuple.into_iter();
Ok(Self {
params: values.next().unwrap().into_fixed_bytes().unwrap(),
timestamps: values.next().unwrap().into_fixed_bytes().unwrap(),
params: values.next().unwrap().into_fixed_bytes().unwrap(),
})
}
}
Expand Down

0 comments on commit 5bd8ce9

Please sign in to comment.