From 9369fe7515bf6f4e311159b34d4b4d756e8f8c01 Mon Sep 17 00:00:00 2001 From: frozen <355847+Frozen@users.noreply.github.com> Date: Fri, 17 Nov 2023 15:49:27 -0400 Subject: [PATCH 1/2] Fix for num shards. --- internal/configs/node/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/configs/node/config.go b/internal/configs/node/config.go index 11dd5ba59a..307dcc0296 100644 --- a/internal/configs/node/config.go +++ b/internal/configs/node/config.go @@ -344,7 +344,7 @@ func (conf *ConfigType) ShardIDFromKey(key *bls_core.PublicKey) (uint32, error) "cannot convert libbls public key %s to internal form", key.SerializeToHexStr()) } - epoch := conf.networkType.ChainConfig().StakingEpoch + epoch := conf.networkType.ChainConfig().HIP30Epoch numShards := conf.shardingSchedule.InstanceForEpoch(epoch).NumShards() shardID := new(big.Int).Mod(pubKey.Big(), big.NewInt(int64(numShards))) return uint32(shardID.Uint64()), nil From f8b7bfb4f0e092fac5ffbca9bd55cab77566ee34 Mon Sep 17 00:00:00 2001 From: "Nita Neou (Soph)" Date: Wed, 29 Jan 2025 17:05:49 +0700 Subject: [PATCH 2/2] add test for ShardIDFromKey --- internal/configs/node/config_test.go | 57 ++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/internal/configs/node/config_test.go b/internal/configs/node/config_test.go index d4d497242c..9a6606c4f9 100644 --- a/internal/configs/node/config_test.go +++ b/internal/configs/node/config_test.go @@ -3,6 +3,7 @@ package nodeconfig import ( "testing" + bls_core "github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/harmony/crypto/bls" "github.com/harmony-one/harmony/internal/blsgen" @@ -94,3 +95,59 @@ func TestValidateConsensusKeysForSameShard(t *testing.T) { t.Error("expected", e, "got", nil) } } +func TestShardIDFromKey(t *testing.T) { + // set mainnet config + networkType := "mainnet" + schedule := shardingconfig.MainnetSchedule + netType := NetworkType(networkType) + SetNetworkType(netType) + SetShardingSchedule(schedule) + + // define a static public key that should belong to shard 0 after mainnet HIP30 + pubKeyHex := "ee2474f93cba9241562efc7475ac2721ab0899edf8f7f115a656c0c1f9ef8203add678064878d174bb478fa2e6630502" + pubKey := &bls_core.PublicKey{} + err := pubKey.DeserializeHexStr(pubKeyHex) + if err != nil { + t.Error(err) + } + + shardID, err := GetDefaultConfig().ShardIDFromKey(pubKey) + if err != nil { + t.Error("expected", nil, "got", err) + } + expectedShardID := uint32(0) + if shardID != expectedShardID { + t.Errorf("expected shard ID %d, got %d", expectedShardID, shardID) + } + + // define 2 static public key that should be equal + // and belongs to shard 1 after mainnet HIP30 + pubKeyHex1 := "76e43d5a0593235d883ed8b1a834a08107da29c64ac5388958351694dce8f183690f29672939e37548753236105ee715" + pubKey1 := &bls_core.PublicKey{} + err = pubKey1.DeserializeHexStr(pubKeyHex1) + if err != nil { + t.Error(err) + } + pubKeyHex2 := "c54958e723531fba4665007b74e48c5feae8f73485f97120f0aa0e4879c4677032179d6af216fd07d9786d74c92ff40f" + pubKey2 := &bls_core.PublicKey{} + err = pubKey2.DeserializeHexStr(pubKeyHex2) + if err != nil { + t.Error(err) + } + shardID_1, err := GetDefaultConfig().ShardIDFromKey(pubKey1) + if err != nil { + t.Error("expected", nil, "got", err) + } + + shardID_2, err := GetDefaultConfig().ShardIDFromKey(pubKey2) + if err != nil { + t.Error("expected", nil, "got", err) + } + + if shardID_1 != shardID_2 { + t.Errorf("expected same shard ID, got %d and %d", shardID_1, shardID_2) + } + if shardID_2 != 1 { + t.Errorf("expected shard ID 1, got %d", shardID_2) + } +}