Skip to content

Commit

Permalink
Updated decoding of events and did other necessary package changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mj52951 committed Nov 13, 2023
1 parent a6a2b78 commit 0b11d6c
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 38 deletions.
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func Run() error {
if err != nil {
panic(err)
}
keyPair, err := signature.KeyringPairFromSecret(config.GeneralChainConfig.Key, uint16(config.SubstrateNetwork))
keyPair, err := signature.KeyringPairFromSecret(config.GeneralChainConfig.Key, config.SubstrateNetwork)
if err != nil {
panic(err)
}
Expand Down
6 changes: 3 additions & 3 deletions chains/substrate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ type SubstrateConfig struct {
StartBlock *big.Int
BlockInterval *big.Int
BlockRetryInterval time.Duration
SubstrateNetwork uint8
SubstrateNetwork uint16
Tip uint64
}

func (c *SubstrateConfig) String() string {
kp, _ := signature.KeyringPairFromSecret(c.GeneralChainConfig.Key, uint16(c.SubstrateNetwork))
kp, _ := signature.KeyringPairFromSecret(c.GeneralChainConfig.Key, c.SubstrateNetwork)
return fmt.Sprintf(`Name: '%s', Id: '%d', Type: '%s', BlockstorePath: '%s', FreshStart: '%t',
LatestBlock: '%t', Key address: '%s', StartBlock: '%s', BlockInterval: '%s',
BlockRetryInterval: '%s', ChainID: '%d', Tip: '%d', SubstrateNetworkPrefix: "%d"`,
Expand Down Expand Up @@ -90,7 +90,7 @@ func NewSubstrateConfig(chainConfig map[string]interface{}) (*SubstrateConfig, e
BlockRetryInterval: time.Duration(c.BlockRetryInterval) * time.Second,
StartBlock: big.NewInt(c.StartBlock),
BlockInterval: big.NewInt(c.BlockInterval),
SubstrateNetwork: uint8(c.SubstrateNetwork),
SubstrateNetwork: uint16(c.SubstrateNetwork),
Tip: uint64(c.Tip),
}

Expand Down
4 changes: 2 additions & 2 deletions chains/substrate/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (s *NewSubstrateConfigTestSuite) Test_ValidConfig() {
},
StartBlock: big.NewInt(0),
ChainID: big.NewInt(5),
SubstrateNetwork: uint8(0),
SubstrateNetwork: uint16(0),
BlockInterval: big.NewInt(5),
BlockRetryInterval: time.Duration(5) * time.Second,
})
Expand Down Expand Up @@ -89,7 +89,7 @@ func (s *NewSubstrateConfigTestSuite) Test_ValidConfigWithCustomParams() {
Id: id,
},
ChainID: big.NewInt(5),
SubstrateNetwork: uint8(0),
SubstrateNetwork: uint16(0),
StartBlock: big.NewInt(1000),
BlockInterval: big.NewInt(2),
BlockRetryInterval: time.Duration(10) * time.Second,
Expand Down
72 changes: 65 additions & 7 deletions chains/substrate/listener/event-handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/ChainSafe/chainbridge-core/relayer/message"
"github.com/ChainSafe/sygma-relayer/chains/substrate/events"
"github.com/centrifuge/go-substrate-rpc-client/v4/registry"
"github.com/centrifuge/go-substrate-rpc-client/v4/registry/parser"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -41,6 +42,67 @@ func (eh *SystemUpdateEventHandler) HandleEvents(evts []*parser.Event, msgChan c
return nil
}

func DecodeEventToDeposit(evtFields registry.DecodedFields) (events.Deposit, error) {
var d events.Deposit

for _, evtField := range evtFields {
switch evtField.Name {
case "dest_domain_id":
err := mapstructure.Decode(evtField.Value, &d.DestDomainID)
if err != nil {
return events.Deposit{}, err
}
case "resource_id":
err := mapstructure.Decode(evtField.Value, &d.ResourceID)
if err != nil {
return events.Deposit{}, err
}
case "deposit_nonce":
err := mapstructure.Decode(evtField.Value, &d.DepositNonce)
if err != nil {
return events.Deposit{}, err
}
case "sygma_traits_TransferType":
err := mapstructure.Decode(evtField.Value, &d.TransferType)
if err != nil {
return events.Deposit{}, err
}
case "deposit_data":
err := mapstructure.Decode(evtField.Value, &d.CallData)
if err != nil {
return events.Deposit{}, err
}
case "handler_response":
err := mapstructure.Decode(evtField.Value, &d.Handler)
if err != nil {
return events.Deposit{}, err
}
}
}
return d, nil
}

func DecodeEventToRetry(evtFields registry.DecodedFields) (events.Retry, error) {
var er events.Retry

for _, evtField := range evtFields {
switch evtField.Name {
case "deposit_on_block_height":
err := mapstructure.Decode(evtField.Value, &er.DepositOnBlockHeight)
if err != nil {
return events.Retry{}, err
}
case "dest_domain_id":
err := mapstructure.Decode(evtField.Value, &er.DestDomainID)
if err != nil {
return events.Retry{}, err
}
}
}

return er, nil
}

type DepositHandler interface {
HandleDeposit(sourceID uint8, destID types.U8, nonce types.U64, resourceID types.Bytes32, calldata []byte, transferType types.U8) (*message.Message, error)
}
Expand Down Expand Up @@ -70,9 +132,7 @@ func (eh *FungibleTransferEventHandler) HandleEvents(evts []*parser.Event, msgCh
log.Error().Msgf("panic occured while handling deposit %+v", evt)
}
}()

