forked from ChainSafe/chainbridge-core
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Multiple GSRPC version support #31
Merged
mpetrun5
merged 6 commits into
fix/check-metadata-hash
from
freddy/multiple-go-substrate-version-support
Aug 19, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
214 changes: 214 additions & 0 deletions
214
chains/substrate/client/check_metadata_hash_enabled_client.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
// The Licensed Work is (c) 2022 Sygma | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package client | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"math/big" | ||
"sync" | ||
"time" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/sygmaprotocol/go-substrate-rpc-client/v4/rpc/author" | ||
"github.com/sygmaprotocol/go-substrate-rpc-client/v4/scale" | ||
"github.com/sygmaprotocol/go-substrate-rpc-client/v4/signature" | ||
"github.com/sygmaprotocol/go-substrate-rpc-client/v4/types" | ||
"github.com/sygmaprotocol/sygma-core/chains/substrate/connection" | ||
"github.com/sygmaprotocol/sygma-core/chains/substrate/events" | ||
) | ||
|
||
type SubstrateCheckMetadataModeEnabledClient struct { | ||
key *signature.KeyringPair // Keyring used for signing | ||
nonceLock sync.Mutex // Locks nonce for updates | ||
nonce types.U32 // Latest account nonce | ||
tip uint64 | ||
Conn *connection.CheckMetadataModeEnabledConnection | ||
ChainID *big.Int | ||
} | ||
|
||
func NewSubstrateCheckMetadataModeEnabledClient(conn *connection.CheckMetadataModeEnabledConnection, key *signature.KeyringPair, chainID *big.Int, tip uint64) *SubstrateCheckMetadataModeEnabledClient { | ||
return &SubstrateCheckMetadataModeEnabledClient{ | ||
key: key, | ||
Conn: conn, | ||
ChainID: chainID, | ||
tip: tip, | ||
} | ||
} | ||
|
||
// Transact constructs and submits an extrinsic to call the method with the given arguments. | ||
// All args are passed directly into GSRPC. GSRPC types are recommended to avoid serialization inconsistencies. | ||
func (c *SubstrateCheckMetadataModeEnabledClient) Transact(method string, args ...interface{}) (types.Hash, *author.ExtrinsicStatusSubscription, error) { | ||
log.Debug().Msgf("Submitting substrate call... method %s, sender %s", method, c.key.Address) | ||
|
||
// Create call and extrinsic | ||
meta := c.Conn.GetMetadata() | ||
call, err := types.NewCall( | ||
&meta, | ||
method, | ||
args..., | ||
) | ||
if err != nil { | ||
return types.Hash{}, nil, fmt.Errorf("failed to construct call: %w", err) | ||
} | ||
|
||
ext := types.NewExtrinsic(call) | ||
// Get latest runtime version | ||
rv, err := c.Conn.RPC.State.GetRuntimeVersionLatest() | ||
if err != nil { | ||
return types.Hash{}, nil, err | ||
} | ||
|
||
c.nonceLock.Lock() | ||
defer c.nonceLock.Unlock() | ||
|
||
nonce, err := c.nextNonce(&meta) | ||
if err != nil { | ||
return types.Hash{}, nil, err | ||
} | ||
|
||
// Sign the extrinsic | ||
o := types.SignatureOptions{ | ||
BlockHash: types.Hash(c.Conn.GenesisHash), | ||
Era: types.ExtrinsicEra{IsMortalEra: false}, | ||
GenesisHash: types.Hash(c.Conn.GenesisHash), | ||
Nonce: types.NewUCompactFromUInt(uint64(nonce)), | ||
SpecVersion: types.U32(rv.SpecVersion), | ||
Tip: types.NewUCompactFromUInt(c.tip), | ||
TransactionVersion: types.U32(rv.TransactionVersion), | ||
} | ||
|
||
sub, err := c.submitAndWatchExtrinsic(o, &ext) | ||
if err != nil { | ||
return types.Hash{}, nil, fmt.Errorf("submission of extrinsic failed: %w", err) | ||
} | ||
|
||
hash, err := ExtrinsicHashCheckMetadataModeEnabled(ext) | ||
if err != nil { | ||
return types.Hash{}, nil, err | ||
} | ||
|
||
log.Info().Str("extrinsic", hash.Hex()).Msgf("Extrinsic call submitted... method %s, sender %s, nonce %d", method, c.key.Address, nonce) | ||
c.nonce = nonce + 1 | ||
|
||
return hash, sub, nil | ||
} | ||
|
||
func (c *SubstrateCheckMetadataModeEnabledClient) TrackExtrinsic(extHash types.Hash, sub *author.ExtrinsicStatusSubscription) error { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Minute*10)) | ||
defer sub.Unsubscribe() | ||
defer cancel() | ||
subChan := sub.Chan() | ||
for { | ||
select { | ||
case status := <-subChan: | ||
{ | ||
if status.IsInBlock { | ||
log.Debug().Str("extrinsic", extHash.Hex()).Msgf("Extrinsic in block with hash: %#x", status.AsInBlock) | ||
} | ||
if status.IsFinalized { | ||
log.Info().Str("extrinsic", extHash.Hex()).Msgf("Extrinsic is finalized in block with hash: %#x", status.AsFinalized) | ||
return c.checkExtrinsicSuccess(extHash, types.Hash(status.AsFinalized)) | ||
} | ||
} | ||
case <-ctx.Done(): | ||
return fmt.Errorf("extrinsic has timed out") | ||
} | ||
} | ||
} | ||
|
||
func (c *SubstrateCheckMetadataModeEnabledClient) nextNonce(meta *types.Metadata) (types.U32, error) { | ||
key, err := types.CreateStorageKey(meta, "System", "Account", c.key.PublicKey, nil) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
var latestNonce types.U32 | ||
var acct types.AccountInfo | ||
exists, err := c.Conn.RPC.State.GetStorageLatest(key, &acct) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
if !exists { | ||
latestNonce = 0 | ||
} else { | ||
latestNonce = acct.Nonce | ||
} | ||
|
||
if latestNonce < c.nonce { | ||
return c.nonce, nil | ||
} | ||
|
||
return latestNonce, nil | ||
} | ||
|
||
func (c *SubstrateCheckMetadataModeEnabledClient) submitAndWatchExtrinsic(opts types.SignatureOptions, ext *types.Extrinsic) (*author.ExtrinsicStatusSubscription, error) { | ||
err := ext.Sign(*c.key, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sub, err := c.Conn.RPC.Author.SubmitAndWatchExtrinsic(*ext) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return sub, nil | ||
} | ||
|
||
func (c *SubstrateCheckMetadataModeEnabledClient) checkExtrinsicSuccess(extHash types.Hash, blockHash types.Hash) error { | ||
block, err := c.Conn.Chain.GetBlock(blockHash) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
evts, err := c.Conn.GetBlockEvents(blockHash) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, event := range evts { | ||
index := event.Phase.AsApplyExtrinsic | ||
hash, err := ExtrinsicHashCheckMetadataModeEnabled(block.Block.Extrinsics[index]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if extHash != hash { | ||
continue | ||
} | ||
|
||
if event.Name == events.ExtrinsicFailedEvent { | ||
return fmt.Errorf("extrinsic failed") | ||
} | ||
if event.Name == events.FailedHandlerExecutionEvent { | ||
return fmt.Errorf("extrinsic failed with failed handler execution") | ||
} | ||
if event.Name == events.ExtrinsicSuccessEvent { | ||
return nil | ||
} | ||
} | ||
|
||
return fmt.Errorf("no event found") | ||
} | ||
|
||
func (c *SubstrateCheckMetadataModeEnabledClient) LatestBlock() (*big.Int, error) { | ||
block, err := c.Conn.Chain.GetBlockLatest() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return big.NewInt(int64(block.Block.Header.Number)), nil | ||
} | ||
|
||
func ExtrinsicHashCheckMetadataModeEnabled(ext types.Extrinsic) (types.Hash, error) { | ||
extHash := bytes.NewBuffer([]byte{}) | ||
encoder := scale.NewEncoder(extHash) | ||
err := ext.Encode(*encoder) | ||
if err != nil { | ||
return types.Hash{}, err | ||
} | ||
return types.NewHash(extHash.Bytes()), nil | ||
} |
106 changes: 106 additions & 0 deletions
106
chains/substrate/connection/check_metadata_hash_enabled_connection.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// The Licensed Work is (c) 2022 Sygma | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package connection | ||
|
||
import ( | ||
"math/big" | ||
"sync" | ||
|
||
"github.com/sygmaprotocol/go-substrate-rpc-client/v4/client" | ||
"github.com/sygmaprotocol/go-substrate-rpc-client/v4/registry/parser" | ||
"github.com/sygmaprotocol/go-substrate-rpc-client/v4/registry/retriever" | ||
"github.com/sygmaprotocol/go-substrate-rpc-client/v4/registry/state" | ||
|
||
"github.com/sygmaprotocol/go-substrate-rpc-client/v4/rpc" | ||
"github.com/sygmaprotocol/go-substrate-rpc-client/v4/rpc/chain" | ||
"github.com/sygmaprotocol/go-substrate-rpc-client/v4/types" | ||
) | ||
|
||
type CheckMetadataModeEnabledConnection struct { | ||
chain.Chain | ||
client.Client | ||
*rpc.RPC | ||
meta types.Metadata // Latest chain metadata | ||
metaLock sync.RWMutex // Lock metadata for updates, allows concurrent reads | ||
GenesisHash types.Hash // Chain genesis hash | ||
} | ||
|
||
func NewCheckMetadataModeEnabledConnection(url string) (*CheckMetadataModeEnabledConnection, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we instead creating another connection just pass in the client instead so we don't copy paste connections? |
||
client, err := client.Connect(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
rpc, err := rpc.NewRPC(client) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
meta, err := rpc.State.GetMetadataLatest() | ||
if err != nil { | ||
return nil, err | ||
} | ||
genesisHash, err := rpc.Chain.GetBlockHash(0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &CheckMetadataModeEnabledConnection{ | ||
meta: *meta, | ||
|
||
RPC: rpc, | ||
Chain: rpc.Chain, | ||
Client: client, | ||
GenesisHash: types.Hash(genesisHash), | ||
}, nil | ||
} | ||
|
||
func (c *CheckMetadataModeEnabledConnection) GetMetadata() (meta types.Metadata) { | ||
c.metaLock.RLock() | ||
meta = c.meta | ||
c.metaLock.RUnlock() | ||
return meta | ||
} | ||
|
||
func (c *CheckMetadataModeEnabledConnection) UpdateMetatdata() error { | ||
c.metaLock.Lock() | ||
meta, err := c.RPC.State.GetMetadataLatest() | ||
if err != nil { | ||
c.metaLock.Unlock() | ||
return err | ||
} | ||
c.meta = *meta | ||
c.metaLock.Unlock() | ||
return nil | ||
} | ||
|
||
func (c *CheckMetadataModeEnabledConnection) GetBlockEvents(hash types.Hash) ([]*parser.Event, error) { | ||
provider := state.NewEventProvider(c.State) | ||
eventRetriever, err := retriever.NewDefaultEventRetriever(provider, c.State) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
evts, err := eventRetriever.GetEvents(hash) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return evts, nil | ||
} | ||
|
||
func (c *CheckMetadataModeEnabledConnection) FetchEvents(startBlock, endBlock *big.Int) ([]*parser.Event, error) { | ||
evts := make([]*parser.Event, 0) | ||
for i := new(big.Int).Set(startBlock); i.Cmp(endBlock) <= 0; i.Add(i, big.NewInt(1)) { | ||
hash, err := c.GetBlockHash(i.Uint64()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
evt, err := c.GetBlockEvents(hash) | ||
if err != nil { | ||
return nil, err | ||
} | ||
evts = append(evts, evt...) | ||
} | ||
return evts, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's different here exactly?
Anyway to write so it is not directly copy-pasted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mpetrun5 I was looking for a way to only change the type of
SignatureOptions
but the new version of it added two more parameters, so if only changing the type, it will not be able to convert to the old version and pass to thesubmitAndWatchExtrinsic
. That's why instead of changing onlySignatureOptions
, I copied the entire Client implementation with new version so there is no type matching issue, same reason for the connection implementation. Obversely it creates duplications which is bad, good side is that it doesn't touch the current implementation at all and if GSRPC is released with compatibility issue fixing, we can simply remove this duplication.To my understanding, this multiple GSRPC support is only a temporary solution until Centrifuge fix on their side. Please correct me if I'm wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can live this I guess, but let's not merge into
main
then.Create a separate feature branch and we will install on relayer temporarily as such and then create a separate fix later.
I would honestly prefer it being inside one client as by their comments I don't foresee them fixing this quickly, but a temporary branch might be ok.