This repository has been archived by the owner on May 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
Generic Deposit CLI command and Generic Resources CLI fixes #283
Open
vezenovm
wants to merge
6
commits into
main
Choose a base branch
from
generic-deposit-cli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4d3b6bf
added cli command for deposit, and changed method of decoding deposit…
vezenovm cb2f279
linter fix and cleanup
vezenovm 619ec20
merge main changes
vezenovm c1e5cee
Merge branch 'main' into generic-deposit-cli
P1sar 93c15ed
add depositer offset flag parsing
vezenovm 968b82a
Merge branch 'generic-deposit-cli' of github.com:ChainSafe/chainbridg…
vezenovm 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,8 +60,9 @@ func BindRegisterGenericResourceFlags(cmd *cobra.Command) { | |
cmd.Flags().StringVar(&ResourceID, "resource", "", "Resource ID to query") | ||
cmd.Flags().StringVar(&Bridge, "bridge", "", "Bridge contract address") | ||
cmd.Flags().StringVar(&Target, "target", "", "Contract address or hash storage to be registered") | ||
cmd.Flags().StringVar(&Deposit, "deposit", "0x00000000", "Deposit function signature") | ||
cmd.Flags().StringVar(&Execute, "execute", "0x00000000", "Execute proposal function signature") | ||
cmd.Flags().StringVar(&Deposit, "deposit", "00000000", "Deposit function signature") | ||
cmd.Flags().Uint64Var(&DepositerOffset, "depositerOffset", 0, "Offset to find the bridge tx depositer address inside the metadata sent on a deposit") | ||
cmd.Flags().StringVar(&Execute, "execute", "00000000", "Execute proposal function signature") | ||
cmd.Flags().BoolVar(&Hash, "hash", false, "Treat signature inputs as function prototype strings, hash and take the first 4 bytes") | ||
flags.MarkFlagsAsRequired(cmd, "handler", "resource", "bridge", "target") | ||
} | ||
|
@@ -103,25 +104,53 @@ func ProcessRegisterGenericResourceFlags(cmd *cobra.Command, args []string) erro | |
ResourceIdBytesArr = callsUtil.SliceTo32Bytes(resourceIdBytes) | ||
|
||
if Hash { | ||
// We must check whether both a deposit and execute function signature is provide or else | ||
// an invalid hash of 0x00000000 will be taken and set as a function selector in the handler | ||
DepositSigBytes = callsUtil.GetSolidityFunctionSig([]byte(Deposit)) | ||
ExecuteSigBytes = callsUtil.GetSolidityFunctionSig([]byte(Execute)) | ||
if Deposit == "00000000" { | ||
DepositSigBytes = [4]byte{} | ||
} | ||
if Execute == "00000000" { | ||
ExecuteSigBytes = [4]byte{} | ||
} | ||
} else { | ||
copy(DepositSigBytes[:], []byte(Deposit)[:]) | ||
copy(ExecuteSigBytes[:], []byte(Execute)[:]) | ||
depositBytes, err := hex.DecodeString(Deposit) | ||
if err != nil { | ||
return err | ||
} | ||
copy(DepositSigBytes[:], depositBytes[:]) | ||
|
||
executeBytes, err := hex.DecodeString(Execute) | ||
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. The same as above |
||
if err != nil { | ||
return err | ||
} | ||
copy(ExecuteSigBytes[:], executeBytes[:]) | ||
} | ||
|
||
log.Debug().Msgf("DepositSigBytes: %x\n", DepositSigBytes[:]) | ||
log.Debug().Msgf("ExecuteSigBytes: %x\n", ExecuteSigBytes[:]) | ||
|
||
return nil | ||
} | ||
|
||
func RegisterGenericResource(cmd *cobra.Command, args []string, contract *bridge.BridgeContract) error { | ||
log.Info().Msgf("Registering contract %s with resource ID %s on handler %s", TargetContractAddr, ResourceID, HandlerAddr) | ||
|
||
depositerOffsetBigInt := new(big.Int).SetUint64(DepositerOffset) | ||
log.Info().Msgf("handlerAddr: %s, resourceId: %s, targetcontract: %s, depositsigbytes: %s, depositerOffset: %s, executeSigBytes: %s", | ||
HandlerAddr, | ||
ResourceIdBytesArr, | ||
TargetContractAddr, | ||
string(DepositSigBytes[:]), | ||
depositerOffsetBigInt, | ||
string(DepositSigBytes[:])) | ||
h, err := contract.AdminSetGenericResource( | ||
HandlerAddr, | ||
ResourceIdBytesArr, | ||
TargetContractAddr, | ||
DepositSigBytes, | ||
big.NewInt(int64(DepositerOffset)), | ||
depositerOffsetBigInt, | ||
ExecuteSigBytes, | ||
transactor.TransactOptions{GasLimit: gasLimit}, | ||
) | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package centrifuge | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/ChainSafe/chainbridge-core/chains/evm/calls/contracts/bridge" | ||
"github.com/ChainSafe/chainbridge-core/chains/evm/calls/evmtransaction" | ||
"github.com/ChainSafe/chainbridge-core/chains/evm/calls/transactor" | ||
"github.com/ChainSafe/chainbridge-core/chains/evm/cli/flags" | ||
"github.com/ChainSafe/chainbridge-core/chains/evm/cli/initialize" | ||
"github.com/ChainSafe/chainbridge-core/chains/evm/cli/logger" | ||
"github.com/ChainSafe/chainbridge-core/util" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var depositCmd = &cobra.Command{ | ||
Use: "deposit", | ||
Short: "Deposit a generic data hash", | ||
Long: "The deposit subcommand creates a new generic data deposit on the bridge contract", | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
logger.LoggerMetadata(cmd.Name(), cmd.Flags()) | ||
}, | ||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | ||
return util.CallPersistentPreRun(cmd, args) | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
c, err := initialize.InitializeClient(url, senderKeyPair) | ||
if err != nil { | ||
return err | ||
} | ||
t, err := initialize.InitializeTransactor(gasPrice, evmtransaction.NewTransaction, c, prepare) | ||
if err != nil { | ||
return err | ||
} | ||
return DepositCmd(cmd, args, bridge.NewBridgeContract(c, BridgeAddr, t)) | ||
}, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
err := ValidateDepositFlags(cmd, args) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = ProcessDepositFlags(cmd, args) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
BindDepositFlags(depositCmd) | ||
} | ||
|
||
func BindDepositFlags(cmd *cobra.Command) { | ||
cmd.Flags().StringVar(&Recipient, "recipient", "", "Address of contract to receive generic data") | ||
cmd.Flags().StringVar(&Bridge, "bridge", "", "Address of bridge contract") | ||
cmd.Flags().StringVar(&Metadata, "metadata", "", "Data (hex bytes) representing params for previously registered functions. Params should be encoded as 32 bytes each") | ||
cmd.Flags().Uint8Var(&DomainID, "domain", 0, "Destination domain ID") | ||
cmd.Flags().StringVar(&ResourceID, "resource", "", "Resource ID for transfer") | ||
cmd.Flags().StringVar(&Priority, "priority", "none", "Transaction priority speed") | ||
flags.MarkFlagsAsRequired(cmd, "recipient", "bridge", "metadata", "domain", "resource") | ||
} | ||
|
||
func ValidateDepositFlags(cmd *cobra.Command, args []string) error { | ||
if !common.IsHexAddress(Recipient) { | ||
return fmt.Errorf("invalid recipient address %s", Recipient) | ||
} | ||
if !common.IsHexAddress(Bridge) { | ||
return fmt.Errorf("invalid bridge address %s", Bridge) | ||
} | ||
switch Priority { | ||
case "none", "slow", "medium", "fast": | ||
return nil | ||
default: | ||
return fmt.Errorf("invalid priority value %s, supported priorities: \"slow|medium|fast\"", Priority) | ||
} | ||
} | ||
|
||
func ProcessDepositFlags(cmd *cobra.Command, args []string) error { | ||
var err error | ||
|
||
RecipientAddr = common.HexToAddress(Recipient) | ||
BridgeAddr = common.HexToAddress(Bridge) | ||
ResourceIdBytesArr, err = flags.ProcessResourceID(ResourceID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
MetadataBytes, err = hex.DecodeString(Metadata) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return err | ||
} | ||
|
||
func DepositCmd(cmd *cobra.Command, args []string, bridgeContract *bridge.BridgeContract) error { | ||
txHash, err := bridgeContract.GenericDeposit( | ||
MetadataBytes, ResourceIdBytesArr, uint8(DomainID), transactor.TransactOptions{GasLimit: gasLimit, Priority: transactor.TxPriorities[Priority]}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Info().Msgf( | ||
`Generic deposit hash: %s | ||
%s metadata was transferred to %s from %s`, | ||
txHash.Hex(), | ||
Metadata, | ||
RecipientAddr.Hex(), | ||
senderKeyPair.CommonAddress().String(), | ||
) | ||
return 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
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.
You could probably use the DepositSigBytes variable here, and then omit the copy stuff
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.
Do you mean substituting
depositBytes
withDepositSigBytes
from thehex.DecodeString
call? CurrentlyDepositSigBytes
is type[4]byte
whereasDecodeString
returns[]byte
and can be used to assign a value toDepositSigBytes
. This could probably still be done but would require slight changes to all other places that useDepositSigBytes