OSDN Git Service

delete first tx vote amount restrict (#301)
[bytom/vapor.git] / protocol / state / consensus_result.go
index 16fcded..53bdbeb 100644 (file)
@@ -14,6 +14,15 @@ import (
        "github.com/vapor/protocol/bc/types"
 )
 
+// fedConsensusPath is used to derive federation root xpubs for signing blocks
+var fedConsensusPath = [][]byte{
+       []byte{0xff, 0xff, 0xff, 0xff},
+       []byte{0xff, 0x00, 0x00, 0x00},
+       []byte{0xff, 0xff, 0xff, 0xff},
+       []byte{0xff, 0x00, 0x00, 0x00},
+       []byte{0xff, 0x00, 0x00, 0x00},
+}
+
 // ConsensusNode represents a consensus node
 type ConsensusNode struct {
        XPub    chainkd.XPub
@@ -38,9 +47,11 @@ type CoinbaseReward struct {
 // SortByAmount implements sort.Interface for CoinbaseReward slices
 type SortByAmount []CoinbaseReward
 
-func (a SortByAmount) Len() int           { return len(a) }
-func (a SortByAmount) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
-func (a SortByAmount) Less(i, j int) bool { return a[i].Amount < a[j].Amount }
+func (a SortByAmount) Len() int      { return len(a) }
+func (a SortByAmount) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
+func (a SortByAmount) Less(i, j int) bool {
+       return a[i].Amount > a[j].Amount || (a[i].Amount == a[j].Amount && hex.EncodeToString(a[i].ControlProgram) > hex.EncodeToString(a[j].ControlProgram))
+}
 
 // CalCoinbaseReward calculate the coinbase reward for block
 func CalCoinbaseReward(block *types.Block) (*CoinbaseReward, error) {
@@ -72,7 +83,7 @@ func CalcVoteSeq(blockHeight uint64) uint64 {
        if blockHeight == 0 {
                return 0
        }
-       return (blockHeight-1)/consensus.RoundVoteBlockNums + 1
+       return (blockHeight-1)/consensus.ActiveNetParams.RoundVoteBlockNums + 1
 }
 
 // ConsensusResult represents a snapshot of each round of DPOS voting
@@ -98,39 +109,47 @@ func (c *ConsensusResult) ApplyBlock(block *types.Block) error {
        }
 
        for _, tx := range block.Transactions {
-               for _, input := range tx.Inputs {
-                       vetoInput, ok := input.TypedInput.(*types.VetoInput)
-                       if !ok {
-                               continue
-                       }
+               if err := c.ApplyTransaction(tx); err != nil {
+                       return err
+               }
+       }
 
-                       pubkey := hex.EncodeToString(vetoInput.Vote)
-                       c.NumOfVote[pubkey], ok = checked.SubUint64(c.NumOfVote[pubkey], vetoInput.Amount)
-                       if !ok {
-                               return checked.ErrOverflow
-                       }
+       c.BlockHash = block.Hash()
+       c.BlockHeight = block.Height
+       c.Seq = CalcVoteSeq(block.Height)
+       return nil
+}
 
-                       if c.NumOfVote[pubkey] == 0 {
-                               delete(c.NumOfVote, pubkey)
-                       }
+// ApplyTransaction calculate the consensus result for transaction
+func (c *ConsensusResult) ApplyTransaction(tx *types.Tx) error {
+       for _, input := range tx.Inputs {
+               vetoInput, ok := input.TypedInput.(*types.VetoInput)
+               if !ok {
+                       continue
                }
 
-               for _, output := range tx.Outputs {
-                       voteOutput, ok := output.TypedOutput.(*types.VoteOutput)
-                       if !ok {
-                               continue
-                       }
+               pubkey := hex.EncodeToString(vetoInput.Vote)
+               c.NumOfVote[pubkey], ok = checked.SubUint64(c.NumOfVote[pubkey], vetoInput.Amount)
+               if !ok {
+                       return checked.ErrOverflow
+               }
 
-                       pubkey := hex.EncodeToString(voteOutput.Vote)
-                       if c.NumOfVote[pubkey], ok = checked.AddUint64(c.NumOfVote[pubkey], voteOutput.Amount); !ok {
-                               return checked.ErrOverflow
-                       }
+               if c.NumOfVote[pubkey] == 0 {
+                       delete(c.NumOfVote, pubkey)
                }
        }
 
-       c.BlockHash = block.Hash()
-       c.BlockHeight = block.Height
-       c.Seq = CalcVoteSeq(block.Height)
+       for _, output := range tx.Outputs {
+               voteOutput, ok := output.TypedOutput.(*types.VoteOutput)
+               if !ok {
+                       continue
+               }
+
+               pubkey := hex.EncodeToString(voteOutput.Vote)
+               if c.NumOfVote[pubkey], ok = checked.AddUint64(c.NumOfVote[pubkey], voteOutput.Amount); !ok {
+                       return checked.ErrOverflow
+               }
+       }
        return nil
 }
 
@@ -141,7 +160,7 @@ func (c *ConsensusResult) AttachCoinbaseReward(block *types.Block) error {
                return err
        }
 
-       if block.Height%consensus.RoundVoteBlockNums == 1 {
+       if block.Height%consensus.ActiveNetParams.RoundVoteBlockNums == 1 {
                c.CoinbaseReward = map[string]uint64{}
        }
 
@@ -158,7 +177,7 @@ func (c *ConsensusResult) AttachCoinbaseReward(block *types.Block) error {
 func (c *ConsensusResult) ConsensusNodes() (map[string]*ConsensusNode, error) {
        var nodes []*ConsensusNode
        for pubkey, voteNum := range c.NumOfVote {
-               if voteNum >= consensus.MinConsensusNodeVoteNum {
+               if voteNum >= consensus.ActiveNetParams.MinConsensusNodeVoteNum {
                        var xpub chainkd.XPub
                        if err := xpub.UnmarshalText([]byte(pubkey)); err != nil {
                                return nil, err
@@ -171,7 +190,7 @@ func (c *ConsensusResult) ConsensusNodes() (map[string]*ConsensusNode, error) {
        // if there is a performance problem, consider the optimization later.
        sort.Sort(byVote(nodes))
        result := make(map[string]*ConsensusNode)
-       for i := 0; i < len(nodes) && i < consensus.NumOfConsensusNode; i++ {
+       for i := 0; i < len(nodes) && int64(i) < consensus.ActiveNetParams.NumOfConsensusNode; i++ {
                nodes[i].Order = uint64(i)
                result[nodes[i].XPub.String()] = nodes[i]
        }
@@ -232,7 +251,7 @@ func (c *ConsensusResult) DetachBlock(block *types.Block) error {
 
 // DetachCoinbaseReward detach coinbase reward
 func (c *ConsensusResult) DetachCoinbaseReward(block *types.Block) error {
-       if block.Height%consensus.RoundVoteBlockNums == 0 {
+       if block.Height%consensus.ActiveNetParams.RoundVoteBlockNums == 0 {
                for i, output := range block.Transactions[0].Outputs {
                        if i == 0 {
                                continue
@@ -281,13 +300,13 @@ func (c *ConsensusResult) Fork() *ConsensusResult {
 
 // IsFinalize check if the result is end of consensus round
 func (c *ConsensusResult) IsFinalize() bool {
-       return c.BlockHeight%consensus.RoundVoteBlockNums == 0
+       return c.BlockHeight%consensus.ActiveNetParams.RoundVoteBlockNums == 0
 }
 
 // GetCoinbaseRewards convert into CoinbaseReward array and sort it by amount
 func (c *ConsensusResult) GetCoinbaseRewards(blockHeight uint64) ([]CoinbaseReward, error) {
        rewards := []CoinbaseReward{}
-       if blockHeight%consensus.RoundVoteBlockNums != 0 {
+       if blockHeight%consensus.ActiveNetParams.RoundVoteBlockNums != 0 {
                return rewards, nil
        }
 
@@ -309,7 +328,8 @@ func (c *ConsensusResult) GetCoinbaseRewards(blockHeight uint64) ([]CoinbaseRewa
 func federationNodes() map[string]*ConsensusNode {
        consensusResult := map[string]*ConsensusNode{}
        for i, xpub := range config.CommonConfig.Federation.Xpubs {
-               consensusResult[xpub.String()] = &ConsensusNode{XPub: xpub, VoteNum: 0, Order: uint64(i)}
+               derivedXPub := xpub.Derive(fedConsensusPath)
+               consensusResult[derivedXPub.String()] = &ConsensusNode{XPub: derivedXPub, VoteNum: 0, Order: uint64(i)}
        }
        return consensusResult
 }