Skip to content

Commit

Permalink
Feat/increase usability (#97)
Browse files Browse the repository at this point in the history
* split merkle tree node hash function

* add base denom event & oracle error type
  • Loading branch information
sh-cha authored Jul 11, 2024
1 parent 608b512 commit 1ab31b7
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
11 changes: 7 additions & 4 deletions x/opchild/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ func (ms MsgServer) FinalizeTokenDeposit(ctx context.Context, req *types.MsgFina
sdk.NewAttribute(types.AttributeKeySender, req.From),
sdk.NewAttribute(types.AttributeKeyRecipient, req.To),
sdk.NewAttribute(types.AttributeKeyDenom, coin.Denom),
sdk.NewAttribute(types.AttributeKeyBaseDenom, req.BaseDenom),
sdk.NewAttribute(types.AttributeKeyAmount, coin.Amount.String()),
sdk.NewAttribute(types.AttributeKeyFinalizeHeight, strconv.FormatUint(req.Height, 10)),
)
Expand Down Expand Up @@ -506,11 +507,12 @@ func (ms MsgServer) InitiateTokenWithdrawal(ctx context.Context, req *types.MsgI
coin := req.Amount
burnCoins := sdk.NewCoins(coin)

// check denom pair existence
if ok, err := ms.DenomPairs.Has(ctx, coin.Denom); err != nil {
baseDenom, err := ms.DenomPairs.Get(ctx, coin.Denom)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return nil, types.ErrNonL1Token
}
return nil, err
} else if !ok {
return nil, types.ErrNonL1Token
}

senderAddr, err := ms.authKeeper.AddressCodec().StringToBytes(req.Sender)
Expand Down Expand Up @@ -572,6 +574,7 @@ func (ms MsgServer) InitiateTokenWithdrawal(ctx context.Context, req *types.MsgI
sdk.NewAttribute(types.AttributeKeyFrom, req.Sender),
sdk.NewAttribute(types.AttributeKeyTo, req.To),
sdk.NewAttribute(types.AttributeKeyDenom, coin.Denom),
sdk.NewAttribute(types.AttributeKeyBaseDenom, baseDenom),
sdk.NewAttribute(types.AttributeKeyAmount, coin.Amount.String()),
sdk.NewAttribute(types.AttributeKeyL2Sequence, strconv.FormatUint(l2Sequence, 10)),
))
Expand Down
12 changes: 9 additions & 3 deletions x/opchild/keeper/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"time"

"cosmossdk.io/collections"
"cosmossdk.io/log"
sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -59,17 +60,22 @@ func NewL2OracleHandler(
func (k L2OracleHandler) UpdateOracle(ctx context.Context, height uint64, extCommitBz []byte) error {
hostStoreLastHeight, err := k.HostValidatorStore.GetLastHeight(ctx)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return types.ErrOracleValidatorsNotRegistered
}
return err
}

if hostStoreLastHeight > int64(height) {
return errors.New("invalid height")
return types.ErrInvalidOracleHeight
}

sdkCtx := sdk.UnwrapSDKContext(ctx)

hostChainID, err := k.L1ChainId(ctx)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return types.ErrBridgeInfoNotExists
}
return err
}

Expand Down Expand Up @@ -99,7 +105,7 @@ func (k L2OracleHandler) UpdateOracle(ctx context.Context, height uint64, extCom

// if there is no timestamp price, skip the price update
if _, ok := prices[tsCp]; !ok {
return types.ErrInvalidPrices.Wrap("timestamp not found")
return types.ErrOracleTimestampNotExists
}

updatedTime := time.Unix(0, prices[tsCp].Int64())
Expand Down
3 changes: 1 addition & 2 deletions x/opchild/l2slinky/aggregator.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package l2slinky

import (
"errors"
"fmt"
"math/big"
"time"
Expand Down Expand Up @@ -51,7 +50,7 @@ func WritePrices(ctx sdk.Context, ok types.OracleKeeper, updatedTime time.Time,

qp, err := ok.GetPriceForCurrencyPair(ctx, cp)
if err == nil && !updatedTime.After(qp.BlockTimestamp) {
return errors.New("try to update the past price")
return types.ErrInvalidOracleTimestamp
}

// Convert the price to a quote price and write it to state.
Expand Down
7 changes: 6 additions & 1 deletion x/opchild/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ var (
ErrMaxValidatorsExceeded = errorsmod.Register(ModuleName, 20, "max validators exceeded")
ErrMaxValidatorsLowerThanCurrent = errorsmod.Register(ModuleName, 21, "max validators cannot be lower than current number of validators")
ErrNonL1Token = errorsmod.Register(ModuleName, 22, "token is not from L1")
ErrInvalidOracleHeight = errorsmod.Register(ModuleName, 23, "oracle height is old")
ErrOracleValidatorsNotRegistered = errorsmod.Register(ModuleName, 24, "validators are not registered in oracle")
ErrOracleTimestampNotExists = errorsmod.Register(ModuleName, 25, "oracle timestamp does not exist")
ErrInvalidOracleTimestamp = errorsmod.Register(ModuleName, 26, "oracle timestamp is old")
ErrBridgeInfoNotExists = errorsmod.Register(ModuleName, 27, "bridge info does not exist")

// Antehandler error
ErrRedundantTx = errorsmod.Register(ModuleName, 23, "tx messages are all redundant")
ErrRedundantTx = errorsmod.Register(ModuleName, 28, "tx messages are all redundant")
)
1 change: 1 addition & 0 deletions x/opchild/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
AttributeKeyRecipient = "recipient"
AttributeKeyAmount = "amount"
AttributeKeyDenom = "denom"
AttributeKeyBaseDenom = "base_denom"
AttributeKeyStructTag = "struct_tag"
AttributeKeyValidator = "validator"
AttributeKeyMetadata = "metadata"
Expand Down
18 changes: 12 additions & 6 deletions x/ophost/types/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,20 @@ func GenerateWithdrawalHash(bridgeId uint64, l2Sequence uint64, sender string, r
return withdrawalHash
}

func GenerateNodeHash(a, b []byte) [32]byte {
var data [32]byte
switch bytes.Compare(a, b) {
case 0, 1: // equal or greater
data = sha3.Sum256(append(b, a...))
case -1: // less
data = sha3.Sum256(append(a, b...))
}
return data
}

func GenerateRootHashFromProofs(data [32]byte, proofs [][]byte) [32]byte {
for _, proof := range proofs {
switch bytes.Compare(data[:], proof) {
case 0, 1: // equal or greater
data = sha3.Sum256(append(proof, data[:]...))
case -1: // less
data = sha3.Sum256(append(data[:], proof...))
}
data = GenerateNodeHash(data[:], proof)
}
return data
}

0 comments on commit 1ab31b7

Please sign in to comment.