|
|
@ -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
|
|
|
|
|
|
|
|
func (node *Node) GetNewStakedNodes() ([]common.Address) { |
|
|
|
|
|
|
|
BlocksPerEpoch := 5 //Hardcoded, will take value from core.Blockchain later.
|
|
|
|
|
|
|
|
currentHeight := node.blockchain.CurrentBlock().NumberU64() |
|
|
|
|
|
|
|
if currentHeight > BlocksPerEpoch { |
|
|
|
|
|
|
|
prevBlock := currentHeight - BlocksPerEpoch |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
prevBlock := 0 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return node.getNewStakedNodesFromBlockNumToBlockNum(prevBlock,currentHeight) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// This function will be used by the beaconLeader
|
|
|
|
|
|
|
|
//GetNewStakedNodesFromBlockNumToBlockNum gives a list of all nodes that have deposited transaction
|
|
|
|
|
|
|
|
func (node *Node) getNewStakedNodesFromBlockNumToBlockNum (prevBlockNum, toCurrentBlock uint64) ([]common.Address) { |
|
|
|
|
|
|
|
Blockchain := node.Blockchain() |
|
|
|
|
|
|
|
signerType := types.HomesteadSigner{} |
|
|
|
signerType := types.HomesteadSigner{} |
|
|
|
newNodesMap = make(map[common.Address]int) |
|
|
|
txns := block.Transactions() |
|
|
|
var newNodes []common.Address |
|
|
|
for i := range txns { |
|
|
|
for i := prevBlockNum,; i < toCurrentBlock + 1; i++ { |
|
|
|
txn := txns[i] |
|
|
|
block = Blockchain.GetBlockByNumber(from) |
|
|
|
value := txn.Value().Int64() |
|
|
|
txns = block.Transactions(), |
|
|
|
currentSender, _ := types.Sender(signerType, txn) |
|
|
|
for txn := range txns { |
|
|
|
_, isPresent := node.CurrentStakes[currentSender] |
|
|
|
if txn.Value() > 0 { //If value >0 means its a staking deposit transaction
|
|
|
|
toAddress := txn.To() |
|
|
|
newSender := types.Sender(signerType,txn) |
|
|
|
if *toAddress != node.StakeContractAddress { //Not a address aimed at the staking contract.
|
|
|
|
_, isPresent := newNodesMap[newSender] |
|
|
|
continue |
|
|
|
if !isPresent { |
|
|
|
} |
|
|
|
newNodesMap[newSender] = 1 |
|
|
|
if value > int64(0) { //If value >0 means its a staking deposit transaction
|
|
|
|
newNodes := append(newNodes, newSender) |
|
|
|
if isPresent { |
|
|
|
|
|
|
|
//This means this node has increaserd its stake
|
|
|
|
|
|
|
|
node.CurrentStakes[currentSender] += value |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
node.CurrentStakes[currentSender] = value |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { //This means node has withdrawn stake.
|
|
|
|
|
|
|
|
getData := txn.Data() |
|
|
|
|
|
|
|
value := decodeStakeCall(getData) //Value being withdrawn
|
|
|
|
|
|
|
|
if isPresent { |
|
|
|
|
|
|
|
//This means this node has increaserd its stake
|
|
|
|
|
|
|
|
if node.CurrentStakes[currentSender] > value { |
|
|
|
|
|
|
|
node.CurrentStakes[currentSender] -= value |
|
|
|
|
|
|
|
} else if node.CurrentStakes[currentSender] == value { |
|
|
|
|
|
|
|
delete(node.CurrentStakes, currentSender) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
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)) |
|
|
|