2018-2-27

This commit is contained in:
BlockchainBase 2018-02-27 18:27:11 +08:00
parent b051cb6d41
commit 5f65581331
13 changed files with 1035 additions and 64 deletions

Binary file not shown.

View file

@ -569,15 +569,7 @@ func AccumulateRewards(config *params.ChainConfig, state *state.StateDB, header
//log.Info("参数变量","TotalReward",TotalReward,"Reward_Justnum",Reward_justnum) //log.Info("参数变量","TotalReward",TotalReward,"Reward_Justnum",Reward_justnum)
TotalReward.Div(TotalReward,math.Exp(big.NewInt(2),Reward_justnum)) // reward/2**justnum TotalReward.Div(TotalReward,math.Exp(big.NewInt(2),Reward_justnum)) // reward/2**justnum
//log.Info("参数变量","TotalReward",TotalReward) //log.Info("参数变量","TotalReward",TotalReward)
if header.Number.Cmp(big.NewInt(100))>0 { //必须在区块号100以前完成挖矿智能合约的部署。
minerpool:=TotalReward.Div(TotalReward,big.NewInt(100))
state.AddBalance(header.Coinbase, minerpool)
// log.Info("参数变量","minerpool",minerpool)
// reward.Sub(reward,minerpool)
state.AddBalance(params.PosMinerContractAddr, TotalReward.Mul(TotalReward,big.NewInt(99)))
//log.Info("参数变量","TotalReward",TotalReward)
}else{
state.AddBalance(header.Coinbase, TotalReward) state.AddBalance(header.Coinbase, TotalReward)
//log.Info("参数变量","TotalReward",TotalReward) //log.Info("参数变量","TotalReward",TotalReward)
}
} }

View file

@ -0,0 +1,95 @@
[
{
"constant": false,
"inputs": [
{
"name": "MinerPool",
"type": "address"
},
{
"name": "status",
"type": "bool"
},
{
"name": "AppAddr",
"type": "string"
}
],
"name": "MinerPoolSetting",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "address"
}
],
"name": "MPools",
"outputs": [
{
"name": "status",
"type": "bool"
},
{
"name": "AppAddr",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "Manager",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "RegistryPoolNum",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "newManager",
"type": "address"
}
],
"name": "transferManagement",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
}
]

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,36 @@
pragma solidity ^0.4.18;
contract MiningPoolManagement {
//Define manager.
address public Manager;
//Only manager can modify.
modifier onlyManager {
require(msg.sender ==Manager);
_;
}
//MiningPool Registry Information.
struct mpool {
bool status;
string AppAddr;
}
mapping (address=>mpool) public MPools;
uint public RegistryPoolNum;
//Construction function, initially define the creator as the manager.
function MiningPoolManagement() public {
Manager=msg.sender;
}
//Management power thansfer.
function transferManagement(address newManager) onlyManager public {
Manager=newManager;
}
//MiningPool regstiry, only manager can modify.
function MinerPoolSetting(address MinerPool,bool status,string AppAddr) onlyManager public {
if (MPools[MinerPool].status!=true){
RegistryPoolNum+=1;
}
MPools[MinerPool]=mpool(status,AppAddr);
if (status==false&&RegistryPoolNum>1) {
RegistryPoolNum-=1;
}
}
}

View file

