Skip to content

Commit

Permalink
feat: add message id to logs (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 authored Apr 12, 2024
1 parent 3bce952 commit 347657b
Show file tree
Hide file tree
Showing 27 changed files with 332 additions and 171 deletions.
18 changes: 8 additions & 10 deletions chains/evm/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (e *Executor) Execute(proposals []*proposal.Proposal) error {
}

p := pool.New().WithErrors()
for _, batch := range batches {
for i, batch := range batches {
if len(batch.proposals) == 0 {
continue
}
Expand All @@ -95,12 +95,14 @@ func (e *Executor) Execute(proposals []*proposal.Proposal) error {
return err
}

sessionID := e.sessionID(propHash)
sessionID := fmt.Sprintf("%s-%d", batch.proposals[0].MessageID, i)
log.Info().Str("messageID", batch.proposals[0].MessageID).Msgf("Starting session with ID: %s", sessionID)

msg := big.NewInt(0)
msg.SetBytes(propHash)
signing, err := signing.NewSigning(
msg,
e.sessionID(propHash),
sessionID,
e.host,
e.comm,
e.fetcher)
Expand Down Expand Up @@ -150,15 +152,15 @@ func (e *Executor) watchExecution(ctx context.Context, cancelExecution context.C
return err
}

log.Info().Str("SessionID", sessionID).Msgf("Sent proposals execution with hash: %s", hash)
log.Info().Str("messageID", sessionID).Msgf("Sent proposals execution with hash: %s", hash)
}
case <-ticker.C:
{
if !e.areProposalsExecuted(batch.proposals) {
continue
}

log.Info().Str("SessionID", sessionID).Msgf("Successfully executed proposals")
log.Info().Str("messageID", sessionID).Msgf("Successfully executed proposals")
return nil
}
case <-timeout.C:
Expand Down Expand Up @@ -194,7 +196,7 @@ func (e *Executor) proposalBatches(proposals []*proposal.Proposal) ([]*Batch, er
return nil, err
}
if isExecuted {
log.Info().Msgf("Proposal %p already executed", transferProposal)
log.Info().Str("messageID", transferProposal.MessageID).Msgf("Proposal %p already executed", transferProposal)
continue
}

Expand Down Expand Up @@ -247,7 +249,3 @@ func (e *Executor) areProposalsExecuted(proposals []*transfer.TransferProposal)

return true
}

func (e *Executor) sessionID(hash []byte) string {
return fmt.Sprintf("signing-%s", ethCommon.Bytes2Hex(hash))
}
15 changes: 6 additions & 9 deletions chains/evm/executor/message-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (
type TransferMessageHandler struct{}

func (h *TransferMessageHandler) HandleMessage(msg *message.Message) (*proposal.Proposal, error) {

transferMessage := &transfer.TransferMessage{
Source: msg.Source,
Destination: msg.Destination,
Data: msg.Data.(transfer.TransferMessageData),
Type: msg.Type,
ID: msg.ID,
}

switch transferMessage.Data.Type {
Expand All @@ -45,7 +45,6 @@ func (h *TransferMessageHandler) HandleMessage(msg *message.Message) (*proposal.
}

func PermissionlessGenericMessageHandler(msg *transfer.TransferMessage) (*proposal.Proposal, error) {

executeFunctionSignature, ok := msg.Data.Payload[0].([]byte)
if !ok {
return nil, errors.New("wrong function signature format")
Expand Down Expand Up @@ -85,7 +84,7 @@ func PermissionlessGenericMessageHandler(msg *transfer.TransferMessage) (*propos
ResourceId: msg.Data.ResourceId,
Metadata: msg.Data.Metadata,
Data: data.Bytes(),
}, transfer.TransferProposalType), nil
}, msg.ID, transfer.TransferProposalType), nil
}

func ERC20MessageHandler(msg *transfer.TransferMessage) (*proposal.Proposal, error) {
Expand All @@ -111,11 +110,10 @@ func ERC20MessageHandler(msg *transfer.TransferMessage) (*proposal.Proposal, err
ResourceId: msg.Data.ResourceId,
Metadata: msg.Data.Metadata,
Data: data,
}, transfer.TransferProposalType), nil
}, msg.ID, transfer.TransferProposalType), nil
}

