remove old design code

pull/1721/head
chao 5 years ago
parent 0c020d8f6a
commit 58448f6b23
  1. 65
      core/state_processor.go
  2. 104
      core/state_transition.go
  3. 34
      core/types/transaction.go
  4. 6
      scripts/setup_bls_build_flags.sh
  5. 3
      staking/types/validator.go

@ -29,7 +29,6 @@ import (
"github.com/harmony-one/harmony/internal/ctxerror"
"github.com/harmony-one/harmony/internal/params"
"github.com/harmony-one/harmony/internal/utils"
staking "github.com/harmony-one/harmony/staking/types"
)
// StateProcessor is a basic Processor, which takes care of transitioning
@ -180,54 +179,6 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo
return receipt, cxReceipt, gas, err
}
// ApplyStakingTransaction attempts to apply a staking transaction to the given state database
// and uses the input parameters for its environment. It returns the receipt
// for the staking transaction, gas used and an error if the transaction failed,
// indicating the block was invalid.
// staking transaction will use the storage field in the account to store the staking information
func ApplyStakingTransaction(
config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.DB,
header *block.Header, tx *staking.StakingTransaction, usedGas *uint64, cfg vm.Config) (receipt *types.Receipt, gas uint64, err error) {
msg, err := StakingToMessage(tx)
if err != nil {
return nil, 0, err
}
stkType := tx.StakingType()
if _, ok := types.StakingTypeMap[stkType]; !ok {
return nil, 0, staking.ErrInvalidStakingKind
}
msg.SetType(types.StakingTypeMap[stkType])
// Create a new context to be used in the EVM environment
context := NewEVMContext(msg, header, bc, author)
// Create a new environment which holds all relevant information
// about the transaction and calling mechanisms.
vmenv := vm.NewEVM(context, statedb, config, cfg)
// Apply the transaction to the current state (included in the env)
gas, err = ApplyStakingMessage(vmenv, msg, gp)
// even there is error, we charge it
if err != nil {
return nil, gas, err
}
// Update the state with pending changes
var root []byte
if config.IsS3(header.Epoch()) {
statedb.Finalise(true)
} else {
root = statedb.IntermediateRoot(config.IsS3(header.Epoch())).Bytes()
}
*usedGas += gas
receipt = types.NewReceipt(root, false, *usedGas)
receipt.TxHash = tx.Hash()
receipt.GasUsed = gas
return receipt, gas, nil
}
// ApplyIncomingReceipt will add amount into ToAddress in the receipt
func ApplyIncomingReceipt(config *params.ChainConfig, db *state.DB, header *block.Header, cxp *types.CXReceiptsProof) error {
if cxp == nil {
@ -248,19 +199,3 @@ func ApplyIncomingReceipt(config *params.ChainConfig, db *state.DB, header *bloc
}
return nil
}
// StakingToMessage returns the staking transaction as a core.Message.
// requires a signer to derive the sender.
// put it here to avoid cyclic import
func StakingToMessage(tx *staking.StakingTransaction) (types.Message, error) {
payload, err := tx.StakingMsgToBytes()
if err != nil {
return types.Message{}, err
}
from, err := tx.SenderAddress()
if err != nil {
return types.Message{}, err
}
msg := types.NewStakingMessage(from, tx.Nonce(), tx.Gas(), tx.Price(), payload)
return msg, nil
}

@ -22,13 +22,10 @@ import (
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/core/vm"
"github.com/harmony-one/harmony/internal/params"
"github.com/harmony-one/harmony/internal/utils"
staking "github.com/harmony-one/harmony/staking/types"
)
var (
@ -137,11 +134,6 @@ func ApplyMessage(evm *vm.EVM, msg Message, gp *GasPool) ([]byte, uint64, bool,
return NewStateTransition(evm, msg, gp).TransitionDb()
}
// ApplyStakingMessage computes the new state for staking message
func ApplyStakingMessage(evm *vm.EVM, msg Message, gp *GasPool) (uint64, error) {
return NewStateTransition(evm, msg, gp).StakingTransitionDb()
}
// to returns the recipient of the message.
func (st *StateTransition) to() common.Address {
if st.msg == nil || st.msg.To() == nil /* contract creation */ {
@ -260,99 +252,3 @@ func (st *StateTransition) refundGas() {
func (st *StateTransition) gasUsed() uint64 {
return st.initialGas - st.gas
}
// StakingTransitionDb will transition the state by applying the staking message and
// returning the result including the used gas. It returns an error if failed.
// It is used for staking transaction only
func (st *StateTransition) StakingTransitionDb() (usedGas uint64, err error) {
if err = st.preCheck(); err != nil {
return 0, err
}
msg := st.msg
sender := vm.AccountRef(msg.From())
homestead := st.evm.ChainConfig().IsS3(st.evm.EpochNumber) // s3 includes homestead
// Pay intrinsic gas
gas, err := IntrinsicGas(st.data, false, homestead)
if err != nil {
return 0, err
}
if err = st.useGas(gas); err != nil {
return 0, err
}
// Increment the nonce for the next transaction
st.state.SetNonce(msg.From(), st.state.GetNonce(sender.Address())+1)
st.refundGas()
switch msg.Type() {
case types.StakeNewVal:
stkMsg := &staking.NewValidator{}
if err = rlp.DecodeBytes(msg.Data(), stkMsg); err != nil {
break
}
err = st.applyNewValidatorTx(stkMsg)
case types.StakeEditVal:
stkMsg := &staking.EditValidator{}
if err = rlp.DecodeBytes(msg.Data(), stkMsg); err != nil {
break
}
err = st.applyEditValidatorTx(stkMsg)
case types.Delegate:
stkMsg := &staking.Delegate{}
if err = rlp.DecodeBytes(msg.Data(), stkMsg); err != nil {
break
}
err = st.applyDelegateTx(stkMsg)
case types.Redelegate:
stkMsg := &staking.Redelegate{}
if err = rlp.DecodeBytes(msg.Data(), stkMsg); err != nil {
break
}
err = st.applyRedelegateTx(stkMsg)
case types.Undelegate:
stkMsg := &staking.Undelegate{}
if err = rlp.DecodeBytes(msg.Data(), stkMsg); err != nil {
break
}
err = st.applyUndelegateTx(stkMsg)
default:
return 0, staking.ErrInvalidStakingKind
}
return st.gasUsed(), err
}
func (st *StateTransition) applyNewValidatorTx(newValidator *staking.NewValidator) error {
// TODO UpdateHeight and UnbondingHeight
commission := staking.Commission{newValidator.CommissionRates, new(big.Int)}
v := staking.Validator{newValidator.StakingAddress, newValidator.PubKey,
newValidator.Amount, new(big.Int), newValidator.MinSelfDelegation, false,
commission, newValidator.Description}
bytes, err := rlp.EncodeToBytes(&v)
if err != nil {
return err
}
hash := crypto.Keccak256Hash(bytes)
st.state.SetState(v.Address, staking.ValidatorHashKey, hash)
return nil
}
func (st *StateTransition) applyEditValidatorTx(editValidator *staking.EditValidator) error {
return nil
}
func (st *StateTransition) applyDelegateTx(delegate *staking.Delegate) error {
return nil
}
func (st *StateTransition) applyRedelegateTx(redelegate *staking.Redelegate) error {
return nil
}
func (st *StateTransition) applyUndelegateTx(undelegate *staking.Undelegate) error {
return nil
}

@ -31,7 +31,6 @@ import (
"github.com/harmony-one/harmony/crypto/hash"
common2 "github.com/harmony-one/harmony/internal/common"
staking "github.com/harmony-one/harmony/staking/types"
)
// no go:generate gencodec -type txdata -field-override txdataMarshaling -out gen_tx_json.go
@ -49,18 +48,8 @@ const (
SameShardTx TransactionType = iota
SubtractionOnly // only subtract tokens from source shard account
InvalidTx
StakeNewVal
StakeEditVal
Delegate
Redelegate
Undelegate
)
// StakingTypeMap is the map from staking type to transactionType
var StakingTypeMap = map[staking.Directive]TransactionType{staking.DirectiveNewValidator: StakeNewVal,
staking.DirectiveEditValidator: StakeEditVal, staking.DirectiveDelegate: Delegate,
staking.DirectiveRedelegate: Redelegate, staking.DirectiveUndelegate: Undelegate}
// Transaction struct.
type Transaction struct {
data txdata
@ -78,16 +67,6 @@ func (txType TransactionType) String() string {
return "SubtractionOnly"
} else if txType == InvalidTx {
return "InvalidTx"
} else if txType == StakeNewVal {
return "StakeNewValidator"
} else if txType == StakeEditVal {
return "StakeEditValidator"
} else if txType == Delegate {
return "Delegate"
} else if txType == Redelegate {
return "Redelegate"
} else if txType == Undelegate {
return "Undelegate"
}
return "Unknown"
}
@ -579,19 +558,6 @@ func NewMessage(from common.Address, to *common.Address, nonce uint64, amount *b
}
}
// NewStakingMessage returns new message of staking type
// always need checkNonce
func NewStakingMessage(from common.Address, nonce uint64, gasLimit uint64, gasPrice *big.Int, data []byte) Message {
return Message{
from: from,
nonce: nonce,
gasLimit: gasLimit,
gasPrice: new(big.Int).Set(gasPrice),
data: data,
checkNonce: true,
}
}
// From returns from address from Message.
func (m Message) From() common.Address {
return m.from

@ -36,3 +36,9 @@ case $OS in
;;
esac
if [ "$1" == '-v' ]; then
echo "{ \"CGO_CFLAGS\" : \"$CGO_CFLAGS\",
\"CGO_LDFLAGS\" : \"$CGO_LDFLAGS\",
\"LD_LIBRARY_PATH\" : \"$LD_LIBRARY_PATH\",
\"DYLD_FALLBACK_LIBRARY_PATH\" : \"$DYLD_FALLBACK_LIBRARY_PATH\"}" | jq "."
fi

@ -4,7 +4,6 @@ import (
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
"github.com/harmony-one/bls/ffi/go/bls"
@ -21,8 +20,6 @@ const (
MaxDetailsLength = 280
)
var ValidatorHashKey = common.Hash(crypto.Keccak256Hash([]byte("ValidatorHash")))
// ValidatorWrapper contains validator and its delegation information
type ValidatorWrapper struct {
Validator `json:"validator" yaml:"validator"`

Loading…
Cancel
Save