Skip to content

Commit

Permalink
Simplify message ID generated by Relayer (#117)
Browse files Browse the repository at this point in the history
* Simplify formatting of contract address in message id

* Fix message id generator in Go SDK
  • Loading branch information
ermyas authored May 1, 2022
1 parent ceb5a77 commit 441cc34
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 17 deletions.
3 changes: 2 additions & 1 deletion docs/message-observer.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ However, the degree to which the ID scheme can guarantee the above properties is
mitigating the possibility of reorgs.

The current ID scheme implemented for a message involves the following structure:
`{network_id}-{contract_address}-{block_number}-{tx_index}-{log_index}`.
`{network_id}-{contract_address}-{block_number}-{tx_index}-{log_index}`.
The contract address is a lowercase hex string and not in [EIP-55](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md) format.

### Message Handler

Expand Down
2 changes: 1 addition & 1 deletion sdk/go/executor/executor_impl_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,5 +432,5 @@ func (exec *ExecutorImplV1) updateTransactOpts(chainAP *ethclient.Client) error

// getEventID gets the ID of an event.
func getEventID(chainID *big.Int, raw types.Log) string {
return fmt.Sprintf("%s-%s-%d-%d-%d", chainID.String(), raw.Address.String(), raw.BlockNumber, raw.TxIndex, raw.Index)
return fmt.Sprintf("%s-%#x-%d-%d-%d", chainID.String(), raw.Address, raw.BlockNumber, raw.TxIndex, raw.Index)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/common"
"time"

v1 "github.com/consensys/gpact/services/relayer/pkg/messages/v1"
Expand All @@ -34,7 +35,7 @@ var startFuncSig = [32]byte{0x77, 0xda, 0xb6, 0x11, 0xad, 0x9a, 0x24, 0xb7, 0x63
var segmentFuncSig = [32]byte{0xb0, 0x15, 0x57, 0xf1, 0xf6, 0x34, 0xb7, 0xc5, 0x07, 0x2a, 0xb5, 0xe3, 0x6d, 0x07, 0xa2, 0x35, 0x5e, 0xf8, 0x19, 0xfa, 0xca, 0x5a, 0x3d, 0x32, 0x14, 0x30, 0xd7, 0x19, 0x87, 0x15, 0x5b, 0x8f}
var rootFuncSig = [32]byte{0xe6, 0x76, 0x3d, 0xd9, 0x9b, 0xf8, 0x94, 0xd7, 0x2f, 0x34, 0x99, 0xdd, 0x57, 0x2a, 0xa4, 0x28, 0x76, 0xea, 0xe7, 0xae, 0x02, 0x8c, 0x32, 0xff, 0xf2, 0x16, 0x54, 0xe1, 0xbb, 0xc4, 0xc8, 0x07}

const MessageIDPattern = "%s-%s-%d-%d-%d"
const MessageIDPattern = "%s-%#x-%d-%d-%d"

type EventTransformer interface {
// ToMessage converts a given event to a relayer message
Expand All @@ -44,7 +45,7 @@ type EventTransformer interface {
// SFCEventTransformer converts events from a simple-function-call bridge contract to relayer messages
type SFCEventTransformer struct {
Source string
SourceAddr string
SourceAddr common.Address
}

// ToMessage converts a 'CrossCall' event emited from a simple-function-call bridge contract to relayer message
Expand All @@ -57,7 +58,7 @@ func (t *SFCEventTransformer) ToMessage(event interface{}) (*v1.Message, error)
return nil, err
}

source := v1.ApplicationAddress{NetworkID: t.Source, ContractAddress: t.SourceAddr}
source := v1.ApplicationAddress{NetworkID: t.Source, ContractAddress: fmt.Sprintf("%#x", t.SourceAddr)}
destination := v1.ApplicationAddress{NetworkID: sfcEvent.DestBcId.String(), ContractAddress: sfcEvent.DestContract.String()}

data, err := json.Marshal(sfcEvent.Raw)
Expand Down Expand Up @@ -97,13 +98,13 @@ func (t *SFCEventTransformer) getIDForEvent(event types.Log) string {
return fmt.Sprintf(MessageIDPattern, t.Source, t.SourceAddr, event.BlockNumber, event.TxIndex, event.Index)
}

func NewSFCEventTransformer(sourceNetwork string, sourceAddr string) *SFCEventTransformer {
func NewSFCEventTransformer(sourceNetwork string, sourceAddr common.Address) *SFCEventTransformer {
return &SFCEventTransformer{sourceNetwork, sourceAddr}
}

type GPACTEventTransformer struct {
Source string
SourceAddr string
SourceAddr common.Address
}

func (t *GPACTEventTransformer) ToMessage(event interface{}) (*v1.Message, error) {
Expand All @@ -129,7 +130,7 @@ func (t *GPACTEventTransformer) ToMessage(event interface{}) (*v1.Message, error
}
}

source := v1.ApplicationAddress{NetworkID: t.Source, ContractAddress: t.SourceAddr}
source := v1.ApplicationAddress{NetworkID: t.Source, ContractAddress: fmt.Sprintf("%#x", t.SourceAddr)}
destination := v1.ApplicationAddress{NetworkID: "0", ContractAddress: ""}

data, err := json.Marshal(raw)
Expand Down Expand Up @@ -157,6 +158,6 @@ func (t *GPACTEventTransformer) getIDForEvent(event types.Log) string {
return fmt.Sprintf(MessageIDPattern, t.Source, t.SourceAddr, event.BlockNumber, event.TxIndex, event.Index)
}

func NewGPACTEventTransformer(sourceNetwork string, sourceAddr string) *GPACTEventTransformer {
func NewGPACTEventTransformer(sourceNetwork string, sourceAddr common.Address) *GPACTEventTransformer {
return &GPACTEventTransformer{sourceNetwork, sourceAddr}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ var fixValidEvent = functioncall.SfcCrossCall{
Raw: fixLog,
}

var transformer = NewSFCEventTransformer("network-001", "0x8e215d06ea7ec1fdb4fc5fd21768f4b34ee92ef4")
var transformer = NewSFCEventTransformer("network-001",
common.HexToAddress("0x8e215d06ea7ec1fdb4fc5fd21768f4b34ee92ef4"))

func TestSFCTransformer(t *testing.T) {
message, err := transformer.ToMessage(&fixValidEvent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package observer

import (
"context"
"github.com/ethereum/go-ethereum/common"

"github.com/consensys/gpact/services/relayer/internal/contracts/functioncall"
"github.com/consensys/gpact/services/relayer/internal/mqserver"
Expand All @@ -30,7 +31,8 @@ type SFCBridgeObserver struct {
SourceNetwork string
}

func NewSFCBridgeRealtimeObserver(source string, sourceAddr string, contract *functioncall.Sfc, mq mqserver.MessageQueue) (*SFCBridgeObserver, error) {
func NewSFCBridgeRealtimeObserver(source string, sourceAddr common.Address, contract *functioncall.Sfc,
mq mqserver.MessageQueue) (*SFCBridgeObserver, error) {
eventTransformer := NewSFCEventTransformer(source, sourceAddr)
messageHandler := NewMessageEnqueueHandler(mq, DefaultRetryOptions)
eventHandler := NewSimpleEventHandler(eventTransformer, messageHandler)
Expand All @@ -46,7 +48,7 @@ func NewSFCBridgeRealtimeObserver(source string, sourceAddr string, contract *fu
return &SFCBridgeObserver{EventWatcher: eventWatcher, EventHandler: eventHandler, SourceNetwork: source}, nil
}

func NewSFCBridgeFinalisedObserver(source string, sourceAddr string, contract *functioncall.Sfc, mq mqserver.MessageQueue,
func NewSFCBridgeFinalisedObserver(source string, sourceAddr common.Address, contract *functioncall.Sfc, mq mqserver.MessageQueue,
confirmationsForFinality uint64, watcherProgressOpts WatcherProgressDsOpts, client BlockHeadProducer) (
*SFCBridgeObserver,
error) {
Expand Down Expand Up @@ -81,7 +83,8 @@ type GPACTBridgeObserver struct {
SourceNetwork string
}

func NewGPACTBridgeRealtimeObserver(source string, sourceAddr string, contract *functioncall.Gpact, mq mqserver.MessageQueue) (*GPACTBridgeObserver, error) {
func NewGPACTBridgeRealtimeObserver(source string, sourceAddr common.Address, contract *functioncall.Gpact,
mq mqserver.MessageQueue) (*GPACTBridgeObserver, error) {
eventTransformer := NewGPACTEventTransformer(source, sourceAddr)
messageHandler := NewMessageEnqueueHandler(mq, DefaultRetryOptions)
eventHandler := NewSimpleEventHandler(eventTransformer, messageHandler)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestSFCBridgeObserver(t *testing.T) {
fixDesAddress := common.HexToAddress("0x8e215d06ea7ec1fdb4fc5fd21768f4b34ee92ef4")
fixDestID := big.NewInt(2)
fixSourceID := "1"
fixSourceAddress := "0x8e215d06ea7ec1fdb4fc5fd21768f4b34ee92ef4"
fixSourceAddress := common.HexToAddress("0x8e215d06ea7ec1fdb4fc5fd21768f4b34ee92ef4")

simBackend, auth := simulatedBackend(t)
contract := deploySFCContract(t, simBackend, auth)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (o *ObserverImplV1) routineSFC(chainID *big.Int, chainAP string, addr commo
return
}

observer, err := o.createFinalisedEventObserver(chainID.String(), addr.String(), sfc, o.mq, chain)
observer, err := o.createFinalisedEventObserver(chainID.String(), addr, sfc, o.mq, chain)

if err != nil {
logging.Error(err.Error())
Expand Down Expand Up @@ -207,7 +207,7 @@ func (o *ObserverImplV1) routineGPACT(chainID *big.Int, chainAP string, addr com
return
}

observer, err := NewGPACTBridgeRealtimeObserver(chainID.String(), addr.String(), gpact, o.mq)
observer, err := NewGPACTBridgeRealtimeObserver(chainID.String(), addr, gpact, o.mq)

if err != nil {
logging.Error(err.Error())
Expand All @@ -224,7 +224,8 @@ func (o *ObserverImplV1) routineGPACT(chainID *big.Int, chainAP string, addr com
}
}

func (o *ObserverImplV1) createFinalisedEventObserver(source string, sourceAddr string, contract *functioncall.Sfc, mq mqserver.MessageQueue,
func (o *ObserverImplV1) createFinalisedEventObserver(source string, sourceAddr common.Address, contract *functioncall.Sfc,
mq mqserver.MessageQueue,
client *ethclient.Client) (*SFCBridgeObserver, error) {
dsProgKey := datastore.NewKey(fmt.Sprintf("/%s/%s/last_finalised_block", source, sourceAddr))
watcherProgOpts := WatcherProgressDsOpts{o.ds, dsProgKey, DefaultRetryOptions}
Expand Down

0 comments on commit 441cc34

Please sign in to comment.