func ERC721MessageHandler(msg *transfer.TransferMessage) (*proposal.Proposal, error) {

if len(msg.Data.Payload) != 3 {
return nil, errors.New("malformed payload. Len of payload should be 3")
}
Expand Down Expand Up @@ -144,11 +142,10 @@ func ERC721MessageHandler(msg *transfer.TransferMessage) (*proposal.Proposal, er
ResourceId: msg.Data.ResourceId,
Metadata: msg.Data.Metadata,
Data: data.Bytes(),
}, transfer.TransferProposalType), nil
}, msg.ID, transfer.TransferProposalType), nil
}

func ERC1155MessageHandler(msg *transfer.TransferMessage) (*proposal.Proposal, error) {

if len(msg.Data.Payload) != 4 {
return nil, errors.New("malformed payload. Len of payload should be 4")
}
Expand Down Expand Up @@ -187,7 +184,7 @@ func ERC1155MessageHandler(msg *transfer.TransferMessage) (*proposal.Proposal, e
ResourceId: msg.Data.ResourceId,
Metadata: msg.Data.Metadata,
Data: data,
}, transfer.TransferProposalType), nil
}, msg.ID, transfer.TransferProposalType), nil
}

func GenericMessageHandler(msg *transfer.TransferMessage) (*proposal.Proposal, error) {
Expand All @@ -207,5 +204,5 @@ func GenericMessageHandler(msg *transfer.TransferMessage) (*proposal.Proposal, e
ResourceId: msg.Data.ResourceId,
Metadata: msg.Data.Metadata,
Data: data.Bytes(),
}, transfer.TransferProposalType), nil
}, msg.ID, transfer.TransferProposalType), nil
}
2 changes: 2 additions & 0 deletions chains/evm/executor/message-handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ func (s *PermissionlessGenericHandlerTestSuite) Test_HandleMessage() {
Type: transfer.PermissionlessGenericTransfer,
},
Type: transfer.TransferMessageType,
ID: "messageID",
}

mh := executor.TransferMessageHandler{}
Expand All @@ -424,6 +425,7 @@ func (s *PermissionlessGenericHandlerTestSuite) Test_HandleMessage() {
Metadata: message.Data.(transfer.TransferMessageData).Metadata,
Data: expectedData,
},
"messageID",
transfer.TransferProposalType,
)
s.Nil(err)
Expand Down
11 changes: 9 additions & 2 deletions chains/evm/listener/depositHandlers/deposit-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ func NewETHDepositHandler(handlerMatcher HandlerMatcher) *ETHDepositHandler {
}
}

