Skip to content

Commit

Permalink
Implement TON VoteOutboundIfConfirmed
Browse files Browse the repository at this point in the history
  • Loading branch information
swift1337 committed Oct 25, 2024
1 parent f107ccc commit 0ca1d0d
Showing 1 changed file with 104 additions and 2 deletions.
106 changes: 104 additions & 2 deletions zetaclient/chains/ton/observer/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package observer
import (
"context"

"cosmossdk.io/math"
eth "github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"github.com/rs/zerolog"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/zeta-chain/node/zetaclient/chains/interfaces"
"github.com/zeta-chain/node/zetaclient/chains/ton/liteapi"
zctx "github.com/zeta-chain/node/zetaclient/context"
gasconst "github.com/zeta-chain/node/zetaclient/zetacore"
)

type outbound struct {
Expand All @@ -23,8 +25,29 @@ type outbound struct {
nonce uint64
}

func (ob *Observer) VoteOutboundIfConfirmed(_ context.Context, _ *cc.CrossChainTx) (bool, error) {
return false, errors.New("not implemented")
// VoteOutboundIfConfirmed checks outbound status and returns (continueKeysign, error)
func (ob *Observer) VoteOutboundIfConfirmed(ctx context.Context, cctx *cc.CrossChainTx) (bool, error) {
nonce := cctx.GetCurrentOutboundParam().TssNonce

outboundRes, exists := ob.getOutboundByNonce(nonce)
if !exists {
return true, nil
}

Check warning on line 35 in zetaclient/chains/ton/observer/outbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/ton/observer/outbound.go#L29-L35

Added lines #L29 - L35 were not covered by tests

withdrawal, err := outboundRes.tx.Withdrawal()
if err != nil {
return false, errors.Wrap(err, "unable to get withdrawal")
}

Check warning on line 40 in zetaclient/chains/ton/observer/outbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/ton/observer/outbound.go#L37-L40

Added lines #L37 - L40 were not covered by tests

// TODO: Add compliance check
// https://github.com/zeta-chain/node/issues/2916

txHash := liteapi.TransactionToHashString(&outboundRes.tx.Transaction)
if err = ob.postVoteOutbound(ctx, cctx, withdrawal, txHash, outboundRes.receiveStatus); err != nil {
return false, errors.Wrap(err, "unable to post vote")
}

Check warning on line 48 in zetaclient/chains/ton/observer/outbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/ton/observer/outbound.go#L45-L48

Added lines #L45 - L48 were not covered by tests

return false, nil

Check warning on line 50 in zetaclient/chains/ton/observer/outbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/ton/observer/outbound.go#L50

Added line #L50 was not covered by tests
}

// watchOutbound watches outbound transactions and caches them in-memory so they can be used later in
Expand Down Expand Up @@ -207,6 +230,7 @@ func extractWithdrawal(tx *toncontracts.Transaction) (toncontracts.Withdrawal, e
return w, s, nil
}

// getOutboundByNonce returns outbound by nonce
func (ob *Observer) getOutboundByNonce(nonce uint64) (outbound, bool) {
v, ok := ob.outbounds.Get(nonce)
if !ok {
Expand All @@ -216,6 +240,84 @@ func (ob *Observer) getOutboundByNonce(nonce uint64) (outbound, bool) {
return v.(outbound), true
}

// setOutboundByNonce stores outbound by nonce
func (ob *Observer) setOutboundByNonce(o outbound) {
ob.outbounds.Add(o.nonce, o)
}

func (ob *Observer) postVoteOutbound(
ctx context.Context,
cctx *cc.CrossChainTx,
w toncontracts.Withdrawal,
txHash string,
status chains.ReceiveStatus,
) error {
// I. Gas
// TON implements a different tx fee model. Basically, each operation in our Gateway has a
// tx_fee(operation) which is based on hard-coded gas values per operation
// multiplied by the current gas fees on-chain. Each withdrawal tx takes gas directly
// from the Gateway i.e. gw pays tx fees for itself.
//
// - Gas price is stores in zetacore thanks to Observer.postGasPrice()
// - Gas limit should be hardcoded in TON ZRC-20
//
// II. Block height
// TON doesn't sequential block height because different txs might end up in different shard chains
// tlb.BlockID is essentially a workchain+shard+seqno tuple. We can't use it as a block height. Thus let's use 0.
// Note that for the sake of gas tracking, we use masterchain block height (not applicable here).
const (
outboundGasUsed = 0
outboundGasPrice = 0
outboundGasLimit = 0
outboundBlockHeight = 0
)

var (
chainID = ob.Chain().ChainId
nonce = cctx.GetCurrentOutboundParam().TssNonce
signerAddress = ob.ZetacoreClient().GetKeys().GetOperatorAddress()
coinType = cctx.InboundParams.CoinType
)

msg := cc.NewMsgVoteOutbound(
signerAddress.String(),
cctx.Index,
txHash,
outboundBlockHeight,
outboundGasUsed,
math.NewInt(outboundGasPrice),
outboundGasLimit,
w.Amount,
status,
chainID,
nonce,
coinType,
)

const gasLimit = gasconst.PostVoteOutboundGasLimit

var retryGasLimit uint64
if msg.Status == chains.ReceiveStatus_failed {
retryGasLimit = gasconst.PostVoteOutboundRevertGasLimit
}

Check warning on line 302 in zetaclient/chains/ton/observer/outbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/ton/observer/outbound.go#L254-L302

Added lines #L254 - L302 were not covered by tests

log := ob.Logger().Outbound.With().
Uint64("outbound.nonce", nonce).
Str("outbound.outbound_tx_hash", txHash).
Logger()

zetaTxHash, ballot, err := ob.ZetacoreClient().PostVoteOutbound(ctx, gasLimit, retryGasLimit, msg)
if err != nil {
log.Error().Err(err).Msg("PostVoteOutbound: error posting vote")
return err
}

Check warning on line 313 in zetaclient/chains/ton/observer/outbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/ton/observer/outbound.go#L304-L313

Added lines #L304 - L313 were not covered by tests

if zetaTxHash != "" {
log.Info().
Str("outbound.vote_tx_hash", zetaTxHash).
Str("outbound.ballot_id", ballot).
Msg("PostVoteOutbound: posted vote")
}

Check warning on line 320 in zetaclient/chains/ton/observer/outbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/ton/observer/outbound.go#L315-L320

Added lines #L315 - L320 were not covered by tests

return nil

Check warning on line 322 in zetaclient/chains/ton/observer/outbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/ton/observer/outbound.go#L322

Added line #L322 was not covered by tests
}

0 comments on commit 0ca1d0d

Please sign in to comment.