Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change output root spec #99

Merged
merged 2 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
484 changes: 203 additions & 281 deletions api/opinit/ophost/v1/tx.pulsar.go

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions proto/opinit/ophost/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,8 @@ message MsgFinalizeTokenWithdrawal {

// version of the output root
bytes version = 8 [(gogoproto.moretags) = "yaml:\"version\""];
bytes state_root = 9 [(gogoproto.moretags) = "yaml:\"state_root\""];
bytes storage_root = 10 [(gogoproto.moretags) = "yaml:\"storage_root\""];
bytes latest_block_hash = 11 [(gogoproto.moretags) = "yaml:\"latest_block_hash\""];
bytes storage_root = 9 [(gogoproto.moretags) = "yaml:\"storage_root\""];
bytes latest_block_hash = 10 [(gogoproto.moretags) = "yaml:\"latest_block_hash\""];
}

// MsgFinalizeTokenWithdrawalResponse returns a message handle result.
Expand Down
6 changes: 0 additions & 6 deletions x/ophost/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,6 @@ func NewFinalizeTokenWithdrawal(ac address.Codec) *cobra.Command {
return err
}

stateRoot, err := hex.DecodeString(withdrawalInfo.StateRoot)
if err != nil {
return err
}

storageRoot, err := hex.DecodeString(withdrawalInfo.StorageRoot)
if err != nil {
return err
Expand All @@ -412,7 +407,6 @@ func NewFinalizeTokenWithdrawal(ac address.Codec) *cobra.Command {
receiver,
amount,
version,
stateRoot,
storageRoot,
latestBlockHash,
)
Expand Down
2 changes: 1 addition & 1 deletion x/ophost/client/cli/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ func (s *CLITestSuite) TestNewFinalizeTokenWithdrawal() {
"sender": "init1k2svyvm60r8rhnzr9vemk5f6fksvm6tyeujp3c",
"sequence": 3,
"amount": "10000000uatom",
"version": "5ca4f3850ccc331aaf8a257d6086e526a3b42a63e18cb11d020847985b31d188",
sh-cha marked this conversation as resolved.
Show resolved Hide resolved
"version": "5c",
"state_root": "1d844ab7b05fad0adab1efa288baeb640ceddc1931dccdd89b521379329bc55d",
"storage_root": "45cba73df03a0d62aa297ea7e949bb0e608b01290205dde56a8fdc8f96239f3b",
"latest_block_hash": "32935c42573839f5ff3065941d98e378e3e73227bf29e349de4aa7af0ca8addd"
Expand Down
2 changes: 1 addition & 1 deletion x/ophost/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func (ms MsgServer) FinalizeTokenWithdrawal(ctx context.Context, req *types.MsgF
}

// validate output root generation
outputRoot := types.GenerateOutputRoot(req.Version, req.StateRoot, req.StorageRoot, req.LatestBlockHash)
outputRoot := types.GenerateOutputRoot(req.Version, req.StorageRoot, req.LatestBlockHash)
if !bytes.Equal(outputProposal.OutputRoot, outputRoot[:]) {
return nil, types.ErrFailedToVerifyWithdrawal.Wrap("invalid output root")
}
Expand Down
33 changes: 22 additions & 11 deletions x/ophost/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/initia-labs/OPinit/x/ophost/keeper"
"github.com/initia-labs/OPinit/x/ophost/types"
ophosttypes "github.com/initia-labs/OPinit/x/ophost/types"
)

func Test_RecordBatch(t *testing.T) {
Expand Down Expand Up @@ -193,32 +194,42 @@ func Test_FinalizeTokenWithdrawal(t *testing.T) {
amount := sdk.NewCoin("uinit", math.NewInt(1_000_000))
input.Faucet.Fund(ctx, types.BridgeAddress(1), amount)

outputRoot := decodeBase64(t, "at+mtcWpUvvV+K/uBm+tRufWD0WH4SZVskw9WKn5N/A=")
version := decodeBase64(t, "Ch4nNnd/gKYr6y33K2SYeEgcDKEBlLgytRNr77rlQBc=")
stateRoot := decodeBase64(t, "C2ZdjJ7uX41NaadA/FjlMiG6btiDfYnxE2ABqJocHxI=")
storageRoot := decodeBase64(t, "8EorDbcn/PYtbfU8+e35gHR5e/Liy/mycsULLPWzJww=")
sender := "osmo174knscjg688ddtxj8smyjz073r3w5mms8ugvx6"
receiver := "cosmos174knscjg688ddtxj8smyjz073r3w5mms08musg"

version := []byte{1}

withdrawal1 := ophosttypes.GenerateWithdrawalHash(1, 1, sender, receiver, amount.Denom, amount.Amount.Uint64())
withdrawal2 := ophosttypes.GenerateWithdrawalHash(1, 2, sender, receiver, amount.Denom, amount.Amount.Uint64())
withdrawal3 := ophosttypes.GenerateWithdrawalHash(1, 3, sender, receiver, amount.Denom, amount.Amount.Uint64())

proof1 := withdrawal2
proof2 := ophosttypes.GenerateNodeHash(withdrawal3[:], withdrawal3[:])

node12 := ophosttypes.GenerateNodeHash(withdrawal1[:], withdrawal2[:])

storageRoot := ophosttypes.GenerateNodeHash(node12[:], proof2[:])
blockHash := decodeBase64(t, "tgmfQJT4uipVToW631xz0RXdrfzu7n5XxGNoPpX6isI=")
outputRoot := ophosttypes.GenerateOutputRoot(version, storageRoot[:], blockHash)
proofs := [][]byte{
decodeBase64(t, "Ux19nu4Nl3N7gBy/ID3rzuXrRpScnxOR9u/PUCxlTC0="),
decodeBase64(t, "vWrbSRyDJ+FnWxY5Plr7Ltgyyusr/uDW7nQDq8PDDQY="),
decodeBase64(t, "opvMy3Dv9tUfa4pNr/IBM1GOw8qOlwfxqoXVH5gKPIo="),
proof1[:],
proof2[:],
}

now := time.Now().UTC()
ctx = ctx.WithBlockTime(now)
_, err = ms.ProposeOutput(ctx, types.NewMsgProposeOutput(addrsStr[0], 1, 100, outputRoot))
_, err = ms.ProposeOutput(ctx, types.NewMsgProposeOutput(addrsStr[0], 1, 100, outputRoot[:]))
require.NoError(t, err)

ctx = ctx.WithBlockTime(now.Add(time.Second * 60))
sender := "osmo174knscjg688ddtxj8smyjz073r3w5mms8ugvx6"
receiver := "cosmos174knscjg688ddtxj8smyjz073r3w5mms08musg"

require.NoError(t, err)
_, err = ms.FinalizeTokenWithdrawal(ctx, types.NewMsgFinalizeTokenWithdrawal(
1, 1, 1, proofs,
sender,
receiver,
amount,
version, stateRoot, storageRoot, blockHash,
version, storageRoot[:], blockHash,
))
require.NoError(t, err)

Expand Down
11 changes: 5 additions & 6 deletions x/ophost/types/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ func (output Output) IsEmpty() bool {
return len(output.OutputRoot) == 0 && output.L1BlockTime.IsZero() && output.L2BlockNumber == 0
}

func GenerateOutputRoot(version []byte, stateRoot []byte, storageRoot []byte, latestBlockHash []byte) [32]byte {
sh-cha marked this conversation as resolved.
Show resolved Hide resolved
seed := make([]byte, 32*4)
copy(seed, version)
copy(seed[32:], stateRoot)
copy(seed[64:], storageRoot)
copy(seed[96:], latestBlockHash)
func GenerateOutputRoot(version []byte, storageRoot []byte, latestBlockHash []byte) [32]byte {
seed := make([]byte, 1+32+32)
seed[0] = version[0]
copy(seed[1:], storageRoot[:32])
copy(seed[1+32:], latestBlockHash[:32])
return sha3.Sum256(seed)
}

Expand Down
8 changes: 1 addition & 7 deletions x/ophost/types/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ func NewMsgFinalizeTokenWithdrawal(
receiver string,
amount sdk.Coin,
version []byte,
stateRoot []byte,
storageRoot []byte,
latestBlockHash []byte,
) *MsgFinalizeTokenWithdrawal {
Expand All @@ -216,7 +215,6 @@ func NewMsgFinalizeTokenWithdrawal(
Sequence: sequence,
Amount: amount,
Version: version,
StateRoot: stateRoot,
StorageRoot: storageRoot,
LatestBlockHash: latestBlockHash,
}
Expand Down Expand Up @@ -255,14 +253,10 @@ func (msg MsgFinalizeTokenWithdrawal) Validate(accAddressCodec address.Codec) er
}
}

if len(msg.Version) != 32 {
if len(msg.Version) != 1 {
return ErrInvalidHashLength.Wrap("version")
}

if len(msg.StateRoot) != 32 {
return ErrInvalidHashLength.Wrap("state_root")
}

if len(msg.StorageRoot) != 32 {
return ErrInvalidHashLength.Wrap("storage_root")
}
Expand Down
Loading
Loading