Merge pull request #1841 from chaosma/fix-stk-copy

add staking transaction unit test for tx copy
pull/1848/head
chaosma 5 years ago committed by GitHub
commit 8fbb231d6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 65
      staking/types/messages.go
  2. 5
      staking/types/transaction.go
  3. 104
      staking/types/transaction_test.go

@ -14,6 +14,12 @@ import (
// Directive says what kind of payload follows // Directive says what kind of payload follows
type Directive byte type Directive byte
// StakeMsg defines the interface of Stake Message
type StakeMsg interface {
Type() Directive
Copy() StakeMsg
}
const ( const (
// DirectiveCreateValidator ... // DirectiveCreateValidator ...
DirectiveCreateValidator Directive = iota DirectiveCreateValidator Directive = iota
@ -86,3 +92,62 @@ type Undelegate struct {
type CollectRewards struct { type CollectRewards struct {
DelegatorAddress common.Address `json:"delegator_address" yaml:"delegator_address"` DelegatorAddress common.Address `json:"delegator_address" yaml:"delegator_address"`
} }
// Type of CreateValidator
func (v CreateValidator) Type() Directive {
return DirectiveCreateValidator
}
// Type of EditValidator
func (v EditValidator) Type() Directive {
return DirectiveEditValidator
}
// Type of Delegate
func (v Delegate) Type() Directive {
return DirectiveDelegate
}
// Type of Undelegate
func (v Undelegate) Type() Directive {
return DirectiveUndelegate
}
// Type of CollectRewards
func (v CollectRewards) Type() Directive {
return DirectiveCollectRewards
}
// Copy deep copy of the interface
func (v CreateValidator) Copy() StakeMsg {
v1 := v
desc := *v.Description
v1.Description = &desc
return v1
}
// Copy deep copy of the interface
func (v EditValidator) Copy() StakeMsg {
v1 := v
desc := *v.Description
v1.Description = &desc
return v1
}
// Copy deep copy of the interface
func (v Delegate) Copy() StakeMsg {
v1 := v
return v1
}
// Copy deep copy of the interface
func (v Undelegate) Copy() StakeMsg {
v1 := v
return v1
}
// Copy deep copy of the interface
func (v CollectRewards) Copy() StakeMsg {
v1 := v
return v1
}

@ -4,7 +4,6 @@ import (
"errors" "errors"
"io" "io"
"math/big" "math/big"
"reflect"
"sync/atomic" "sync/atomic"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -31,11 +30,11 @@ type txdata struct {
} }
func (d *txdata) CopyFrom(d2 *txdata) { func (d *txdata) CopyFrom(d2 *txdata) {
d.Directive = d2.Directive
d.AccountNonce = d2.AccountNonce d.AccountNonce = d2.AccountNonce
d.Price = new(big.Int).Set(d2.Price) d.Price = new(big.Int).Set(d2.Price)
d.GasLimit = d2.GasLimit d.GasLimit = d2.GasLimit
d.StakeMsg = reflect.New(reflect.ValueOf(d2.StakeMsg).Elem().Type()).Interface() // TODO: add code to protect crashing
d.StakeMsg = d2.StakeMsg.(StakeMsg).Copy()
d.V = new(big.Int).Set(d2.V) d.V = new(big.Int).Set(d2.V)
d.R = new(big.Int).Set(d2.R) d.R = new(big.Int).Set(d2.R)
d.S = new(big.Int).Set(d2.S) d.S = new(big.Int).Set(d2.S)

@ -0,0 +1,104 @@
package types
import (
"fmt"
"math/big"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/harmony-one/bls/ffi/go/bls"
common2 "github.com/harmony-one/harmony/internal/common"
numeric "github.com/harmony-one/harmony/numeric"
"github.com/harmony-one/harmony/shard"
)
// for testing purpose
var (
testAccount = "one1pdv9lrdwl0rg5vglh4xtyrv3wjk3wsqket7zxy"
testBLSPubKey = "65f55eb3052f9e9f632b2923be594ba77c55543f5c58ee1454b9cfd658d25e06373b0f7d42a19c84768139ea294f6204"
testBLSPubKey2 = "40379eed79ed82bebfb4310894fd33b6a3f8413a78dc4d43b98d0adc9ef69f3285df05eaab9f2ce5f7227f8cb920e809"
)
func CreateTestNewTransaction() (*StakingTransaction, error) {
dAddr, _ := common2.Bech32ToAddress(testAccount)
stakePayloadMaker := func() (Directive, interface{}) {
p := &bls.PublicKey{}
p.DeserializeHexStr(testBLSPubKey)
pub := shard.BlsPublicKey{}
pub.FromLibBLSPublicKey(p)
ra, _ := numeric.NewDecFromStr("0.7")
maxRate, _ := numeric.NewDecFromStr("1")
maxChangeRate, _ := numeric.NewDecFromStr("0.5")
return DirectiveCreateValidator, CreateValidator{
Description: &Description{
Name: "SuperHero",
Identity: "YouWouldNotKnow",
Website: "Secret Website",
SecurityContact: "LicenseToKill",
Details: "blah blah blah",
},
CommissionRates: CommissionRates{
Rate: ra,
MaxRate: maxRate,
MaxChangeRate: maxChangeRate,
},
MinSelfDelegation: big.NewInt(10),
MaxTotalDelegation: big.NewInt(3000),
ValidatorAddress: common.Address(dAddr),
SlotPubKeys: []shard.BlsPublicKey{pub},
Amount: big.NewInt(100),
}
}
gasPrice := big.NewInt(1)
return NewStakingTransaction(0, 600000, gasPrice, stakePayloadMaker)
}
func TestTransactionCopy(t *testing.T) {
tx1, err := CreateTestNewTransaction()
if err != nil {
t.Errorf("cannot create new staking transaction, %v\n", err)
}
tx2 := tx1.Copy()
cv1 := tx1.data.StakeMsg.(CreateValidator)
// modify cv1 fields
cv1.Amount = big.NewInt(20)
cv1.Description.Name = "NewName"
newRate, _ := numeric.NewDecFromStr("0.5")
cv1.CommissionRates.Rate = newRate
p := &bls.PublicKey{}
p.DeserializeHexStr(testBLSPubKey2)
pub := shard.BlsPublicKey{}
pub.FromLibBLSPublicKey(p)
cv1.SlotPubKeys = append(cv1.SlotPubKeys, pub)
tx1.data.StakeMsg = cv1
cv2 := tx2.data.StakeMsg.(CreateValidator)
if cv1.Amount.Cmp(cv2.Amount) == 0 {
t.Errorf("Amount should not be equal")
}
if len(cv1.SlotPubKeys) == len(cv2.SlotPubKeys) {
t.Errorf("SlotPubKeys should not be equal length")
}
if len(cv1.Description.Name) == len(cv2.Description.Name) {
t.Errorf("Description name should not be the same")
}
if cv1.CommissionRates.Rate.Equal(cv2.CommissionRates.Rate) {
t.Errorf("CommissionRate should not be equal")
}
fmt.Println("cv1", cv1)
fmt.Println("cv2", cv2)
fmt.Println("cv1", cv1.Description)
fmt.Println("cv2", cv2.Description)
}
Loading…
Cancel
Save