Skip to content

Commit

Permalink
Merge branch 'master' into refactor_buildindexer
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie authored Aug 1, 2023
2 parents 217516d + 789336a commit 8dcce8f
Show file tree
Hide file tree
Showing 46 changed files with 2,928 additions and 598 deletions.
18 changes: 12 additions & 6 deletions action/candidate_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,20 @@ func (cr *CandidateRegister) EncodeABIBinary() ([]byte, error) {
}

func (cr *CandidateRegister) encodeABIBinary() ([]byte, error) {
operatorEthAddr := common.BytesToAddress(cr.operatorAddress.Bytes())
rewardEthAddr := common.BytesToAddress(cr.rewardAddress.Bytes())
ownerEthAddr := common.BytesToAddress(cr.ownerAddress.Bytes())
if cr.operatorAddress == nil {
return nil, ErrAddress
}
if cr.rewardAddress == nil {
return nil, ErrAddress
}
if cr.ownerAddress == nil {
return nil, ErrAddress
}
data, err := _candidateRegisterMethod.Inputs.Pack(
cr.name,
operatorEthAddr,
rewardEthAddr,
ownerEthAddr,
common.BytesToAddress(cr.operatorAddress.Bytes()),
common.BytesToAddress(cr.rewardAddress.Bytes()),
common.BytesToAddress(cr.ownerAddress.Bytes()),
cr.amount,
cr.duration,
cr.autoStake,
Expand Down
12 changes: 9 additions & 3 deletions action/candidate_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,15 @@ func (cu *CandidateUpdate) EncodeABIBinary() ([]byte, error) {
}

func (cu *CandidateUpdate) encodeABIBinary() ([]byte, error) {
operatorEthAddr := common.BytesToAddress(cu.operatorAddress.Bytes())
rewardEthAddr := common.BytesToAddress(cu.rewardAddress.Bytes())
data, err := _candidateUpdateMethod.Inputs.Pack(cu.name, operatorEthAddr, rewardEthAddr)
if cu.operatorAddress == nil {
return nil, ErrAddress
}
if cu.rewardAddress == nil {
return nil, ErrAddress
}
data, err := _candidateUpdateMethod.Inputs.Pack(cu.name,
common.BytesToAddress(cu.operatorAddress.Bytes()),
common.BytesToAddress(cu.rewardAddress.Bytes()))
if err != nil {
return nil, err
}
Expand Down
10 changes: 10 additions & 0 deletions action/candidateregister_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ func TestCandidateRegisterABIEncodeAndDecode(t *testing.T) {
require.Equal(test.Duration, stake.Duration())
require.Equal(test.AutoStake, stake.AutoStake())
require.Equal(test.Payload, stake.Payload())

stake.ownerAddress = nil
_, err = stake.EncodeABIBinary()
require.Equal(ErrAddress, err)
stake.rewardAddress = nil
_, err = stake.EncodeABIBinary()
require.Equal(ErrAddress, err)
stake.operatorAddress = nil
_, err = stake.EncodeABIBinary()
require.Equal(ErrAddress, err)
}

func TestIsValidCandidateName(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions action/candidateupdate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,11 @@ func TestCandidateUpdateABIEncodeAndDecode(t *testing.T) {
require.Equal(_cuName, stake.Name())
require.Equal(_cuOperatorAddrStr, stake.OperatorAddress().String())
require.Equal(_cuRewardAddrStr, stake.RewardAddress().String())

stake.rewardAddress = nil
_, err = stake.EncodeABIBinary()
require.Equal(ErrAddress, err)
stake.operatorAddress = nil
_, err = stake.EncodeABIBinary()
require.Equal(ErrAddress, err)
}
11 changes: 10 additions & 1 deletion action/protocol/execution/evm/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,20 @@ func TestConstantinople(t *testing.T) {
"io1pcg2ja9krrhujpazswgz77ss46xgt88afqlk6y",
22991400,
},
// after Palau
// Palau - Quebec
{
action.EmptyAddress,
22991401,
},
{
"io1pcg2ja9krrhujpazswgz77ss46xgt88afqlk6y",
24838200,
},
// after Quebec
{
action.EmptyAddress,
24838201,
},
{
"io1pcg2ja9krrhujpazswgz77ss46xgt88afqlk6y",
math.MaxUint64,
Expand Down
9 changes: 9 additions & 0 deletions action/protocol/staking/contractstake_bucket_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ func (bt *ContractStakingBucketType) Deserialize(b []byte) error {
return bt.loadProto(&m)
}

// Clone clones the bucket type
func (bt *ContractStakingBucketType) Clone() *ContractStakingBucketType {
return &ContractStakingBucketType{
Amount: big.NewInt(0).Set(bt.Amount),
Duration: bt.Duration,
ActivatedAt: bt.ActivatedAt,
}
}

func (bt *ContractStakingBucketType) toProto() *stakingpb.BucketType {
return &stakingpb.BucketType{
Amount: bt.Amount.String(),
Expand Down
34 changes: 0 additions & 34 deletions action/protocol/staking/contractstake_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,4 @@ type (
// BucketTypes returns the active bucket types
BucketTypes() ([]*ContractStakingBucketType, error)
}

// TODO (iip-13): remove this empty contract staking indexer
emptyContractStakingIndexer struct{}
)

var _ ContractStakingIndexer = (*emptyContractStakingIndexer)(nil)

// NewEmptyContractStakingIndexer creates an empty contract staking indexer
func NewEmptyContractStakingIndexer() ContractStakingIndexer {
return &emptyContractStakingIndexer{}
}

func (f *emptyContractStakingIndexer) CandidateVotes(ownerAddr address.Address) *big.Int {
return big.NewInt(0)
}

func (f *emptyContractStakingIndexer) Buckets() ([]*VoteBucket, error) {
return nil, nil
}

func (f *emptyContractStakingIndexer) BucketsByIndices([]uint64) ([]*VoteBucket, error) {
return nil, nil
}

func (f *emptyContractStakingIndexer) TotalBucketCount() uint64 {
return 0
}

func (f *emptyContractStakingIndexer) BucketTypes() ([]*ContractStakingBucketType, error) {
return nil, nil
}

func (f *emptyContractStakingIndexer) BucketsByCandidate(ownerAddr address.Address) ([]*VoteBucket, error) {
return nil, nil
}
4 changes: 2 additions & 2 deletions action/protocol/staking/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestProtocol_HandleCreateStake(t *testing.T) {
p, err := NewProtocol(depositGas, &BuilderConfig{
Staking: genesis.Default.Staking,
PersistStakingPatchBlock: math.MaxUint64,
}, nil, &emptyContractStakingIndexer{}, genesis.Default.GreenlandBlockHeight)
}, nil, nil, genesis.Default.GreenlandBlockHeight)
require.NoError(err)

// set up candidate
Expand Down Expand Up @@ -2680,7 +2680,7 @@ func initAll(t *testing.T, ctrl *gomock.Controller) (protocol.StateManager, *Pro
p, err := NewProtocol(depositGas, &BuilderConfig{
Staking: genesis.Default.Staking,
PersistStakingPatchBlock: math.MaxUint64,
}, nil, &emptyContractStakingIndexer{}, genesis.Default.GreenlandBlockHeight)
}, nil, nil, genesis.Default.GreenlandBlockHeight)
require.NoError(err)

// set up candidate
Expand Down
4 changes: 2 additions & 2 deletions action/protocol/staking/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,11 @@ func (p *Protocol) ActiveCandidates(ctx context.Context, sr protocol.StateReader
if err != nil {
return nil, errors.Wrap(err, "failed to get ActiveCandidates")
}
featureCtx, ok := protocol.GetFeatureCtx(ctx)
list := c.AllCandidates()
cand := make(CandidateList, 0, len(list))
featureCtx := protocol.MustGetFeatureCtx(ctx)
for i := range list {
if ok && featureCtx.AddContractStakingVotes {
if p.contractStakingIndexer != nil && featureCtx.AddContractStakingVotes {
list[i].Votes.Add(list[i].Votes, p.contractStakingIndexer.CandidateVotes(list[i].Owner))
}
if list[i].SelfStake.Cmp(p.config.RegistrationConsts.MinSelfStake) >= 0 {
Expand Down
8 changes: 4 additions & 4 deletions action/protocol/staking/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestProtocol(t *testing.T) {
stk, err := NewProtocol(nil, &BuilderConfig{
Staking: genesis.Default.Staking,
PersistStakingPatchBlock: math.MaxUint64,
}, nil, &emptyContractStakingIndexer{}, genesis.Default.GreenlandBlockHeight)
}, nil, nil, genesis.Default.GreenlandBlockHeight)
r.NotNil(stk)
r.NoError(err)
buckets, _, err := csr.getAllBuckets()
Expand Down Expand Up @@ -199,7 +199,7 @@ func TestCreatePreStates(t *testing.T) {
p, err := NewProtocol(nil, &BuilderConfig{
Staking: genesis.Default.Staking,
PersistStakingPatchBlock: math.MaxUint64,
}, nil, &emptyContractStakingIndexer{}, genesis.Default.GreenlandBlockHeight, genesis.Default.GreenlandBlockHeight)
}, nil, nil, genesis.Default.GreenlandBlockHeight, genesis.Default.GreenlandBlockHeight)
require.NoError(err)
ctx := protocol.WithBlockCtx(
genesis.WithGenesisContext(context.Background(), genesis.Default),
Expand Down Expand Up @@ -262,7 +262,7 @@ func Test_CreatePreStatesWithRegisterProtocol(t *testing.T) {
p, err := NewProtocol(nil, &BuilderConfig{
Staking: genesis.Default.Staking,
PersistStakingPatchBlock: math.MaxUint64,
}, cbi, &emptyContractStakingIndexer{}, genesis.Default.GreenlandBlockHeight, genesis.Default.GreenlandBlockHeight)
}, cbi, nil, genesis.Default.GreenlandBlockHeight, genesis.Default.GreenlandBlockHeight)
require.NoError(err)

rol := rolldpos.NewProtocol(23, 4, 3)
Expand Down Expand Up @@ -378,7 +378,7 @@ func Test_CreateGenesisStates(t *testing.T) {
p, err := NewProtocol(nil, &BuilderConfig{
Staking: cfg,
PersistStakingPatchBlock: math.MaxUint64,
}, nil, &emptyContractStakingIndexer{}, genesis.Default.GreenlandBlockHeight)
}, nil, nil, genesis.Default.GreenlandBlockHeight)
require.NoError(err)

v, err := p.Start(ctx, sm)
Expand Down
47 changes: 42 additions & 5 deletions action/protocol/staking/staking_statereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ func (c *compositeStakingStateReader) readStateBuckets(ctx context.Context, req
return nil, 0, err
}
}

if !c.isContractStakingEnabled() {
buckets.Buckets = getPageOfArray(buckets.Buckets, int(req.GetPagination().GetOffset()), int(req.GetPagination().GetLimit()))
return buckets, height, nil
}

// read LSD buckets
lsdBuckets, err := c.contractIndexer.Buckets()
if err != nil {
Expand All @@ -75,7 +81,6 @@ func (c *compositeStakingStateReader) readStateBuckets(ctx context.Context, req
if err != nil {
return nil, 0, err
}

// merge native and LSD buckets
buckets.Buckets = append(buckets.Buckets, lsdIoTeXBuckets.Buckets...)
buckets.Buckets = getPageOfArray(buckets.Buckets, int(req.GetPagination().GetOffset()), int(req.GetPagination().GetLimit()))
Expand All @@ -88,6 +93,10 @@ func (c *compositeStakingStateReader) readStateBucketsByVoter(ctx context.Contex
if err != nil {
return nil, 0, err
}
if !c.isContractStakingEnabled() {
buckets.Buckets = getPageOfArray(buckets.Buckets, int(req.GetPagination().GetOffset()), int(req.GetPagination().GetLimit()))
return buckets, height, err
}

// read LSD buckets
lsdBuckets, err := c.contractIndexer.Buckets()
Expand All @@ -99,7 +108,6 @@ func (c *compositeStakingStateReader) readStateBucketsByVoter(ctx context.Contex
if err != nil {
return nil, 0, err
}

// merge native and LSD buckets
buckets.Buckets = append(buckets.Buckets, lsdIoTeXBuckets.Buckets...)
buckets.Buckets = getPageOfArray(buckets.Buckets, int(req.GetPagination().GetOffset()), int(req.GetPagination().GetLimit()))
Expand All @@ -112,6 +120,12 @@ func (c *compositeStakingStateReader) readStateBucketsByCandidate(ctx context.Co
if err != nil {
return nil, 0, err
}

if !c.isContractStakingEnabled() {
buckets.Buckets = getPageOfArray(buckets.Buckets, int(req.GetPagination().GetOffset()), int(req.GetPagination().GetLimit()))
return buckets, height, err
}

// read LSD buckets
candidate := c.nativeSR.GetCandidateByName(req.GetCandName())
if candidate == nil {
Expand All @@ -137,6 +151,10 @@ func (c *compositeStakingStateReader) readStateBucketByIndices(ctx context.Conte
if err != nil {
return nil, 0, err
}
if !c.isContractStakingEnabled() {
return buckets, height, nil
}

// read LSD buckets
lsdBuckets, err := c.contractIndexer.BucketsByIndices(req.GetIndex())
if err != nil {
Expand All @@ -156,6 +174,9 @@ func (c *compositeStakingStateReader) readStateBucketCount(ctx context.Context,
if err != nil {
return nil, 0, err
}
if !c.isContractStakingEnabled() {
return bucketCnt, height, nil
}
buckets, err := c.contractIndexer.Buckets()
if err != nil {
return nil, 0, err
Expand Down Expand Up @@ -192,11 +213,12 @@ func (c *compositeStakingStateReader) readStateCandidates(ctx context.Context, r
return nil, 0, err
}
}

if !protocol.MustGetFeatureCtx(ctx).AddContractStakingVotes {
return candidates, height, nil
}

if !c.isContractStakingEnabled() {
return candidates, height, nil
}
for _, candidate := range candidates.Candidates {
if err = addContractStakingVotes(candidate, c.contractIndexer); err != nil {
return nil, 0, err
Expand All @@ -210,6 +232,9 @@ func (c *compositeStakingStateReader) readStateCandidateByName(ctx context.Conte
if err != nil {
return nil, 0, err
}
if !c.isContractStakingEnabled() {
return candidate, height, nil
}
if !protocol.MustGetFeatureCtx(ctx).AddContractStakingVotes {
return candidate, height, nil
}
Expand All @@ -224,6 +249,9 @@ func (c *compositeStakingStateReader) readStateCandidateByAddress(ctx context.Co
if err != nil {
return nil, 0, err
}
if !c.isContractStakingEnabled() {
return candidate, height, nil
}
if !protocol.MustGetFeatureCtx(ctx).AddContractStakingVotes {
return candidate, height, nil
}
Expand All @@ -243,7 +271,9 @@ func (c *compositeStakingStateReader) readStateTotalStakingAmount(ctx context.Co
if !ok {
return nil, 0, errors.Errorf("invalid balance %s", accountMeta.Balance)
}

if !c.isContractStakingEnabled() {
return accountMeta, height, nil
}
// add contract staking amount
buckets, err := c.contractIndexer.Buckets()
if err != nil {
Expand All @@ -258,6 +288,9 @@ func (c *compositeStakingStateReader) readStateTotalStakingAmount(ctx context.Co
}

func (c *compositeStakingStateReader) readStateContractStakingBucketTypes(ctx context.Context, _ *iotexapi.ReadStakingDataRequest_ContractStakingBucketTypes) (*iotextypes.ContractStakingBucketTypeList, uint64, error) {
if !c.isContractStakingEnabled() {
return &iotextypes.ContractStakingBucketTypeList{}, c.nativeSR.Height(), nil
}
bts, err := c.contractIndexer.BucketTypes()
if err != nil {
return nil, 0, err
Expand All @@ -272,6 +305,10 @@ func (c *compositeStakingStateReader) readStateContractStakingBucketTypes(ctx co
return &iotextypes.ContractStakingBucketTypeList{BucketTypes: pbBts}, c.nativeSR.Height(), nil
}

func (c *compositeStakingStateReader) isContractStakingEnabled() bool {
return c.contractIndexer != nil
}

func addContractStakingVotes(candidate *iotextypes.CandidateV2, contractStakingSR ContractStakingIndexer) error {
votes, ok := big.NewInt(0).SetString(candidate.TotalWeightedVotes, 10)
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion action/protocol/staking/validations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func initTestProtocol(t *testing.T) (*Protocol, []*Candidate) {
p, err := NewProtocol(nil, &BuilderConfig{
Staking: genesis.Default.Staking,
PersistStakingPatchBlock: math.MaxUint64,
}, nil, &emptyContractStakingIndexer{}, genesis.Default.GreenlandBlockHeight)
}, nil, nil, genesis.Default.GreenlandBlockHeight)
require.NoError(err)

var cans []*Candidate
Expand Down
2 changes: 1 addition & 1 deletion action/protocol/staking/vote_reviser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestVoteReviser(t *testing.T) {
PersistStakingPatchBlock: math.MaxUint64,
},
nil,
&emptyContractStakingIndexer{},
nil,
genesis.Default.OkhotskBlockHeight,
genesis.Default.HawaiiBlockHeight,
genesis.Default.GreenlandBlockHeight,
Expand Down
Loading

0 comments on commit 8dcce8f

Please sign in to comment.