Skip to content

Commit

Permalink
channel through DepositTxHash and DepositBlock to the voter for possi…
Browse files Browse the repository at this point in the history
…ble further validation
  • Loading branch information
simone1999 committed Nov 10, 2022
1 parent 8d2afd5 commit 47df81e
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 46 deletions.
20 changes: 17 additions & 3 deletions chains/evm/calls/evmclient/evm-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ type DepositLogs struct {
HandlerResponse []byte
}

type DepositLogsEnriched struct {
// Deposit Logs
DepositLogs
// DepositTxHash
DepositTxHash common.Hash
// Deposit Block
DepositBlock uint64
}

type CommonTransaction interface {
// Hash returns the transaction hash.
Hash() common.Hash
Expand Down Expand Up @@ -162,12 +171,12 @@ func (c *EVMClient) GetTransactionByHash(h common.Hash) (tx *types.Transaction,
return c.Client.TransactionByHash(context.Background(), h)
}

func (c *EVMClient) FetchDepositLogs(ctx context.Context, contractAddress common.Address, startBlock *big.Int, endBlock *big.Int) ([]*DepositLogs, error) {
func (c *EVMClient) FetchDepositLogs(ctx context.Context, contractAddress common.Address, startBlock *big.Int, endBlock *big.Int) ([]*DepositLogsEnriched, error) {
logs, err := c.FilterLogs(ctx, buildQuery(contractAddress, string(util.Deposit), startBlock, endBlock))
if err != nil {
return nil, err
}
depositLogs := make([]*DepositLogs, 0)
depositLogs := make([]*DepositLogsEnriched, 0)

abi, err := abi.JSON(strings.NewReader(consts.BridgeABI))
if err != nil {
Expand All @@ -176,13 +185,18 @@ func (c *EVMClient) FetchDepositLogs(ctx context.Context, contractAddress common

for _, l := range logs {
dl, err := c.UnpackDepositEventLog(abi, l.Data)
dlEnriched := DepositLogsEnriched{
*dl,
l.TxHash,
l.BlockNumber,
}
if err != nil {
log.Error().Msgf("failed unpacking deposit event log: %v", err)
continue
}
log.Debug().Msgf("Found deposit log in block: %d, TxHash: %s, contractAddress: %s, sender: %s", l.BlockNumber, l.TxHash, l.Address, dl.SenderAddress)

depositLogs = append(depositLogs, dl)
depositLogs = append(depositLogs, &dlEnriched)
}

return depositLogs, nil
Expand Down
48 changes: 27 additions & 21 deletions chains/evm/listener/event-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

type EventHandlers map[common.Address]EventHandlerFunc
type EventHandlerFunc func(sourceID, destId uint8, nonce uint64, resourceID types.ResourceID, calldata, handlerResponse []byte) (*message.Message, error)
type EventHandlerFunc func(sourceID, destId uint8, nonce uint64, resourceID types.ResourceID, calldata, handlerResponse []byte, depositTxHash common.Hash, depositBlock uint64) (*message.Message, error)

type ETHEventHandler struct {
bridgeContract bridge.BridgeContract
Expand All @@ -27,7 +27,7 @@ func NewETHEventHandler(bridgeContract bridge.BridgeContract) *ETHEventHandler {
}
}

func (e *ETHEventHandler) HandleEvent(sourceID, destID uint8, depositNonce uint64, resourceID types.ResourceID, calldata, handlerResponse []byte) (*message.Message, error) {
func (e *ETHEventHandler) HandleEvent(sourceID, destID uint8, depositNonce uint64, resourceID types.ResourceID, calldata, handlerResponse []byte, depositTxHash common.Hash, depositBlock uint64) (*message.Message, error) {
handlerAddr, err := e.bridgeContract.GetHandlerAddressForResourceID(resourceID)
if err != nil {
return nil, err
Expand All @@ -38,7 +38,7 @@ func (e *ETHEventHandler) HandleEvent(sourceID, destID uint8, depositNonce uint6
return nil, err
}

return eventHandler(sourceID, destID, depositNonce, resourceID, calldata, handlerResponse)
return eventHandler(sourceID, destID, depositNonce, resourceID, calldata, handlerResponse, depositTxHash, depositBlock)
}

// matchAddressWithHandlerFunc matches a handler address with an associated handler function
Expand Down Expand Up @@ -67,7 +67,7 @@ func (e *ETHEventHandler) RegisterEventHandler(handlerAddress string, handler Ev

// Erc20EventHandler converts data pulled from event logs into message
// handlerResponse can be an empty slice
func Erc20EventHandler(sourceID, destId uint8, nonce uint64, resourceID types.ResourceID, calldata, handlerResponse []byte) (*message.Message, error) {
func Erc20EventHandler(sourceID, destId uint8, nonce uint64, resourceID types.ResourceID, calldata, handlerResponse []byte, depositTxHash common.Hash, depositBlock uint64) (*message.Message, error) {
if len(calldata) < 84 {
err := errors.New("invalid calldata length: less than 84 bytes")
return nil, err
Expand All @@ -91,11 +91,13 @@ func Erc20EventHandler(sourceID, destId uint8, nonce uint64, resourceID types.Re
recipientAddress := calldata[64:]

return &message.Message{
Source: sourceID,
Destination: destId,
DepositNonce: nonce,
ResourceId: resourceID,
Type: message.FungibleTransfer,
DepositTxHash: depositTxHash,
DepositBlock: depositBlock,
Source: sourceID,
Destination: destId,
DepositNonce: nonce,
ResourceId: resourceID,
Type: message.FungibleTransfer,
Payload: []interface{}{
amount,
recipientAddress,
Expand All @@ -104,7 +106,7 @@ func Erc20EventHandler(sourceID, destId uint8, nonce uint64, resourceID types.Re
}

// GenericEventHandler converts data pulled from generic deposit event logs into message
func GenericEventHandler(sourceID, destId uint8, nonce uint64, resourceID types.ResourceID, calldata, handlerResponse []byte) (*message.Message, error) {
func GenericEventHandler(sourceID, destId uint8, nonce uint64, resourceID types.ResourceID, calldata, handlerResponse []byte, depositTxHash common.Hash, depositBlock uint64) (*message.Message, error) {
if len(calldata) < 32 {
err := errors.New("invalid calldata length: less than 32 bytes")
return nil, err
Expand All @@ -114,19 +116,21 @@ func GenericEventHandler(sourceID, destId uint8, nonce uint64, resourceID types.
metadata := calldata[32:]

return &message.Message{
Source: sourceID,
Destination: destId,
DepositNonce: nonce,
ResourceId: resourceID,
Type: message.GenericTransfer,
DepositTxHash: depositTxHash,
DepositBlock: depositBlock,
Source: sourceID,
Destination: destId,
DepositNonce: nonce,
ResourceId: resourceID,
Type: message.GenericTransfer,
Payload: []interface{}{
metadata,
},
}, nil
}

// Erc721EventHandler converts data pulled from ERC721 deposit event logs into message
func Erc721EventHandler(sourceID, destId uint8, nonce uint64, resourceID types.ResourceID, calldata, handlerResponse []byte) (*message.Message, error) {
func Erc721EventHandler(sourceID, destId uint8, nonce uint64, resourceID types.ResourceID, calldata, handlerResponse []byte, depositTxHash common.Hash, depositBlock uint64) (*message.Message, error) {
if len(calldata) < 64 {
err := errors.New("invalid calldata length: less than 84 bytes")
return nil, err
Expand Down Expand Up @@ -154,11 +158,13 @@ func Erc721EventHandler(sourceID, destId uint8, nonce uint64, resourceID types.R
}

return &message.Message{
Source: sourceID,
Destination: destId,
DepositNonce: nonce,
ResourceId: resourceID,
Type: message.NonFungibleTransfer,
DepositTxHash: depositTxHash,
DepositBlock: depositBlock,
Source: sourceID,
Destination: destId,
DepositNonce: nonce,
ResourceId: resourceID,
Type: message.NonFungibleTransfer,
Payload: []interface{}{
tokenId,
recipientAddress,
Expand Down
6 changes: 3 additions & 3 deletions chains/evm/listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import (
)

type EventHandler interface {
HandleEvent(sourceID, destID uint8, nonce uint64, resourceID types.ResourceID, calldata, handlerResponse []byte) (*message.Message, error)
HandleEvent(sourceID, destID uint8, nonce uint64, resourceID types.ResourceID, calldata, handlerResponse []byte, depositTxHash common.Hash, depositBlock uint64) (*message.Message, error)
}
type ChainClient interface {
LatestBlock() (*big.Int, error)
FetchDepositLogs(ctx context.Context, address common.Address, startBlock *big.Int, endBlock *big.Int) ([]*evmclient.DepositLogs, error)
FetchDepositLogs(ctx context.Context, address common.Address, startBlock *big.Int, endBlock *big.Int) ([]*evmclient.DepositLogsEnriched, error)
CallContract(ctx context.Context, callArgs map[string]interface{}, blockNumber *big.Int) ([]byte, error)
}

Expand Down Expand Up @@ -80,7 +80,7 @@ func (l *EVMListener) ListenToEvents(
}
for _, eventLog := range logs {
log.Debug().Msgf("Deposit log found from sender: %s in block: %s with destinationDomainId: %v, resourceID: %X, depositNonce: %v", eventLog.SenderAddress, startBlock.String(), eventLog.DestinationDomainID, eventLog.ResourceID[:], eventLog.DepositNonce)
m, err := l.eventHandler.HandleEvent(domainID, eventLog.DestinationDomainID, eventLog.DepositNonce, eventLog.ResourceID, eventLog.Data, eventLog.HandlerResponse)
m, err := l.eventHandler.HandleEvent(domainID, eventLog.DestinationDomainID, eventLog.DepositNonce, eventLog.ResourceID, eventLog.Data, eventLog.HandlerResponse, eventLog.DepositTxHash, eventLog.DepositBlock)
if err != nil {
log.Error().Str("block", startBlock.String()).Uint8("domainID", domainID).Msgf("%v", err)
} else {
Expand Down
6 changes: 3 additions & 3 deletions chains/evm/listener/mock/listener.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions chains/evm/voter/message-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ func (mh *EVMMessageHandler) RegisterMessageHandler(address string, handler Mess
mh.handlers[common.HexToAddress(address)] = handler
}

func ERC20MessageHandler(m *message.Message, handlerAddr, bridgeAddress common.Address) (*proposal.Proposal, error) {
if len(m.Payload) != 2 {
func ERC20MessageHandler(msg *message.Message, handlerAddr, bridgeAddress common.Address) (*proposal.Proposal, error) {
if len(msg.Payload) != 2 {
return nil, errors.New("malformed payload. Len of payload should be 2")
}
amount, ok := m.Payload[0].([]byte)
amount, ok := msg.Payload[0].([]byte)
if !ok {
return nil, errors.New("wrong payloads amount format")
}
recipient, ok := m.Payload[1].([]byte)
recipient, ok := msg.Payload[1].([]byte)
if !ok {
return nil, errors.New("wrong payloads recipient format")
}
Expand All @@ -86,7 +86,7 @@ func ERC20MessageHandler(m *message.Message, handlerAddr, bridgeAddress common.A
recipientLen := big.NewInt(int64(len(recipient))).Bytes()
data = append(data, common.LeftPadBytes(recipientLen, 32)...) // length of recipient (uint256)
data = append(data, recipient...) // recipient ([]byte)
return proposal.NewProposal(m.Source, m.DepositNonce, m.ResourceId, data, handlerAddr, bridgeAddress), nil
return proposal.NewProposal(msg.Source, msg.DepositNonce, msg.ResourceId, data, handlerAddr, bridgeAddress, msg.DepositTxHash, msg.DepositBlock), nil
}

func ERC721MessageHandler(msg *message.Message, handlerAddr, bridgeAddress common.Address) (*proposal.Proposal, error) {
Expand All @@ -113,7 +113,7 @@ func ERC721MessageHandler(msg *message.Message, handlerAddr, bridgeAddress commo
metadataLen := big.NewInt(int64(len(metadata))).Bytes()
data.Write(common.LeftPadBytes(metadataLen, 32))
data.Write(metadata)
return proposal.NewProposal(msg.Source, msg.DepositNonce, msg.ResourceId, data.Bytes(), handlerAddr, bridgeAddress), nil
return proposal.NewProposal(msg.Source, msg.DepositNonce, msg.ResourceId, data.Bytes(), handlerAddr, bridgeAddress, msg.DepositTxHash, msg.DepositBlock), nil
}

func GenericMessageHandler(msg *message.Message, handlerAddr, bridgeAddress common.Address) (*proposal.Proposal, error) {
Expand All @@ -128,5 +128,5 @@ func GenericMessageHandler(msg *message.Message, handlerAddr, bridgeAddress comm
metadataLen := big.NewInt(int64(len(metadata))).Bytes()
data.Write(common.LeftPadBytes(metadataLen, 32)) // length of metadata (uint256)
data.Write(metadata)
return proposal.NewProposal(msg.Source, msg.DepositNonce, msg.ResourceId, data.Bytes(), handlerAddr, bridgeAddress), nil
return proposal.NewProposal(msg.Source, msg.DepositNonce, msg.ResourceId, data.Bytes(), handlerAddr, bridgeAddress, msg.DepositTxHash, msg.DepositBlock), nil
}
10 changes: 7 additions & 3 deletions chains/evm/voter/proposal/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"github.com/ethereum/go-ethereum/crypto"
)

func NewProposal(source uint8, depositNonce uint64, resourceId types.ResourceID, data []byte, handlerAddress, bridgeAddress common.Address) *Proposal {
func NewProposal(source uint8, depositNonce uint64, resourceId types.ResourceID, data []byte, handlerAddress, bridgeAddress common.Address, depositTxHash common.Hash, depositBlock uint64) *Proposal {
return &Proposal{
DepositTxHash: depositTxHash,
DepositBlock: depositBlock,
Source: source,
DepositNonce: depositNonce,
ResourceId: resourceId,
Expand All @@ -18,8 +20,10 @@ func NewProposal(source uint8, depositNonce uint64, resourceId types.ResourceID,
}

type Proposal struct {
Source uint8 // Source domainID where message was initiated
DepositNonce uint64 // Nonce for the deposit
DepositTxHash common.Hash // transaction hash of the deposit transaction
DepositBlock uint64 // Block the Deposit transaction were made
Source uint8 // Source domainID where message was initiated
DepositNonce uint64 // Nonce for the deposit
ResourceId types.ResourceID
Payload []interface{} // data associated with event sequence
Data []byte
Expand Down
15 changes: 9 additions & 6 deletions relayer/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package message
import (
"errors"
"fmt"
"github.com/ethereum/go-ethereum/common"
"math/big"

"github.com/ChainSafe/chainbridge-core/types"
Expand Down Expand Up @@ -39,12 +40,14 @@ var (
)

type Message struct {
Source uint8 // Source where message was initiated
Destination uint8 // Destination chain of message
DepositNonce uint64 // Nonce for the deposit
ResourceId types.ResourceID
Payload []interface{} // data associated with event sequence
Type TransferType
DepositTxHash common.Hash // transaction hash of the deposit transaction
DepositBlock uint64 // Block the Deposit transaction were made
Source uint8 // Source where message was initiated
Destination uint8 // Destination chain of message
DepositNonce uint64 // Nonce for the deposit
ResourceId types.ResourceID
Payload []interface{} // data associated with event sequence
Type TransferType
}

func (m *Message) String() string {
Expand Down

0 comments on commit 47df81e

Please sign in to comment.