|
|
|
@ -2,9 +2,12 @@ package shard |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"bytes" |
|
|
|
|
"math/big" |
|
|
|
|
"testing" |
|
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common" |
|
|
|
|
"github.com/ethereum/go-ethereum/rlp" |
|
|
|
|
"github.com/harmony-one/harmony/numeric" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
@ -18,6 +21,11 @@ var ( |
|
|
|
|
blsPubKey22 = [48]byte{} |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
const ( |
|
|
|
|
json1 = `[{"shard-id":0,"member-count":4,"subcommittee":[{"bls-pubkey":"72616e646f6d206b65792031000000000000000000000000000000000000000000000000000000000000000000000000","total-stake":null,"ecdsa-address":"one1qqqqqqqqqqqqqqqqqqqqqqq423ktdf2pznf238"},{"bls-pubkey":"72616e646f6d206b65792032000000000000000000000000000000000000000000000000000000000000000000000000","total-stake":null,"ecdsa-address":"one1qqqqqqqqqqqqqqqqqqqqqqq423ktdf2pznf238"},{"bls-pubkey":"72616e646f6d206b65792033000000000000000000000000000000000000000000000000000000000000000000000000","total-stake":null,"ecdsa-address":"one1qqqqqqqqqqqqqqqqqqqqqqq423ktdf2pznf238"},{"bls-pubkey":"72616e646f6d206b65792034000000000000000000000000000000000000000000000000000000000000000000000000","total-stake":null,"ecdsa-address":"one1qqqqqqqqqqqqqqqqqqqqqqq423ktdf2pznf238"}]},{"shard-id":1,"member-count":2,"subcommittee":[{"bls-pubkey":"72616e646f6d206b65792035000000000000000000000000000000000000000000000000000000000000000000000000","total-stake":null,"ecdsa-address":"one1qqqqqqqqqqqqqqqqqqqqqqq423ktdf2pznf238"},{"bls-pubkey":"72616e646f6d206b65792036000000000000000000000000000000000000000000000000000000000000000000000000","total-stake":null,"ecdsa-address":"one1qqqqqqqqqqqqqqqqqqqqqqq423ktdf2pznf238"}]}]` |
|
|
|
|
json2 = `[{"shard-id":0,"member-count":5,"subcommittee":[{"bls-pubkey":"72616e646f6d206b65792031000000000000000000000000000000000000000000000000000000000000000000000000","total-stake":null,"ecdsa-address":"one1qqqqqqqqqqqqqqqqqqqqqqq423ktdf2pznf238"},{"bls-pubkey":"72616e646f6d206b65792032000000000000000000000000000000000000000000000000000000000000000000000000","total-stake":null,"ecdsa-address":"one1qqqqqqqqqqqqqqqqqqqqqqq423ktdf2pznf238"},{"bls-pubkey":"72616e646f6d206b65792033000000000000000000000000000000000000000000000000000000000000000000000000","total-stake":"10.000000000000000000","ecdsa-address":"one1qqqqqqqqqqqqqqqqqqqqqqq423ktdf2pznf238"},{"bls-pubkey":"72616e646f6d206b65792035000000000000000000000000000000000000000000000000000000000000000000000000","total-stake":null,"ecdsa-address":"one1qqqqqqqqqqqqqqqqqqqqqqq423ktdf2pznf238"},{"bls-pubkey":"72616e646f6d206b65792031000000000000000000000000000000000000000000000000000000000000000000000000","total-stake":"45.123000000000000000","ecdsa-address":"one1qqqqqqqqqqqqqqqqqqqqqqq423ktdf2pznf238"}]},{"shard-id":1,"member-count":4,"subcommittee":[{"bls-pubkey":"72616e646f6d206b65792032000000000000000000000000000000000000000000000000000000000000000000000000","total-stake":"10.000000000000000000","ecdsa-address":"one1qqqqqqqqqqqqqqqqqqqqqqq423ktdf2pznf238"},{"bls-pubkey":"72616e646f6d206b65792033000000000000000000000000000000000000000000000000000000000000000000000000","total-stake":null,"ecdsa-address":"one1qqqqqqqqqqqqqqqqqqqqqqq423ktdf2pznf238"},{"bls-pubkey":"72616e646f6d206b65792034000000000000000000000000000000000000000000000000000000000000000000000000","total-stake":null,"ecdsa-address":"one1qqqqqqqqqqqqqqqqqqqqqqq423ktdf2pznf238"},{"bls-pubkey":"72616e646f6d206b65792035000000000000000000000000000000000000000000000000000000000000000000000000","total-stake":"45.123000000000000000","ecdsa-address":"one1qqqqqqqqqqqqqqqqqqqqqqq423ktdf2pznf238"}]}]` |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func init() { |
|
|
|
|
copy(blsPubKey1[:], []byte("random key 1")) |
|
|
|
|
copy(blsPubKey2[:], []byte("random key 2")) |
|
|
|
@ -92,3 +100,67 @@ func TestHash(t *testing.T) { |
|
|
|
|
t.Error("shardState1 and shardState2 should have equal hash") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestCompatibilityOldShardStateIntoNew(t *testing.T) { |
|
|
|
|
|
|
|
|
|
junkA := common.BigToAddress(big.NewInt(23452345345345)) |
|
|
|
|
stake1 := numeric.NewDec(10) |
|
|
|
|
stake2 := numeric.MustNewDecFromStr("45.123") |
|
|
|
|
|
|
|
|
|
preStakingState := StateLegacy{ |
|
|
|
|
CommitteeLegacy{ShardID: 0, Slots: SlotListLegacy{ |
|
|
|
|
SlotLegacy{junkA, blsPubKey1}, |
|
|
|
|
SlotLegacy{junkA, blsPubKey2}, |
|
|
|
|
SlotLegacy{junkA, blsPubKey3}, |
|
|
|
|
SlotLegacy{junkA, blsPubKey4}, |
|
|
|
|
}}, |
|
|
|
|
CommitteeLegacy{ShardID: 1, Slots: SlotListLegacy{ |
|
|
|
|
SlotLegacy{junkA, blsPubKey5}, |
|
|
|
|
SlotLegacy{junkA, blsPubKey6}, |
|
|
|
|
}}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
postStakingState := State{ |
|
|
|
|
Committee{ShardID: 0, Slots: SlotList{ |
|
|
|
|
Slot{junkA, blsPubKey1, nil}, |
|
|
|
|
Slot{junkA, blsPubKey2, nil}, |
|
|
|
|
Slot{junkA, blsPubKey3, &stake1}, |
|
|
|
|
Slot{junkA, blsPubKey5, nil}, |
|
|
|
|
Slot{junkA, blsPubKey1, &stake2}, |
|
|
|
|
}}, |
|
|
|
|
Committee{ShardID: 1, Slots: SlotList{ |
|
|
|
|
Slot{junkA, blsPubKey2, &stake1}, |
|
|
|
|
Slot{junkA, blsPubKey3, nil}, |
|
|
|
|
Slot{junkA, blsPubKey4, nil}, |
|
|
|
|
Slot{junkA, blsPubKey5, &stake2}, |
|
|
|
|
}}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
preStakingStateBytes, _ := rlp.EncodeToBytes(preStakingState) |
|
|
|
|
postStakingStateBytes, _ := rlp.EncodeToBytes(postStakingState) |
|
|
|
|
decodeJunk := []byte{0x10, 0x12, 0x04, 0x4} |
|
|
|
|
// Decode old shard state into new shard state
|
|
|
|
|
a, err1 := DecodeWrapper(preStakingStateBytes) |
|
|
|
|
b, err2 := DecodeWrapper(postStakingStateBytes) |
|
|
|
|
_, err3 := DecodeWrapper(decodeJunk) |
|
|
|
|
|
|
|
|
|
if err1 != nil { |
|
|
|
|
t.Errorf("Could not decode old format") |
|
|
|
|
} |
|
|
|
|
if err2 != nil { |
|
|
|
|
t.Errorf("Could not decode new format") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if a.JSON() != json1 { |
|
|
|
|
t.Error("old shard state into new shard state as JSON is not equal") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if b.JSON() != json2 { |
|
|
|
|
t.Error("new shard state into new shard state as JSON is not equal") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err3 == nil { |
|
|
|
|
t.Errorf("Should have caused error %v", err3) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|