-
Notifications
You must be signed in to change notification settings - Fork 0
/
relay.go
76 lines (60 loc) · 2.1 KB
/
relay.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package txs
// ------------------------------------------------------------
// Relay : Builds and encodes EthBridgeClaim Msgs with the
// specified variables, before presenting the unsigned
// transaction to validators for optional signing.
// Once signed, the data packets are sent as transactions
// on the Cosmos Bridge.
// ------------------------------------------------------------
import (
sdk "github.com/cosmos/cosmos-sdk/types"
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
amino "github.com/tendermint/go-amino"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/peggy/x/ethbridge"
"github.com/cosmos/peggy/x/ethbridge/types"
)
// RelayEvent : RelayEvent applies validator's signature to an EthBridgeClaim message
// containing information about an event on the Ethereum blockchain before sending
// it to the Bridge blockchain. For this relay, the chain id (chainID) and codec
// (cdc) of the Bridge blockchain are required.
//
func RelayEvent(chainID string, cdc *amino.Codec, validatorAddress sdk.ValAddress, moniker string, passphrase string, claim *types.EthBridgeClaim) error {
cliCtx := context.NewCLIContext().
WithCodec(cdc).
WithAccountDecoder(cdc).
WithFromAddress(sdk.AccAddress(validatorAddress)).
WithFromName(moniker)
cliCtx.SkipConfirm = true
txBldr := authtxb.NewTxBuilderFromCLI().
WithTxEncoder(utils.GetTxEncoder(cdc)).
WithChainID(chainID)
err := cliCtx.EnsureAccountExistsFromAddr(sdk.AccAddress(claim.ValidatorAddress))
if err != nil {
return err
}
msg := ethbridge.NewMsgCreateEthBridgeClaim(*claim)
err = msg.ValidateBasic()
if err != nil {
return err
}
cliCtx.PrintResponse = true
// Prepare tx
txBldr, err = utils.PrepareTxBuilder(txBldr, cliCtx)
if err != nil {
return err
}
// Build and sign the transaction
txBytes, err := txBldr.BuildAndSign(moniker, passphrase, []sdk.Msg{msg})
if err != nil {
return err
}
// Broadcast to a Tendermint node
res, err := cliCtx.BroadcastTx(txBytes)
if err != nil {
return err
}
cliCtx.PrintOutput(res)
return nil
}