"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
}
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
}
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
}