The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/node/node_test.go

306 lines
8.5 KiB

package node
import (
"errors"
"fmt"
"os"
"sync"
"testing"
"time"
proto_discovery "github.com/harmony-one/harmony/api/proto/discovery"
"github.com/harmony-one/harmony/consensus"
"github.com/harmony-one/harmony/consensus/quorum"
bls2 "github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/crypto/pki"
"github.com/harmony-one/harmony/drand"
"github.com/harmony-one/harmony/internal/shardchain"
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/multibls"
"github.com/harmony-one/harmony/p2p"
"github.com/harmony-one/harmony/p2p/p2pimpl"
"github.com/harmony-one/harmony/shard"
"github.com/stretchr/testify/assert"
)
var testDBFactory = &shardchain.MemDBFactory{}
6 years ago
func TestNewNode(t *testing.T) {
blsKey := bls2.RandPrivateKey()
pubKey := blsKey.GetPublicKey()
leader := p2p.Peer{IP: "127.0.0.1", Port: "8882", ConsensusPubKey: pubKey}
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")
host, err := p2pimpl.NewHost(&leader, priKey)
if err != nil {
t.Fatalf("newhost failure: %v", err)
}
[rpc][availability][apr] Richer validator information, implement APR, unify EPoS computation, remove fall 2019 tech debt (#2484) * [rpc][validator] Extend hmy blockchain validator information * [availability] Optimize bump count * [staking][validator][rpc] Remove validator stats rpc, fold into validator information, make existing pattern default behavior * [slash] Reimplement SetDifference * [reward][engine][network] Remove bad API from fall, begin setup for Per validator awards * [header] Custom Marshal header for downstream, remove dev code * [effective][committee] Factor out EPoS round of computation thereby unification in codebase of EPoS * [unit-test] Fix semantically wrong validator unit tests, punt on maxBLS key wrt tx-pool test * [reward] Use excellent singleflight package for caching lookup of subcommittees * [apr][reward] Begin APR package itself, iterate on iterface signatures * [reward] Handle possible error from singleflight * [rpc][validator][reward] Adjust RPC committees, singleflight on votingPower, foldStats into Validator Information * [apr] Stub out computation of APR * [effective][committee] Upgrade SlotPurchase with named fields, provide marshal * [effective] Update Tests * [blockchain] TODO Remove the validators no longer in committee * [validator][effective] More expressive string representation of eligibilty, ValidatorRPC explicit say if in committee now * [rpc] Median-stake more semantic meaningful * [validator] Iterate on semantic meaning of JSON representation * [offchain] Make validator stats return explicit error * [availability] Small typo * [rpc] Quick visual hack until fix delete out kicked out validators * [offchain] Delete validator from offchain that lost their slot * [apr] Forgot to update interface signature * [apr] Mul instead of Div * [protocol][validator] Fold block reward accum per vaidator into validator-wrapper, off-chain => on-chain * [votepower] Refactor votepower Roster, simplify aggregation of network wide rosters * [votepower][shard] Adjust roster, optimize usage of BLSPublicKey as key, use MarshalText trick * [shard] Granular errors * [votepower][validator] Unify votepower data structure with off-chain usage * [votepower][consensus][validator] Further simplify and unify votepower with off-chain, validator stats * [votepower] Use RJs naming convention group,overall * [votepower] Remove Println, do keep enforcing order * [effective][reward] Expand semantics of eligibility as it was overloaded and confusing, evict old voting power computations * [apr] Adjust json field name * [votepower] Only aggregate on external validator * [votepower] Mistake on aggregation, custom presentation network-wide * [rpc][validator][availability] Remove parameter, take into account empty snapshot * [apr] Use snapshots from two, one epochs ago. Still have question on header * [apr] Use GetHeaderByNumber for the header needed for time stamp * [chain] Evict > 3 epoch old voting power * [blockchain] Leave Delete Validator snapshot as TODO * [validator][rpc][effective] Undo changes to Protocol field, use virtual construct at RPC layer for meaning * [project] Address PR comments * [committee][rpc] Move +1 to computation of epos round rather than hack mutation * [reward] Remove entire unnecessary loop, hook on AddReward. Remove unnecessary new big int * [votepower][rpc][validator] Stick with numeric.Dec for token involved with computation, expose accumulate block-reward in RPC * [effective][committee] Track the candidates for the EPoS auction, RPC median-stake benefits * [node] Add hack way to get real error reason of why cannot load shardchain * [consensus] Expand log on current issue on nil block * [apr] Do the actual call to compute for validator's APR * [committee] Wrap SlotOrder with validator address, manifests in median-stake RPC * [apr] Incorrect error handle order * [quorum] Remove incorrect compare on bls Key, (typo), remove redundant error check * [shard] Add log if stakedSlots is 0 * [apr] More sanity check on div by zero, more lenient on error when dont have historical data yet * [committee] Remove + 1 on seat count * [apr] Use int64() directly * [apr] Log when odd empty nil header * [apr] Do not crash on empty header, figure out later
5 years ago
decider := quorum.NewDecider(
quorum.SuperMajorityVote, shard.BeaconChainShardID,
)
consensus, err := consensus.New(
host, shard.BeaconChainShardID, leader, multibls.GetPrivateKey(blsKey), decider,
)
if err != nil {
t.Fatalf("Cannot craeate consensus: %v", err)
}
node := New(host, consensus, testDBFactory, nil, false)
if node.Consensus == nil {
t.Error("Consensus is not initialized for the node")
}
if node.Blockchain() == nil {
t.Error("Blockchain is not initialized for the node")
}
if node.Blockchain().CurrentBlock() == nil {
t.Error("Genesis block is not initialized for the node")
}
}
func TestLegacySyncingPeerProvider(t *testing.T) {
t.Run("ShardChain", func(t *testing.T) {
p := makeLegacySyncingPeerProvider()
expectedPeers := []p2p.Peer{
{IP: "127.0.0.1", Port: "6001"},
{IP: "127.0.0.1", Port: "6003"},
}
actualPeers, err := p.SyncingPeers(1)
if assert.NoError(t, err) {
assert.ElementsMatch(t, actualPeers, expectedPeers)
}
})
t.Run("BeaconChain", func(t *testing.T) {
p := makeLegacySyncingPeerProvider()
expectedPeers := []p2p.Peer{
{IP: "127.0.0.1", Port: "6000"},
{IP: "127.0.0.1", Port: "6002"},
}
actualPeers, err := p.SyncingPeers(0)
if assert.NoError(t, err) {
assert.ElementsMatch(t, actualPeers, expectedPeers)
}
})
t.Run("NoMatch", func(t *testing.T) {
p := makeLegacySyncingPeerProvider()
_, err := p.SyncingPeers(999)
assert.Error(t, err)
})
}
func makeLegacySyncingPeerProvider() *LegacySyncingPeerProvider {
node := makeSyncOnlyNode()
p := NewLegacySyncingPeerProvider(node)
p.shardID = func() uint32 { return 1 }
return p
}
func makeSyncOnlyNode() *Node {
node := &Node{
Neighbors: sync.Map{},
BeaconNeighbors: sync.Map{},
}
node.Neighbors.Store(
"127.0.0.1:9001:omg", p2p.Peer{IP: "127.0.0.1", Port: "9001"})
node.Neighbors.Store(
"127.0.0.1:9003:wtf", p2p.Peer{IP: "127.0.0.1", Port: "9003"})
node.BeaconNeighbors.Store(
"127.0.0.1:9000:bbq", p2p.Peer{IP: "127.0.0.1", Port: "9000"})
node.BeaconNeighbors.Store(
"127.0.0.1:9002:cakes", p2p.Peer{IP: "127.0.0.1", Port: "9002"})
return node
}
func TestDNSSyncingPeerProvider(t *testing.T) {
t.Run("Happy", func(t *testing.T) {
p := NewDNSSyncingPeerProvider("example.com", "1234")
lookupCount := 0
lookupName := ""
p.lookupHost = func(name string) (addrs []string, err error) {
lookupCount++
lookupName = name
return []string{"1.2.3.4", "5.6.7.8"}, nil
}
expectedPeers := []p2p.Peer{
{IP: "1.2.3.4", Port: "1234"},
{IP: "5.6.7.8", Port: "1234"},
}
actualPeers, err := p.SyncingPeers( /*shardID*/ 3)
if assert.NoError(t, err) {
assert.Equal(t, actualPeers, expectedPeers)
}
assert.Equal(t, lookupCount, 1)
assert.Equal(t, lookupName, "s3.example.com")
if err != nil {
t.Fatalf("SyncingPeers returned non-nil error %#v", err)
}
})
t.Run("LookupError", func(t *testing.T) {
p := NewDNSSyncingPeerProvider("example.com", "1234")
p.lookupHost = func(_ string) ([]string, error) {
return nil, errors.New("omg")
}
_, actualErr := p.SyncingPeers( /*shardID*/ 3)
assert.Error(t, actualErr)
})
}
func TestLocalSyncingPeerProvider(t *testing.T) {
t.Run("BeaconChain", func(t *testing.T) {
p := makeLocalSyncingPeerProvider()
expectedBeaconPeers := []p2p.Peer{
{IP: "127.0.0.1", Port: "6000"},
{IP: "127.0.0.1", Port: "6002"},
{IP: "127.0.0.1", Port: "6004"},
}
if actualPeers, err := p.SyncingPeers(0); assert.NoError(t, err) {
assert.ElementsMatch(t, actualPeers, expectedBeaconPeers)
}
})
t.Run("Shard1Chain", func(t *testing.T) {
p := makeLocalSyncingPeerProvider()
expectedShard1Peers := []p2p.Peer{
// port 6001 omitted because self
{IP: "127.0.0.1", Port: "6003"},
{IP: "127.0.0.1", Port: "6005"},
}
if actualPeers, err := p.SyncingPeers(1); assert.NoError(t, err) {
assert.ElementsMatch(t, actualPeers, expectedShard1Peers)
}
})
t.Run("InvalidShard", func(t *testing.T) {
p := makeLocalSyncingPeerProvider()
_, err := p.SyncingPeers(999)
assert.Error(t, err)
})
}
func makeLocalSyncingPeerProvider() *LocalSyncingPeerProvider {
return NewLocalSyncingPeerProvider(6000, 6001, 2, 3)
}
func TestAddPeers(t *testing.T) {
6 years ago
pubKey1 := pki.GetBLSPrivateKeyFromInt(333).GetPublicKey()
pubKey2 := pki.GetBLSPrivateKeyFromInt(444).GetPublicKey()
peers1 := []*p2p.Peer{
{
6 years ago
IP: "127.0.0.1",
Port: "8888",
ConsensusPubKey: pubKey1,
},
{
6 years ago
IP: "127.0.0.1",
Port: "9999",
ConsensusPubKey: pubKey2,
},
}
blsKey := bls2.RandPrivateKey()
pubKey := blsKey.GetPublicKey()
leader := p2p.Peer{IP: "127.0.0.1", Port: "8982", ConsensusPubKey: pubKey}
validator := p2p.Peer{IP: "127.0.0.1", Port: "8985"}
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")
host, err := p2pimpl.NewHost(&leader, priKey)
if err != nil {
t.Fatalf("newhost failure: %v", err)
}
[rpc][availability][apr] Richer validator information, implement APR, unify EPoS computation, remove fall 2019 tech debt (#2484) * [rpc][validator] Extend hmy blockchain validator information * [availability] Optimize bump count * [staking][validator][rpc] Remove validator stats rpc, fold into validator information, make existing pattern default behavior * [slash] Reimplement SetDifference * [reward][engine][network] Remove bad API from fall, begin setup for Per validator awards * [header] Custom Marshal header for downstream, remove dev code * [effective][committee] Factor out EPoS round of computation thereby unification in codebase of EPoS * [unit-test] Fix semantically wrong validator unit tests, punt on maxBLS key wrt tx-pool test * [reward] Use excellent singleflight package for caching lookup of subcommittees * [apr][reward] Begin APR package itself, iterate on iterface signatures * [reward] Handle possible error from singleflight * [rpc][validator][reward] Adjust RPC committees, singleflight on votingPower, foldStats into Validator Information * [apr] Stub out computation of APR * [effective][committee] Upgrade SlotPurchase with named fields, provide marshal * [effective] Update Tests * [blockchain] TODO Remove the validators no longer in committee * [validator][effective] More expressive string representation of eligibilty, ValidatorRPC explicit say if in committee now * [rpc] Median-stake more semantic meaningful * [validator] Iterate on semantic meaning of JSON representation * [offchain] Make validator stats return explicit error * [availability] Small typo * [rpc] Quick visual hack until fix delete out kicked out validators * [offchain] Delete validator from offchain that lost their slot * [apr] Forgot to update interface signature * [apr] Mul instead of Div * [protocol][validator] Fold block reward accum per vaidator into validator-wrapper, off-chain => on-chain * [votepower] Refactor votepower Roster, simplify aggregation of network wide rosters * [votepower][shard] Adjust roster, optimize usage of BLSPublicKey as key, use MarshalText trick * [shard] Granular errors * [votepower][validator] Unify votepower data structure with off-chain usage * [votepower][consensus][validator] Further simplify and unify votepower with off-chain, validator stats * [votepower] Use RJs naming convention group,overall * [votepower] Remove Println, do keep enforcing order * [effective][reward] Expand semantics of eligibility as it was overloaded and confusing, evict old voting power computations * [apr] Adjust json field name * [votepower] Only aggregate on external validator * [votepower] Mistake on aggregation, custom presentation network-wide * [rpc][validator][availability] Remove parameter, take into account empty snapshot * [apr] Use snapshots from two, one epochs ago. Still have question on header * [apr] Use GetHeaderByNumber for the header needed for time stamp * [chain] Evict > 3 epoch old voting power * [blockchain] Leave Delete Validator snapshot as TODO * [validator][rpc][effective] Undo changes to Protocol field, use virtual construct at RPC layer for meaning * [project] Address PR comments * [committee][rpc] Move +1 to computation of epos round rather than hack mutation * [reward] Remove entire unnecessary loop, hook on AddReward. Remove unnecessary new big int * [votepower][rpc][validator] Stick with numeric.Dec for token involved with computation, expose accumulate block-reward in RPC * [effective][committee] Track the candidates for the EPoS auction, RPC median-stake benefits * [node] Add hack way to get real error reason of why cannot load shardchain * [consensus] Expand log on current issue on nil block * [apr] Do the actual call to compute for validator's APR * [committee] Wrap SlotOrder with validator address, manifests in median-stake RPC * [apr] Incorrect error handle order * [quorum] Remove incorrect compare on bls Key, (typo), remove redundant error check * [shard] Add log if stakedSlots is 0 * [apr] More sanity check on div by zero, more lenient on error when dont have historical data yet * [committee] Remove + 1 on seat count * [apr] Use int64() directly * [apr] Log when odd empty nil header * [apr] Do not crash on empty header, figure out later
5 years ago
decider := quorum.NewDecider(
quorum.SuperMajorityVote, shard.BeaconChainShardID,
)
consensus, err := consensus.New(
host, shard.BeaconChainShardID, leader, multibls.GetPrivateKey(blsKey), decider,
)
if err != nil {
t.Fatalf("Cannot craeate consensus: %v", err)
}
dRand := drand.New(host, 0, []p2p.Peer{leader, validator}, leader, nil, nil)
node := New(host, consensus, testDBFactory, nil, false)
node.DRand = dRand
r1 := node.AddPeers(peers1)
e1 := 2
if r1 != e1 {
t.Errorf("Add %v peers, expectd %v", r1, e1)
}
r2 := node.AddPeers(peers1)
e2 := 0
if r2 != e2 {
t.Errorf("Add %v peers, expectd %v", r2, e2)
}
}
func TestAddBeaconPeer(t *testing.T) {
pubKey1 := bls2.RandPrivateKey().GetPublicKey()
pubKey2 := bls2.RandPrivateKey().GetPublicKey()
peers1 := []*p2p.Peer{
{
6 years ago
IP: "127.0.0.1",
Port: "8888",
ConsensusPubKey: pubKey1,
PeerID: "1234",
},
{
6 years ago
IP: "127.0.0.1",
Port: "9999",
ConsensusPubKey: pubKey2,
PeerID: "4567",
},
}
blsKey := bls2.RandPrivateKey()
pubKey := blsKey.GetPublicKey()
leader := p2p.Peer{IP: "127.0.0.1", Port: "8982", ConsensusPubKey: pubKey}
validator := p2p.Peer{IP: "127.0.0.1", Port: "8985"}
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")
host, err := p2pimpl.NewHost(&leader, priKey)
if err != nil {
t.Fatalf("newhost failure: %v", err)
}
[rpc][availability][apr] Richer validator information, implement APR, unify EPoS computation, remove fall 2019 tech debt (#2484) * [rpc][validator] Extend hmy blockchain validator information * [availability] Optimize bump count * [staking][validator][rpc] Remove validator stats rpc, fold into validator information, make existing pattern default behavior * [slash] Reimplement SetDifference * [reward][engine][network] Remove bad API from fall, begin setup for Per validator awards * [header] Custom Marshal header for downstream, remove dev code * [effective][committee] Factor out EPoS round of computation thereby unification in codebase of EPoS * [unit-test] Fix semantically wrong validator unit tests, punt on maxBLS key wrt tx-pool test * [reward] Use excellent singleflight package for caching lookup of subcommittees * [apr][reward] Begin APR package itself, iterate on iterface signatures * [reward] Handle possible error from singleflight * [rpc][validator][reward] Adjust RPC committees, singleflight on votingPower, foldStats into Validator Information * [apr] Stub out computation of APR * [effective][committee] Upgrade SlotPurchase with named fields, provide marshal * [effective] Update Tests * [blockchain] TODO Remove the validators no longer in committee * [validator][effective] More expressive string representation of eligibilty, ValidatorRPC explicit say if in committee now * [rpc] Median-stake more semantic meaningful * [validator] Iterate on semantic meaning of JSON representation * [offchain] Make validator stats return explicit error * [availability] Small typo * [rpc] Quick visual hack until fix delete out kicked out validators * [offchain] Delete validator from offchain that lost their slot * [apr] Forgot to update interface signature * [apr] Mul instead of Div * [protocol][validator] Fold block reward accum per vaidator into validator-wrapper, off-chain => on-chain * [votepower] Refactor votepower Roster, simplify aggregation of network wide rosters * [votepower][shard] Adjust roster, optimize usage of BLSPublicKey as key, use MarshalText trick * [shard] Granular errors * [votepower][validator] Unify votepower data structure with off-chain usage * [votepower][consensus][validator] Further simplify and unify votepower with off-chain, validator stats * [votepower] Use RJs naming convention group,overall * [votepower] Remove Println, do keep enforcing order * [effective][reward] Expand semantics of eligibility as it was overloaded and confusing, evict old voting power computations * [apr] Adjust json field name * [votepower] Only aggregate on external validator * [votepower] Mistake on aggregation, custom presentation network-wide * [rpc][validator][availability] Remove parameter, take into account empty snapshot * [apr] Use snapshots from two, one epochs ago. Still have question on header * [apr] Use GetHeaderByNumber for the header needed for time stamp * [chain] Evict > 3 epoch old voting power * [blockchain] Leave Delete Validator snapshot as TODO * [validator][rpc][effective] Undo changes to Protocol field, use virtual construct at RPC layer for meaning * [project] Address PR comments * [committee][rpc] Move +1 to computation of epos round rather than hack mutation * [reward] Remove entire unnecessary loop, hook on AddReward. Remove unnecessary new big int * [votepower][rpc][validator] Stick with numeric.Dec for token involved with computation, expose accumulate block-reward in RPC * [effective][committee] Track the candidates for the EPoS auction, RPC median-stake benefits * [node] Add hack way to get real error reason of why cannot load shardchain * [consensus] Expand log on current issue on nil block * [apr] Do the actual call to compute for validator's APR * [committee] Wrap SlotOrder with validator address, manifests in median-stake RPC * [apr] Incorrect error handle order * [quorum] Remove incorrect compare on bls Key, (typo), remove redundant error check * [shard] Add log if stakedSlots is 0 * [apr] More sanity check on div by zero, more lenient on error when dont have historical data yet * [committee] Remove + 1 on seat count * [apr] Use int64() directly * [apr] Log when odd empty nil header * [apr] Do not crash on empty header, figure out later
5 years ago
decider := quorum.NewDecider(
quorum.SuperMajorityVote, shard.BeaconChainShardID,
)
consensus, err := consensus.New(
host, shard.BeaconChainShardID, leader, multibls.GetPrivateKey(blsKey), decider,
)
if err != nil {
t.Fatalf("Cannot craeate consensus: %v", err)
}
dRand := drand.New(host, 0, []p2p.Peer{leader, validator}, leader, nil, nil)
node := New(host, consensus, testDBFactory, nil, false)
node.DRand = dRand
for _, p := range peers1 {
ret := node.AddBeaconPeer(p)
if ret {
t.Errorf("AddBeaconPeer Failed, expecting false, got %v, peer %v", ret, p)
}
}
for _, p := range peers1 {
ret := node.AddBeaconPeer(p)
if !ret {
t.Errorf("AddBeaconPeer Failed, expecting true, got %v, peer %v", ret, p)
}
}
}
func sendPingMessage(node *Node, leader p2p.Peer) {
6 years ago
pubKey1 := pki.GetBLSPrivateKeyFromInt(333).GetPublicKey()
p1 := p2p.Peer{
6 years ago
IP: "127.0.0.1",
Port: "9999",
ConsensusPubKey: pubKey1,
}
ping1 := proto_discovery.NewPingMessage(p1, true)
ping2 := proto_discovery.NewPingMessage(p1, false)
_ = ping1.ConstructPingMessage()
_ = ping2.ConstructPingMessage()
}
func exitServer() {
fmt.Println("wait 5 seconds to terminate the process ...")
time.Sleep(5 * time.Second)
os.Exit(0)
}