Fix bugs and refactor NodeList

pull/1834/head
Rongjian Lan 5 years ago
parent 92f80b0445
commit f6ffb43277
  1. 16
      api/service/explorer/service.go
  2. 8
      api/service/explorer/storage_test.go
  3. 2
      consensus/consensus_service.go
  4. 64
      core/blockchain.go
  5. 6
      core/rawdb/accessors_chain.go
  6. 4
      internal/chain/engine.go
  7. 4
      internal/chain/reward.go
  8. 14
      internal/hmyapi/blockchain.go
  9. 2
      node/node_cross_shard.go
  10. 6
      node/node_resharding.go
  11. 26
      shard/committee/assignment.go
  12. 42
      shard/shard_state.go
  13. 12
      shard/shard_state_test.go

@ -282,8 +282,8 @@ func (s *Service) GetExplorerBlocks(w http.ResponseWriter, r *http.Request) {
curEpoch = int64(block.Epoch)
}
if withSigners {
pubkeys := make([]*bls.PublicKey, len(committee.NodeList))
for i, validator := range committee.NodeList {
pubkeys := make([]*bls.PublicKey, len(committee.Slots))
for i, validator := range committee.Slots {
pubkeys[i] = new(bls.PublicKey)
validator.BlsPublicKey.ToLibBLSPublicKey(pubkeys[i])
}
@ -291,7 +291,7 @@ func (s *Service) GetExplorerBlocks(w http.ResponseWriter, r *http.Request) {
if err == nil && accountBlocks[id+1] != nil {
err = mask.SetMask(accountBlocks[id+1].Header().LastCommitBitmap())
if err == nil {
for _, validator := range committee.NodeList {
for _, validator := range committee.Slots {
oneAddress, err := common2.AddressToBech32(validator.EcdsaAddress)
if err != nil {
continue
@ -403,8 +403,8 @@ func (s *ServiceAPI) GetExplorerBlocks(ctx context.Context, from, to, page, offs
curEpoch = int64(block.Epoch)
}
if withSigners {
pubkeys := make([]*bls.PublicKey, len(committee.NodeList))
for i, validator := range committee.NodeList {
pubkeys := make([]*bls.PublicKey, len(committee.Slots))
for i, validator := range committee.Slots {
pubkeys[i] = new(bls.PublicKey)
validator.BlsPublicKey.ToLibBLSPublicKey(pubkeys[i])
}
@ -412,7 +412,7 @@ func (s *ServiceAPI) GetExplorerBlocks(ctx context.Context, from, to, page, offs
if err == nil && accountBlocks[id+1] != nil {
err = mask.SetMask(accountBlocks[id+1].Header().LastCommitBitmap())
if err == nil {
for _, validator := range committee.NodeList {
for _, validator := range committee.Slots {
oneAddress, err := common2.AddressToBech32(validator.EcdsaAddress)
if err != nil {
continue
@ -592,7 +592,7 @@ func (s *Service) GetExplorerCommittee(w http.ResponseWriter, r *http.Request) {
return
}
validators := &Committee{}
for _, validator := range committee.NodeList {
for _, validator := range committee.Slots {
validatorBalance := big.NewInt(0)
validatorBalance, err := s.GetAccountBalance(validator.EcdsaAddress)
if err != nil {
@ -645,7 +645,7 @@ func (s *ServiceAPI) GetExplorerCommittee(ctx context.Context, shardID uint32, e
return nil, err
}
validators := &Committee{}
for _, validator := range committee.NodeList {
for _, validator := range committee.Slots {
validatorBalance := big.NewInt(0)
validatorBalance, err := s.Service.GetAccountBalance(validator.EcdsaAddress)
if err != nil {

@ -90,10 +90,10 @@ func TestDumpCommittee(t *testing.T) {
BlsPublicKey2 := new(shard.BlsPublicKey)
BlsPublicKey1.FromLibBLSPublicKey(blsPubKey1)
BlsPublicKey2.FromLibBLSPublicKey(blsPubKey2)
nodeID1 := shard.NodeID{EcdsaAddress: common.HexToAddress("52789f18a342da8023cc401e5d2b14a6b710fba9"), BlsPublicKey: *BlsPublicKey1}
nodeID2 := shard.NodeID{EcdsaAddress: common.HexToAddress("7c41e0668b551f4f902cfaec05b5bdca68b124ce"), BlsPublicKey: *BlsPublicKey2}
nodeIDList := []shard.NodeID{nodeID1, nodeID2}
committee := shard.Committee{ShardID: uint32(0), NodeList: nodeIDList}
nodeID1 := shard.Slot{EcdsaAddress: common.HexToAddress("52789f18a342da8023cc401e5d2b14a6b710fba9"), BlsPublicKey: *BlsPublicKey1}
nodeID2 := shard.Slot{EcdsaAddress: common.HexToAddress("7c41e0668b551f4f902cfaec05b5bdca68b124ce"), BlsPublicKey: *BlsPublicKey2}
nodeIDList := []shard.Slot{nodeID1, nodeID2}
committee := shard.Committee{ShardID: uint32(0), Slots: nodeIDList}
shardID := uint32(0)
epoch := uint64(0)
ins := GetStorageInstance("1.1.1.1", "3333", true)

@ -428,7 +428,7 @@ func (consensus *Consensus) getLeaderPubKeyFromCoinbase(header *block.Header) (*
)
}
committerKey := new(bls.PublicKey)
for _, member := range committee.NodeList {
for _, member := range committee.Slots {
if member.EcdsaAddress == header.Coinbase() {
err := member.BlsPublicKey.ToLibBLSPublicKey(committerKey)
if err != nil {

@ -1135,6 +1135,7 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.
//// Shard State and Validator Update
header := block.Header()
if header.ShardStateHash() != (common.Hash{}) {
// Write shard state for the new epoch
epoch := new(big.Int).Add(header.Epoch(), common.Big1)
shardState, err := bc.WriteShardStateBytes(batch, epoch, header.ShardState())
if err != nil {
@ -1142,15 +1143,18 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.
return NonStatTy, err
}
processed := make(map[common.Address]struct{})
// Find all the active validator addresses and do a snapshot
allActiveValidators := []common.Address{}
processed := make(map[common.Address]struct{})
for i := range *shardState {
shard := (*shardState)[i]
for j := range shard.NodeList {
if shard.NodeList[j].StakeWithDelegationApplied != nil { // For external validator
_, ok := processed[shard.NodeList[j].EcdsaAddress]
for j := range shard.Slots {
slot := shard.Slots[j]
if slot.StakeWithDelegationApplied != nil { // For external validator
_, ok := processed[slot.EcdsaAddress]
if !ok {
allActiveValidators = append(allActiveValidators, shard.NodeList[j].EcdsaAddress)
processed[slot.EcdsaAddress] = struct{}{}
allActiveValidators = append(allActiveValidators, shard.Slots[j].EcdsaAddress)
}
}
}
@ -1158,6 +1162,7 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.
bc.UpdateActiveValidatorsSnapshot(allActiveValidators)
}
// Do bookkeeping for new staking txns
if bc.chainConfig.IsStaking(block.Epoch()) {
for _, tx := range block.StakingTransactions() {
err = bc.UpdateStakingMetaData(tx)
@ -2296,7 +2301,7 @@ func (bc *BlockChain) ReadTxLookupEntry(txID common.Hash) (common.Hash, uint64,
// ReadValidatorData reads staking information of given validatorWrapper
func (bc *BlockChain) ReadValidatorData(addr common.Address) (*staking.ValidatorWrapper, error) {
if cached, ok := bc.validatorCache.Get("v-" + string(addr.Bytes())); ok {
if cached, ok := bc.validatorCache.Get("validator-" + string(addr.Bytes())); ok {
by := cached.([]byte)
v := staking.ValidatorWrapper{}
if err := rlp.DecodeBytes(by, &v); err != nil {
@ -2318,14 +2323,14 @@ func (bc *BlockChain) WriteValidatorData(v *staking.ValidatorWrapper) error {
if err != nil {
return err
}
bc.validatorCache.Add("v-"+string(v.Address.Bytes()), by)
bc.validatorCache.Add("validator-"+string(v.Address.Bytes()), by)
return nil
}
// ReadValidatorSnapshot reads the snapshot staking information of given validator address
// TODO: put epoch number in to snapshot too.
func (bc *BlockChain) ReadValidatorSnapshot(addr common.Address) (*staking.ValidatorWrapper, error) {
if cached, ok := bc.validatorCache.Get("vs-" + string(addr.Bytes())); ok {
if cached, ok := bc.validatorCache.Get("validator-snapshot-" + string(addr.Bytes())); ok {
by := cached.([]byte)
v := staking.ValidatorWrapper{}
if err := rlp.DecodeBytes(by, &v); err != nil {
@ -2339,6 +2344,7 @@ func (bc *BlockChain) ReadValidatorSnapshot(addr common.Address) (*staking.Valid
// WriteValidatorSnapshots writes the snapshot of provided list of validators
func (bc *BlockChain) WriteValidatorsSnapshot(addrs []common.Address) error {
// Read all validator's current data
validators := []*staking.ValidatorWrapper{}
for _, addr := range addrs {
validator, err := bc.ReadValidatorData(addr)
@ -2348,6 +2354,7 @@ func (bc *BlockChain) WriteValidatorsSnapshot(addrs []common.Address) error {
validators = append(validators, validator)
}
// Batch write the current data as snapshot
batch := bc.db.NewBatch()
for i := range validators {
err := rawdb.WriteValidatorSnapshot(batch, validators[i])
@ -2359,10 +2366,11 @@ func (bc *BlockChain) WriteValidatorsSnapshot(addrs []common.Address) error {
return err
}
// Update cache
for i := range validators {
by, err := rlp.EncodeToBytes(validators[i])
if err == nil {
bc.validatorCache.Add("vs-"+string(validators[i].Address.Bytes()), by)
bc.validatorCache.Add("validator-snapshot-"+string(validators[i].Address.Bytes()), by)
}
}
return nil
@ -2378,7 +2386,25 @@ func (bc *BlockChain) DeleteValidatorsSnapshot(addrs []common.Address) error {
return err
}
for i := range addrs {
bc.validatorCache.Remove("vs-" + string(addrs[i].Bytes()))
bc.validatorCache.Remove("validator-snapshot-" + string(addrs[i].Bytes()))
}
return nil
}
// UpdateActiveValidatorsSnapshot updates the list of active validators and updates the content snapshot of the active validators
func (bc *BlockChain) UpdateActiveValidatorsSnapshot(activeValidators []common.Address) error {
prevActiveValidators, err := bc.ReadActiveValidatorList()
if err != nil {
return err
}
err = bc.DeleteValidatorsSnapshot(prevActiveValidators)
if err != nil {
return err
}
if err = bc.WriteValidatorsSnapshot(activeValidators); err != nil {
return err
}
return nil
}
@ -2435,24 +2461,6 @@ func (bc *BlockChain) WriteActiveValidatorList(addrs []common.Address) error {
return nil
}
// UpdateActiveValidatorsSnapshot updates the list of active validators and updates the content snapshot of the active validators
func (bc *BlockChain) UpdateActiveValidatorsSnapshot(activeValidators []common.Address) error {
prevActiveValidators, err := bc.ReadActiveValidatorList()
if err != nil {
return err
}
err = bc.DeleteValidatorsSnapshot(prevActiveValidators)
if err != nil {
return err
}
if err = bc.WriteValidatorsSnapshot(activeValidators); err != nil {
return err
}
return nil
}
// ReadValidatorListByDelegator reads the addresses of validators delegated by a delegator
func (bc *BlockChain) ReadValidatorListByDelegator(delegator common.Address) ([]common.Address, error) {
if cached, ok := bc.validatorListByDelegatorCache.Get(delegator.Bytes()); ok {

@ -629,7 +629,7 @@ func ReadValidatorData(db DatabaseReader, addr common.Address) (*staking.Validat
return &v, nil
}
// WriteValidatorData stores staking validator's information by its address
// WriteValidatorData stores validator's information by its address
func WriteValidatorData(db DatabaseWriter, v *staking.ValidatorWrapper) error {
bytes, err := rlp.EncodeToBytes(v)
if err != nil {
@ -643,7 +643,7 @@ func WriteValidatorData(db DatabaseWriter, v *staking.ValidatorWrapper) error {
return err
}
// ReadValidatorSnapshot retrieves staking validator's snapshot by its address
// ReadValidatorSnapshot retrieves validator's snapshot by its address
func ReadValidatorSnapshot(db DatabaseReader, addr common.Address) (*staking.ValidatorWrapper, error) {
data, err := db.Get(validatorSnapshotKey(addr))
if len(data) == 0 || err != nil {
@ -658,7 +658,7 @@ func ReadValidatorSnapshot(db DatabaseReader, addr common.Address) (*staking.Val
return &v, nil
}
// WriteValidatorSnapshot stores staking validator's snapshot by its address
// WriteValidatorSnapshot stores validator's snapshot by its address
func WriteValidatorSnapshot(db DatabaseWriter, v *staking.ValidatorWrapper) error {
bytes, err := rlp.EncodeToBytes(v)
if err != nil {

@ -227,7 +227,7 @@ func QuorumForBlock(chain engine.ChainReader, h *block.Header, reCalculate bool)
return 0, errors.Errorf(
"cannot find shard %d in shard state", h.ShardID())
}
return (len(c.NodeList))*2/3 + 1, nil
return (len(c.Slots))*2/3 + 1, nil
}
// Similiar to VerifyHeader, which is only for verifying the block headers of one's own chain, this verification
@ -289,7 +289,7 @@ func GetPublicKeys(chain engine.ChainReader, header *block.Header, reCalculate b
)
}
var committerKeys []*bls.PublicKey
for _, member := range committee.NodeList {
for _, member := range committee.Slots {
committerKey := new(bls.PublicKey)
err := member.BlsPublicKey.ToLibBLSPublicKey(committerKey)
if err != nil {

@ -61,7 +61,7 @@ func AccumulateRewards(
)
}
var committerKeys []*bls.PublicKey
for _, member := range parentCommittee.NodeList {
for _, member := range parentCommittee.Slots {
committerKey := new(bls.PublicKey)
err := member.BlsPublicKey.ToLibBLSPublicKey(committerKey)
if err != nil {
@ -80,7 +80,7 @@ func AccumulateRewards(
accounts := []common.Address{}
for idx, member := range parentCommittee.NodeList {
for idx, member := range parentCommittee.Slots {
if signed, err := mask.IndexEnabled(idx); err != nil {
return ctxerror.New("cannot check for committer bit",
"committerIndex", idx,

@ -153,7 +153,7 @@ func (s *PublicBlockChainAPI) GetValidators(ctx context.Context, epoch int64) (m
return nil, err
}
validators := make([]map[string]interface{}, 0)
for _, validator := range committee.NodeList {
for _, validator := range committee.Slots {
validatorBalance := new(hexutil.Big)
validatorBalance, err = s.b.GetBalance(validator.EcdsaAddress)
if err != nil {
@ -193,8 +193,8 @@ func (s *PublicBlockChainAPI) GetBlockSigners(ctx context.Context, blockNr rpc.B
if err != nil {
return nil, err
}
pubkeys := make([]*bls.PublicKey, len(committee.NodeList))
for i, validator := range committee.NodeList {
pubkeys := make([]*bls.PublicKey, len(committee.Slots))
for i, validator := range committee.Slots {
pubkeys[i] = new(bls.PublicKey)
validator.BlsPublicKey.ToLibBLSPublicKey(pubkeys[i])
}
@ -210,7 +210,7 @@ func (s *PublicBlockChainAPI) GetBlockSigners(ctx context.Context, blockNr rpc.B
if err != nil {
return result, err
}
for _, validator := range committee.NodeList {
for _, validator := range committee.Slots {
oneAddress, err := internal_common.AddressToBech32(validator.EcdsaAddress)
if err != nil {
return result, err
@ -241,8 +241,8 @@ func (s *PublicBlockChainAPI) IsBlockSigner(ctx context.Context, blockNr rpc.Blo
if err != nil {
return false, err
}
pubkeys := make([]*bls.PublicKey, len(committee.NodeList))
for i, validator := range committee.NodeList {
pubkeys := make([]*bls.PublicKey, len(committee.Slots))
for i, validator := range committee.Slots {
pubkeys[i] = new(bls.PublicKey)
validator.BlsPublicKey.ToLibBLSPublicKey(pubkeys[i])
}
@ -254,7 +254,7 @@ func (s *PublicBlockChainAPI) IsBlockSigner(ctx context.Context, blockNr rpc.Blo
if err != nil {
return false, err
}
for _, validator := range committee.NodeList {
for _, validator := range committee.Slots {
oneAddress, err := internal_common.AddressToBech32(validator.EcdsaAddress)
if err != nil {
return false, err

@ -298,7 +298,7 @@ func (node *Node) VerifyCrosslinkHeader(prevHeader, header *block.Header) error
var committerKeys []*bls.PublicKey
parseKeysSuccess := true
for _, member := range committee.NodeList {
for _, member := range committee.Slots {
committerKey := new(bls.PublicKey)
err = member.BlsPublicKey.ToLibBLSPublicKey(committerKey)
if err != nil {

@ -201,7 +201,7 @@ func (node *Node) transitionIntoNextEpoch(shardState types.State) {
for _, c := range shardState {
utils.Logger().Debug().
Uint32("shardID", c.ShardID).
Str("nodeList", c.NodeList).
Str("nodeList", c.Slots).
Msg("new shard information")
}
myShardID, isNextLeader := findRoleInShardState(
@ -219,7 +219,7 @@ func (node *Node) transitionIntoNextEpoch(shardState types.State) {
// Update public keys
var publicKeys []*bls.PublicKey
for idx, nodeID := range myShardState.NodeList {
for idx, nodeID := range myShardState.Slots {
key := &bls.PublicKey{}
err := key.Deserialize(nodeID.BlsPublicKey[:])
if err != nil {
@ -249,7 +249,7 @@ func findRoleInShardState(
) (shardID uint32, isLeader bool) {
keyBytes := key.Serialize()
for idx, shard := range state {
for nodeIdx, nodeID := range shard.NodeList {
for nodeIdx, nodeID := range shard.Slots {
if bytes.Compare(nodeID.BlsPublicKey[:], keyBytes) == 0 {
return uint32(idx), nodeIdx == 0
}

@ -85,12 +85,12 @@ func preStakingEnabledCommittee(s shardingconfig.Instance) shard.State {
pubKey := shard.BlsPublicKey{}
pubKey.FromLibBLSPublicKey(pub)
// TODO: directly read address for bls too
curNodeID := shard.NodeID{
curNodeID := shard.Slot{
common2.ParseAddr(hmyAccounts[index].Address),
pubKey,
nil,
}
com.NodeList = append(com.NodeList, curNodeID)
com.Slots = append(com.Slots, curNodeID)
}
// add FN runner's key
for j := shardHarmonyNodes; j < shardSize; j++ {
@ -100,12 +100,12 @@ func preStakingEnabledCommittee(s shardingconfig.Instance) shard.State {
pubKey := shard.BlsPublicKey{}
pubKey.FromLibBLSPublicKey(pub)
// TODO: directly read address for bls too
curNodeID := shard.NodeID{
curNodeID := shard.Slot{
common2.ParseAddr(fnAccounts[index].Address),
pubKey,
nil,
}
com.NodeList = append(com.NodeList, curNodeID)
com.Slots = append(com.Slots, curNodeID)
}
shardState = append(shardState, com)
}
@ -137,7 +137,7 @@ func eposStakedCommittee(
hAccounts := s.HmyAccounts()
for i := 0; i < shardCount; i++ {
superComm[i] = shard.Committee{uint32(i), shard.NodeIDList{}}
superComm[i] = shard.Committee{uint32(i), shard.SlotList{}}
}
for i := range hAccounts {
@ -146,7 +146,7 @@ func eposStakedCommittee(
pub.DeserializeHexStr(hAccounts[i].BlsPublicKey)
pubKey := shard.BlsPublicKey{}
pubKey.FromLibBLSPublicKey(pub)
superComm[spot].NodeList = append(superComm[spot].NodeList, shard.NodeID{
superComm[spot].Slots = append(superComm[spot].Slots, shard.Slot{
common2.ParseAddr(hAccounts[i].Address),
pubKey,
nil,
@ -164,7 +164,7 @@ func eposStakedCommittee(
for i := 0; i < stakedSlotsCount; i++ {
bucket := int(new(big.Int).Mod(staked[i].BlsPublicKey.Big(), shardBig).Int64())
slot := staked[i]
superComm[bucket].NodeList = append(superComm[bucket].NodeList, shard.NodeID{
superComm[bucket].Slots = append(superComm[bucket].Slots, shard.Slot{
slot.Address,
staked[i].BlsPublicKey,
&slot.Dec,
@ -191,10 +191,10 @@ func (def partialStakingEnabled) ComputePublicKeys(
allIdentities := make([][]*bls.PublicKey, len(superComm))
for i := range superComm {
allIdentities[i] = make([]*bls.PublicKey, len(superComm[i].NodeList))
for j := range superComm[i].NodeList {
allIdentities[i] = make([]*bls.PublicKey, len(superComm[i].Slots))
for j := range superComm[i].Slots {
identity := &bls.PublicKey{}
superComm[i].NodeList[j].BlsPublicKey.ToLibBLSPublicKey(identity)
superComm[i].Slots[j].BlsPublicKey.ToLibBLSPublicKey(identity)
allIdentities[i][j] = identity
}
}
@ -220,12 +220,12 @@ func (def partialStakingEnabled) ReadPublicKeysFromDB(
}
committerKeys := []*bls.PublicKey{}
for i := range subCommittee.NodeList {
for i := range subCommittee.Slots {
committerKey := new(bls.PublicKey)
err := subCommittee.NodeList[i].BlsPublicKey.ToLibBLSPublicKey(committerKey)
err := subCommittee.Slots[i].BlsPublicKey.ToLibBLSPublicKey(committerKey)
if err != nil {
return nil, ctxerror.New("cannot convert BLS public key",
"blsPublicKey", subCommittee.NodeList[i].BlsPublicKey).WithCause(err)
"blsPublicKey", subCommittee.Slots[i].BlsPublicKey).WithCause(err)
}
committerKeys = append(committerKeys, committerKey)
}

@ -34,27 +34,27 @@ type State []Committee
// BlsPublicKey defines the bls public key
type BlsPublicKey [PublicKeySizeInBytes]byte
// NodeID represents node id (BLS address)
type NodeID struct {
// Slot represents node id (BLS address)
type Slot struct {
EcdsaAddress common.Address `json:"ecdsa-address"`
BlsPublicKey BlsPublicKey `json:"bls-pubkey"`
// nil means not active, 0 means our node, >= 0 means staked node
StakeWithDelegationApplied *numeric.Dec `json:"staked-validator" rlp:"nil"`
}
// NodeIDList is a list of NodeIDList.
type NodeIDList []NodeID
// SlotList is a list of SlotList.
type SlotList []Slot
// Committee contains the active nodes in one shard
type Committee struct {
ShardID uint32 `json:"shard-id"`
NodeList NodeIDList `json:"subcommittee"`
ShardID uint32 `json:"shard-id"`
Slots SlotList `json:"subcommittee"`
}
// JSON produces a non-pretty printed JSON string of the SuperCommittee
func (ss State) JSON() string {
type t struct {
NodeID
Slot
EcdsaAddress string `json:"one-address"`
}
type v struct {
@ -64,12 +64,12 @@ func (ss State) JSON() string {
}
dump := make([]v, len(ss))
for i := range ss {
c := len(ss[i].NodeList)
c := len(ss[i].Slots)
dump[i].ShardID = ss[i].ShardID
dump[i].NodeList = make([]t, c)
dump[i].Count = c
for j := range ss[i].NodeList {
n := ss[i].NodeList[j]
for j := range ss[i].Slots {
n := ss[i].Slots[j]
dump[i].NodeList[j].BlsPublicKey = n.BlsPublicKey
dump[i].NodeList[j].StakeWithDelegationApplied = n.StakeWithDelegationApplied
dump[i].NodeList[j].EcdsaAddress = common2.MustAddressToBech32(n.EcdsaAddress)
@ -166,7 +166,7 @@ func CompareBlsPublicKey(k1, k2 BlsPublicKey) int {
}
// CompareNodeID compares two node IDs.
func CompareNodeID(id1, id2 *NodeID) int {
func CompareNodeID(id1, id2 *Slot) int {
if c := bytes.Compare(id1.EcdsaAddress[:], id2.EcdsaAddress[:]); c != 0 {
return c
}
@ -177,12 +177,12 @@ func CompareNodeID(id1, id2 *NodeID) int {
}
// DeepCopy returns a deep copy of the receiver.
func (l NodeIDList) DeepCopy() NodeIDList {
func (l SlotList) DeepCopy() SlotList {
return append(l[:0:0], l...)
}
// CompareNodeIDList compares two node ID lists.
func CompareNodeIDList(l1, l2 NodeIDList) int {
func CompareNodeIDList(l1, l2 SlotList) int {
commonLen := len(l1)
if commonLen > len(l2) {
commonLen = len(l2)
@ -205,7 +205,7 @@ func CompareNodeIDList(l1, l2 NodeIDList) int {
func (c Committee) DeepCopy() Committee {
r := Committee{}
r.ShardID = c.ShardID
r.NodeList = c.NodeList.DeepCopy()
r.Slots = c.Slots.DeepCopy()
return r
}
@ -217,7 +217,7 @@ func CompareCommittee(c1, c2 *Committee) int {
case c1.ShardID > c2.ShardID:
return +1
}
if c := CompareNodeIDList(c1.NodeList, c2.NodeList); c != 0 {
if c := CompareNodeIDList(c1.Slots, c2.Slots); c != 0 {
return c
}
return 0
@ -225,7 +225,7 @@ func CompareCommittee(c1, c2 *Committee) int {
// GetHashFromNodeList will sort the list, then use Keccak256 to hash the list
// NOTE: do not modify the underlining content for hash
func GetHashFromNodeList(nodeList []NodeID) []byte {
func GetHashFromNodeList(nodeList []Slot) []byte {
// in general, nodeList should not be empty
if nodeList == nil || len(nodeList) == 0 {
return []byte{}
@ -248,7 +248,7 @@ func (ss State) Hash() (h common.Hash) {
})
d := sha3.NewLegacyKeccak256()
for i := range copy {
hash := GetHashFromNodeList(copy[i].NodeList)
hash := GetHashFromNodeList(copy[i].Slots)
d.Write(hash)
}
d.Sum(h[:0])
@ -256,15 +256,15 @@ func (ss State) Hash() (h common.Hash) {
}
// CompareNodeIDByBLSKey compares two nodes by their ID; used to sort node list
func CompareNodeIDByBLSKey(n1 NodeID, n2 NodeID) int {
func CompareNodeIDByBLSKey(n1 Slot, n2 Slot) int {
return bytes.Compare(n1.BlsPublicKey[:], n2.BlsPublicKey[:])
}
// Serialize serialize NodeID into bytes
func (n NodeID) Serialize() []byte {
// Serialize serialize Slot into bytes
func (n Slot) Serialize() []byte {
return append(n.EcdsaAddress[:], n.BlsPublicKey[:]...)
}
func (n NodeID) String() string {
func (n Slot) String() string {
return "ECDSA: " + common2.MustAddressToBech32(n.EcdsaAddress) + ", BLS: " + hex.EncodeToString(n.BlsPublicKey[:])
}

@ -30,12 +30,12 @@ func init() {
}
func TestGetHashFromNodeList(t *testing.T) {
l1 := []NodeID{
l1 := []Slot{
{common.Address{0x11}, blsPubKey1, nil},
{common.Address{0x22}, blsPubKey2, nil},
{common.Address{0x33}, blsPubKey3, nil},
}
l2 := []NodeID{
l2 := []Slot{
{common.Address{0x22}, blsPubKey2, nil},
{common.Address{0x11}, blsPubKey1, nil},
{common.Address{0x33}, blsPubKey3, nil},
@ -51,7 +51,7 @@ func TestGetHashFromNodeList(t *testing.T) {
func TestHash(t *testing.T) {
com1 := Committee{
ShardID: 22,
NodeList: []NodeID{
Slots: []Slot{
{common.Address{0x12}, blsPubKey11, nil},
{common.Address{0x23}, blsPubKey22, nil},
{common.Address{0x11}, blsPubKey1, nil},
@ -59,7 +59,7 @@ func TestHash(t *testing.T) {
}
com2 := Committee{
ShardID: 2,
NodeList: []NodeID{
Slots: []Slot{
{common.Address{0x44}, blsPubKey4, nil},
{common.Address{0x55}, blsPubKey5, nil},
{common.Address{0x66}, blsPubKey6, nil},
@ -70,7 +70,7 @@ func TestHash(t *testing.T) {
com3 := Committee{
ShardID: 2,
NodeList: []NodeID{
Slots: []Slot{
{common.Address{0x44}, blsPubKey4, nil},
{common.Address{0x55}, blsPubKey5, nil},
{common.Address{0x66}, blsPubKey6, nil},
@ -78,7 +78,7 @@ func TestHash(t *testing.T) {
}
com4 := Committee{
ShardID: 22,
NodeList: []NodeID{
Slots: []Slot{
{common.Address{0x12}, blsPubKey11, nil},
{common.Address{0x23}, blsPubKey22, nil},
{common.Address{0x11}, blsPubKey1, nil},

Loading…
Cancel
Save