|
|
@ -159,6 +159,11 @@ type Node struct { |
|
|
|
// Service manager.
|
|
|
|
// Service manager.
|
|
|
|
serviceManager *service_manager.Manager |
|
|
|
serviceManager *service_manager.Manager |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Staked Accounts and Contract
|
|
|
|
|
|
|
|
CurrentStakes map[common.Address]int64 |
|
|
|
|
|
|
|
StakeContractAddress common.Address |
|
|
|
|
|
|
|
WithdrawStakeFunc []byte |
|
|
|
|
|
|
|
|
|
|
|
//Node Account
|
|
|
|
//Node Account
|
|
|
|
AccountKey *ecdsa.PrivateKey |
|
|
|
AccountKey *ecdsa.PrivateKey |
|
|
|
Address common.Address |
|
|
|
Address common.Address |
|
|
@ -570,44 +575,51 @@ func (node *Node) RemovePeersHandler() { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//UpdateStakingList updates the stakes of every node.
|
|
|
|
// This function will be used by the beaconLeader
|
|
|
|
func (node *Node) UpdateStakingList(block *types.Block) error { |
|
|
|
// GetNewStakedNodes gives a list of all nodes that have deposited transaction
|
|
|
|
signerType := types.HomesteadSigner{} |
|
|
|
func (node *Node) GetNewStakedNodes() ([]common.Address) { |
|
|
|
txns := block.Transactions() |
|
|
|
BlocksPerEpoch := 5 //Hardcoded, will take value from core.Blockchain later.
|
|
|
|
for i := range txns { |
|
|
|
currentHeight := node.blockchain.CurrentBlock().NumberU64() |
|
|
|
txn := txns[i] |
|
|
|
if currentHeight > BlocksPerEpoch { |
|
|
|
value := txn.Value().Int64() |
|
|
|
prevBlock := currentHeight - BlocksPerEpoch |
|
|
|
currentSender, _ := types.Sender(signerType, txn) |
|
|
|
} else { |
|
|
|
_, isPresent := node.CurrentStakes[currentSender] |
|
|
|
prevBlock := 0 |
|
|
|
toAddress := txn.To() |
|
|
|
} |
|
|
|
if *toAddress != node.StakeContractAddress { //Not a address aimed at the staking contract.
|
|
|
|
return node.getNewStakedNodesFromBlockNumToBlockNum(prevBlock,currentHeight) |
|
|
|
continue |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if value > int64(0) { //If value >0 means its a staking deposit transaction
|
|
|
|
// This function will be used by the beaconLeader
|
|
|
|
if isPresent { |
|
|
|
//GetNewStakedNodesFromBlockNumToBlockNum gives a list of all nodes that have deposited transaction
|
|
|
|
//This means this node has increaserd its stake
|
|
|
|
func (node *Node) getNewStakedNodesFromBlockNumToBlockNum (prevBlockNum, toCurrentBlock uint64) ([]common.Address) { |
|
|
|
node.CurrentStakes[currentSender] += value |
|
|
|
Blockchain := node.Blockchain() |
|
|
|
} else { |
|
|
|
signerType := types.HomesteadSigner{} |
|
|
|
node.CurrentStakes[currentSender] = value |
|
|
|
newNodesMap = make(map[common.Address]int) |
|
|
|
} |
|
|
|
var newNodes []common.Address |
|
|
|
} else { //This means node has withdrawn stake.
|
|
|
|
for i := prevBlockNum,; i < toCurrentBlock + 1; i++ { |
|
|
|
getData := txn.Data() |
|
|
|
block = Blockchain.GetBlockByNumber(from) |
|
|
|
value := decodeStakeCall(getData) //Value being withdrawn
|
|
|
|
txns = block.Transactions(), |
|
|
|
if isPresent { |
|
|
|
for txn := range txns { |
|
|
|
//This means this node has increaserd its stake
|
|
|
|
if txn.Value() > 0 { //If value >0 means its a staking deposit transaction
|
|
|
|
if node.CurrentStakes[currentSender] > value { |
|
|
|
newSender := types.Sender(signerType,txn) |
|
|
|
node.CurrentStakes[currentSender] -= value |
|
|
|
_, isPresent := newNodesMap[newSender] |
|
|
|
} else if node.CurrentStakes[currentSender] == value { |
|
|
|
if !isPresent { |
|
|
|
delete(node.CurrentStakes, currentSender) |
|
|
|
newNodesMap[newSender] = 1 |
|
|
|
} else { |
|
|
|
newNodes := append(newNodes, newSender) |
|
|
|
continue //Overdraft protection.
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
node.CurrentStakes[currentSender] = value |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return newNodes |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func decodeStakeCall(getData []byte) int64 { |
|
|
|
|
|
|
|
value := new(big.Int) |
|
|
|
|
|
|
|
value.SetBytes(getData[4:]) |
|
|
|
|
|
|
|
return value.Int64() |
|
|
|
|
|
|
|
} |
|
|
|
func (node *Node) setupForShardLeader() { |
|
|
|
func (node *Node) setupForShardLeader() { |
|
|
|
// Register explorer service.
|
|
|
|
// Register explorer service.
|
|
|
|
node.serviceManager.RegisterService(service_manager.SupportExplorer, explorer.New(&node.SelfPeer)) |
|
|
|
node.serviceManager.RegisterService(service_manager.SupportExplorer, explorer.New(&node.SelfPeer)) |
|
|
|