@ -0,0 +1,132 @@
[
{
"constant": false,
"inputs": [],
"name": "Mine",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "address"
}
],
"name": "Miners",
"outputs": [
{
"name": "Registry",
"type": "bool"
},
{
"name": "TotalPay",
"type": "uint256"
},
{
"name": "PayTime",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "Manager",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "ReceiveFoundation",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "MobileMiner",
"type": "address"
}
],
"name": "MinerSetting",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "ActiveUsers",
"outputs": [
{
"name": "LastTime",
"type": "uint256"
},
{
"name": "ActiveNum",
"type": "uint256"
},
{
"name": "RegistryUsers",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "newManager",
"type": "address"
}
],
"name": "transferManagement",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"payable": true,
"stateMutability": "payable",
"type": "fallback"
}
]

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,68 @@
pragma solidity ^0.4.18;
contract MobileMine {
//Define the Manager
address public Manager;
//Only manager can modify.
modifier onlyManager {
require(msg.sender ==Manager);
_;
}
//Define miner mining informaiton.
struct Miner {
bool Registry;
uint TotalPay; //Having mined reward.
uint PayTime; //Reward pay time.
}
//Active users' information.
struct ActiveInfo {
uint LastTime; //Last calculate time.
uint ActiveNum; //Active users number.
uint RegistryUsers; //the number of already registrtyUsers.
}
/*Miner and active users defining */
mapping (address => Miner) public Miners;
ActiveInfo public ActiveUsers;
uint public ReceiveFoundation; //Having received reward foundation.
uint MineAmount;
//Constuct function£¬initially define the creator as the manager.
function MobileMine() public {
Manager=msg.sender;
}
//Define the contract can receive mining reward foundation.
function () payable public {
ReceiveFoundation+=msg.value;
}
//Management power transfer.
function transferManagement(address newManager) onlyManager public {
Manager=newManager;
}
//Miner registry setting, only manager can modify.
function MinerSetting(address MobileMiner) onlyManager public {
if (Miners[MobileMiner].Registry!=true){
Miners[MobileMiner].Registry=true;
ActiveUsers.RegistryUsers+=1;
}
}
/* Miner mine function, modify miner's status*/
function Mine() public returns (bool success){
//If not registry or has been payed in one day, return false.
if (Miners[msg.sender].Registry!=true||Miners[msg.sender].PayTime+86400>now){
return false;
}
//Pay the reward and change the miner's status.
MineAmount=this.balance/(ActiveUsers.RegistryUsers+1);
msg.sender.transfer(MineAmount);
Miners[msg.sender].TotalPay+= MineAmount;
Miners[msg.sender].PayTime=now;
//Check if the calculating time of active user is lasting one day or not.
if(ActiveUsers.LastTime+86400<now){
ActiveUsers.LastTime=now;
ActiveUsers.ActiveNum=1;
Manager.transfer(this.balance/100);
}else{
ActiveUsers.ActiveNum+=1;
}
return true;
}
}

View file

