[dpos] add BP index updater
@@ -36,14 +36,28 @@ type blockProducer struct {
func NewCluster(ids []string, blockProducers uint16) (*Cluster, error) {
c := &Cluster{
size: blockProducers,
- member: make(map[uint16]*blockProducer),
- index: make(map[peer.ID]uint16),
}
+ if err := c.Update(ids); err != nil {
+ return nil, err
+ }
+
+ return c, nil
+}
+
+func newBlockProducer(id peer.ID) *blockProducer {
+ return &blockProducer{id: id}
+}
+
+// Update updates old cluster index by using ids.
+func (c *Cluster) Update(ids []string) error {
+ c.member = make(map[uint16]*blockProducer)
+ c.index = make(map[peer.ID]uint16)
+
for i, id := range ids {
bpID, err := peer.IDB58Decode(id)
if err != nil {
- return nil, fmt.Errorf("invalid node ID[%d]: %s", i, err.Error())
+ return fmt.Errorf("invalid node ID[%d]: %s", i, err.Error())
}
index := uint16(i)
@@ -51,15 +65,11 @@ func NewCluster(ids []string, blockProducers uint16) (*Cluster, error) {
c.index[bpID] = index
}
- if len(c.index) != int(blockProducers) {
- return nil, errBpSize{required: blockProducers, given: uint16(len(ids))}
+ if len(c.index) != int(c.size) {
+ return errBpSize{required: c.size, given: uint16(len(ids))}
}
- return c, nil
-}
-
-func newBlockProducer(id peer.ID) *blockProducer {
- return &blockProducer{id: id}
+ return nil
}
// BpIndex2ID returns the ID correspinding to idx.