var d events.Deposit
err := mapstructure.Decode(evt.Fields[0].Value, &d)
d, err := DecodeEventToDeposit(evt.Fields)
if err != nil {
log.Error().Err(err).Msgf("%v", err)
return
Expand Down Expand Up @@ -135,8 +195,7 @@ func (rh *RetryEventHandler) HandleEvents(evts []*parser.Event, msgChan chan []*
log.Error().Msgf("panic occured while handling retry event %+v because %s", evt, r)
}
}()
var er events.Retry
err = mapstructure.Decode(evt.Fields[0].Value, &er)
er, err := DecodeEventToRetry(evt.Fields)
if err != nil {
return err
}
Expand All @@ -158,8 +217,7 @@ func (rh *RetryEventHandler) HandleEvents(evts []*parser.Event, msgChan chan []*

for _, event := range bEvts {
if event.Name == events.DepositEvent {
var d events.Deposit
err = mapstructure.Decode(event.Fields[0].Value, &d)
d, err := DecodeEventToDeposit(event.Fields)
if err != nil {
return err
}
Expand Down
84 changes: 67 additions & 17 deletions chains/substrate/listener/event-handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,23 @@ func (s *DepositHandlerTestSuite) Test_HandleDepositFails_ExecutionContinue() {
{
Name: "SygmaBridge.Deposit",
Fields: registry.DecodedFields{
&registry.DecodedField{Name: "SygmaBridge.Deposit", Value: d1},
&registry.DecodedField{Name: "dest_domain_id", Value: d1["dest_domain_id"]},
&registry.DecodedField{Name: "resource_id", Value: d1["resource_id"]},
&registry.DecodedField{Name: "deposit_nonce", Value: d1["deposit_nonce"]},
&registry.DecodedField{Name: "sygma_traits_TransferType", Value: d1["sygma_traits_TransferType"]},
&registry.DecodedField{Name: "deposit_data", Value: d1["deposit_data"]},
&registry.DecodedField{Name: "handler_response", Value: d1["handler_response"]},
},
},
{
Name: "SygmaBridge.Deposit",
Fields: registry.DecodedFields{
&registry.DecodedField{Name: "SygmaBridge.Deposit", Value: d2},
&registry.DecodedField{Name: "dest_domain_id", Value: d2["dest_domain_id"]},
&registry.DecodedField{Name: "resource_id", Value: d2["resource_id"]},
&registry.DecodedField{Name: "deposit_nonce", Value: d2["deposit_nonce"]},
&registry.DecodedField{Name: "sygma_traits_TransferType", Value: d2["sygma_traits_TransferType"]},
&registry.DecodedField{Name: "deposit_data", Value: d2["deposit_data"]},
&registry.DecodedField{Name: "handler_response", Value: d2["handler_response"]},
},
},
}
Expand Down Expand Up @@ -199,13 +209,23 @@ func (s *DepositHandlerTestSuite) Test_SuccessfulHandleDeposit() {
{
Name: "SygmaBridge.Deposit",
Fields: registry.DecodedFields{
&registry.DecodedField{Name: "SygmaBridge.Deposit", Value: d1},
&registry.DecodedField{Name: "dest_domain_id", Value: d1["dest_domain_id"]},
&registry.DecodedField{Name: "resource_id", Value: d1["resource_id"]},
&registry.DecodedField{Name: "deposit_nonce", Value: d1["deposit_nonce"]},
&registry.DecodedField{Name: "sygma_traits_TransferType", Value: d1["sygma_traits_TransferType"]},
&registry.DecodedField{Name: "deposit_data", Value: d1["deposit_data"]},
&registry.DecodedField{Name: "handler_response", Value: d1["handler_response"]},
},
},
{
Name: "SygmaBridge.Deposit",
Fields: registry.DecodedFields{
&registry.DecodedField{Name: "SygmaBridge.Deposit", Value: d2},
&registry.DecodedField{Name: "dest_domain_id", Value: d2["dest_domain_id"]},
&registry.DecodedField{Name: "resource_id", Value: d2["resource_id"]},
&registry.DecodedField{Name: "deposit_nonce", Value: d2["deposit_nonce"]},
&registry.DecodedField{Name: "sygma_traits_TransferType", Value: d2["sygma_traits_TransferType"]},
&registry.DecodedField{Name: "deposit_data", Value: d2["deposit_data"]},
&registry.DecodedField{Name: "handler_response", Value: d2["handler_response"]},
},
},
}
Expand Down Expand Up @@ -260,13 +280,23 @@ func (s *DepositHandlerTestSuite) Test_HandleDepositPanics_ExecutionContinues()
{
Name: "SygmaBridge.Deposit",
Fields: registry.DecodedFields{
&registry.DecodedField{Name: "SygmaBridge.Deposit", Value: d1},
&registry.DecodedField{Name: "dest_domain_id", Value: d1["dest_domain_id"]},
&registry.DecodedField{Name: "resource_id", Value: d1["resource_id"]},
&registry.DecodedField{Name: "deposit_nonce", Value: d1["deposit_nonce"]},
&registry.DecodedField{Name: "sygma_traits_TransferType", Value: d1["sygma_traits_TransferType"]},
&registry.DecodedField{Name: "deposit_data", Value: d1["deposit_data"]},
&registry.DecodedField{Name: "handler_response", Value: d1["handler_response"]},
},
},
{
Name: "SygmaBridge.Deposit",
Fields: registry.DecodedFields{
&registry.DecodedField{Name: "SygmaBridge.Deposit", Value: d2},
&registry.DecodedField{Name: "dest_domain_id", Value: d2["dest_domain_id"]},
&registry.DecodedField{Name: "resource_id", Value: d2["resource_id"]},
&registry.DecodedField{Name: "deposit_nonce", Value: d2["deposit_nonce"]},
&registry.DecodedField{Name: "sygma_traits_TransferType", Value: d2["sygma_traits_TransferType"]},
&registry.DecodedField{Name: "deposit_data", Value: d2["deposit_data"]},
&registry.DecodedField{Name: "handler_response", Value: d2["handler_response"]},
},
},
}
Expand Down Expand Up @@ -322,7 +352,7 @@ func (s *RetryHandlerTestSuite) Test_EventTooNew() {
{
Name: "SygmaBridge.Retry",
Fields: registry.DecodedFields{
&registry.DecodedField{Name: "SygmaBridge.Retry", Value: rtry},
&registry.DecodedField{Name: "deposit_on_block_height", Value: rtry["deposit_on_block_height"]},
},
},
}
Expand Down Expand Up @@ -351,7 +381,7 @@ func (s *RetryHandlerTestSuite) Test_FetchingBlockHashFails() {
{
Name: "SygmaBridge.Retry",
Fields: registry.DecodedFields{
&registry.DecodedField{Name: "SygmaBridge.Retry", Value: rtry},
&registry.DecodedField{Name: "deposit_on_block_height", Value: rtry["deposit_on_block_height"]},
},
},
}
Expand Down Expand Up @@ -380,7 +410,7 @@ func (s *RetryHandlerTestSuite) Test_FetchingBlockEventsFails() {
{
Name: "SygmaBridge.Retry",
Fields: registry.DecodedFields{
&registry.DecodedField{Name: "SygmaBridge.Retry", Value: rtry},
&registry.DecodedField{Name: "deposit_on_block_height", Value: rtry["deposit_on_block_height"]},
},
},
}
Expand Down Expand Up @@ -409,7 +439,7 @@ func (s *RetryHandlerTestSuite) Test_NoEvents() {
{
Name: "SygmaBridge.Retry",
Fields: registry.DecodedFields{
&registry.DecodedField{Name: "SygmaBridge.Retry", Value: rtry},
&registry.DecodedField{Name: "deposit_on_block_height", Value: rtry["deposit_on_block_height"]},
},
},
}
Expand Down Expand Up @@ -447,13 +477,23 @@ func (s *RetryHandlerTestSuite) Test_ValidEvents() {
{
Name: "SygmaBridge.Deposit",
Fields: registry.DecodedFields{
&registry.DecodedField{Name: "SygmaBridge.Deposit", Value: d1},
&registry.DecodedField{Name: "dest_domain_id", Value: d1["dest_domain_id"]},
&registry.DecodedField{Name: "resource_id", Value: d1["resource_id"]},
&registry.DecodedField{Name: "deposit_nonce", Value: d1["deposit_nonce"]},
&registry.DecodedField{Name: "sygma_traits_TransferType", Value: d1["sygma_traits_TransferType"]},
&registry.DecodedField{Name: "deposit_data", Value: d1["deposit_data"]},
&registry.DecodedField{Name: "handler_response", Value: d1["handler_response"]},
},
},
{
Name: "SygmaBridge.Deposit",
Fields: registry.DecodedFields{
&registry.DecodedField{Name: "SygmaBridge.Deposit", Value: d2},
&registry.DecodedField{Name: "dest_domain_id", Value: d2["dest_domain_id"]},
&registry.DecodedField{Name: "resource_id", Value: d2["resource_id"]},
&registry.DecodedField{Name: "deposit_nonce", Value: d2["deposit_nonce"]},
&registry.DecodedField{Name: "sygma_traits_TransferType", Value: d2["sygma_traits_TransferType"]},
&registry.DecodedField{Name: "deposit_data", Value: d2["deposit_data"]},
&registry.DecodedField{Name: "handler_response", Value: d2["handler_response"]},
},
},
}
Expand Down Expand Up @@ -490,7 +530,7 @@ func (s *RetryHandlerTestSuite) Test_ValidEvents() {
{
Name: "SygmaBridge.Retry",
Fields: registry.DecodedFields{
&registry.DecodedField{Name: "SygmaBridge.Retry", Value: rtry},
&registry.DecodedField{Name: "deposit_on_block_height", Value: rtry["deposit_on_block_height"]},
},
},
}
Expand Down Expand Up @@ -532,15 +572,25 @@ func (s *RetryHandlerTestSuite) Test_EventPanics() {
{
Name: "SygmaBridge.Deposit",
Fields: registry.DecodedFields{
&registry.DecodedField{Name: "SygmaBridge.Deposit", Value: d1},
&registry.DecodedField{Name: "dest_domain_id", Value: d1["dest_domain_id"]},
&registry.DecodedField{Name: "resource_id", Value: d1["resource_id"]},
&registry.DecodedField{Name: "deposit_nonce", Value: d1["deposit_nonce"]},
&registry.DecodedField{Name: "sygma_traits_TransferType", Value: d1["sygma_traits_TransferType"]},
&registry.DecodedField{Name: "deposit_data", Value: d1["deposit_data"]},
&registry.DecodedField{Name: "handler_response", Value: d1["handler_response"]},
},
},
}
blockEvts2 := []*parser.Event{
{
Name: "SygmaBridge.Deposit",
Fields: registry.DecodedFields{
&registry.DecodedField{Name: "SygmaBridge.Deposit", Value: d2},
&registry.DecodedField{Name: "dest_domain_id", Value: d2["dest_domain_id"]},
&registry.DecodedField{Name: "resource_id", Value: d2["resource_id"]},
&registry.DecodedField{Name: "deposit_nonce", Value: d2["deposit_nonce"]},
&registry.DecodedField{Name: "sygma_traits_TransferType", Value: d2["sygma_traits_TransferType"]},
&registry.DecodedField{Name: "deposit_data", Value: d2["deposit_data"]},
&registry.DecodedField{Name: "handler_response", Value: d2["handler_response"]},
},
},
}
Expand Down Expand Up @@ -577,13 +627,13 @@ func (s *RetryHandlerTestSuite) Test_EventPanics() {
{
Name: "SygmaBridge.Retry",
Fields: registry.DecodedFields{
&registry.DecodedField{Name: "SygmaBridgeRetry", Value: rtry},
&registry.DecodedField{Name: "deposit_on_block_height", Value: rtry["deposit_on_block_height"]},
},
},
{
Name: "SygmaBridge.Retry",
Fields: registry.DecodedFields{
&registry.DecodedField{Name: "SygmaBridgeRetry", Value: rtry},
&registry.DecodedField{Name: "deposit_on_block_height", Value: rtry["deposit_on_block_height"]},
},
},
}
Expand Down
6 changes: 3 additions & 3 deletions cli/utils/derivateSS58AccountFromPK.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ var (

var (
privateKey string
networkID uint8
networkID uint16
)

func init() {
derivateSS58AccountFromPKCMD.PersistentFlags().StringVar(&privateKey, "privateKey", "", "hex encoded private key")
_ = derivateSS58AccountFromPKCMD.MarkFlagRequired("privateKey")
derivateSS58AccountFromPKCMD.PersistentFlags().Uint8Var(&networkID, "networkID", 0, "network id for a checksum. Registry https://github.com/paritytech/ss58-registry/blob/main/ss58-registry.json")
derivateSS58AccountFromPKCMD.PersistentFlags().Uint16Var(&networkID, "networkID", 0, "network id for a checksum. Registry https://github.com/paritytech/ss58-registry/blob/main/ss58-registry.json")
_ = derivateSS58AccountFromPKCMD.MarkFlagRequired("networkID")
}

func derivateSS58(cmd *cobra.Command, args []string) error {
account, err := signature.KeyringPairFromSecret(privateKey, uint16(networkID))
account, err := signature.KeyringPairFromSecret(privateKey, networkID)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 0b11d6c

Please sign in to comment.