@ -42,9 +42,11 @@ import (
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
"github.com/hashicorp/golang-lru" "github.com/hashicorp/golang-lru"
"github.com/ethereum/go-ethereum/contracts/posminer" "github.com/ethereum/go-ethereum/contracts/MinerPoolManagement"
"github.com/ethereum/go-ethereum/contracts/MobileMine"
"github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/ethclient"
) )
var ( var (
@ -859,59 +861,40 @@ func (bc *BlockChain) WriteBlockAndState(block *types.Block, receipts []*types.R
//统计移动应用权重帐户的用户权重 //统计移动应用权重帐户的用户权重
func (bc *BlockChain) CalcTokenTime(Coinbase common.Address) (Tokentime *big.Int) { func (bc *BlockChain) CalcTokenTime(Coinbase common.Address) (Tokentime *big.Int) {
//var sumToken *big.Int = big.NewInt(0)
//var Tokentime *big.Int = big.NewInt(0) //var Pos_Addjust *big.Int
var exist bool
exist=false
Tokentime=big.NewInt(0) Tokentime=big.NewInt(0)
var ActiveMiners []common.Address
State,_:=bc.State() State,_:=bc.State()
if State.Exist(params.PosMinerContractAddr)!=true{ if State.Exist(params.PosMinerContractAddr)!=true || State.GetCode(Coinbase)==nil{
//log.Info("警告","RPC未使能",Tokentime) log.Info("警告","矿池管理合约未部署,尚不能使用权重挖矿!",Tokentime)
return Tokentime return Tokentime
} }
//log.Info("posminer.PosConn",posminer.PosConn) //log.Info("posminer.PosConn",posminer.PosConn)
if posminer.PosConn==nil { if MinerPoolManagement.PosConn==nil {
posminer.PosConn, _ = ethclient.Dial("//./pipe/geth.ipc") MinerPoolManagement.PosConn, _ = ethclient.Dial("//./pipe/geth.ipc")
log.Info("AppChain连接", "连接IPC服务") log.Info("AppChain连接", "连接IPC服务")
} }
//if err != nil { MinerPool, err := MinerPoolManagement.NewMinerPoolManagement(params.PosMinerContractAddr, MinerPoolManagement.PosConn)
// log.Info("警告", "设置了ipcdisable不能使用权重挖矿!",err) //log.Info("params.posminerContractAddr",params.PosMinerContractAddr)
// return Tokentime
//}
PosMiner, err := posminer.NewPosminer(params.PosMinerContractAddr, posminer.PosConn)
//log.Info("错误码","err",err,"PosMiner",PosMiner)
if err !=nil{ if err !=nil{
log.Info("调用矿智能合约posminer失败","错误",err) log.Info("调用矿池智能合约失败","错误",err)
return Tokentime return Tokentime
} }
//M,err:=PosMiner.RegLength(nil) //查询移动应用矿池状态
for i:=uint64(0);i<bc.currentBlock.NumberU64() ;i++{ Mpool,err:= MinerPool.MPools(nil, Coinbase)
for _,tx:=range bc.GetBlockByNumber(i).Transactions() { //log.Info("警告","Mpool.status",Mpool.Status)
msg,err:= tx.AsMessage(types.MakeSigner(bc.Config(), bc.GetBlockByNumber(i).Header().Number)) if Mpool.Status!=true {
if err != nil {
return Tokentime return Tokentime
} }
CmpMiner,err:= PosMiner.Registers(nil, msg.From()) //查询移动应用矿池的权重
if err != nil { MpoolMine,err:=MobileMine.NewMobileMine(Coinbase,MinerPoolManagement.PosConn)
if err !=nil{
log.Info("调用移动应用矿池合约失败","错误",err)
return Tokentime return Tokentime
} }
RegistryTime := new(big.Int).Set(CmpMiner.RegistryTime) MpoolActiveNum,err:=MpoolMine.ActiveUsers(nil)
if RegistryTime.Add(RegistryTime,big.NewInt(86400)).Cmp(bc.currentBlock.Time())>0 &&CmpMiner.MinerPool==Coinbase { Tokentime.Sqrt(MpoolActiveNum.ActiveNum)
for j:=0;j<len(ActiveMiners);j++{ //log.Info("调用移动应用矿池合约失败","Tokentime1",Tokentime1,"Tokentime2",Tokentime2)
if ActiveMiners[j]==msg.From(){
exist=true
break
}
}
if exist==false {
Tokentime.Add(Tokentime,big.NewInt(1))
ActiveMiners=append(ActiveMiners,msg.From())
}
exist=false
}
}
}
return Tokentime return Tokentime
} }

View file

@ -263,15 +263,7 @@ func (st *StateTransition) TransitionDb() (ret []byte, requiredGas, usedGas *big
st.refundGas() st.refundGas()
Total_gas := new(big.Int).Mul(st.gasUsed(), st.gasPrice) Total_gas := new(big.Int).Mul(st.gasUsed(), st.gasPrice)
//检查合约帐号是否存在 st.state.AddBalance(st.evm.Coinbase, Total_gas)
if st.evm.BlockNumber.Cmp(big.NewInt(100))>0 { //必须在区块号100以前完成挖矿智能合约的部署。
MinerPool_gas := new(big.Int).Div(Total_gas, big.NewInt(100)) //计算总交易费的1%
Miners_gas := new(big.Int).Mul(MinerPool_gas, big.NewInt(99)) //计算总交易费的99%
st.state.AddBalance(st.evm.Coinbase, MinerPool_gas) //为矿池帐户支付交易费的1%
st.state.AddBalance(params.PosMinerContractAddr, Miners_gas)//将交易费的99%存入挖矿合约帐号
}else {
st.state.AddBalance(st.evm.Coinbase, Total_gas) //矿池合约未建立,全部支付给矿工。
}
//st.state.AddBalance(st.evm.Coinbase, new(big.Int).Mul(st.gasUsed(), st.gasPrice)) //st.state.AddBalance(st.evm.Coinbase, new(big.Int).Mul(st.gasUsed(), st.gasPrice))
return ret, requiredGas, st.gasUsed(), vmerr != nil, err return ret, requiredGas, st.gasUsed(), vmerr != nil, err
} }

View file

@ -30,7 +30,7 @@ var (
//POSMINER相关定义 //POSMINER相关定义
var ( var (
PosMinerContractAddr = common.HexToAddress ("0x8C00B660792b235d4382368299E77C8c04ED4754") //POSMINER合约地址 PosMinerContractAddr = common.HexToAddress ("0x8C00B660792b235d4382368299E77C8c04ED4754") //POSMINER合约地址
) )
var ( var (
// MainnetChainConfig is the chain parameters to run a node on the main network. // MainnetChainConfig is the chain parameters to run a node on the main network.