Skip to content

Commit

Permalink
Add creategenesis, signpolytx, sendpolytx subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
devfans committed Apr 19, 2022
1 parent 7fb44a5 commit 541c871
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
46 changes: 46 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,52 @@ func main() {
&cli.Int64Flag{
Name: "chain",
Usage: "chain id",
Required: true,
},
},
},
&cli.Command{
Name: relayer.CREATE_GENESIS,
Usage: "Create raw tx to sync side chain genesis ",
Action: command(relayer.CREATE_GENESIS),
Flags: []cli.Flag{
&cli.Int64Flag{
Name: "height",
Usage: "target block height",
},
&cli.Int64Flag{
Name: "chain",
Usage: "chain id",
Required: true,
},
&cli.StringFlag{
Name: "keys",
Usage: "public keys seperated by ','",
Required: true,
},
},
},
&cli.Command{
Name: relayer.SIGN_POLY_TX,
Usage: "Sign raw poly multi-sig tx",
Action: command(relayer.SIGN_POLY_TX),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "tx",
Usage: "raw tx hex",
Required: true,
},
},
},
&cli.Command{
Name: relayer.SEND_POLY_TX,
Usage: "Send poly multi-sig tx",
Action: command(relayer.SEND_POLY_TX),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "tx",
Usage: "raw tx hex",
Required: true,
},
},
},
Expand Down
6 changes: 6 additions & 0 deletions relayer/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ const (
CHECK_WALLET = "wallet"
ADD_SIDECHAIN = "addsidechain"
SYNC_GENESIS = "syncgenesis"
CREATE_GENESIS = "creategenesis"
SIGN_POLY_TX = "signpolytx"
SEND_POLY_TX = "sendpolytx"
APPROVE_SIDECHAIN = "approvesidechain"
INIT_GENESIS = "initgenesis"
SYNC_HEADER = "syncheader"
Expand All @@ -85,6 +88,9 @@ func init() {
_Handlers[CREATE_ACCOUNT] = CreateAccount
_Handlers[ADD_SIDECHAIN] = AddSideChain
_Handlers[SYNC_GENESIS] = SyncGenesis
_Handlers[CREATE_GENESIS] = CreateGenesis
_Handlers[SIGN_POLY_TX] = SignPolyTx
_Handlers[SEND_POLY_TX] = SendPolyTx
_Handlers[SYNC_HEADER] = SyncHeader
_Handlers[APPROVE_SIDECHAIN] = ApproveSideChain
_Handlers[INIT_GENESIS] = SyncContractGenesis
Expand Down
109 changes: 109 additions & 0 deletions relayer/sidechain.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/btcsuite/btcd/btcec"
"github.com/ontio/ontology-crypto/sm2"
"github.com/urfave/cli/v2"
"github.com/polynetwork/poly/core/types"

"github.com/ethereum/go-ethereum/accounts/abi"
ecom "github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -200,6 +201,114 @@ func SyncHeader(ctx *cli.Context) (err error) {
return
}

func SendPolyTx(ctx *cli.Context) (err error) {
raw := ctx.String("tx")
tx := &types.Transaction{}
data, err := hex.DecodeString(raw)
if err != nil {
return err
}
if err := tx.Deserialization(common.NewZeroCopySource(data)); err != nil {
return err
}

log.Info("MultiSigned tx", "progress", len(tx.Sigs[0].SigData), "required", tx.Sigs[0].M)
if uint16(len(tx.Sigs[0].SigData)) < tx.Sigs[0].M {
log.Error("Still missing signatures", "progress", len(tx.Sigs[0].SigData), "required", tx.Sigs[0].M)
return fmt.Errorf("MultiSign lack, progress %v/%v", len(tx.Sigs[0].SigData), tx.Sigs[0].M)
}

ps, err := PolySubmitter()
if err != nil {
return
}
log.Info("Sending poly tx to node...")
hash, err := ps.SDK().Node().SendTransaction(tx)
if err != nil { return }
log.Info("Waiting poly tx to be confirmed")
height, err := ps.SDK().Node().Confirm(hash.ToHexString(), 1, 30)
if err != nil { return }
log.Info("SendMultiSignTx succeed", "height", height)
return
}

func SignPolyTx(ctx *cli.Context) (err error) {
raw := ctx.String("tx")
tx := &types.Transaction{}
data, err := hex.DecodeString(raw)
if err != nil {
return err
}
if err := tx.Deserialization(common.NewZeroCopySource(data)); err != nil {
return err
}

ps, err := PolySubmitter()
if err != nil {
return
}

accounts, err := GetPolyWallets()
if err != nil { return }

for i, acc := range accounts {
err = ps.Poly().Node().MultiSignToTransaction(tx, tx.Sigs[0].M, tx.Sigs[0].PubKeys, acc)
if err != nil {
return fmt.Errorf("multi sign failed, err: %s", err)
}
log.Info("MultiSigned tx", "index", i, "account", acc.Address.ToHexString())
}

sink := common.NewZeroCopySink(nil)
err = tx.Serialization(sink)
if err != nil {
return err
}

fmt.Printf("%x\n", sink.Bytes())
log.Info("MultiSigned tx", "progress", len(tx.Sigs[0].SigData), "required", tx.Sigs[0].M)
return
}

func CreateGenesis(ctx *cli.Context) (err error) {
chainID := ctx.Uint64("chain")
height := ctx.Uint64("height")
pubKeys := strings.Split(ctx.String("keys"), ",")
sc := GetSideChain(chainID)
header, err := sc.GenesisHeader(height)
if err != nil { return }

ps, err := PolySubmitter()
if err != nil {
return
}

tx, err := ps.SDK().Node().Native.Hs.NewSyncGenesisHeaderTransaction(chainID, header)
if err != nil { return }

keys := make([]keypair.PublicKey, len(pubKeys))
for i, v := range pubKeys {
pk, err := vconfig.Pubkey(v)
if err != nil {
return fmt.Errorf("failed to parse no%d pubkey: %v", i, err)
}
keys[i] = pk
}

tx.Sigs = append(tx.Sigs, types.Sig{
SigData: make([][]byte, 0),
M: uint16(len(pubKeys) - (len(pubKeys)-1)/3),
PubKeys: keys,
})
sink := common.NewZeroCopySink(nil)
if err := tx.Serialization(sink); err != nil {
return err
}
fmt.Printf("%x\n", sink.Bytes())
log.Info("SyncGenesis raw tx created", "keys", ctx.String("keys"))
return
}

func SyncGenesis(ctx *cli.Context) (err error) {
chainID := ctx.Uint64("chain")
height := ctx.Uint64("height")
Expand Down

0 comments on commit 541c871

Please sign in to comment.