mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-18 02:40:45 +00:00
evm mem db
This commit is contained in:
parent
1f2a1b34bd
commit
e8336d2770
2 changed files with 96 additions and 6 deletions
|
|
@ -27,6 +27,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/core"
|
"github.com/ethereum/go-ethereum/core"
|
||||||
"github.com/ethereum/go-ethereum/core/state"
|
"github.com/ethereum/go-ethereum/core/state"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
|
"github.com/ethereum/go-ethereum/core/vm"
|
||||||
"github.com/ethereum/go-ethereum/ethdb"
|
"github.com/ethereum/go-ethereum/ethdb"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/ethereum/go-ethereum/params"
|
"github.com/ethereum/go-ethereum/params"
|
||||||
|
|
@ -70,7 +71,7 @@ func CreateTransactionSign(chainConfig *params.ChainConfig, pool *core.TxPool, m
|
||||||
// Add tx signed to local tx pool.
|
// Add tx signed to local tx pool.
|
||||||
err = pool.AddLocal(txSigned)
|
err = pool.AddLocal(txSigned)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Fail to add tx sign to local pool.", "error", err, "number", block.NumberU64(), "hash", block.Hash().Hex(), "from", account.Address, "nonce", nonce)
|
log.Warn("Fail to add tx sign to local pool.", "error", err, "number", block.NumberU64(), "hash", block.Hash().Hex(), "from", account.Address, "nonce", nonce)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create secret tx.
|
// Create secret tx.
|
||||||
|
|
@ -98,7 +99,7 @@ func CreateTransactionSign(chainConfig *params.ChainConfig, pool *core.TxPool, m
|
||||||
// Add tx signed to local tx pool.
|
// Add tx signed to local tx pool.
|
||||||
err = pool.AddLocal(txSigned)
|
err = pool.AddLocal(txSigned)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("Fail to add tx sign to local pool.", "error", err, "number", block.NumberU64(), "hash", block.Hash().Hex(), "from", account.Address, "nonce", nonce)
|
log.Error("Fail to add tx secret to local pool.", "error", err, "number", block.NumberU64(), "hash", block.Hash().Hex(), "from", account.Address, "nonce", nonce)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put randomize key into chainDb.
|
// Put randomize key into chainDb.
|
||||||
|
|
@ -359,6 +360,17 @@ func CalculateRewardForSigner(chainReward *big.Int, signers map[common.Address]*
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get candidate owner by address.
|
// Get candidate owner by address.
|
||||||
|
func GetCandidatesOwnerBySigner2(vmenv *vm.EVM, signerAddr common.Address) common.Address {
|
||||||
|
owner := signerAddr
|
||||||
|
owner, err := core.GetCandidateOwner(signerAddr, vmenv)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Fail get candidate owner", "error", err)
|
||||||
|
return owner
|
||||||
|
}
|
||||||
|
|
||||||
|
return owner
|
||||||
|
}
|
||||||
|
|
||||||
func GetCandidatesOwnerBySigner(validator *contractValidator.XDCValidator, signerAddr common.Address) common.Address {
|
func GetCandidatesOwnerBySigner(validator *contractValidator.XDCValidator, signerAddr common.Address) common.Address {
|
||||||
owner := signerAddr
|
owner := signerAddr
|
||||||
opts := new(bind.CallOpts)
|
opts := new(bind.CallOpts)
|
||||||
|
|
@ -371,6 +383,81 @@ func GetCandidatesOwnerBySigner(validator *contractValidator.XDCValidator, signe
|
||||||
return owner
|
return owner
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Calculate reward for holders.
|
||||||
|
func CalculateRewardForHolders2(foudationWalletAddr common.Address, vmenv *vm.EVM, state *state.StateDB, signer common.Address, calcReward *big.Int) (error, map[common.Address]*big.Int) {
|
||||||
|
rewards, err := GetRewardBalancesRate2(foudationWalletAddr, signer, calcReward, vmenv)
|
||||||
|
if err != nil {
|
||||||
|
return err, nil
|
||||||
|
}
|
||||||
|
if len(rewards) > 0 {
|
||||||
|
for holder, reward := range rewards {
|
||||||
|
state.AddBalance(holder, reward)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, rewards
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get reward balance rates for master node, founder and holders.
|
||||||
|
func GetRewardBalancesRate2(foudationWalletAddr common.Address, masterAddr common.Address, totalReward *big.Int, vmenv *vm.EVM) (map[common.Address]*big.Int, error) {
|
||||||
|
owner := GetCandidatesOwnerBySigner2(vmenv, masterAddr)
|
||||||
|
balances := make(map[common.Address]*big.Int)
|
||||||
|
rewardMaster := new(big.Int).Mul(totalReward, new(big.Int).SetInt64(common.RewardMasterPercent))
|
||||||
|
rewardMaster = new(big.Int).Div(rewardMaster, new(big.Int).SetInt64(100))
|
||||||
|
balances[owner] = rewardMaster
|
||||||
|
// Get voters for masternode.
|
||||||
|
voters, err := core.GetVoters(masterAddr, vmenv)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Fail to get voters", "error", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(voters) > 0 {
|
||||||
|
totalVoterReward := new(big.Int).Mul(totalReward, new(big.Int).SetUint64(common.RewardVoterPercent))
|
||||||
|
totalVoterReward = new(big.Int).Div(totalVoterReward, new(big.Int).SetUint64(100))
|
||||||
|
totalCap := new(big.Int)
|
||||||
|
// Get voters capacities.
|
||||||
|
voterCaps := make(map[common.Address]*big.Int)
|
||||||
|
for _, voteAddr := range voters {
|
||||||
|
voterCap, err := core.GetVoterCap(masterAddr, voteAddr, vmenv)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Fail to get vote capacity", "error", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
totalCap.Add(totalCap, voterCap)
|
||||||
|
voterCaps[voteAddr] = voterCap
|
||||||
|
}
|
||||||
|
if totalCap.Cmp(new(big.Int).SetInt64(0)) > 0 {
|
||||||
|
for addr, voteCap := range voterCaps {
|
||||||
|
// Only valid voter has cap > 0.
|
||||||
|
if voteCap.Cmp(new(big.Int).SetInt64(0)) > 0 {
|
||||||
|
rcap := new(big.Int).Mul(totalVoterReward, voteCap)
|
||||||
|
rcap = new(big.Int).Div(rcap, totalCap)
|
||||||
|
if balances[addr] != nil {
|
||||||
|
balances[addr].Add(balances[addr], rcap)
|
||||||
|
} else {
|
||||||
|
balances[addr] = rcap
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foudationReward := new(big.Int).Mul(totalReward, new(big.Int).SetInt64(common.RewardFoundationPercent))
|
||||||
|
foudationReward = new(big.Int).Div(foudationReward, new(big.Int).SetInt64(100))
|
||||||
|
balances[foudationWalletAddr] = foudationReward
|
||||||
|
|
||||||
|
jsonHolders, err := json.Marshal(balances)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Fail to parse json holders", "error", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
log.Info("Holders reward", "holders", string(jsonHolders), "master node", masterAddr.String())
|
||||||
|
fmt.Println("Holders reward", "holders", string(jsonHolders), "master node", masterAddr.String())
|
||||||
|
|
||||||
|
return balances, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Calculate reward for holders.
|
// Calculate reward for holders.
|
||||||
func CalculateRewardForHolders(foudationWalletAddr common.Address, validator *contractValidator.XDCValidator, state *state.StateDB, signer common.Address, calcReward *big.Int) (error, map[common.Address]*big.Int) {
|
func CalculateRewardForHolders(foudationWalletAddr common.Address, validator *contractValidator.XDCValidator, state *state.StateDB, signer common.Address, calcReward *big.Int) (error, map[common.Address]*big.Int) {
|
||||||
rewards, err := GetRewardBalancesRate(foudationWalletAddr, signer, calcReward, validator)
|
rewards, err := GetRewardBalancesRate(foudationWalletAddr, signer, calcReward, validator)
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/consensus/ethash"
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
||||||
"github.com/ethereum/go-ethereum/consensus/XDPoS"
|
"github.com/ethereum/go-ethereum/consensus/XDPoS"
|
||||||
"github.com/ethereum/go-ethereum/contracts"
|
"github.com/ethereum/go-ethereum/contracts"
|
||||||
"github.com/ethereum/go-ethereum/contracts/validator/contract"
|
// "github.com/ethereum/go-ethereum/contracts/validator/contract"
|
||||||
"github.com/ethereum/go-ethereum/core"
|
"github.com/ethereum/go-ethereum/core"
|
||||||
"github.com/ethereum/go-ethereum/core/bloombits"
|
"github.com/ethereum/go-ethereum/core/bloombits"
|
||||||
"github.com/ethereum/go-ethereum/core/state"
|
"github.com/ethereum/go-ethereum/core/state"
|
||||||
|
|
@ -324,7 +324,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
||||||
return err, nil
|
return err, nil
|
||||||
}
|
}
|
||||||
// Get validator.
|
// Get validator.
|
||||||
validator, err := contract.NewXDCValidator(common.HexToAddress(common.MasternodeVotingSMC), client)
|
// validator, err := contract.NewXDCValidator(common.HexToAddress(common.MasternodeVotingSMC), client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Fail get instance of XDC Validator", "error", err)
|
log.Error("Fail get instance of XDC Validator", "error", err)
|
||||||
return err, nil
|
return err, nil
|
||||||
|
|
@ -332,8 +332,10 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
||||||
// Add reward for coin holders.
|
// Add reward for coin holders.
|
||||||
voterResults := make(map[common.Address]interface{})
|
voterResults := make(map[common.Address]interface{})
|
||||||
if len(signers) > 0 {
|
if len(signers) > 0 {
|
||||||
|
vmenv := core.NewRuntimeEVM(state)
|
||||||
for signer, calcReward := range rewardSigners {
|
for signer, calcReward := range rewardSigners {
|
||||||
err, rewards := contracts.CalculateRewardForHolders(foudationWalletAddr, validator, state, signer, calcReward)
|
// err, rewards := contracts.CalculateRewardForHolders(foudationWalletAddr, validator, state, signer, calcReward)
|
||||||
|
err, rewards := contracts.CalculateRewardForHolders2(foudationWalletAddr, vmenv, state, signer, calcReward)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Fail to calculate reward for holders.", "error", err)
|
log.Error("Fail to calculate reward for holders.", "error", err)
|
||||||
return err, nil
|
return err, nil
|
||||||
|
|
@ -342,7 +344,8 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rewards["rewards"] = voterResults
|
rewards["rewards"] = voterResults
|
||||||
log.Debug("Time Calculated HookReward ", "block", header.Number.Uint64(), "time", common.PrettyDuration(time.Since(start)))
|
// log.Debug("Time Calculated HookReward ", "block", header.Number.Uint64(), "time", common.PrettyDuration(time.Since(start)))
|
||||||
|
fmt.Println("Time Calculated HookReward ", "block", header.Number.Uint64(), "time", common.PrettyDuration(time.Since(start)))
|
||||||
}
|
}
|
||||||
return nil, rewards
|
return nil, rewards
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue