Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Sep 10, 2024
1 parent d6e12fe commit a27160b
Show file tree
Hide file tree
Showing 27 changed files with 145 additions and 424 deletions.
58 changes: 17 additions & 41 deletions actpool/actioniterator/actioniterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,60 +28,42 @@ func TestActionIterator(t *testing.T) {
c := identityset.Address(30)
priKeyC := identityset.PrivateKey(30)
accMap := make(map[string][]*action.SealedEnvelope)
tsf1, err := action.NewTransfer(uint64(1), big.NewInt(100), b.String(), nil, uint64(0), big.NewInt(13))
require.NoError(err)
bd := &action.EnvelopeBuilder{}
elp := bd.SetNonce(1).
SetGasPrice(big.NewInt(13)).
tsf1 := action.NewTransfer(big.NewInt(100), b.String(), nil)
elp := (&action.EnvelopeBuilder{}).SetNonce(1).SetGasPrice(big.NewInt(13)).
SetAction(tsf1).Build()
selp1, err := action.Sign(elp, priKeyA)
require.NoError(err)

tsf2, err := action.NewTransfer(uint64(2), big.NewInt(100), "2", nil, uint64(0), big.NewInt(30))
require.NoError(err)
bd = &action.EnvelopeBuilder{}
elp = bd.SetNonce(2).
SetGasPrice(big.NewInt(30)).
tsf2 := action.NewTransfer(big.NewInt(100), "2", nil)
elp = (&action.EnvelopeBuilder{}).SetNonce(2).SetGasPrice(big.NewInt(30)).
SetAction(tsf2).Build()
selp2, err := action.Sign(elp, priKeyA)
require.NoError(err)

accMap[a.String()] = []*action.SealedEnvelope{selp1, selp2}

tsf3, err := action.NewTransfer(uint64(1), big.NewInt(100), c.String(), nil, uint64(0), big.NewInt(15))
require.NoError(err)
bd = &action.EnvelopeBuilder{}
elp = bd.SetNonce(1).
SetGasPrice(big.NewInt(15)).
tsf3 := action.NewTransfer(big.NewInt(100), c.String(), nil)
elp = (&action.EnvelopeBuilder{}).SetNonce(1).SetGasPrice(big.NewInt(15)).
SetAction(tsf3).Build()
selp3, err := action.Sign(elp, priKeyB)
require.NoError(err)

tsf4, err := action.NewTransfer(uint64(2), big.NewInt(100), "3", nil, uint64(0), big.NewInt(10))
require.NoError(err)
bd = &action.EnvelopeBuilder{}
elp = bd.SetNonce(2).
SetGasPrice(big.NewInt(10)).
tsf4 := action.NewTransfer(big.NewInt(100), "3", nil)
elp = (&action.EnvelopeBuilder{}).SetNonce(2).SetGasPrice(big.NewInt(10)).
SetAction(tsf4).Build()
selp4, err := action.Sign(elp, priKeyB)
require.NoError(err)

tsf5, err := action.NewTransfer(uint64(3), big.NewInt(100), a.String(), nil, uint64(0), big.NewInt(2))
require.NoError(err)
bd = &action.EnvelopeBuilder{}
elp = bd.SetNonce(3).
SetGasPrice(big.NewInt(20)).
tsf5 := action.NewTransfer(big.NewInt(100), a.String(), nil)
elp = (&action.EnvelopeBuilder{}).SetNonce(3).SetGasPrice(big.NewInt(20)).
SetAction(tsf5).Build()
selp5, err := action.Sign(elp, priKeyB)
require.NoError(err)

accMap[b.String()] = []*action.SealedEnvelope{selp3, selp4, selp5}

tsf6, err := action.NewTransfer(uint64(1), big.NewInt(100), "1", nil, uint64(0), big.NewInt(5))
require.NoError(err)
bd = &action.EnvelopeBuilder{}
elp = bd.SetNonce(1).
SetGasPrice(big.NewInt(5)).
tsf6 := action.NewTransfer(big.NewInt(100), "1", nil)
elp = (&action.EnvelopeBuilder{}).SetNonce(1).SetGasPrice(big.NewInt(5)).
SetAction(tsf6).Build()
selp6, err := action.Sign(elp, priKeyC)
require.NoError(err)
Expand All @@ -106,11 +88,8 @@ func TestActionByPrice(t *testing.T) {
s := &actionByPrice{}
require.Equal(0, s.Len())

tsf1, err := action.NewTransfer(uint64(1), big.NewInt(100), "100", nil, uint64(0), big.NewInt(13))
require.NoError(err)
bd := &action.EnvelopeBuilder{}
elp := bd.SetNonce(1).
SetGasPrice(big.NewInt(5)).
tsf1 := action.NewTransfer(big.NewInt(100), "100", nil)
elp := (&action.EnvelopeBuilder{}).SetNonce(1).SetGasPrice(big.NewInt(5)).
SetAction(tsf1).Build()
selp, err := action.Sign(elp, identityset.PrivateKey(28))
require.NoError(err)
Expand All @@ -130,12 +109,9 @@ func BenchmarkLooping(b *testing.B) {
require.NoError(b, err)
addr := priKey.PublicKey().Address()
require.NotNil(b, addr)
tsf, err := action.NewTransfer(uint64(1), big.NewInt(100), "1", nil, uint64(100000), big.NewInt(int64(i)))
require.NoError(b, err)
bd := &action.EnvelopeBuilder{}
elp := bd.SetNonce(1).
SetGasPrice(big.NewInt(5)).
SetAction(tsf).Build()
tsf := action.NewTransfer(big.NewInt(100), "1", nil)
elp := (&action.EnvelopeBuilder{}).SetNonce(1).SetGasPrice(big.NewInt(int64(i))).
SetGasLimit(100000).SetAction(tsf).Build()
selp, err := action.Sign(elp, priKey)
require.NoError(b, err)
accMap[addr.String()] = []*action.SealedEnvelope{selp}
Expand Down
44 changes: 9 additions & 35 deletions actpool/actpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,8 @@ func TestActPool_AddActs(t *testing.T) {
require.NoError(err)
err = ap.Add(ctx, replaceTsf)
require.Equal(action.ErrReplaceUnderpriced, errors.Cause(err))
replaceTransfer, err := action.NewTransfer(uint64(4), big.NewInt(1), _addr2, []byte{}, uint64(100000), big.NewInt(0))
require.NoError(err)

bd := &action.EnvelopeBuilder{}
elp := bd.SetNonce(4).
SetAction(replaceTransfer).
SetGasLimit(100000).Build()
replaceTransfer := action.NewTransfer(big.NewInt(1), _addr2, []byte{})
elp := (&action.EnvelopeBuilder{}).SetNonce(4).SetAction(replaceTransfer).SetGasLimit(100000).Build()
selp, err := action.Sign(elp, _priKey1)

require.NoError(err)
Expand All @@ -264,20 +259,9 @@ func TestActPool_AddActs(t *testing.T) {
require.Equal(action.ErrInsufficientFunds, errors.Cause(err))
// Case VII: insufficient gas
tmpData := [1234]byte{}
creationExecution, err := action.NewExecution(
action.EmptyAddress,
uint64(5),
big.NewInt(int64(0)),
10,
big.NewInt(10),
tmpData[:],
)
require.NoError(err)

bd = &action.EnvelopeBuilder{}
elp = bd.SetNonce(5).
SetGasPrice(big.NewInt(10)).
SetGasLimit(10).
creationExecution := action.NewExecution(action.EmptyAddress, big.NewInt(int64(0)), tmpData[:])
bd := &action.EnvelopeBuilder{}
elp = bd.SetNonce(5).SetGasPrice(big.NewInt(10)).SetGasLimit(10).
SetAction(creationExecution).Build()
selp, err = action.Sign(elp, _priKey1)
require.NoError(err)
Expand Down Expand Up @@ -724,27 +708,17 @@ func TestActPool_Reset(t *testing.T) {
require.NoError(err)
tsf22, err := action.SignedTransfer(_addr5, _priKey4, uint64(2), big.NewInt(10), []byte{}, uint64(20000), big.NewInt(0))
require.NoError(err)
tsf23, err := action.NewTransfer(uint64(3), big.NewInt(1), "", []byte{}, uint64(100000), big.NewInt(0))
require.NoError(err)

bd := &action.EnvelopeBuilder{}
elp := bd.SetNonce(3).
SetGasLimit(20000).
SetAction(tsf23).Build()
tsf23 := action.NewTransfer(big.NewInt(1), "", []byte{})
elp := (&action.EnvelopeBuilder{}).SetNonce(3).SetGasLimit(20000).SetAction(tsf23).Build()
selp23, err := action.Sign(elp, _priKey4)
require.NoError(err)

tsf24, err := action.SignedTransfer(_addr5, _priKey5, uint64(1), big.NewInt(10), []byte{}, uint64(20000), big.NewInt(0))
require.NoError(err)
tsf25, err := action.SignedTransfer(_addr4, _priKey5, uint64(2), big.NewInt(10), []byte{}, uint64(20000), big.NewInt(0))
require.NoError(err)
tsf26, err := action.NewTransfer(uint64(3), big.NewInt(1), _addr4, []byte{}, uint64(20000), big.NewInt(0))
require.NoError(err)

bd = &action.EnvelopeBuilder{}
elp = bd.SetNonce(3).
SetGasLimit(20000).
SetAction(tsf26).Build()
tsf26 := action.NewTransfer(big.NewInt(1), _addr4, []byte{})
elp = (&action.EnvelopeBuilder{}).SetNonce(3).SetGasLimit(20000).SetAction(tsf26).Build()
selp26, err := action.Sign(elp, _priKey5)
require.NoError(err)

Expand Down
56 changes: 15 additions & 41 deletions api/coreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,23 +543,14 @@ func (core *coreService) ReadContract(ctx context.Context, callerAddr address.Ad
return res.Data, res.Receipt, nil
}
}
ctx = genesis.WithGenesisContext(ctx, core.bc.Genesis())
ctx, err := core.bc.Context(ctx)
if err != nil {
return "", nil, err
}
ctx = protocol.WithFeatureCtx(protocol.WithBlockCtx(ctx, protocol.BlockCtx{
BlockHeight: core.bc.TipHeight(),
}))
var (
g = core.bc.Genesis()
blockGasLimit = g.BlockGasLimitByHeight(core.bc.TipHeight())
)
if elp.GasLimit() == 0 || blockGasLimit < elp.GasLimit() {
elp.SetGas(blockGasLimit)
}

retval, receipt, err := core.simulateExecution(ctx, callerAddr, elp, core.dao.GetBlockHash, core.getBlockTime)
retval, receipt, err := core.simulateExecution(ctx, callerAddr, elp)
if err != nil {
return "", nil, status.Error(codes.Internal, err.Error())
}
Expand Down Expand Up @@ -1533,14 +1524,6 @@ func (core *coreService) EstimateMigrateStakeGasConsumption(ctx context.Context,

// EstimateExecutionGasConsumption estimate gas consumption for execution action
func (core *coreService) EstimateExecutionGasConsumption(ctx context.Context, elp action.Envelope, callerAddr address.Address, opts ...protocol.SimulateOption) (uint64, error) {
ctx = genesis.WithGenesisContext(ctx, core.bc.Genesis())
ctx = protocol.WithFeatureCtx(protocol.WithBlockCtx(ctx, protocol.BlockCtx{
BlockHeight: core.bc.TipHeight(),
}))
ctx, err := core.bc.Context(ctx)
if err != nil {
return 0, status.Error(codes.Internal, err.Error())
}
var (
g = core.bc.Genesis()
blockGasLimit = g.BlockGasLimitByHeight(core.bc.TipHeight())
Expand Down Expand Up @@ -1592,11 +1575,7 @@ func (core *coreService) isGasLimitEnough(
) (bool, *action.Receipt, error) {
ctx, span := tracer.NewSpan(ctx, "Server.isGasLimitEnough")
defer span.End()
ctx, err := core.bc.Context(ctx)
if err != nil {
return false, nil, err
}
_, receipt, err := core.simulateExecution(ctx, caller, elp, core.dao.GetBlockHash, core.getBlockTime, opts...)
_, receipt, err := core.simulateExecution(ctx, caller, elp, opts...)
if err != nil {
return false, nil, err
}
Expand Down Expand Up @@ -1735,21 +1714,12 @@ func (core *coreService) ReceiveBlock(blk *block.Block) error {
}

func (core *coreService) SimulateExecution(ctx context.Context, addr address.Address, elp action.Envelope) ([]byte, *action.Receipt, error) {
ctx = genesis.WithGenesisContext(ctx, core.bc.Genesis())
ctx, err := core.bc.Context(ctx)
if err != nil {
return nil, nil, err
}
// TODO (liuhaai): Use original nonce and gas limit properly
ctx = protocol.WithFeatureCtx(protocol.WithBlockCtx(ctx, protocol.BlockCtx{
BlockHeight: core.bc.TipHeight(),
}))
var (
g = core.bc.Genesis()
blockGasLimit = g.BlockGasLimitByHeight(core.bc.TipHeight())
)
elp.SetGas(blockGasLimit)
return core.simulateExecution(ctx, addr, elp, core.dao.GetBlockHash, core.getBlockTime)
return core.simulateExecution(ctx, addr, elp)
}

// SyncingProgress returns the syncing status of node
Expand All @@ -1773,7 +1743,7 @@ func (core *coreService) TraceTransaction(ctx context.Context, actHash string, c
}
addr, _ := address.FromString(address.ZeroAddress)
return core.traceTx(ctx, new(tracers.Context), config, func(ctx context.Context) ([]byte, *action.Receipt, error) {
return core.simulateExecution(ctx, addr, act.Envelope, core.dao.GetBlockHash, core.getBlockTime)
return core.simulateExecution(ctx, addr, act.Envelope)
})
}

Expand Down Expand Up @@ -1801,7 +1771,7 @@ func (core *coreService) TraceCall(ctx context.Context,
elp := (&action.EnvelopeBuilder{}).SetAction(action.NewExecution(contractAddress, amount, data)).
SetGasLimit(gasLimit).Build()
return core.traceTx(ctx, new(tracers.Context), config, func(ctx context.Context) ([]byte, *action.Receipt, error) {
return core.simulateExecution(ctx, callerAddr, elp, core.dao.GetBlockHash, core.getBlockTime)
return core.simulateExecution(ctx, callerAddr, elp)
})
}

Expand Down Expand Up @@ -1855,28 +1825,32 @@ func (core *coreService) traceTx(ctx context.Context, txctx *tracers.Context, co
Tracer: tracer,
NoBaseFee: true,
})
ctx = protocol.WithBlockCtx(ctx, protocol.BlockCtx{})
ctx = genesis.WithGenesisContext(ctx, core.bc.Genesis())
ctx = protocol.WithBlockchainCtx(protocol.WithFeatureCtx(ctx), protocol.BlockchainCtx{})
retval, receipt, err := simulateFn(ctx)
return retval, receipt, tracer, err
}

func (core *coreService) simulateExecution(ctx context.Context, addr address.Address, elp action.Envelope, getBlockHash evm.GetBlockHash, getBlockTime evm.GetBlockTime, opts ...protocol.SimulateOption) ([]byte, *action.Receipt, error) {
func (core *coreService) simulateExecution(ctx context.Context, addr address.Address, elp action.Envelope, opts ...protocol.SimulateOption) ([]byte, *action.Receipt, error) {
ctx, err := core.bc.Context(ctx)
if err != nil {
return nil, nil, status.Error(codes.Internal, err.Error())
}
state, err := accountutil.AccountState(ctx, core.sf, addr)
if err != nil {
return nil, nil, status.Error(codes.InvalidArgument, err.Error())
}
var pendingNonce uint64
ctx = protocol.WithFeatureCtx(protocol.WithBlockCtx(ctx, protocol.BlockCtx{
BlockHeight: core.bc.TipHeight(),
}))
if protocol.MustGetFeatureCtx(ctx).RefactorFreshAccountConversion {
pendingNonce = state.PendingNonceConsideringFreshAccount()
} else {
pendingNonce = state.PendingNonce()
}
elp.SetNonce(pendingNonce)
ctx = evm.WithHelperCtx(ctx, evm.HelperContext{
GetBlockHash: getBlockHash,
GetBlockTime: getBlockTime,
GetBlockHash: core.dao.GetBlockHash,
GetBlockTime: core.getBlockTime,
DepositGasFunc: rewarding.DepositGas,
})
return core.sf.SimulateExecution(ctx, addr, elp, opts...)
Expand Down
Loading

0 comments on commit a27160b

Please sign in to comment.