[staking] Provide correct Encoding/Decoding of Directive in StakingTransaction (#1773)

pull/1753/head
Edgar Aroutiounian 5 years ago committed by GitHub
parent 4443630503
commit 825a5f0f43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 58
      cmd/staking/root.go
  2. 3
      core/state_processor.go
  3. 3
      staking/types/messages.go
  4. 61
      staking/types/transaction.go

@ -18,6 +18,7 @@ import (
"github.com/harmony-one/harmony/accounts"
"github.com/harmony-one/harmony/accounts/keystore"
common2 "github.com/harmony-one/harmony/internal/common"
"github.com/harmony-one/harmony/numeric"
"github.com/harmony-one/harmony/shard"
staking "github.com/harmony-one/harmony/staking/types"
"github.com/spf13/cobra"
@ -58,29 +59,30 @@ func (s *staker) run(cmd *cobra.Command, args []string) error {
p.DeserializeHexStr(testBLSPubKey)
pub := shard.BlsPublicKey{}
pub.FromLibBLSPublicKey(p)
// return staking.DirectiveCreateValidator, staking.CreateValidator{
// Description: staking.Description{
// Name: "something",
// Identity: "something else",
// Website: "some site, harmony.one",
// SecurityContact: "mr.smith",
// Details: "blah blah details",
// },
// CommissionRates: staking.CommissionRates{
// Rate: staking.NewDec(100),
// MaxRate: staking.NewDec(150),
// MaxChangeRate: staking.NewDec(5),
// },
// MinSelfDelegation: big.NewInt(10),
// StakingAddress: common.Address(dAddr),
// PubKey: pub,
// Amount: big.NewInt(100),
// }
return staking.DirectiveDelegate, staking.Delegate{
common.Address(dAddr),
common.Address(dAddr),
big.NewInt(10),
return staking.DirectiveCreateValidator, staking.CreateValidator{
Description: &staking.Description{
Name: "SuperHero",
Identity: "YouWouldNotKnow",
Website: "Secret Website",
SecurityContact: "Mr.DoubleZeroSeven",
Details: "blah blah blah",
},
CommissionRates: staking.CommissionRates{
Rate: numeric.NewDec(100),
MaxRate: numeric.NewDec(150),
MaxChangeRate: numeric.NewDec(5),
},
MinSelfDelegation: big.NewInt(10),
MaxTotalDelegation: big.NewInt(3000),
ValidatorAddress: common.Address(dAddr),
SlotPubKeys: []shard.BlsPublicKey{pub},
Amount: big.NewInt(100),
}
// return staking.DirectiveDelegate, staking.Delegate{
// common.Address(dAddr),
// common.Address(dAddr),
// big.NewInt(10),
// }
}
stakingTx, err := staking.NewStakingTransaction(2, 100, gasPrice, stakePayloadMaker)
@ -99,8 +101,16 @@ func (s *staker) run(cmd *cobra.Command, args []string) error {
if err := rlp.DecodeBytes(enc, tx); err != nil {
return err
}
fmt.Printf("In Client side: %+v\n", tx)
// return nil
payload, err := tx.RLPEncodeStakeMsg()
restored, errRestor := staking.RLPDecodeStakeMsg(
payload, staking.DirectiveCreateValidator,
)
fmt.Printf("In Client side: %+v\n", restored)
fmt.Println(errRestor)
rlp.DecodeBytes(enc, tx)
hexSignature := hexutil.Encode(enc)
param := []interface{}{hexSignature}

@ -249,7 +249,7 @@ func ApplyIncomingReceipt(config *params.ChainConfig, db *state.DB, header *bloc
// requires a signer to derive the sender.
// put it here to avoid cyclic import
func StakingToMessage(tx *staking.StakingTransaction, blockNum *big.Int) (types.Message, error) {
payload, err := tx.StakingMsgToBytes()
payload, err := tx.RLPEncodeStakeMsg()
if err != nil {
return types.Message{}, err
}
@ -257,6 +257,7 @@ func StakingToMessage(tx *staking.StakingTransaction, blockNum *big.Int) (types.
if err != nil {
return types.Message{}, err
}
msg := types.NewStakingMessage(from, tx.Nonce(), tx.Gas(), tx.Price(), payload, blockNum)
stkType := tx.StakingType()
if _, ok := types.StakingTypeMap[stkType]; !ok {

@ -21,8 +21,6 @@ const (
DirectiveEditValidator
// DirectiveDelegate ...
DirectiveDelegate
// DirectiveRedelegate ...
DirectiveRedelegate
// DirectiveUndelegate ...
DirectiveUndelegate
// DirectiveCollectRewards ...
@ -34,7 +32,6 @@ var (
DirectiveCreateValidator: "CreateValidator",
DirectiveEditValidator: "EditValidator",
DirectiveDelegate: "Delegate",
DirectiveRedelegate: "Redelegate",
DirectiveUndelegate: "Undelegate",
DirectiveCollectRewards: "CollectRewards",
}

@ -153,31 +153,43 @@ func (tx *StakingTransaction) Nonce() uint64 {
return tx.data.AccountNonce
}
// StakingMsgToBytes returns the bytes of staking message
func (tx *StakingTransaction) StakingMsgToBytes() (by []byte, err error) {
stakeType := tx.StakingType()
switch stakeType {
case DirectiveCreateValidator:
createValidator := tx.StakingMessage().(CreateValidator)
by, err = rlp.EncodeToBytes(createValidator)
case DirectiveEditValidator:
editValidator := tx.StakingMessage().(EditValidator)
by, err = rlp.EncodeToBytes(editValidator)
case DirectiveDelegate:
delegate := tx.StakingMessage().(Delegate)
by, err = rlp.EncodeToBytes(delegate)
case DirectiveUndelegate:
undelegate := tx.StakingMessage().(Undelegate)
by, err = rlp.EncodeToBytes(undelegate)
case DirectiveCollectRewards:
collectRewards := tx.StakingMessage().(CollectRewards)
by, err = rlp.EncodeToBytes(collectRewards)
// RLPEncodeStakeMsg ..
func (tx *StakingTransaction) RLPEncodeStakeMsg() (by []byte, err error) {
return rlp.EncodeToBytes(tx.data.StakeMsg)
}
// RLPDecodeStakeMsg ..
func RLPDecodeStakeMsg(payload []byte, d Directive) (interface{}, error) {
var oops error
var ds interface{}
switch _, ok := directiveNames[d]; ok {
case false:
return nil, ErrInvalidStakingKind
default:
by = []byte{}
err = ErrInvalidStakingKind
switch d {
case DirectiveCreateValidator:
ds = &CreateValidator{}
case DirectiveEditValidator:
ds = &EditValidator{}
case DirectiveDelegate:
ds = &Delegate{}
case DirectiveUndelegate:
ds = &Undelegate{}
case DirectiveCollectRewards:
ds = &CollectRewards{}
default:
return nil, nil
}
}
return
oops = rlp.DecodeBytes(payload, ds)
if oops != nil {
return nil, oops
}
return ds, nil
}
// StakingType returns the type of staking transaction
@ -192,8 +204,7 @@ func (tx *StakingTransaction) StakingMessage() interface{} {
// SenderAddress returns the address of staking transaction sender
func (tx *StakingTransaction) SenderAddress() (common.Address, error) {
signer := NewEIP155Signer(tx.ChainID())
addr, err := Sender(signer, tx)
addr, err := Sender(NewEIP155Signer(tx.ChainID()), tx)
if err != nil {
return common.Address{}, err
}

Loading…
Cancel
Save