func (e *ETHDepositHandler) HandleDeposit(sourceID, destID uint8, depositNonce uint64, resourceID [32]byte, calldata, handlerResponse []byte) (*message.Message, error) {
func (e *ETHDepositHandler) HandleDeposit(
sourceID,
destID uint8,
depositNonce uint64,
resourceID [32]byte,
calldata,
handlerResponse []byte,
messageID string) (*message.Message, error) {
handlerAddr, err := e.handlerMatcher.GetHandlerAddressForResourceID(resourceID)
if err != nil {
return nil, err
Expand All @@ -42,7 +49,7 @@ func (e *ETHDepositHandler) HandleDeposit(sourceID, destID uint8, depositNonce u
return nil, err
}

return depositHandler.HandleDeposit(sourceID, destID, depositNonce, resourceID, calldata, handlerResponse)
return depositHandler.HandleDeposit(sourceID, destID, depositNonce, resourceID, calldata, handlerResponse, messageID)
}

// matchAddressWithHandlerFunc matches a handler address with an associated handler function
Expand Down
66 changes: 38 additions & 28 deletions chains/evm/listener/depositHandlers/erc1155.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,44 @@ import (

type Erc1155DepositHandler struct{}

func (dh *Erc1155DepositHandler) HandleDeposit(
sourceID,
destID uint8,
nonce uint64,
resourceID [32]byte,
calldata, handlerResponse []byte,
messageID string) (*message.Message, error) {
erc1155Type, err := GetErc1155Type()
if err != nil {
return nil, err
}

decodedCallData, err := erc1155Type.UnpackValues(calldata)
if err != nil {
return nil, err
}

payload := []interface{}{
decodedCallData[0],
decodedCallData[1],
decodedCallData[2],
decodedCallData[3],
}

return message.NewMessage(
sourceID,
destID,
transfer.TransferMessageData{
DepositNonce: nonce,
ResourceId: resourceID,
Metadata: nil,
Payload: payload,
Type: transfer.SemiFungibleTransfer,
},
messageID,
transfer.TransferMessageType), nil
}

func GetErc1155Type() (abi.Arguments, error) {
tokenIDsType, err := abi.NewType("uint256[]", "", nil)
if err != nil {
Expand Down Expand Up @@ -37,31 +75,3 @@ func GetErc1155Type() (abi.Arguments, error) {
abi.Argument{Name: "transferData", Type: transferDataType, Indexed: false},
}, nil
}

func (dh *Erc1155DepositHandler) HandleDeposit(sourceID, destId uint8, nonce uint64, resourceID [32]byte, calldata, handlerResponse []byte) (*message.Message, error) {

erc1155Type, err := GetErc1155Type()
if err != nil {
return nil, err
}

decodedCallData, err := erc1155Type.UnpackValues(calldata)
if err != nil {
return nil, err
}

payload := []interface{}{
decodedCallData[0],
decodedCallData[1],
decodedCallData[2],
decodedCallData[3],
}

return message.NewMessage(sourceID, destId, transfer.TransferMessageData{
DepositNonce: nonce,
ResourceId: resourceID,
Metadata: nil,
Payload: payload,
Type: transfer.SemiFungibleTransfer,
}, transfer.TransferMessageType), nil
}
19 changes: 17 additions & 2 deletions chains/evm/listener/depositHandlers/erc1155_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,17 @@ func (s *Erc1155HandlerTestSuite) Test_Erc1155HandleEvent() {
Type: transfer.SemiFungibleTransfer,
},
Type: transfer.TransferMessageType,
ID: "messageID",
}
erc1155DepositHandler := depositHandlers.Erc1155DepositHandler{}
message, err := erc1155DepositHandler.HandleDeposit(sourceID, depositLog.DestinationDomainID, depositLog.DepositNonce, depositLog.ResourceID, depositLog.Data, depositLog.HandlerResponse)
message, err := erc1155DepositHandler.HandleDeposit(
sourceID,
depositLog.DestinationDomainID,
depositLog.DepositNonce,
depositLog.ResourceID,
depositLog.Data,
depositLog.HandlerResponse,
"messageID")

s.Nil(err)
s.NotNil(message)
Expand All @@ -75,7 +83,14 @@ func (s *Erc1155HandlerTestSuite) Test_Erc1155HandleEvent_ErrInvalidCallData() {
sourceID := uint8(1)

erc1155DepositHandler := depositHandlers.Erc1155DepositHandler{}
message, err := erc1155DepositHandler.HandleDeposit(sourceID, depositLog.DestinationDomainID, depositLog.DepositNonce, depositLog.ResourceID, depositLog.Data, depositLog.HandlerResponse)
message, err := erc1155DepositHandler.HandleDeposit(
sourceID,
depositLog.DestinationDomainID,
depositLog.DepositNonce,
depositLog.ResourceID,
depositLog.Data,
depositLog.HandlerResponse,
"messageID")

s.Nil(message)
s.NotNil(err)
Expand Down
28 changes: 20 additions & 8 deletions chains/evm/listener/depositHandlers/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ type Erc20DepositHandler struct{}

// Erc20DepositHandler converts data pulled from event logs into message
// handlerResponse can be an empty slice
func (dh *Erc20DepositHandler) HandleDeposit(sourceID, destId uint8, nonce uint64, resourceID [32]byte, calldata, handlerResponse []byte) (*message.Message, error) {
func (dh *Erc20DepositHandler) HandleDeposit(
sourceID,
destID uint8,
nonce uint64,
resourceID [32]byte,
calldata,
handlerResponse []byte,
messageID string) (*message.Message, error) {
if len(calldata) < 84 {
err := errors.New("invalid calldata length: less than 84 bytes")
return nil, err
Expand All @@ -33,11 +40,16 @@ func (dh *Erc20DepositHandler) HandleDeposit(sourceID, destId uint8, nonce uint6
amount,
recipientAddress,
}
return message.NewMessage(sourceID, destId, transfer.TransferMessageData{
DepositNonce: nonce,
ResourceId: resourceID,
Metadata: nil,
Payload: payload,
Type: transfer.FungibleTransfer,
}, transfer.TransferMessageType), nil
return message.NewMessage(
sourceID,
destID,
transfer.TransferMessageData{
DepositNonce: nonce,
ResourceId: resourceID,
Metadata: nil,
Payload: payload,
Type: transfer.FungibleTransfer,
},
messageID,
transfer.TransferMessageType), nil
}
25 changes: 18 additions & 7 deletions chains/evm/listener/depositHandlers/erc20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ func TestRunErc20HandlerTestSuite(t *testing.T) {
suite.Run(t, new(Erc20HandlerTestSuite))
}

func (s *Erc20HandlerTestSuite) SetupSuite() {}
func (s *Erc20HandlerTestSuite) TearDownSuite() {}
func (s *Erc20HandlerTestSuite) SetupTest() {}
func (s *Erc20HandlerTestSuite) TearDownTest() {}

func (s *Erc20HandlerTestSuite) TestErc20HandleEvent() {
// 0xf1e58fb17704c2da8479a533f9fad4ad0993ca6b
recipientByteSlice := []byte{241, 229, 143, 177, 119, 4, 194, 218, 132, 121, 165, 51, 249, 250, 212, 173, 9, 147, 202, 107}
Expand Down Expand Up @@ -62,9 +57,17 @@ func (s *Erc20HandlerTestSuite) TestErc20HandleEvent() {
Type: transfer.FungibleTransfer,
},
Type: transfer.TransferMessageType,
ID: "messageID",
}
erc20DepositHandler := depositHandlers.Erc20DepositHandler{}
message, err := erc20DepositHandler.HandleDeposit(sourceID, depositLog.DestinationDomainID, depositLog.DepositNonce, depositLog.ResourceID, depositLog.Data, depositLog.HandlerResponse)
message, err := erc20DepositHandler.HandleDeposit(
sourceID,
depositLog.DestinationDomainID,
depositLog.DepositNonce,
depositLog.ResourceID,
depositLog.Data,
depositLog.HandlerResponse,
"messageID")

s.Nil(err)
s.NotNil(message)
Expand All @@ -90,7 +93,15 @@ func (s *Erc20HandlerTestSuite) TestErc20HandleEventIncorrectDataLen() {
sourceID := uint8(1)

erc20DepositHandler := depositHandlers.Erc20DepositHandler{}
message, err := erc20DepositHandler.HandleDeposit(sourceID, depositLog.DestinationDomainID, depositLog.DepositNonce, depositLog.ResourceID, depositLog.Data, depositLog.HandlerResponse)
message, err := erc20DepositHandler.HandleDeposit(
sourceID,
depositLog.DestinationDomainID,
depositLog.DepositNonce,
depositLog.ResourceID,
depositLog.Data,
depositLog.HandlerResponse,
"messageID",
)

s.Nil(message)
s.EqualError(err, errIncorrectDataLen.Error())
Expand Down
Loading

0 comments on commit 347657b

Please sign in to comment.