-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[e2etest] add tests for original actions after candidate ownership tr…
…ansfer (#4295)
- Loading branch information
Showing
6 changed files
with
542 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package e2etest | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/iotexproject/iotex-core/action" | ||
"github.com/iotexproject/iotex-core/actpool" | ||
"github.com/iotexproject/iotex-core/blockchain" | ||
"github.com/iotexproject/iotex-core/blockchain/block" | ||
"github.com/iotexproject/iotex-core/chainservice" | ||
"github.com/iotexproject/iotex-core/config" | ||
"github.com/iotexproject/iotex-core/server/itx" | ||
"github.com/iotexproject/iotex-core/testutil" | ||
) | ||
|
||
type ( | ||
actionWithTime struct { | ||
act *action.SealedEnvelope | ||
t time.Time | ||
} | ||
testcase struct { | ||
name string | ||
preActs []*actionWithTime | ||
act *actionWithTime | ||
expect []actionExpect | ||
} | ||
accountNonceManager map[string]uint64 | ||
e2etest struct { | ||
cfg config.Config | ||
svr *itx.Server | ||
cs *chainservice.ChainService | ||
t *testing.T | ||
nonceMgr accountNonceManager | ||
} | ||
) | ||
|
||
func (m accountNonceManager) pop(addr string) uint64 { | ||
nonce := m[addr] | ||
m[addr] = nonce + 1 | ||
return nonce | ||
} | ||
|
||
func newE2ETest(t *testing.T, cfg config.Config) *e2etest { | ||
require := require.New(t) | ||
// Create a new blockchain | ||
svr, err := itx.NewServer(cfg) | ||
require.NoError(err) | ||
ctx := context.Background() | ||
require.NoError(svr.Start(ctx)) | ||
return &e2etest{ | ||
cfg: cfg, | ||
svr: svr, | ||
cs: svr.ChainService(cfg.Chain.ID), | ||
t: t, | ||
nonceMgr: make(accountNonceManager), | ||
} | ||
} | ||
|
||
func (e *e2etest) run(cases []*testcase) { | ||
ctx := context.Background() | ||
// run subcases | ||
for _, sub := range cases { | ||
e.t.Run(sub.name, func(t *testing.T) { | ||
e.withTest(t).runCase(ctx, sub) | ||
}) | ||
} | ||
} | ||
|
||
func (e *e2etest) runCase(ctx context.Context, c *testcase) { | ||
require := require.New(e.t) | ||
bc := e.cs.Blockchain() | ||
ap := e.cs.ActionPool() | ||
// run pre-actions | ||
for _, act := range c.preActs { | ||
_, _, err := addOneTx(ctx, ap, bc, act) | ||
require.NoError(err) | ||
} | ||
// run action | ||
act, receipt, err := addOneTx(ctx, ap, bc, c.act) | ||
for _, exp := range c.expect { | ||
exp.expect(e, act, receipt, err) | ||
} | ||
} | ||
|
||
func (e *e2etest) teardown() { | ||
require := require.New(e.t) | ||
// clean up | ||
testutil.CleanupPath(e.cfg.Chain.ChainDBPath) | ||
testutil.CleanupPath(e.cfg.Chain.TrieDBPath) | ||
testutil.CleanupPath(e.cfg.Chain.BloomfilterIndexDBPath) | ||
testutil.CleanupPath(e.cfg.Chain.CandidateIndexDBPath) | ||
testutil.CleanupPath(e.cfg.Chain.StakingIndexDBPath) | ||
testutil.CleanupPath(e.cfg.Chain.ContractStakingIndexDBPath) | ||
testutil.CleanupPath(e.cfg.DB.DbPath) | ||
testutil.CleanupPath(e.cfg.Chain.IndexDBPath) | ||
testutil.CleanupPath(e.cfg.System.SystemLogDBPath) | ||
testutil.CleanupPath(e.cfg.Chain.SGDIndexDBPath) | ||
require.NoError(e.svr.Stop(context.Background())) | ||
} | ||
|
||
func (e *e2etest) withTest(t *testing.T) *e2etest { | ||
return &e2etest{ | ||
cfg: e.cfg, | ||
svr: e.svr, | ||
cs: e.cs, | ||
t: t, | ||
} | ||
} | ||
|
||
func addOneTx(ctx context.Context, ap actpool.ActPool, bc blockchain.Blockchain, tx *actionWithTime) (*action.SealedEnvelope, *action.Receipt, error) { | ||
if err := ap.Add(ctx, tx.act); err != nil { | ||
return tx.act, nil, err | ||
} | ||
blk, err := createAndCommitBlock(bc, ap, tx.t) | ||
if err != nil { | ||
return tx.act, nil, err | ||
} | ||
h, err := tx.act.Hash() | ||
if err != nil { | ||
return tx.act, nil, err | ||
} | ||
for _, r := range blk.Receipts { | ||
if r.ActionHash == h { | ||
return tx.act, r, nil | ||
} | ||
} | ||
return tx.act, nil, errors.Errorf("failed to find receipt for %x", h) | ||
} | ||
|
||
func createAndCommitBlock(bc blockchain.Blockchain, ap actpool.ActPool, blkTime time.Time) (*block.Block, error) { | ||
blk, err := bc.MintNewBlock(blkTime) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err := bc.CommitBlock(blk); err != nil { | ||
return nil, err | ||
} | ||
ap.Reset() | ||
return blk, nil | ||
} | ||
|
||
func mustNoErr[T any](t T, err error) T { | ||
if err != nil { | ||
panic(err) | ||
} | ||
return t | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package e2etest | ||
|
||
import ( | ||
"context" | ||
"slices" | ||
|
||
"github.com/iotexproject/iotex-proto/golang/iotexapi" | ||
"github.com/iotexproject/iotex-proto/golang/iotextypes" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/protobuf/proto" | ||
|
||
"github.com/iotexproject/iotex-core/action" | ||
"github.com/iotexproject/iotex-core/action/protocol" | ||
"github.com/iotexproject/iotex-core/action/protocol/staking" | ||
"github.com/iotexproject/iotex-core/blockchain/genesis" | ||
) | ||
|
||
type ( | ||
actionExpect interface { | ||
expect(test *e2etest, act *action.SealedEnvelope, receipt *action.Receipt, err error) | ||
} | ||
basicActionExpect struct { | ||
err error | ||
status uint64 | ||
executionRevertMsg string | ||
} | ||
candidateExpect struct { | ||
candName string | ||
cand *iotextypes.CandidateV2 | ||
} | ||
bucketExpect struct { | ||
bucket *iotextypes.VoteBucket | ||
} | ||
) | ||
|
||
func (be *basicActionExpect) expect(test *e2etest, act *action.SealedEnvelope, receipt *action.Receipt, err error) { | ||
require := require.New(test.t) | ||
require.ErrorIs(err, be.err) | ||
require.Equal(be.status, receipt.Status) | ||
require.Equal(be.executionRevertMsg, receipt.ExecutionRevertMsg()) | ||
} | ||
|
||
func (ce *candidateExpect) expect(test *e2etest, act *action.SealedEnvelope, receipt *action.Receipt, err error) { | ||
require := require.New(test.t) | ||
method := &iotexapi.ReadStakingDataMethod{ | ||
Method: iotexapi.ReadStakingDataMethod_CANDIDATE_BY_NAME, | ||
} | ||
methodBytes, err := proto.Marshal(method) | ||
require.NoError(err) | ||
r := &iotexapi.ReadStakingDataRequest{ | ||
Request: &iotexapi.ReadStakingDataRequest_CandidateByName_{ | ||
CandidateByName: &iotexapi.ReadStakingDataRequest_CandidateByName{ | ||
CandName: ce.candName, | ||
}, | ||
}, | ||
} | ||
cs := test.svr.ChainService(test.cfg.Chain.ID) | ||
sr := cs.StateFactory() | ||
bc := cs.Blockchain() | ||
prtcl, ok := cs.Registry().Find("staking") | ||
require.True(ok) | ||
stkPrtcl := prtcl.(*staking.Protocol) | ||
reqBytes, err := proto.Marshal(r) | ||
require.NoError(err) | ||
ctx := protocol.WithRegistry(context.Background(), cs.Registry()) | ||
ctx = genesis.WithGenesisContext(ctx, test.cfg.Genesis) | ||
ctx = protocol.WithBlockCtx(ctx, protocol.BlockCtx{ | ||
BlockHeight: bc.TipHeight(), | ||
}) | ||
ctx = protocol.WithFeatureCtx(ctx) | ||
respData, _, err := stkPrtcl.ReadState(ctx, sr, methodBytes, reqBytes) | ||
require.NoError(err) | ||
candidate := &iotextypes.CandidateV2{} | ||
require.NoError(proto.Unmarshal(respData, candidate)) | ||
require.EqualValues(ce.cand.String(), candidate.String()) | ||
} | ||
|
||
func (be *bucketExpect) expect(test *e2etest, act *action.SealedEnvelope, receipt *action.Receipt, err error) { | ||
require := require.New(test.t) | ||
method := &iotexapi.ReadStakingDataMethod{ | ||
Method: iotexapi.ReadStakingDataMethod_BUCKETS_BY_INDEXES, | ||
} | ||
methodBytes, err := proto.Marshal(method) | ||
require.NoError(err) | ||
r := &iotexapi.ReadStakingDataRequest{ | ||
Request: &iotexapi.ReadStakingDataRequest_BucketsByIndexes{ | ||
BucketsByIndexes: &iotexapi.ReadStakingDataRequest_VoteBucketsByIndexes{ | ||
Index: []uint64{be.bucket.Index}, | ||
}, | ||
}, | ||
} | ||
cs := test.svr.ChainService(test.cfg.Chain.ID) | ||
sr := cs.StateFactory() | ||
bc := cs.Blockchain() | ||
prtcl, ok := cs.Registry().Find("staking") | ||
require.True(ok) | ||
stkPrtcl := prtcl.(*staking.Protocol) | ||
reqBytes, err := proto.Marshal(r) | ||
require.NoError(err) | ||
ctx := protocol.WithRegistry(context.Background(), cs.Registry()) | ||
ctx = genesis.WithGenesisContext(ctx, test.cfg.Genesis) | ||
ctx = protocol.WithBlockCtx(ctx, protocol.BlockCtx{ | ||
BlockHeight: bc.TipHeight(), | ||
}) | ||
ctx = protocol.WithFeatureCtx(ctx) | ||
respData, _, err := stkPrtcl.ReadState(ctx, sr, methodBytes, reqBytes) | ||
require.NoError(err) | ||
vbs := &iotextypes.VoteBucketList{} | ||
require.NoError(proto.Unmarshal(respData, vbs)) | ||
idx := slices.IndexFunc(vbs.Buckets, func(vb *iotextypes.VoteBucket) bool { | ||
return vb.ContractAddress == be.bucket.ContractAddress | ||
}) | ||
require.True(idx != -1) | ||
require.EqualValues(be.bucket.String(), vbs.Buckets[idx].String()) | ||
} |
Oops, something went wrong.