Skip to content

Commit

Permalink
handle multi-msg scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
agouin committed Jan 15, 2024
1 parent bd7b450 commit 1ac3de8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 38 deletions.
8 changes: 5 additions & 3 deletions cmd/noble/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ func StartListener(cfg config.Config, logger log.Logger, processingQueue chan *t
}

for _, tx := range response.Result.Txs {
parsedMsg, err := types.NobleLogToMessageState(tx)
parsedMsgs, err := types.NobleLogToMessageState(tx)
if err != nil {
continue
}
logger.Info(fmt.Sprintf("New stream msg from %d with tx hash %s", parsedMsg.SourceDomain, parsedMsg.SourceTxHash))
processingQueue <- parsedMsg
for _, parsedMsg := range parsedMsgs {
logger.Info(fmt.Sprintf("New stream msg from %d with tx hash %s", parsedMsg.SourceDomain, parsedMsg.SourceTxHash))
processingQueue <- parsedMsg
}
}
}
}()
Expand Down
79 changes: 44 additions & 35 deletions types/message_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,11 @@ func EvmLogToMessageState(abi abi.ABI, messageSent abi.Event, log *ethtypes.Log)
return messageState, nil
}

return nil, errors.New(fmt.Sprintf("unable to parse tx into message, tx hash %s", log.TxHash.Hex()))
return nil, fmt.Errorf("unable to parse tx into message, tx hash %s", log.TxHash.Hex())
}

// NobleLogToMessageState transforms a Noble log into a messageState
func NobleLogToMessageState(tx Tx) (messageState *MessageState, err error) {

func NobleLogToMessageState(tx Tx) (messageStates []*MessageState, err error) {
var eventsList []struct {
Events []Event `json:"events"`
}
Expand All @@ -93,44 +92,54 @@ func NobleLogToMessageState(tx Tx) (messageState *MessageState, err error) {
return nil, errors.New("unable to parse log events")
}

for _, event := range eventsList[0].Events {
if event.Type == "circle.cctp.v1.MessageSent" {
for _, attr := range event.Attributes {
if attr.Key == "message" {
encoded := attr.Value[1 : len(attr.Value)-1]
rawMessageSentBytes, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
continue
for _, log := range eventsList {
for _, event := range log.Events {
if event.Type == "circle.cctp.v1.MessageSent" {
var parsed bool
var parseErrs error
for _, attr := range event.Attributes {
if attr.Key == "message" {
encoded := attr.Value[1 : len(attr.Value)-1]
rawMessageSentBytes, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
parseErrs = errors.Join(parseErrs, fmt.Errorf("failed to decode message: %w", err))
continue
}

hashed := crypto.Keccak256(rawMessageSentBytes)
hashedHexStr := hex.EncodeToString(hashed)

msg, err := new(types.Message).Parse(rawMessageSentBytes)
if err != nil {
parseErrs = errors.Join(parseErrs, fmt.Errorf("failed to parse message: %w", err))
continue
}

messageState := &MessageState{
IrisLookupId: hashedHexStr,
Type: Mint,
Status: Created,
SourceDomain: msg.SourceDomain,
DestDomain: msg.DestinationDomain,
SourceTxHash: tx.Hash,
MsgSentBytes: rawMessageSentBytes,
DestinationCaller: msg.DestinationCaller,
Created: time.Now(),
Updated: time.Now(),
}

messageStates = append(messageStates, messageState)
}

hashed := crypto.Keccak256(rawMessageSentBytes)
hashedHexStr := hex.EncodeToString(hashed)

msg, err := new(types.Message).Parse(rawMessageSentBytes)
if err != nil {
continue
}

messageState = &MessageState{
IrisLookupId: hashedHexStr,
Type: Mint,
Status: Created,
SourceDomain: msg.SourceDomain,
DestDomain: msg.DestinationDomain,
SourceTxHash: tx.Hash,
MsgSentBytes: rawMessageSentBytes,
DestinationCaller: msg.DestinationCaller,
Created: time.Now(),
Updated: time.Now(),
}

return messageState, nil
}
if !parsed {
return nil, fmt.Errorf("unable to parse cctp message. tx hash %s: %w", tx.Hash, parseErrs)
}
}
}
}

return nil, errors.New(fmt.Sprintf("unable to parse txn into message. tx hash %s", tx.Hash))
return messageStates, nil

}

// DecodeDestinationCaller transforms an encoded Noble cctp address into a noble bech32 address
Expand Down

0 comments on commit 1ac3de8

Please sign in to comment.