diff --git a/blockchain/blockchain_test.go b/blockchain/blockchain_test.go index ec64598180..980acf79aa 100644 --- a/blockchain/blockchain_test.go +++ b/blockchain/blockchain_test.go @@ -787,7 +787,6 @@ func TestBlockchainInitialCandidate(t *testing.T) { cfg := config.Default cfg.Chain.TrieDBPath = testTriePath cfg.Chain.ChainDBPath = testDBPath - cfg.Chain.NumCandidates = 2 sf, err := factory.NewFactory(cfg, factory.DefaultTrieOption()) require.NoError(err) @@ -817,7 +816,7 @@ func TestBlockchainInitialCandidate(t *testing.T) { }() candidate, err := sf.CandidatesByHeight(0) require.NoError(err) - require.True(len(candidate) == 2) + require.Equal(24, len(candidate)) } func TestBlockchain_StateByAddr(t *testing.T) { diff --git a/config/config.go b/config/config.go index 2d2de429a0..227932a2a9 100644 --- a/config/config.go +++ b/config/config.go @@ -97,7 +97,6 @@ var ( Address: "", ProducerPrivKey: PrivateKey.HexString(), EmptyGenesis: false, - NumCandidates: 101, GravityChainDB: DB{DbPath: "./poll.db", NumRetries: 10}, Committee: committee.Config{ BeaconChainAPIs: []string{}, @@ -191,7 +190,6 @@ var ( ValidateExplorer, ValidateAPI, ValidateActPool, - ValidateChain, } // PrivateKey is a randomly generated producer's key for testing purpose @@ -217,7 +215,6 @@ type ( Address string `yaml:"address"` ProducerPrivKey string `yaml:"producerPrivKey"` EmptyGenesis bool `yaml:"emptyGenesis"` - NumCandidates uint `yaml:"numCandidates"` GravityChainDB DB `yaml:"gravityChainDB"` Committee committee.Config `yaml:"committee"` @@ -481,14 +478,6 @@ func (cfg Config) ProducerPrivateKey() keypair.PrivateKey { return sk } -// ValidateChain validates the chain configure -func ValidateChain(cfg Config) error { - if cfg.Chain.NumCandidates <= 0 { - return errors.Wrapf(ErrInvalidCfg, "candidate number should be greater than 0") - } - return nil -} - // ValidateDispatcher validates the dispatcher configs func ValidateDispatcher(cfg Config) error { if cfg.Dispatcher.EventChanSize <= 0 { diff --git a/config/config_test.go b/config/config_test.go index 01178ea835..c6fb7b449e 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -160,19 +160,6 @@ func TestValidateExplorer(t *testing.T) { ) } -func TestValidateChain(t *testing.T) { - cfg := Default - cfg.Chain.NumCandidates = 0 - - err := ValidateChain(cfg) - require.Error(t, err) - require.Equal(t, ErrInvalidCfg, errors.Cause(err)) - require.True( - t, - strings.Contains(err.Error(), "candidate number should be greater than 0"), - ) -} - func TestValidateDispatcher(t *testing.T) { cfg := Default cfg.Dispatcher.EventChanSize = 0 diff --git a/e2etest/local_test.go b/e2etest/local_test.go index e52a87986b..18cc3e0fbe 100644 --- a/e2etest/local_test.go +++ b/e2etest/local_test.go @@ -8,9 +8,7 @@ package e2etest import ( "context" - "fmt" "math/big" - "sort" "testing" "time" @@ -481,321 +479,6 @@ func TestLocalSync(t *testing.T) { t.Log("4 blocks received correctly") } -func TestVoteLocalCommit(t *testing.T) { - require := require.New(t) - - testutil.CleanupPath(t, testTriePath) - testutil.CleanupPath(t, testDBPath) - - cfg, err := newTestConfig() - cfg.Chain.NumCandidates = 2 - require.Nil(err) - - // create node - ctx := context.Background() - svr, err := itx.NewServer(cfg) - require.Nil(err) - require.Nil(svr.Start(ctx)) - - chainID := cfg.Chain.ID - bc := svr.ChainService(chainID).Blockchain() - require.NotNil(bc) - require.Nil(addTestingTsfBlocks(bc)) - require.NotNil(svr.ChainService(chainID).ActionPool()) - - cfg, err = newTestConfig() - require.NoError(err) - cfg.Network.BootstrapNodes = []string{svr.P2PAgent().Self()[0].String()} - p := p2p.NewAgent( - cfg.Network, - func(_ context.Context, _ uint32, _ proto.Message) {}, - func(_ context.Context, _ uint32, _ peerstore.PeerInfo, _ proto.Message) {}, - ) - require.NotNil(p) - require.NoError(p.Start(ctx)) - - defer func() { - require.Nil(p.Stop(ctx)) - require.Nil(svr.Stop(ctx)) - testutil.CleanupPath(t, testTriePath) - testutil.CleanupPath(t, testDBPath) - }() - require.True(5 == bc.TipHeight()) - - // create local chain - testutil.CleanupPath(t, testTriePath2) - testutil.CleanupPath(t, testDBPath2) - cfg.Chain.TrieDBPath = testTriePath2 - cfg.Chain.ChainDBPath = testDBPath2 - require.NoError(copyDB(testTriePath, testTriePath2)) - require.NoError(copyDB(testDBPath, testDBPath2)) - registry := protocol.Registry{} - chain := blockchain.NewBlockchain( - cfg, - blockchain.DefaultStateFactoryOption(), - blockchain.BoltDBDaoOption(), - blockchain.RegistryOption(®istry), - ) - rolldposProtocol := rolldpos.NewProtocol( - cfg.Genesis.NumCandidateDelegates, - cfg.Genesis.NumDelegates, - cfg.Genesis.NumSubEpochs, - ) - require.NoError(registry.Register(rolldpos.ProtocolID, rolldposProtocol)) - rewardingProtocol := rewarding.NewProtocol(chain, rolldposProtocol) - registry.Register(rewarding.ProtocolID, rewardingProtocol) - acc := account.NewProtocol() - registry.Register(account.ProtocolID, acc) - chain.Validator().AddActionEnvelopeValidators(protocol.NewGenericValidator(chain, genesis.Default.ActionGasLimit)) - v := vote.NewProtocol(chain) - chain.Validator().AddActionValidators(acc, v, rewardingProtocol) - require.NotNil(chain) - chain.GetFactory().AddActionHandlers(acc, v, rewardingProtocol) - require.NoError(chain.Start(ctx)) - require.True(5 == bc.TipHeight()) - defer func() { - require.NoError(chain.Stop(ctx)) - testutil.CleanupPath(t, testTriePath2) - testutil.CleanupPath(t, testDBPath2) - }() - - _, err = chain.CreateState(ta.Addrinfo["galilei"].String(), unit.ConvertIotxToRau(2000000000)) - require.NoError(err) - _, err = svr.ChainService(chainID).Blockchain().CreateState(ta.Addrinfo["galilei"].String(), unit.ConvertIotxToRau(2000000000)) - require.NoError(err) - - fmt.Println(ta.Addrinfo["galilei"].String()) - // Add block 1 - // Alfa, Bravo and Charlie selfnomination - tsf1, err := testutil.SignedTransfer(ta.Addrinfo["alfa"].String(), ta.Keyinfo["galilei"].PriKey, 1, unit.ConvertIotxToRau(200000000), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPrice)) - require.NoError(err) - - tsf2, err := testutil.SignedTransfer(ta.Addrinfo["bravo"].String(), ta.Keyinfo["galilei"].PriKey, 2, unit.ConvertIotxToRau(200000000), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPrice)) - require.NoError(err) - - tsf3, err := testutil.SignedTransfer(ta.Addrinfo["charlie"].String(), ta.Keyinfo["galilei"].PriKey, 3, unit.ConvertIotxToRau(200000000), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPrice)) - require.NoError(err) - - tsf4, err := testutil.SignedTransfer(ta.Addrinfo["delta"].String(), ta.Keyinfo["galilei"].PriKey, 4, unit.ConvertIotxToRau(200000000), []byte{}, testutil.TestGasLimit, big.NewInt(testutil.TestGasPrice)) - require.NoError(err) - - vote1, err := testutil.SignedVote(ta.Addrinfo["alfa"].String(), ta.Keyinfo["alfa"].PriKey, 1, 100000, big.NewInt(0)) - require.NoError(err) - - vote2, err := testutil.SignedVote(ta.Addrinfo["bravo"].String(), ta.Keyinfo["bravo"].PriKey, 1, 100000, big.NewInt(0)) - require.NoError(err) - - vote3, err := testutil.SignedVote(ta.Addrinfo["charlie"].String(), ta.Keyinfo["charlie"].PriKey, 6, 100000, big.NewInt(0)) - require.NoError(err) - - act1 := vote1.Proto() - act2 := vote2.Proto() - act3 := vote3.Proto() - acttsf1 := tsf1.Proto() - acttsf2 := tsf2.Proto() - acttsf3 := tsf3.Proto() - acttsf4 := tsf4.Proto() - - p2pCtx := p2p.WitContext(ctx, p2p.Context{ChainID: cfg.Chain.ID}) - err = testutil.WaitUntil(100*time.Millisecond, 60*time.Second, func() (bool, error) { - if err := p.BroadcastOutbound(p2pCtx, act1); err != nil { - return false, err - } - if err := p.BroadcastOutbound(p2pCtx, act2); err != nil { - return false, err - } - if err := p.BroadcastOutbound(p2pCtx, act3); err != nil { - return false, err - } - if err := p.BroadcastOutbound(p2pCtx, acttsf1); err != nil { - return false, err - } - if err := p.BroadcastOutbound(p2pCtx, acttsf2); err != nil { - return false, err - } - if err := p.BroadcastOutbound(p2pCtx, acttsf3); err != nil { - return false, err - } - if err := p.BroadcastOutbound(p2pCtx, acttsf4); err != nil { - return false, err - } - acts := svr.ChainService(chainID).ActionPool().PendingActionMap() - return lenPendingActionMap(acts) == 7, nil - }) - require.Nil(err) - - actionMap := svr.ChainService(chainID).ActionPool().PendingActionMap() - blk1, err := chain.MintNewBlock( - actionMap, - 0, - ) - require.Nil(err) - require.Nil(chain.ValidateBlock(blk1)) - require.Nil(chain.CommitBlock(blk1)) - - require.NoError(p.BroadcastOutbound(p2pCtx, blk1.ConvertToBlockPb())) - err = testutil.WaitUntil(100*time.Millisecond, 60*time.Second, func() (bool, error) { - height := bc.TipHeight() - return int(height) == 6, nil - }) - require.NoError(err) - tipheight := bc.TipHeight() - require.Equal(6, int(tipheight)) - - // Add block 2 - // Vote A -> B, C -> A - vote4, err := testutil.SignedVote(ta.Addrinfo["bravo"].String(), ta.Keyinfo["alfa"].PriKey, uint64(2), uint64(100000), big.NewInt(0)) - require.Nil(err) - vote5, err := testutil.SignedVote(ta.Addrinfo["alfa"].String(), ta.Keyinfo["charlie"].PriKey, uint64(7), uint64(100000), big.NewInt(0)) - require.Nil(err) - - actionMap = make(map[string][]action.SealedEnvelope) - actionMap[ta.Addrinfo["alfa"].String()] = []action.SealedEnvelope{vote4} - actionMap[ta.Addrinfo["charlie"].String()] = []action.SealedEnvelope{vote5} - blk2, err := chain.MintNewBlock( - actionMap, - 0, - ) - require.Nil(err) - require.Nil(chain.ValidateBlock(blk2)) - require.Nil(chain.CommitBlock(blk2)) - // broadcast to P2P - act4 := vote4.Proto() - act5 := vote5.Proto() - err = testutil.WaitUntil(100*time.Millisecond, 60*time.Second, func() (bool, error) { - if err := p.BroadcastOutbound(p2pCtx, act4); err != nil { - return false, err - } - if err := p.BroadcastOutbound(p2pCtx, act5); err != nil { - return false, err - } - acts := svr.ChainService(chainID).ActionPool().PendingActionMap() - return lenPendingActionMap(acts) == 2, nil - }) - require.Nil(err) - - require.NoError(p.BroadcastOutbound(p2pCtx, blk2.ConvertToBlockPb())) - err = testutil.WaitUntil(100*time.Millisecond, 60*time.Second, func() (bool, error) { - height := bc.TipHeight() - return int(height) == 7, nil - }) - require.Nil(err) - tipheight = bc.TipHeight() - require.Equal(7, int(tipheight)) - - candidates, err := bc.CandidatesByHeight(tipheight) - require.NoError(err) - candidatesAddr := make([]string, len(candidates)) - for i, can := range candidates { - candidatesAddr[i] = can.Address - } - require.Equal(2, len(candidates)) - - sort.Sort(sort.StringSlice(candidatesAddr)) - require.Equal(ta.Addrinfo["alfa"].String(), candidatesAddr[0]) - require.Equal(ta.Addrinfo["bravo"].String(), candidatesAddr[1]) - - // Add block 3 - // D self nomination - vote6, err := testutil.SignedVote(ta.Addrinfo["delta"].String(), ta.Keyinfo["delta"].PriKey, 5, 100000, big.NewInt(0)) - require.NoError(err) - - actionMap = make(map[string][]action.SealedEnvelope) - actionMap[ta.Addrinfo["delta"].String()] = []action.SealedEnvelope{vote6} - blk3, err := chain.MintNewBlock( - actionMap, - 0, - ) - require.Nil(err) - require.Nil(chain.ValidateBlock(blk3)) - require.Nil(chain.CommitBlock(blk3)) - // broadcast to P2P - act6 := vote6.Proto() - err = testutil.WaitUntil(100*time.Millisecond, 60*time.Second, func() (bool, error) { - if err := p.BroadcastOutbound(p2pCtx, act6); err != nil { - return false, err - } - acts := svr.ChainService(chainID).ActionPool().PendingActionMap() - return lenPendingActionMap(acts) == 1, nil - }) - require.Nil(err) - - err = p.BroadcastOutbound(p2pCtx, blk3.ConvertToBlockPb()) - require.NoError(err) - - err = testutil.WaitUntil(100*time.Millisecond, 60*time.Second, func() (bool, error) { - height := bc.TipHeight() - return int(height) == 8, nil - }) - require.Nil(err) - tipheight = bc.TipHeight() - require.Equal(8, int(tipheight)) - - candidates, err = bc.CandidatesByHeight(tipheight) - require.NoError(err) - candidatesAddr = make([]string, len(candidates)) - for i, can := range candidates { - candidatesAddr[i] = can.Address - } - require.Equal(2, len(candidates)) - - sort.Sort(sort.StringSlice(candidatesAddr)) - require.Equal(ta.Addrinfo["delta"].String(), candidatesAddr[0]) - require.Equal(ta.Addrinfo["bravo"].String(), candidatesAddr[1]) - - // Add block 4 - // Unvote B - vote7, err := action.NewVote(uint64(2), "", uint64(100000), big.NewInt(0)) - require.NoError(err) - bd := &action.EnvelopeBuilder{} - elp := bd.SetAction(vote7).SetNonce(2).SetGasLimit(100000).SetGasPrice(big.NewInt(0)).Build() - selp, err := action.Sign(elp, ta.Keyinfo["bravo"].PriKey) - require.NoError(err) - - actionMap = make(map[string][]action.SealedEnvelope) - actionMap[ta.Addrinfo["bravo"].String()] = []action.SealedEnvelope{selp} - blk4, err := chain.MintNewBlock( - actionMap, - 0, - ) - require.Nil(err) - require.Nil(chain.ValidateBlock(blk4)) - require.Nil(chain.CommitBlock(blk4)) - // broadcast to P2P - act7 := selp.Proto() - err = testutil.WaitUntil(100*time.Millisecond, 60*time.Second, func() (bool, error) { - if err := p.BroadcastOutbound(p2pCtx, act7); err != nil { - return false, err - } - acts := svr.ChainService(chainID).ActionPool().PendingActionMap() - return lenPendingActionMap(acts) == 1, nil - }) - require.Nil(err) - - err = p.BroadcastOutbound(p2pCtx, blk4.ConvertToBlockPb()) - require.NoError(err) - - err = testutil.WaitUntil(100*time.Millisecond, 60*time.Second, func() (bool, error) { - height := bc.TipHeight() - return int(height) == 9, nil - }) - require.Nil(err) - tipheight = bc.TipHeight() - require.Equal(9, int(tipheight)) - - candidates, err = bc.CandidatesByHeight(tipheight) - require.NoError(err) - candidatesAddr = make([]string, len(candidates)) - for i, can := range candidates { - candidatesAddr[i] = can.Address - } - require.Equal(2, len(candidates)) - - sort.Sort(sort.StringSlice(candidatesAddr)) - require.Equal(ta.Addrinfo["delta"].String(), candidatesAddr[0]) - require.Equal(ta.Addrinfo["alfa"].String(), candidatesAddr[1]) -} - func TestStartExistingBlockchain(t *testing.T) { require := require.New(t) ctx := context.Background() diff --git a/state/factory/factory.go b/state/factory/factory.go index 160c3c90ff..0fa666448c 100644 --- a/state/factory/factory.go +++ b/state/factory/factory.go @@ -68,7 +68,6 @@ type ( lifecycle lifecycle.Lifecycle mutex sync.RWMutex currentChainHeight uint64 - numCandidates uint accountTrie trie.Trie // global state trie dao db.KVStore // the underlying DB for account/contract storage actionHandlers []protocol.ActionHandler // the handlers to handle actions @@ -115,7 +114,6 @@ func InMemTrieOption() Option { func NewFactory(cfg config.Config, opts ...Option) (Factory, error) { sf := &factory{ currentChainHeight: 0, - numCandidates: cfg.Chain.NumCandidates, } for _, opt := range opts { @@ -294,9 +292,6 @@ func (sf *factory) CandidatesByHeight(height uint64) ([]*state.Candidate, error) ) if errors.Cause(err) == nil { if len(candidates) > 0 { - if len(candidates) > int(sf.numCandidates) { - candidates = candidates[:sf.numCandidates] - } return candidates, nil } err = state.ErrStateNotExist diff --git a/state/factory/factory_test.go b/state/factory/factory_test.go index b6325a1171..65d31e5cc7 100644 --- a/state/factory/factory_test.go +++ b/state/factory/factory_test.go @@ -16,6 +16,11 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" + + "github.com/iotexproject/iotex-core/action/protocol/vote/candidatesutil" + "github.com/iotexproject/iotex-core/test/identityset" + "github.com/pkg/errors" "github.com/stretchr/testify/require" @@ -50,22 +55,6 @@ func randStringRunes(n int) string { return string(b) } -func compareStrings(actual []string, expected []string) bool { - act := make(map[string]bool) - for i := 0; i < len(actual); i++ { - act[actual[i]] = true - } - - for i := 0; i < len(expected); i++ { - if _, ok := act[expected[i]]; ok { - delete(act, expected[i]) - } else { - return false - } - } - return len(act) == 0 -} - func voteForm(height uint64, cs []*state.Candidate) []string { r := make([]string, len(cs)) for i := 0; i < len(cs); i++ { @@ -159,663 +148,35 @@ func testSnapshot(ws WorkingSet, t *testing.T) { } func TestCandidates(t *testing.T) { - testutil.CleanupPath(t, testTriePath) - defer testutil.CleanupPath(t, testTriePath) cfg := config.Default - cfg.Chain.NumCandidates = 2 - cfg.DB.DbPath = testTriePath - sf, err := NewFactory(cfg, PrecreatedTrieDBOption(db.NewOnDiskDB(cfg.DB))) + sf, err := NewFactory(cfg, InMemTrieOption()) require.NoError(t, err) - testCandidates(sf, t, true) + testCandidates(sf, t) } func TestSDBCandidates(t *testing.T) { - testutil.CleanupPath(t, testStateDBPath) - defer testutil.CleanupPath(t, testStateDBPath) cfg := config.Default - cfg.Chain.NumCandidates = 2 - cfg.Chain.TrieDBPath = testStateDBPath - sdb, err := NewStateDB(cfg, DefaultStateDBOption()) + sdb, err := NewStateDB(cfg, InMemStateDBOption()) require.NoError(t, err) - testCandidates(sdb, t, false) -} - -func candidatesByHeight(sf Factory, height uint64) ([]*state.Candidate, error) { - for { - candidates, err := sf.CandidatesByHeight(height) - if err == nil { - return candidates, err - } - if height == 0 { - return nil, errors.New("not found") - } - height-- - } + testCandidates(sdb, t) } -func testCandidates(sf Factory, t *testing.T, checkStateRoot bool) { - - // Create three dummy iotex addresses - a := testaddress.Addrinfo["alfa"].String() - priKeyA := testaddress.Keyinfo["alfa"].PriKey - b := testaddress.Addrinfo["bravo"].String() - priKeyB := testaddress.Keyinfo["bravo"].PriKey - c := testaddress.Addrinfo["charlie"].String() - priKeyC := testaddress.Keyinfo["charlie"].PriKey - d := testaddress.Addrinfo["delta"].String() - priKeyD := testaddress.Keyinfo["delta"].PriKey - e := testaddress.Addrinfo["echo"].String() - priKeyE := testaddress.Keyinfo["echo"].PriKey - f := testaddress.Addrinfo["foxtrot"].String() - priKeyF := testaddress.Keyinfo["foxtrot"].PriKey - - sf.AddActionHandlers(account.NewProtocol(), vote.NewProtocol(nil)) - require.NoError(t, sf.Start(context.Background())) - defer func() { - require.NoError(t, sf.Stop(context.Background())) - }() +func testCandidates(sf Factory, t *testing.T) { ws, err := sf.NewWorkingSet() require.NoError(t, err) - _, err = accountutil.LoadOrCreateAccount(ws, a, big.NewInt(100)) - require.NoError(t, err) - _, err = accountutil.LoadOrCreateAccount(ws, b, big.NewInt(200)) - require.NoError(t, err) - _, err = accountutil.LoadOrCreateAccount(ws, c, big.NewInt(300)) - require.NoError(t, err) - _, err = accountutil.LoadOrCreateAccount(ws, d, big.NewInt(100)) - require.NoError(t, err) - _, err = accountutil.LoadOrCreateAccount(ws, e, big.NewInt(100)) - require.NoError(t, err) - _, err = accountutil.LoadOrCreateAccount(ws, f, big.NewInt(300)) - require.NoError(t, err) - // a:100(0) b:200(0) c:300(0) - tx1, err := action.NewTransfer(uint64(1), big.NewInt(10), b, nil, uint64(0), big.NewInt(0)) - require.NoError(t, err) - bd := &action.EnvelopeBuilder{} - elp := bd.SetNonce(1).SetAction(tx1).Build() - selp1, err := action.Sign(elp, priKeyA) - require.NoError(t, err) - - tx2, err := action.NewTransfer(uint64(2), big.NewInt(20), c, nil, uint64(0), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetNonce(2).SetAction(tx2).Build() - selp2, err := action.Sign(elp, priKeyA) - require.NoError(t, err) - - gasLimit := uint64(1000000) - raCtx := protocol.RunActionsCtx{ - Producer: testaddress.Addrinfo["producer"], - GasLimit: gasLimit, - } - ctx := protocol.WithRunActionsCtx(context.Background(), raCtx) - _, err = ws.RunActions(ctx, 0, []action.SealedEnvelope{selp1, selp2}) - require.Nil(t, err) - require.Nil(t, sf.Commit(ws)) - balanceB, err := sf.Balance(b) - require.NoError(t, err) - require.Equal(t, balanceB, big.NewInt(210)) - balanceC, err := sf.Balance(c) - require.NoError(t, err) - require.Equal(t, balanceC, big.NewInt(320)) - h, _ := sf.Height() - cand, _ := sf.CandidatesByHeight(h) - require.True(t, compareStrings(voteForm(h, cand), []string{})) - // a:70 b:210 c:320 - - vote, err := action.NewVote(0, a, uint64(20000), big.NewInt(0)) - require.NoError(t, err) - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(vote).SetGasLimit(20000).Build() - selp, err := action.Sign(elp, priKeyA) - require.NoError(t, err) - zeroGasLimit := uint64(0) - zctx := protocol.WithRunActionsCtx(context.Background(), - protocol.RunActionsCtx{ - Producer: testaddress.Addrinfo["producer"], - GasLimit: zeroGasLimit, - }) - _, err = ws.RunActions(zctx, 0, []action.SealedEnvelope{selp}) - require.NotNil(t, err) - _, err = ws.RunAction(raCtx, selp) - require.NoError(t, err) - newRoot := ws.UpdateBlockLevelInfo(0) - if checkStateRoot { - require.NotEqual(t, hash.ZeroHash256, newRoot) - } - require.NoError(t, sf.Commit(ws)) - h, _ = sf.Height() - cand, _ = sf.CandidatesByHeight(h) - require.True(t, compareStrings(voteForm(h, cand), []string{a + ":70"})) - // a(a):70(+0=70) b:210 c:320 - - vote2, err := action.NewVote(0, b, uint64(20000), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(vote2).SetGasLimit(20000).Build() - selp, err = action.Sign(elp, priKeyB) - require.NoError(t, err) - _, err = ws.RunAction(raCtx, selp) - require.NoError(t, err) - newRoot = ws.UpdateBlockLevelInfo(1) - if checkStateRoot { - require.NotEqual(t, hash.ZeroHash256, newRoot) - } - require.NoError(t, sf.Commit(ws)) - h, _ = sf.Height() - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{a + ":70", b + ":210"})) - // a(a):70(+0=70) b(b):210(+0=210) !c:320 - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - vote3, err := action.NewVote(1, b, uint64(20000), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(vote3).SetNonce(1).SetGasLimit(20000).Build() - selp, err = action.Sign(elp, priKeyA) - require.NoError(t, err) - - _, err = ws.RunAction(raCtx, selp) - require.NoError(t, err) - newRoot = ws.UpdateBlockLevelInfo(2) - if checkStateRoot { - require.NotEqual(t, hash.ZeroHash256, newRoot) - } - require.NoError(t, sf.Commit(ws)) - h, _ = sf.Height() - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{a + ":0", b + ":280"})) - // a(b):70(0) b(b):210(+70=280) !c:320 - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - tx3, err := action.NewTransfer(uint64(2), big.NewInt(20), a, nil, uint64(0), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(tx3).SetNonce(2).Build() - selp, err = action.Sign(elp, priKeyB) - require.NoError(t, err) - - _, err = ws.RunAction(raCtx, selp) - require.NoError(t, err) - newRoot = ws.UpdateBlockLevelInfo(3) - if checkStateRoot { - require.NotEqual(t, hash.ZeroHash256, newRoot) - } - require.NoError(t, sf.Commit(ws)) - h, _ = sf.Height() - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{a + ":0", b + ":280"})) - // a(b):90(0) b(b):190(+90=280) !c:320 - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - tx4, err := action.NewTransfer(uint64(2), big.NewInt(20), b, nil, uint64(0), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(tx4).SetNonce(2).Build() - selp, err = action.Sign(elp, priKeyA) - require.NoError(t, err) - - _, err = ws.RunAction(raCtx, selp) - require.NoError(t, err) - newRoot = ws.UpdateBlockLevelInfo(4) - if checkStateRoot { - require.NotEqual(t, hash.ZeroHash256, newRoot) - } + require.NoError(t, candidatesutil.LoadAndAddCandidates(ws, 1, identityset.Address(0).String())) + require.NoError(t, candidatesutil.LoadAndUpdateCandidates(ws, 1, identityset.Address(0).String(), big.NewInt(0))) + require.NoError(t, candidatesutil.LoadAndAddCandidates(ws, 1, identityset.Address(1).String())) + require.NoError(t, candidatesutil.LoadAndUpdateCandidates(ws, 1, identityset.Address(1).String(), big.NewInt(1))) require.NoError(t, sf.Commit(ws)) - h, _ = sf.Height() - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{a + ":0", b + ":280"})) - // a(b):70(0) b(b):210(+70=280) !c:320 - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - vote4, err := action.NewVote(1, a, uint64(20000), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(vote4).SetNonce(1).SetGasLimit(20000).Build() - selp, err = action.Sign(elp, priKeyB) - require.NoError(t, err) - - _, err = ws.RunActions(ctx, 5, []action.SealedEnvelope{selp}) - require.Nil(t, err) - require.Nil(t, sf.Commit(ws)) - h, _ = sf.Height() - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{a + ":210", b + ":70"})) - // a(b):70(210) b(a):210(70) !c:320 - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - vote5, err := action.NewVote(2, b, uint64(20000), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(vote5).SetNonce(2).SetGasLimit(20000).Build() - selp, err = action.Sign(elp, priKeyB) - require.NoError(t, err) - - _, err = ws.RunActions(ctx, 6, []action.SealedEnvelope{selp}) - require.Nil(t, err) - require.Nil(t, sf.Commit(ws)) - h, _ = sf.Height() - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{a + ":0", b + ":280"})) - // a(b):70(0) b(b):210(+70=280) !c:320 - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - vote6, err := action.NewVote(3, b, uint64(20000), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(vote6).SetNonce(3).SetGasLimit(20000).Build() - selp, err = action.Sign(elp, priKeyB) - require.NoError(t, err) - - _, err = ws.RunActions(ctx, 7, []action.SealedEnvelope{selp}) - require.Nil(t, err) - require.Nil(t, sf.Commit(ws)) - h, _ = sf.Height() - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{a + ":0", b + ":280"})) - // a(b):70(0) b(b):210(+70=280) !c:320 - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - tx5, err := action.NewTransfer(uint64(2), big.NewInt(20), a, nil, uint64(0), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(tx5).SetNonce(2).Build() - selp, err = action.Sign(elp, priKeyC) - require.NoError(t, err) - - _, err = ws.RunActions(ctx, 8, []action.SealedEnvelope{selp}) - require.Nil(t, err) - require.Nil(t, sf.Commit(ws)) - h, _ = sf.Height() - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{a + ":0", b + ":300"})) - // a(b):90(0) b(b):210(+90=300) !c:300 - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - vote7, err := action.NewVote(0, a, uint64(100000), big.NewInt(0)) - require.NoError(t, err) - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(vote7).SetNonce(0).SetGasLimit(100000).Build() - selp, err = action.Sign(elp, priKeyC) - require.NoError(t, err) - - _, err = ws.RunActions(ctx, 9, []action.SealedEnvelope{selp}) - require.Nil(t, err) - require.Nil(t, sf.Commit(ws)) - h, _ = sf.Height() - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{a + ":300", b + ":300"})) - // a(b):90(300) b(b):210(+90=300) !c(a):300 - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - vote8, err := action.NewVote(4, c, uint64(100000), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(vote8).SetNonce(4).Build() - selp, err = action.Sign(elp, priKeyB) - require.NoError(t, err) - - _, err = ws.RunActions(ctx, 10, []action.SealedEnvelope{selp}) - require.Nil(t, err) - require.Nil(t, sf.Commit(ws)) - h, _ = sf.Height() - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{a + ":300", b + ":90"})) - // a(b):90(300) b(c):210(90) !c(a):300 - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - vote9, err := action.NewVote(1, c, uint64(100000), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(vote9).SetNonce(1). - SetGasLimit(100000).Build() - selp, err = action.Sign(elp, priKeyC) - require.NoError(t, err) - - _, err = ws.RunActions(ctx, 11, []action.SealedEnvelope{selp}) - require.Nil(t, err) - require.Nil(t, sf.Commit(ws)) - h, _ = sf.Height() - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{c + ":510", b + ":90"})) - // a(b):90(0) b(c):210(90) c(c):300(+210=510) - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - vote10, err := action.NewVote(0, e, uint64(100000), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(vote10).SetNonce(0). - SetGasLimit(100000).Build() - selp, err = action.Sign(elp, priKeyD) - require.NoError(t, err) - - _, err = ws.RunActions(ctx, 12, []action.SealedEnvelope{selp}) - require.Nil(t, err) - require.Nil(t, sf.Commit(ws)) - h, _ = sf.Height() - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{c + ":510", b + ":90"})) - // a(b):90(0) b(c):210(90) c(c):300(+210=510) - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - vote11, err := action.NewVote(1, d, uint64(100000), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(vote11).SetNonce(1).SetGasLimit(100000).Build() - selp, err = action.Sign(elp, priKeyD) - require.NoError(t, err) - - _, err = ws.RunActions(ctx, 13, []action.SealedEnvelope{selp}) - require.Nil(t, err) - require.Nil(t, sf.Commit(ws)) - h, _ = sf.Height() - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{c + ":510", d + ":100"})) - // a(b):90(0) b(c):210(90) c(c):300(+210=510) d(d): 100(100) - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - vote12, err := action.NewVote(2, a, uint64(100000), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(vote12).SetNonce(2).SetGasLimit(100000).Build() - selp, err = action.Sign(elp, priKeyD) - require.NoError(t, err) - - _, err = ws.RunActions(ctx, 14, []action.SealedEnvelope{selp}) - require.Nil(t, err) - require.Nil(t, sf.Commit(ws)) - h, _ = sf.Height() - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{c + ":510", a + ":100"})) - // a(b):90(100) b(c):210(90) c(c):300(+210=510) d(a): 100(0) - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - vote13, err := action.NewVote(2, d, uint64(100000), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(vote13).SetNonce(2).SetGasLimit(100000).Build() - selp, err = action.Sign(elp, priKeyC) - require.NoError(t, err) - - _, err = ws.RunActions(ctx, 15, []action.SealedEnvelope{selp}) - require.Nil(t, err) - require.Nil(t, sf.Commit(ws)) - h, _ = sf.Height() - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{c + ":210", d + ":300"})) - // a(b):90(100) b(c):210(90) c(d):300(210) d(a): 100(300) - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - vote14, err := action.NewVote(3, c, uint64(100000), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(vote14).SetNonce(3).SetGasLimit(100000).Build() - selp, err = action.Sign(elp, priKeyC) - require.NoError(t, err) - - _, err = ws.RunActions(ctx, 16, []action.SealedEnvelope{selp}) - require.Nil(t, err) - require.Nil(t, sf.Commit(ws)) - h, _ = sf.Height() - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{c + ":510", a + ":100"})) - // a(b):90(100) b(c):210(90) c(c):300(+210=510) d(a): 100(0) - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - tx6, err := action.NewTransfer(uint64(1), big.NewInt(200), e, nil, uint64(0), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(tx6).SetNonce(1).Build() - selp1, err = action.Sign(elp, priKeyC) - require.NoError(t, err) - - tx7, err := action.NewTransfer(uint64(2), big.NewInt(200), e, nil, uint64(0), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(tx7).SetNonce(2).Build() - selp2, err = action.Sign(elp, priKeyB) - require.NoError(t, err) - - _, err = ws.RunActions(ctx, 17, []action.SealedEnvelope{selp1, selp2}) - require.Nil(t, err) - require.Nil(t, sf.Commit(ws)) - h, _ = sf.Height() - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{c + ":110", a + ":100"})) - // a(b):90(100) b(c):10(90) c(c):100(+10=110) d(a): 100(0) !e:500 - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - vote15, err := action.NewVote(0, e, uint64(100000), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(vote15).SetNonce(0).SetGasLimit(100000).Build() - selp, err = action.Sign(elp, priKeyE) - require.NoError(t, err) - - _, err = ws.RunActions(ctx, 18, []action.SealedEnvelope{selp}) - require.Nil(t, err) - require.Nil(t, sf.Commit(ws)) - h, _ = sf.Height() - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{c + ":110", e + ":500"})) - // a(b):90(100) b(c):10(90) c(c):100(+10=110) d(a): 100(0) e(e):500(+0=500) - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - vote16, err := action.NewVote(0, f, uint64(100000), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(vote16).SetNonce(0).SetGasLimit(100000).Build() - selp, err = action.Sign(elp, priKeyF) - require.NoError(t, err) - - _, err = ws.RunActions(ctx, 19, []action.SealedEnvelope{selp}) - require.Nil(t, err) - require.Nil(t, sf.Commit(ws)) - h, _ = sf.Height() - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{f + ":300", e + ":500"})) - // a(b):90(100) b(c):10(90) c(c):100(+10=110) d(a): 100(0) e(e):500(+0=500) f(f):300(+0=300) - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - vote17, err := action.NewVote(0, d, uint64(100000), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(vote17).SetNonce(0). - SetGasLimit(100000).Build() - selp1, err = action.Sign(elp, priKeyF) - require.NoError(t, err) - - vote18, err := action.NewVote(1, d, uint64(100000), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(vote18).SetNonce(1). - SetGasLimit(100000).Build() - selp2, err = action.Sign(elp, priKeyF) - require.NoError(t, err) - - _, err = ws.RunAction(raCtx, selp1) - require.NoError(t, err) - _, err = ws.RunAction(raCtx, selp2) - require.NoError(t, err) - newRoot = ws.UpdateBlockLevelInfo(20) - if checkStateRoot { - require.NotEqual(t, hash.ZeroHash256, newRoot) - } - require.NoError(t, sf.Commit(ws)) - h, _ = sf.Height() - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{d + ":300", e + ":500"})) - // a(b):90(100) b(c):10(90) c(c):100(+10=110) d(a): 100(300) e(e):500(+0=500) f(d):300(0) - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - tx8, err := action.NewTransfer(uint64(1), big.NewInt(200), b, nil, uint64(0), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(tx8).SetNonce(1).Build() - selp, err = action.Sign(elp, priKeyF) - require.NoError(t, err) - - _, err = ws.RunActions(ctx, 21, []action.SealedEnvelope{selp}) - require.Nil(t, err) - require.Nil(t, sf.Commit(ws)) - h, _ = sf.Height() - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{c + ":310", e + ":500"})) - // a(b):90(100) b(c):210(90) c(c):100(+210=310) d(a): 100(100) e(e):500(+0=500) f(d):100(0) - //fmt.Printf("%v \n", voteForm(sf.candidatesBuffer())) - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - tx9, err := action.NewTransfer(uint64(1), big.NewInt(10), a, nil, uint64(0), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(tx9).SetNonce(1).Build() - selp, err = action.Sign(elp, priKeyB) - require.NoError(t, err) - - _, err = ws.RunActions(ctx, 22, []action.SealedEnvelope{selp}) - require.Nil(t, err) - require.Nil(t, sf.Commit(ws)) - h, _ = sf.Height() - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{c + ":300", e + ":500"})) - // a(b):100(100) b(c):200(100) c(c):100(+200=300) d(a): 100(100) e(e):500(+0=500) f(d):100(0) - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - tx10, err := action.NewTransfer(uint64(1), big.NewInt(300), d, nil, uint64(0), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(tx10).SetNonce(1).Build() - selp, err = action.Sign(elp, priKeyE) - require.NoError(t, err) - - _, err = ws.RunActions(ctx, 23, []action.SealedEnvelope{selp}) - require.Nil(t, err) - require.Nil(t, sf.Commit(ws)) - h, err = sf.Height() - require.Equal(t, uint64(23), h) - require.NoError(t, err) - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{c + ":300", a + ":400"})) - // a(b):100(400) b(c):200(100) c(c):100(+200=300) d(a): 400(100) e(e):200(+0=200) f(d):100(0) - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - vote19, err := action.NewVote(0, a, uint64(100000), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(vote19).SetNonce(0).SetGasLimit(100000).Build() - selp1, err = action.Sign(elp, priKeyD) - require.NoError(t, err) - - vote20, err := action.NewVote(3, b, uint64(100000), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(vote20).SetNonce(3).SetGasLimit(100000).Build() - selp2, err = action.Sign(elp, priKeyD) - require.NoError(t, err) - - _, err = ws.RunActions(ctx, 24, []action.SealedEnvelope{selp1, selp2}) - require.Nil(t, err) - require.Nil(t, sf.Commit(ws)) - h, err = sf.Height() - require.Equal(t, uint64(24), h) - require.NoError(t, err) - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{c + ":300", b + ":500"})) - // a(b):100(0) b(c):200(500) c(c):100(+200=300) d(b): 400(100) e(e):200(+0=200) f(d):100(0) - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - vote21, err := action.NewVote(4, "", uint64(100000), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(vote21).SetNonce(4).SetGasLimit(100000).Build() - selp, err = action.Sign(elp, priKeyC) - require.NoError(t, err) - - _, err = ws.RunActions(ctx, 25, []action.SealedEnvelope{selp}) - require.Nil(t, err) - require.Nil(t, sf.Commit(ws)) - h, _ = sf.Height() - require.Equal(t, uint64(25), h) - require.NoError(t, err) - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{e + ":200", b + ":500"})) - // a(b):100(0) b(c):200(500) [c(c):100(+200=300)] d(b): 400(100) e(e):200(+0=200) f(d):100(0) - - ws, err = sf.NewWorkingSet() - require.NoError(t, err) - vote22, err := action.NewVote(4, "", uint64(100000), big.NewInt(0)) - require.NoError(t, err) - - bd = &action.EnvelopeBuilder{} - elp = bd.SetAction(vote22).SetNonce(4).SetGasLimit(100000).Build() - selp, err = action.Sign(elp, priKeyF) - require.NoError(t, err) - - _, err = ws.RunActions(ctx, 26, []action.SealedEnvelope{selp}) - require.Nil(t, err) - require.Nil(t, sf.Commit(ws)) - h, _ = sf.Height() - require.Equal(t, uint64(26), h) - require.NoError(t, err) - cand, _ = candidatesByHeight(sf, h) - require.True(t, compareStrings(voteForm(h, cand), []string{e + ":200", b + ":500"})) - // a(b):100(0) b(c):200(500) [c(c):100(+200=300)] d(b): 400(100) e(e):200(+0=200) f(d):100(0) - stateA, err := accountutil.LoadOrCreateAccount(ws, a, big.NewInt(0)) + candidates, err := sf.CandidatesByHeight(1) require.NoError(t, err) - require.Equal(t, stateA.Balance, big.NewInt(100)) + require.Equal(t, 2, len(candidates)) + assert.Equal(t, candidates[0].Address, identityset.Address(1).String()) + assert.Equal(t, candidates[0].Votes, big.NewInt(1)) + assert.Equal(t, candidates[1].Address, identityset.Address(0).String()) + assert.Equal(t, candidates[1].Votes, big.NewInt(0)) } func TestState(t *testing.T) { @@ -834,7 +195,6 @@ func TestSDBState(t *testing.T) { defer testutil.CleanupPath(t, testStateDBPath) cfg := config.Default - cfg.Chain.NumCandidates = 2 cfg.Chain.TrieDBPath = testStateDBPath sdb, err := NewStateDB(cfg, DefaultStateDBOption()) require.NoError(t, err) @@ -875,7 +235,7 @@ func testState(sf Factory, t *testing.T) { require.NoError(t, sf.Commit(ws)) h, _ := sf.Height() cand, _ := sf.CandidatesByHeight(h) - require.True(t, compareStrings(voteForm(h, cand), []string{a + ":100"})) + require.Equal(t, voteForm(h, cand), []string{a + ":100"}) // a(a):100(+0=100) b:200 c:300 //test AccountState() & State() @@ -907,7 +267,6 @@ func TestSDBNonce(t *testing.T) { defer testutil.CleanupPath(t, testStateDBPath) cfg := config.Default - cfg.Chain.NumCandidates = 2 cfg.Chain.TrieDBPath = testStateDBPath sdb, err := NewStateDB(cfg, DefaultStateDBOption()) require.NoError(t, err) @@ -970,7 +329,6 @@ func TestUnvote(t *testing.T) { defer testutil.CleanupPath(t, testTriePath) cfg := config.Default - cfg.Chain.NumCandidates = 2 cfg.DB.DbPath = testTriePath f, err := NewFactory(cfg, PrecreatedTrieDBOption(db.NewOnDiskDB(cfg.DB))) require.NoError(t, err) @@ -981,7 +339,6 @@ func TestSDBUnvote(t *testing.T) { testutil.CleanupPath(t, testStateDBPath) defer testutil.CleanupPath(t, testStateDBPath) cfg := config.Default - cfg.Chain.NumCandidates = 2 cfg.Chain.TrieDBPath = testStateDBPath sdb, err := NewStateDB(cfg, DefaultStateDBOption()) require.NoError(t, err) @@ -1026,7 +383,7 @@ func testUnvote(sf Factory, t *testing.T) { require.Nil(t, sf.Commit(ws)) h, _ := sf.Height() cand, _ := sf.CandidatesByHeight(h) - require.True(t, compareStrings(voteForm(h, cand), []string{})) + require.Equal(t, voteForm(h, cand), []string{}) vote2, err := action.NewVote(0, a, uint64(100000), big.NewInt(0)) require.NoError(t, err) @@ -1041,7 +398,7 @@ func testUnvote(sf Factory, t *testing.T) { require.Nil(t, sf.Commit(ws)) h, _ = sf.Height() cand, _ = sf.CandidatesByHeight(h) - require.True(t, compareStrings(voteForm(h, cand), []string{a + ":100"})) + require.Equal(t, voteForm(h, cand), []string{a + ":100"}) vote3, err := action.NewVote(0, "", uint64(20000), big.NewInt(0)) require.NoError(t, err) @@ -1056,7 +413,7 @@ func testUnvote(sf Factory, t *testing.T) { require.Nil(t, sf.Commit(ws)) h, _ = sf.Height() cand, _ = sf.CandidatesByHeight(h) - require.True(t, compareStrings(voteForm(h, cand), []string{})) + require.Equal(t, voteForm(h, cand), []string{}) vote4, err := action.NewVote(0, b, uint64(20000), big.NewInt(0)) require.NoError(t, err) @@ -1087,7 +444,7 @@ func testUnvote(sf Factory, t *testing.T) { require.Nil(t, sf.Commit(ws)) h, _ = sf.Height() cand, _ = sf.CandidatesByHeight(h) - require.True(t, compareStrings(voteForm(h, cand), []string{b + ":200"})) + require.Equal(t, voteForm(h, cand), []string{b + ":200"}) } func TestLoadStoreHeight(t *testing.T) { diff --git a/state/factory/statedb.go b/state/factory/statedb.go index 031b4a1372..5e649dae0c 100644 --- a/state/factory/statedb.go +++ b/state/factory/statedb.go @@ -32,7 +32,6 @@ import ( type stateDB struct { mutex sync.RWMutex currentChainHeight uint64 - numCandidates uint dao db.KVStore // the underlying DB for account/contract storage actionHandlers []protocol.ActionHandler // the handlers to handle actions timerFactory *prometheustimer.TimerFactory @@ -66,7 +65,6 @@ func InMemStateDBOption() StateDBOption { func NewStateDB(cfg config.Config, opts ...StateDBOption) (Factory, error) { sdb := stateDB{ currentChainHeight: 0, - numCandidates: cfg.Chain.NumCandidates, } for _, opt := range opts { @@ -208,9 +206,6 @@ func (sdb *stateDB) CandidatesByHeight(height uint64) ([]*state.Candidate, error ) if errors.Cause(err) == nil { if len(candidates) > 0 { - if len(candidates) > int(sdb.numCandidates) { - candidates = candidates[:sdb.numCandidates] - } return candidates, nil } err = state.ErrStateNotExist diff --git a/tools/minicluster/minicluster.go b/tools/minicluster/minicluster.go index 8bd5d60383..a46f0e753d 100644 --- a/tools/minicluster/minicluster.go +++ b/tools/minicluster/minicluster.go @@ -272,7 +272,6 @@ func newConfig( cfg.Chain.ID = 1 cfg.Chain.ChainDBPath = chainDBPath cfg.Chain.TrieDBPath = trieDBPath - cfg.Chain.NumCandidates = numNodes cfg.Chain.CompressBlock = true cfg.Chain.ProducerPrivKey = producerPriKey.HexString()