Skip to content

Commit

Permalink
MAT-1371 - topup by address
Browse files Browse the repository at this point in the history
  • Loading branch information
mankenavenkatesh committed May 14, 2020
1 parent 0a59e60 commit ea850f8
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 201 deletions.
2 changes: 1 addition & 1 deletion bridge/setu/listener/rootchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func (rl *RootChainListener) queryAndBroadcastEvents(rootchainContext *RootChain
if err := helper.UnpackLog(rl.stakingInfoAbi, event, selectedEvent.Name, &vLog); err != nil {
rl.Logger.Error("Error while parsing event", "name", selectedEvent.Name, "error", err)
}
if bytes.Equal(event.Signer.Bytes(), helper.GetAddress()) {
if bytes.Equal(event.User.Bytes(), helper.GetAddress()) {
rl.sendTaskWithDelay("sendTopUpFeeToHeimdall", selectedEvent.Name, logBytes, 0)
} else if isCurrentValidator, delay := util.CalculateTaskDelay(rl.cliCtx); isCurrentValidator {
rl.sendTaskWithDelay("sendTopUpFeeToHeimdall", selectedEvent.Name, logBytes, delay)
Expand Down
8 changes: 3 additions & 5 deletions bridge/setu/processor/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ func (fp *FeeProcessor) sendTopUpFeeToHeimdall(eventName string, logBytes string
if isOld, _ := fp.isOldTx(fp.cliCtx, vLog.TxHash.String(), uint64(vLog.Index)); isOld {
fp.Logger.Info("Ignoring task to send topup to heimdall as already processed",
"event", eventName,
"validatorId", event.ValidatorId,
"Signer", event.Signer,
"user", event.User,
"Fee", event.Fee,
"txHash", hmTypes.BytesToHeimdallHash(vLog.TxHash.Bytes()),
"logIndex", uint64(vLog.Index),
Expand All @@ -70,16 +69,15 @@ func (fp *FeeProcessor) sendTopUpFeeToHeimdall(eventName string, logBytes string

fp.Logger.Info("✅ sending topup to heimdall",
"event", eventName,
"validatorId", event.ValidatorId,
"Signer", event.Signer,
"user", event.User,
"Fee", event.Fee,
"txHash", hmTypes.BytesToHeimdallHash(vLog.TxHash.Bytes()),
"logIndex", uint64(vLog.Index),
"blockNumber", vLog.BlockNumber,
)

// create msg checkpoint ack message
msg := topupTypes.NewMsgTopup(helper.GetFromAddress(fp.cliCtx), event.ValidatorId.Uint64(), hmTypes.BytesToHeimdallAddress(event.Signer.Bytes()), sdk.NewIntFromBigInt(event.Fee), hmTypes.BytesToHeimdallHash(vLog.TxHash.Bytes()), uint64(vLog.Index), vLog.BlockNumber)
msg := topupTypes.NewMsgTopup(helper.GetFromAddress(fp.cliCtx), hmTypes.BytesToHeimdallAddress(event.User.Bytes()), sdk.NewIntFromBigInt(event.Fee), hmTypes.BytesToHeimdallHash(vLog.TxHash.Bytes()), uint64(vLog.Index), vLog.BlockNumber)

// return broadcast to heimdall
if err := fp.txBroadcaster.BroadcastToHeimdall(msg); err != nil {
Expand Down
98 changes: 38 additions & 60 deletions contracts/stakinginfo/stakinginfo.abi
Original file line number Diff line number Diff line change
Expand Up @@ -49,38 +49,12 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"internalType": "uint256",
"name": "validatorId",
"type": "uint256"
}
],
"name": "getValidatorContractAddress",
"outputs": [
{
"internalType": "address",
"name": "ValidatorContract",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"internalType": "uint256",
"name": "validatorId",
"type": "uint256"
},
{
"internalType": "address",
"name": "signer",
"name": "user",
"type": "address"
},
{
Expand All @@ -89,35 +63,31 @@
"type": "uint256"
}
],
"name": "logTopUpFee",
"name": "logClaimFee",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"constant": true,
"inputs": [
{
"internalType": "uint256",
"name": "validatorId",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
}
],
"name": "getValidatorContractAddress",
"outputs": [
{
"internalType": "uint256",
"name": "total",
"type": "uint256"
"internalType": "address",
"name": "ValidatorContract",
"type": "address"
}
],
"name": "logReStaked",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"stateMutability": "view",
"type": "function"
},
{
Expand All @@ -129,17 +99,17 @@
"type": "uint256"
},
{
"internalType": "address",
"name": "signer",
"type": "address"
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "fee",
"name": "total",
"type": "uint256"
}
],
"name": "logClaimFee",
"name": "logReStaked",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
Expand Down Expand Up @@ -371,6 +341,26 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"internalType": "address",
"name": "user",
"type": "address"
},
{
"internalType": "uint256",
"name": "fee",
"type": "uint256"
}
],
"name": "logTopUpFee",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
Expand Down Expand Up @@ -1165,16 +1155,10 @@
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"name": "validatorId",
"type": "uint256"
},
{
"indexed": true,
"internalType": "address",
"name": "signer",
"name": "user",
"type": "address"
},
{
Expand All @@ -1190,16 +1174,10 @@
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"name": "validatorId",
"type": "uint256"
},
{
"indexed": true,
"internalType": "address",
"name": "signer",
"name": "user",
"type": "address"
},
{
Expand Down
144 changes: 63 additions & 81 deletions contracts/stakinginfo/stakinginfo.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion topup/client/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cli

const (
FlagProposerAddress = "proposer"
FlagSignerAddress = "signer"
FlagUserAddress = "user"
FlagValidatorID = "validator-id"
FlagTxHash = "tx-hash"
FlagLogIndex = "log-index"
Expand Down
21 changes: 8 additions & 13 deletions topup/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ func TopupTxCmd(cdc *codec.Codec) *cobra.Command {
return fmt.Errorf("Validator ID cannot be zero")
}

// get signer
signer := types.HexToHeimdallAddress(viper.GetString(FlagSignerAddress))
if signer.Empty() {
return fmt.Errorf("Signer address cannot be zero")
// get user
user := types.HexToHeimdallAddress(viper.GetString(FlagUserAddress))
if user.Empty() {
return fmt.Errorf("user address cannot be zero")
}

// fee amount
Expand All @@ -78,8 +78,7 @@ func TopupTxCmd(cdc *codec.Codec) *cobra.Command {
// build and sign the transaction, then broadcast to Tendermint
msg := topupTypes.NewMsgTopup(
proposer,
validatorID,
signer,
user,
fee,
types.HexToHeimdallHash(txhash),
viper.GetUint64(FlagLogIndex),
Expand All @@ -92,24 +91,20 @@ func TopupTxCmd(cdc *codec.Codec) *cobra.Command {
}

cmd.Flags().StringP(FlagProposerAddress, "p", "", "--proposer=<proposer-address>")
cmd.Flags().Uint64(FlagValidatorID, 0, "--validator-id=<validator ID here>")
cmd.Flags().String(FlagTxHash, "", "--tx-hash=<transaction-hash>")
cmd.Flags().String(FlagSignerAddress, "", "--signer=<signer>")
cmd.Flags().String(FlagUserAddress, "", "--user=<user>")
cmd.Flags().String(FlagFeeAmount, "", "--topup-amount=<topup-amount>")
cmd.Flags().Uint64(FlagLogIndex, 0, "--log-index=<log-index>")
cmd.Flags().Uint64(FlagBlockNumber, 0, "--block-number=<block-number>")

if err := cmd.MarkFlagRequired(FlagValidatorID); err != nil {
cliLogger.Error("TopupTxCmd | MarkFlagRequired | FlagValidatorID", "Error", err)
}
if err := cmd.MarkFlagRequired(FlagTxHash); err != nil {
cliLogger.Error("TopupTxCmd | MarkFlagRequired | FlagTxHash", "Error", err)
}
if err := cmd.MarkFlagRequired(FlagLogIndex); err != nil {
cliLogger.Error("TopupTxCmd | MarkFlagRequired | FlagLogIndex", "Error", err)
}
if err := cmd.MarkFlagRequired(FlagSignerAddress); err != nil {
cliLogger.Error("TopupTxCmd | MarkFlagRequired | FlagSignerAddress", "Error", err)
if err := cmd.MarkFlagRequired(FlagUserAddress); err != nil {
cliLogger.Error("TopupTxCmd | MarkFlagRequired | FlagUserAddress", "Error", err)
}
if err := cmd.MarkFlagRequired(FlagFeeAmount); err != nil {
cliLogger.Error("TopupTxCmd | MarkFlagRequired | FlagFeeAmount", "Error", err)
Expand Down
8 changes: 3 additions & 5 deletions topup/client/rest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) {
type TopupReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`

ID uint64 `json:"id" yaml:"id"`
TxHash string `json:"tx_hash" yaml:"tx_hash"`
LogIndex uint64 `json:"log_index" yaml:"log_index"`
Signer string `json:"signer" yaml:"signer"`
User string `json:"user" yaml:"user"`
Fee string `json:"fee" yaml:"fee"`
BlockNumber uint64 `json:"block_number" yaml:"block_number"`
}
Expand All @@ -54,7 +53,7 @@ func TopupHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
fromAddr := types.HexToHeimdallAddress(req.BaseReq.From)

// get signer
signer := types.HexToHeimdallAddress(req.Signer)
user := types.HexToHeimdallAddress(req.User)

// fee amount
fee, ok := sdk.NewIntFromString(req.Fee)
Expand All @@ -64,8 +63,7 @@ func TopupHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {

msg := topupTypes.NewMsgTopup(
fromAddr,
req.ID,
signer,
user,
fee,
types.HexToHeimdallHash(req.TxHash),
req.LogIndex,
Expand Down
5 changes: 2 additions & 3 deletions topup/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ func NewHandler(k Keeper, contractCaller helper.IContractCaller) sdk.Handler {
func HandleMsgTopup(ctx sdk.Context, k Keeper, msg types.MsgTopup, contractCaller helper.IContractCaller) sdk.Result {

k.Logger(ctx).Debug("✅ Validating topup msg",
"validatorId", msg.ID,
"Signer", msg.Signer,
"User", msg.User,
"Fee", msg.Fee,
"txHash", hmTypes.BytesToHeimdallHash(msg.TxHash.Bytes()),
"logIndex", uint64(msg.LogIndex),
Expand Down Expand Up @@ -102,7 +101,7 @@ func HandleMsgWithdrawFee(ctx sdk.Context, k Keeper, msg types.MsgWithdrawFee) s
types.EventTypeFeeWithdraw,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeyValidatorID, validator.ID.String()),
sdk.NewAttribute(types.AttributeKeyValidatorSigner, msg.ValidatorAddress.String()),
sdk.NewAttribute(types.AttributeKeyValidatorUser, msg.ValidatorAddress.String()),
sdk.NewAttribute(types.AttributeKeyFeeWithdrawAmount, feeAmount.String()),
),
})
Expand Down
33 changes: 12 additions & 21 deletions topup/side_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,11 @@ func SideHandleMsgTopup(ctx sdk.Context, k Keeper, msg types.MsgTopup, contractC
return hmCommon.ErrorSideTx(k.Codespace(), common.CodeInvalidMsg)
}

if eventLog.ValidatorId.Uint64() != msg.ID.Uint64() {
k.Logger(ctx).Error("ID in message doesn't match id in logs", "MsgID", msg.ID, "IdFromTx", eventLog.ValidatorId)
return hmCommon.ErrorSideTx(k.Codespace(), common.CodeInvalidMsg)
}

if !bytes.Equal(eventLog.Signer.Bytes(), msg.Signer.Bytes()) {
if !bytes.Equal(eventLog.User.Bytes(), msg.User.Bytes()) {
k.Logger(ctx).Error(
"Signer Address from event does not match with Msg signer",
"EventSigner", eventLog.Signer.String(),
"MsgSigner", msg.Signer.String(),
"User Address from event does not match with Msg user",
"EventUser", eventLog.User.String(),
"MsgUser", msg.User.String(),
)
return hmCommon.ErrorSideTx(k.Codespace(), common.CodeInvalidMsg)
}
Expand Down Expand Up @@ -121,28 +116,25 @@ func PostHandleMsgTopup(ctx sdk.Context, k Keeper, msg types.MsgTopup, sideTxRes

k.Logger(ctx).Debug("Persisting topup state", "sideTxResult", sideTxResult)

// use event log signer
signer := msg.Signer
// if validator exists use siger from local state
validator, found := k.sk.GetValidatorFromValID(ctx, msg.ID)
if found {
signer = validator.Signer
}
// use event log user
user := msg.User

// create topup amount
topupAmount := sdk.Coins{sdk.Coin{Denom: authTypes.FeeToken, Amount: msg.Fee}}

// increase coins in account
if _, err := k.bk.AddCoins(ctx, signer, topupAmount); err != nil {
k.Logger(ctx).Error("Error while adding coins to signer", "signer", signer, "topupAmount", topupAmount, "error", err)
if _, err := k.bk.AddCoins(ctx, user, topupAmount); err != nil {
k.Logger(ctx).Error("Error while adding coins to user", "user", user, "topupAmount", topupAmount, "error", err)
return err.Result()
}

// transfer fees to sender (proposer)
if err := k.bk.SendCoins(ctx, signer, msg.FromAddress, auth.DefaultFeeWantedPerTx); err != nil {
if err := k.bk.SendCoins(ctx, user, msg.FromAddress, auth.DefaultFeeWantedPerTx); err != nil {
return err.Result()
}

k.Logger(ctx).Debug("Persisted topup state for", "user", user, "topupAmount", topupAmount.String())

// save topup
k.SetTopupSequence(ctx, sequence.String())

Expand All @@ -157,8 +149,7 @@ func PostHandleMsgTopup(ctx sdk.Context, k Keeper, msg types.MsgTopup, sideTxRes
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), // module name
sdk.NewAttribute(hmTypes.AttributeKeyTxHash, hmTypes.BytesToHeimdallHash(hash).Hex()), // tx hash
sdk.NewAttribute(hmTypes.AttributeKeySideTxResult, sideTxResult.String()), // result
sdk.NewAttribute(types.AttributeKeyValidatorID, msg.ID.String()),
sdk.NewAttribute(types.AttributeKeyValidatorSigner, signer.String()),
sdk.NewAttribute(types.AttributeKeyValidatorUser, user.String()),
sdk.NewAttribute(types.AttributeKeyTopupAmount, msg.Fee.String()),
),
})
Expand Down
2 changes: 1 addition & 1 deletion topup/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const (
AttributeKeyRecipient = "recipient"
AttributeKeySender = "sender"
AttributeKeyValidatorID = "validator-id"
AttributeKeyValidatorSigner = "validator-siger"
AttributeKeyValidatorUser = "user-addr"
AttributeKeyTopupAmount = "topup-amount"
AttributeKeyFeeWithdrawAmount = "fee-withdraw-amount"

Expand Down
Loading

0 comments on commit ea850f8

Please sign in to comment.