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

fastcommit #33

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 26 additions & 0 deletions cmd/ethereum/broadcast.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package ethereum

import (
"context"
"cosmossdk.io/log"
"encoding/hex"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/strangelove-ventures/noble-cctp-relayer/config"
"github.com/strangelove-ventures/noble-cctp-relayer/types"
Expand Down Expand Up @@ -52,6 +54,29 @@ func Broadcast(
nonce := sequenceMap.Next(cfg.Networks.Destination.Ethereum.DomainId)
auth.Nonce = big.NewInt(nonce)

// check if nonce already used
co := &bind.CallOpts{
Pending: true,
Context: context.Background(),
}
key := append(
common.LeftPadBytes((big.NewInt(int64(msg.SourceDomain))).Bytes(), 4),
common.LeftPadBytes((big.NewInt(int64(msg.Nonce))).Bytes(), 8)...,
)

response, nonceErr := messageTransmitter.UsedNonces(co, [32]byte(crypto.Keccak256(key)))
if nonceErr != nil {
logger.Debug("Error querying whether nonce was used. Continuing...")
} else {
if response.Uint64() == uint64(1) {
// nonce has already been used, mark as complete
logger.Debug(fmt.Sprintf("This source domain/nonce has already been used: %d %d",
msg.SourceDomain, msg.Nonce))
msg.Status = types.Complete
return nil, errors.New("receive message was already broadcasted")
}
}

// broadcast txn
tx, err := messageTransmitter.ReceiveMessage(
auth,
Expand Down Expand Up @@ -88,6 +113,7 @@ func Broadcast(
}

// if it's not the last attempt, retry
// TODO increase the destination.ethereum.broadcast retries (3-5) and retry interval (15s). By checking for used nonces, there is no gas cost for failed mints.
if attempt != cfg.Networks.Destination.Ethereum.BroadcastRetries {
logger.Info(fmt.Sprintf("Retrying in %d seconds", cfg.Networks.Destination.Ethereum.BroadcastRetryInterval))
time.Sleep(time.Duration(cfg.Networks.Destination.Ethereum.BroadcastRetryInterval) * time.Second)
Expand Down
6 changes: 3 additions & 3 deletions cmd/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,11 @@ func filterNonWhitelistedChannels(cfg config.Config, logger log.Logger, msg *typ

// filterMessages filters out non-burn messages. It returns true if the message is not a burn.
func filterMessages(_ config.Config, logger log.Logger, msg *types.MessageState) bool {
result := msg.Type != types.Mint
if result {
if msg.Type != types.Mint {
logger.Info(fmt.Sprintf("Filtered tx %s because it's a not a burn", msg.SourceTxHash))
return true
}
return result
return false
}

func LookupKey(sourceTxHash string, messageType string) string {
Expand Down
3 changes: 3 additions & 0 deletions types/message_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type MessageState struct {
Channel string // "channel-%d" if a forward, empty if not a forward
Created time.Time
Updated time.Time
Nonce uint64
}

// EvmLogToMessageState transforms an evm log into a messageState given an ABI
Expand All @@ -63,6 +64,7 @@ func EvmLogToMessageState(abi abi.ABI, messageSent abi.Event, log *ethtypes.Log)
SourceTxHash: log.TxHash.Hex(),
MsgSentBytes: rawMessageSentBytes,
DestinationCaller: message.DestinationCaller,
Nonce: message.Nonce,
Created: time.Now(),
Updated: time.Now(),
}
Expand Down Expand Up @@ -120,6 +122,7 @@ func NobleLogToMessageState(tx Tx) (messageState *MessageState, err error) {
SourceTxHash: tx.Hash,
MsgSentBytes: rawMessageSentBytes,
DestinationCaller: msg.DestinationCaller,
Nonce: msg.Nonce,
Created: time.Now(),
Updated: time.Now(),
}
Expand Down