Skip to content

Commit edbcde1

Browse files
authored
delete candidates by height api from blockchain (iotexproject#1666)
delete productivity_by_epoch return protocols in registry in order introduce genesis states creator interface introduce PreStatesCreator and PostSystemActionsCreator to protocol fix dardanelles bug
1 parent e9ac9b4 commit edbcde1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1124
-1084
lines changed

action/protocol/account/protocol.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,13 @@ func (p *Protocol) ReadState(context.Context, protocol.StateManager, []byte, ...
6666
return nil, protocol.ErrUnimplemented
6767
}
6868

69-
// Initialize initializes the protocol by setting the initial balances to some addresses
70-
func (p *Protocol) Initialize(
71-
ctx context.Context,
72-
sm protocol.StateManager,
73-
addrs []address.Address,
74-
amounts []*big.Int,
75-
) error {
69+
// CreateGenesisStates initializes the protocol by setting the initial balances to some addresses
70+
func (p *Protocol) CreateGenesisStates(ctx context.Context, sm protocol.StateManager) error {
7671
raCtx := protocol.MustGetRunActionsCtx(ctx)
7772
if err := p.assertZeroBlockHeight(raCtx.BlockHeight); err != nil {
7873
return err
7974
}
75+
addrs, amounts := raCtx.Genesis.InitBalances()
8076
if err := p.assertEqualLength(addrs, amounts); err != nil {
8177
return err
8278
}

action/protocol/account/protocol_test.go

+13-18
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ import (
1212
"testing"
1313

1414
"github.com/golang/mock/gomock"
15-
"github.com/stretchr/testify/assert"
1615
"github.com/stretchr/testify/require"
1716

1817
"github.com/iotexproject/go-pkgs/hash"
19-
"github.com/iotexproject/iotex-address/address"
2018
"github.com/iotexproject/iotex-core/action/protocol"
2119
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
2220
"github.com/iotexproject/iotex-core/config"
@@ -65,7 +63,7 @@ func TestLoadOrCreateAccountState(t *testing.T) {
6563
}
6664

6765
func TestProtocol_Initialize(t *testing.T) {
68-
p := NewProtocol()
66+
require := require.New(t)
6967
ctrl := gomock.NewController(t)
7068
defer ctrl.Finish()
7169
sm := mock_chainmanager.NewMockStateManager(ctrl)
@@ -88,28 +86,25 @@ func TestProtocol_Initialize(t *testing.T) {
8886
return nil
8987
}).AnyTimes()
9088

89+
ge := config.Default.Genesis
90+
ge.Account.InitBalanceMap = map[string]string{
91+
identityset.Address(0).String(): big.NewInt(100).String(),
92+
identityset.Address(1).String(): big.NewInt(200).String(),
93+
}
94+
p := NewProtocol()
9195
require.NoError(
92-
t,
93-
p.Initialize(
96+
p.CreateGenesisStates(
9497
protocol.WithRunActionsCtx(context.Background(), protocol.RunActionsCtx{
9598
BlockHeight: 0,
96-
Genesis: config.Default.Genesis,
99+
Genesis: ge,
97100
}),
98101
sm,
99-
[]address.Address{
100-
identityset.Address(0),
101-
identityset.Address(1),
102-
},
103-
[]*big.Int{
104-
big.NewInt(100),
105-
big.NewInt(200),
106-
},
107102
),
108103
)
109104
acc0, err := accountutil.LoadOrCreateAccount(sm, identityset.Address(0).String(), big.NewInt(0))
110-
require.NoError(t, err)
111-
assert.Equal(t, big.NewInt(100), acc0.Balance)
105+
require.NoError(err)
106+
require.Equal(big.NewInt(100), acc0.Balance)
112107
acc1, err := accountutil.LoadOrCreateAccount(sm, identityset.Address(1).String(), big.NewInt(0))
113-
require.NoError(t, err)
114-
assert.Equal(t, big.NewInt(200), acc1.Balance)
108+
require.NoError(err)
109+
require.Equal(big.NewInt(200), acc1.Balance)
115110
}

action/protocol/account/transfer.go

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ func (p *Protocol) handleTransfer(ctx context.Context, act action.Action, sm pro
120120
return nil, err
121121
}
122122
}
123+
123124
return &action.Receipt{
124125
Status: uint64(iotextypes.ReceiptStatus_Success),
125126
BlockHeight: raCtx.BlockHeight,

action/protocol/account/transfer_test.go

+14-15
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ func TestProtocol_HandleTransfer(t *testing.T) {
3939
cfg := config.Default
4040
ctx := context.Background()
4141
sm := mock_chainmanager.NewMockStateManager(ctrl)
42-
cm := mock_chainmanager.NewMockChainManager(ctrl)
4342
cb := db.NewCachedBatch()
4443
sm.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn(
4544
func(addrHash hash.Hash160, account interface{}) error {
@@ -60,30 +59,30 @@ func TestProtocol_HandleTransfer(t *testing.T) {
6059
}).AnyTimes()
6160

6261
p := NewProtocol()
63-
reward := rewarding.NewProtocol(cm, rolldpos.NewProtocol(1, 1, 1))
64-
registry := protocol.Registry{}
62+
reward := rewarding.NewProtocol(nil, rolldpos.NewProtocol(1, 1, 1))
63+
registry := protocol.NewRegistry()
6564
require.NoError(registry.Register(rewarding.ProtocolID, reward))
65+
cfg.Genesis.Rewarding.InitBalanceStr = "0"
66+
cfg.Genesis.Rewarding.BlockRewardStr = "0"
67+
cfg.Genesis.Rewarding.EpochRewardStr = "0"
68+
cfg.Genesis.Rewarding.NumDelegatesForEpochReward = 1
69+
cfg.Genesis.Rewarding.ExemptAddrStrsFromEpochReward = []string{}
70+
cfg.Genesis.Rewarding.FoundationBonusStr = "0"
71+
cfg.Genesis.Rewarding.NumDelegatesForFoundationBonus = 0
72+
cfg.Genesis.Rewarding.FoundationBonusLastEpoch = 0
73+
cfg.Genesis.Rewarding.ProductivityThreshold = 0
6674
require.NoError(
67-
reward.Initialize(
75+
reward.CreateGenesisStates(
6876
protocol.WithRunActionsCtx(context.Background(),
6977
protocol.RunActionsCtx{
7078
BlockHeight: 0,
7179
Producer: identityset.Address(27),
7280
Caller: identityset.Address(28),
7381
GasLimit: testutil.TestGasLimit,
74-
Registry: &registry,
82+
Registry: registry,
7583
Genesis: cfg.Genesis,
7684
}),
7785
sm,
78-
big.NewInt(0),
79-
big.NewInt(0),
80-
big.NewInt(0),
81-
1,
82-
nil,
83-
big.NewInt(0),
84-
0,
85-
0,
86-
0,
8786
),
8887
)
8988

@@ -118,7 +117,7 @@ func TestProtocol_HandleTransfer(t *testing.T) {
118117
Caller: identityset.Address(28),
119118
GasLimit: testutil.TestGasLimit,
120119
IntrinsicGas: gas,
121-
Registry: &registry,
120+
Registry: registry,
122121
})
123122
receipt, err := p.Handle(ctx, transfer, sm)
124123
require.NoError(err)

action/protocol/account/util/util.go

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func LoadOrCreateAccount(sm protocol.StateManager, encodedAddr string, init *big
3939
addrHash := hash.BytesToHash160(addr.Bytes())
4040
err = sm.State(addrHash, &account)
4141
if err == nil {
42+
// TODO: init value cannot be non-zero
4243
return &account, nil
4344
}
4445
if errors.Cause(err) == state.ErrStateNotExist {

action/protocol/context.go

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/iotexproject/iotex-address/address"
1616
"github.com/iotexproject/iotex-core/blockchain/genesis"
1717
"github.com/iotexproject/iotex-core/pkg/log"
18+
"github.com/iotexproject/iotex-core/state"
1819
)
1920

2021
type runActionsCtxKey struct{}
@@ -48,6 +49,9 @@ type RunActionsCtx struct {
4849
// History indicates whether to save account/contract history or not
4950
History bool
5051
// Registry is the pointer of protocol registry
52+
// Candidates is a list of candidates of current round
53+
Candidates []*state.Candidate
54+
// Registry is the pointer protocol registry
5155
Registry *Registry
5256
}
5357

action/protocol/execution/protocol_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ func (sct *SmartContractTest) prepareBlockchain(
281281
if sct.InitGenesis.IsBering {
282282
cfg.Genesis.Blockchain.BeringBlockHeight = 0
283283
}
284-
registry := protocol.Registry{}
284+
registry := protocol.NewRegistry()
285285
acc := account.NewProtocol()
286286
r.NoError(registry.Register(account.ProtocolID, acc))
287287
rp := rolldpos.NewProtocol(cfg.Genesis.NumCandidateDelegates, cfg.Genesis.NumDelegates, cfg.Genesis.NumSubEpochs)
@@ -296,9 +296,9 @@ func (sct *SmartContractTest) prepareBlockchain(
296296
cfg,
297297
dao,
298298
blockchain.InMemStateFactoryOption(),
299-
blockchain.RegistryOption(&registry),
299+
blockchain.RegistryOption(registry),
300300
)
301-
reward := rewarding.NewProtocol(bc, rp)
301+
reward := rewarding.NewProtocol(nil, rp)
302302
r.NoError(registry.Register(rewarding.ProtocolID, reward))
303303

304304
r.NotNil(bc)
@@ -319,7 +319,7 @@ func (sct *SmartContractTest) prepareBlockchain(
319319
Producer: identityset.Address(27),
320320
GasLimit: uint64(10000000),
321321
Genesis: cfg.Genesis,
322-
Registry: &registry,
322+
Registry: registry,
323323
})
324324
_, err = ws.RunActions(ctx, 0, nil)
325325
r.NoError(err)
@@ -465,7 +465,7 @@ func TestProtocol_Handle(t *testing.T) {
465465
cfg.Chain.IndexDBPath = testIndexPath
466466
cfg.Chain.EnableAsyncIndexWrite = false
467467
cfg.Genesis.EnableGravityChainVoting = false
468-
registry := protocol.Registry{}
468+
registry := protocol.NewRegistry()
469469
acc := account.NewProtocol()
470470
require.NoError(registry.Register(account.ProtocolID, acc))
471471
rp := rolldpos.NewProtocol(cfg.Genesis.NumCandidateDelegates, cfg.Genesis.NumDelegates, cfg.Genesis.NumSubEpochs)
@@ -482,7 +482,7 @@ func TestProtocol_Handle(t *testing.T) {
482482
cfg,
483483
dao,
484484
blockchain.DefaultStateFactoryOption(),
485-
blockchain.RegistryOption(&registry),
485+
blockchain.RegistryOption(registry),
486486
)
487487
exeProtocol := NewProtocol(bc.BlockDAO().GetBlockHash)
488488
require.NoError(registry.Register(ProtocolID, exeProtocol))
@@ -506,7 +506,7 @@ func TestProtocol_Handle(t *testing.T) {
506506
Producer: identityset.Address(27),
507507
GasLimit: gasLimit,
508508
Genesis: cfg.Genesis,
509-
Registry: &registry,
509+
Registry: registry,
510510
})
511511
_, err = ws.RunActions(ctx, 0, nil)
512512
require.NoError(err)

action/protocol/mock_protocol_test.go

+112
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

action/protocol/poll/nativestaking.go

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ func (ns *NativeStaking) readBuckets(prevIndx, limit *big.Int, height uint64, ts
131131
if len(pygg.CanNames) == 0 {
132132
return nil, ErrEndOfData
133133
}
134+
134135
buckets := make([]*types.Bucket, len(pygg.CanNames))
135136
for i := range pygg.CanNames {
136137
buckets[i], err = types.NewBucket(

0 commit comments

Comments
 (0)