Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unit tests #128

Merged
merged 13 commits into from
Oct 12, 2023
4 changes: 2 additions & 2 deletions consensus/hotstuff/backend/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ func (s *backend) BlockTransactions(block *types.Block, state *state.StateDB) (t
signer := types.MakeSigner(s.chainConfig, block.Number())
for i, tx := range systemTransactions {
includedTx := allTransactions[commonTransactionCount + i]
if includedTx.Hash() != tx.Hash() {
return nil, nil, nil, fmt.Errorf("unexpected system tx hash detected, tx index %v, hash %s, expected: %s", commonTransactionCount + i, includedTx.Hash(), tx.Hash())
if signer.Hash(includedTx) != signer.Hash(tx) {
return nil, nil, nil, fmt.Errorf("unexpected system tx hash detected, tx index %v, hash %s, expected: %s", commonTransactionCount + i, signer.Hash(includedTx), signer.Hash(tx))
}
from, err := signer.Sender(includedTx)
if err != nil {
Expand Down
28 changes: 3 additions & 25 deletions contracts/native/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ var (
DebugSpentOpen bool = true
)

// the gasUsage for the native contract transaction calculated according to the following formula:
// * `gasUsage = gasRatio * gasTable[methodId]`
// the value in gas table for native tx is the max num for bench test in linux.
const (
basicGas = uint64(21000) // minimum gas spent by transaction which failed before contract.handler, the default value is 21000 wei.
gasRatio = float64(1.0) // gasRatio is used to adjust the final value of gasUsage.
)

type NativeContract struct {
ref *ContractRef
db *state.StateDB
Expand Down Expand Up @@ -81,8 +73,7 @@ func (s *NativeContract) Prepare(ab *abiPkg.ABI, gasTb map[string]uint64) {
s.gasTable = make(map[string]uint64)
for name, gas := range gasTb {
id := utils.MethodID(s.ab, name)
final := uint64(float64(basicGas) + float64(gas)*gasRatio)
s.gasTable[id] = final
s.gasTable[id] = gas
}
}

Expand All @@ -93,15 +84,6 @@ func (s *NativeContract) Register(name string, handler MethodHandler) {

// Invoke return execute ret and cost gas
func (s *NativeContract) Invoke() ([]byte, error) {

// pre-cost for failed tx which failed before `handler` execution.
if gasLeft := s.ref.gasLeft; gasLeft < basicGas {
s.ref.gasLeft = 0
return nil, fmt.Errorf("gasLeft not enough, need %d, got %d", basicGas, gasLeft)
} else {
s.ref.gasLeft -= basicGas
}

// check context
if !s.ref.CheckContexts() {
return nil, fmt.Errorf("context error")
Expand Down Expand Up @@ -132,18 +114,14 @@ func (s *NativeContract) Invoke() ([]byte, error) {
if !ok {
return nil, fmt.Errorf("failed to find method: [%s]", methodID)
}
if gasUsage < basicGas {
gasUsage = basicGas
}
// refund basic gas before tx get into `handler`
s.ref.gasLeft += basicGas

if gasLeft := s.ref.gasLeft; gasLeft < gasUsage {
return nil, fmt.Errorf("gasLeft not enough, need %d, got %d", gasUsage, gasLeft)
}
s.ref.gasLeft -= gasUsage

// execute transaction and cost gas
ret, err := handler(s)
s.ref.gasLeft -= gasUsage
return ret, err
}

Expand Down
42 changes: 19 additions & 23 deletions contracts/native/cross_chain_manager/entrance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,31 @@ func init() {
param := new(side_chain_manager.RegisterSideChainParam)
param.ChainID = 8
param.Name = "mychain"
param.Router = 1

param1 := new(side_chain_manager.RegisterSideChainParam)
param1.ChainID = 9
param1.ChainID = 79
param1.Name = strings.Repeat("1", 100)
param1.ExtraInfo = make([]byte, 1000000)
param1.CCMCAddress = make([]byte, 1000)
param1.Router = 1

ccd := common.HexToAddress("0xdedace1809079e241234d546e44517f31b57ab8f")
param2 := new(side_chain_manager.RegisterSideChainParam)
param2.ChainID = 10
param2.Router = 15
param2.Router = 2
param2.Name = "chain10"
param2.CCMCAddress = ccd.Bytes()

param3 := new(side_chain_manager.RegisterSideChainParam)
param3.ChainID = 11
param3.Router = 15
param3.Router = 2
param3.Name = strings.Repeat("1", 100)
param3.ExtraInfo = make([]byte, 1000000)
param3.CCMCAddress = ccd.Bytes()

param4 := *param3
param4.ChainID = 79
param4.ChainID = 2

for _, param := range []*side_chain_manager.RegisterSideChainParam{param, param1, param2, param3, &param4} {
input, err := utils.PackMethodWithStruct(side_chain_manager.ABI, side_chain_manager_abi.MethodRegisterSideChain, param)
Expand Down Expand Up @@ -171,7 +173,7 @@ func TestImportOuterTransfer(t *testing.T) {
param.Extra = event

param1 := new(scom.EntranceParam)
param1.SourceChainID = 9
param1.SourceChainID = 79
param1.Extra = event

param2 := new(scom.EntranceParam)
Expand Down Expand Up @@ -204,17 +206,16 @@ func TestImportOuterTransfer(t *testing.T) {
assert.Nil(t, err)

blockNumber := big.NewInt(1)
extra := uint64(10)
caller := common.Address{}
contractRef := native.NewContractRef(sdb, caller, caller, blockNumber, common.Hash{}, gasTable[cross_chain_manager_abi.MethodImportOuterTransfer]+extra, nil)
contractRef := native.NewContractRef(sdb, caller, caller, blockNumber, common.Hash{}, gasTable[cross_chain_manager_abi.MethodImportOuterTransfer], nil)
tr.Start()
ret, leftOverGas, err := contractRef.NativeCall(caller, utils.CrossChainManagerContractAddress, input)
tr.Stop()
assert.Nil(t, err)
result, err := utils.PackOutputs(scom.ABI, cross_chain_manager_abi.MethodImportOuterTransfer, true)
assert.Nil(t, err)
assert.Equal(t, ret, result)
assert.Equal(t, leftOverGas, extra)
assert.Equal(t, leftOverGas, uint64(0))
}
}

Expand All @@ -223,17 +224,16 @@ func TestImportOuterTransfer(t *testing.T) {
assert.Nil(t, err)

blockNumber := big.NewInt(1)
extra := uint64(10)
caller := common.Address{}
contractRef := native.NewContractRef(sdb, caller, caller, blockNumber, common.Hash{}, gasTable[cross_chain_manager_abi.MethodImportOuterTransfer]+extra, nil)
contractRef := native.NewContractRef(sdb, caller, caller, blockNumber, common.Hash{}, gasTable[cross_chain_manager_abi.MethodImportOuterTransfer], nil)
tr.Start()
ret, leftOverGas, err := contractRef.NativeCall(caller, utils.CrossChainManagerContractAddress, input)
tr.Stop()
assert.Nil(t, err)
result, err := utils.PackOutputs(scom.ABI, cross_chain_manager_abi.MethodImportOuterTransfer, true)
assert.Nil(t, err)
assert.Equal(t, ret, result)
assert.Equal(t, leftOverGas, extra)
assert.Equal(t, leftOverGas, uint64(0))
}
tr.Dump()
}
Expand All @@ -255,17 +255,16 @@ func TestReplenish(t *testing.T) {
assert.Nil(t, err)

blockNumber := big.NewInt(1)
extra := uint64(10)
caller := common.Address{}
contractRef := native.NewContractRef(sdb, caller, caller, blockNumber, common.Hash{}, gasTable[cross_chain_manager_abi.MethodReplenish]+extra, nil)
contractRef := native.NewContractRef(sdb, caller, caller, blockNumber, common.Hash{}, gasTable[cross_chain_manager_abi.MethodReplenish], nil)
tr.Start()
ret, leftOverGas, err := contractRef.NativeCall(caller, utils.CrossChainManagerContractAddress, input)
tr.Stop()
assert.Nil(t, err)
result, err := utils.PackOutputs(scom.ABI, cross_chain_manager_abi.MethodReplenish, true)
assert.Nil(t, err)
assert.Equal(t, ret, result)
assert.Equal(t, leftOverGas, extra)
assert.Equal(t, leftOverGas, uint64(0))
}
tr.Dump()
}
Expand All @@ -283,17 +282,16 @@ func TestCheckDone(t *testing.T) {
assert.Nil(t, err)

blockNumber := big.NewInt(1)
extra := uint64(10)
caller := common.Address{}
contractRef := native.NewContractRef(sdb, caller, caller, blockNumber, common.Hash{}, gasTable[cross_chain_manager_abi.MethodCheckDone]+extra, nil)
contractRef := native.NewContractRef(sdb, caller, caller, blockNumber, common.Hash{}, gasTable[cross_chain_manager_abi.MethodCheckDone], nil)
tr.Start()
ret, leftOverGas, err := contractRef.NativeCall(caller, utils.CrossChainManagerContractAddress, input)
tr.Stop()
assert.Nil(t, err)
result, err := utils.PackOutputs(scom.ABI, cross_chain_manager_abi.MethodCheckDone, false)
assert.Nil(t, err)
assert.Equal(t, ret, result)
assert.Equal(t, leftOverGas, extra)
assert.Equal(t, leftOverGas, uint64(0))
}
tr.Dump()
}
Expand All @@ -311,17 +309,16 @@ func TestWhiteChain(t *testing.T) {
assert.Nil(t, err)

blockNumber := big.NewInt(1)
extra := uint64(10)
caller := signers[0]
contractRef := native.NewContractRef(sdb, caller, caller, blockNumber, common.Hash{}, gasTable[cross_chain_manager_abi.MethodWhiteChain]+extra, nil)
contractRef := native.NewContractRef(sdb, caller, caller, blockNumber, common.Hash{}, gasTable[cross_chain_manager_abi.MethodWhiteChain], nil)
tr.Start()
ret, leftOverGas, err := contractRef.NativeCall(caller, utils.CrossChainManagerContractAddress, input)
tr.Stop()
assert.Nil(t, err)
result, err := utils.PackOutputs(scom.ABI, cross_chain_manager_abi.MethodWhiteChain, true)
assert.Nil(t, err)
assert.Equal(t, ret, result)
assert.Equal(t, leftOverGas, extra)
assert.Equal(t, leftOverGas, uint64(0))
}
tr.Dump()
}
Expand All @@ -339,17 +336,16 @@ func TestBlackChain(t *testing.T) {
assert.Nil(t, err)

blockNumber := big.NewInt(1)
extra := uint64(10)
caller := signers[0]
contractRef := native.NewContractRef(sdb, caller, caller, blockNumber, common.Hash{}, gasTable[cross_chain_manager_abi.MethodBlackChain]+extra, nil)
contractRef := native.NewContractRef(sdb, caller, caller, blockNumber, common.Hash{}, gasTable[cross_chain_manager_abi.MethodBlackChain], nil)
tr.Start()
ret, leftOverGas, err := contractRef.NativeCall(caller, utils.CrossChainManagerContractAddress, input)
tr.Stop()
assert.Nil(t, err)
result, err := utils.PackOutputs(scom.ABI, cross_chain_manager_abi.MethodBlackChain, true)
assert.Nil(t, err)
assert.Equal(t, ret, result)
assert.Equal(t, leftOverGas, extra)
assert.Equal(t, leftOverGas, uint64(0))
}
tr.Dump()
}
6 changes: 3 additions & 3 deletions contracts/native/economic/economic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestName(t *testing.T) {
payload, err := new(MethodContractNameInput).Encode()
assert.NoError(t, err)

raw, err := native.TestNativeCall(t, this, name, payload, common.Big0)
raw, err := native.TestNativeCall(t, this, name, payload, common.Big0, gasTable[MethodName])
assert.NoError(t, err)
var got string
assert.NoError(t, utils.UnpackOutputs(ABI, name, &got, raw))
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestTotalSupply(t *testing.T) {
var supply *big.Int

payload, _ := new(MethodTotalSupplyInput).Encode()
raw, err := native.TestNativeCall(t, this, name, payload, common.Big0, tc.height)
raw, err := native.TestNativeCall(t, this, name, payload, common.Big0, tc.height, gasTable[MethodTotalSupply])
assert.NoError(t, err)

if tc.testABI {
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestReward(t *testing.T) {
payload, _ := new(MethodRewardInput).Encode()
raw, err := native.TestNativeCall(t, this, name, payload, common.Big0, tc.height, func(state *state.StateDB) {
community.StoreCommunityInfo(state, big.NewInt(int64(tc.rate)), tc.pool)
})
}, gasTable[MethodReward])
if tc.err == nil {
assert.NoError(t, err)
assert.NoError(t, got.Decode(raw))
Expand Down
2 changes: 1 addition & 1 deletion contracts/native/governance/entrance.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package governance
import (
"fmt"


"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/contracts/native/go_abi/node_manager_abi"
nm "github.com/ethereum/go-ethereum/contracts/native/governance/node_manager"
Expand All @@ -43,6 +42,7 @@ func AssembleSystemTransactions(state *state.StateDB, height uint64) (types.Tran
if err != nil {
return nil, err
}

gas, err := core.IntrinsicGas(payload, nil, false, true, true)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions contracts/native/governance/node_manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ func TestChangeEpoch(t *testing.T) {
param := new(CreateValidatorParam)
param.ConsensusAddress = consensusAddr
param.SignerAddress = consensusAddr
param.ProposalAddress = caller
param.ProposalAddress = consensusAddr
param.Commission = new(big.Int).SetUint64(2000)
param.Desc = "test"
validatorsKey = append(validatorsKey, &ValidatorKey{param.ConsensusAddress, caller})
Expand Down Expand Up @@ -537,7 +537,7 @@ func TestDistribute(t *testing.T) {
param := new(CreateValidatorParam)
param.ConsensusAddress = consensusAddr
param.SignerAddress = consensusAddr
param.ProposalAddress = caller
param.ProposalAddress = consensusAddr
param.Commission = new(big.Int).SetUint64(2000)
param.Desc = "test"
validatorsKey = append(validatorsKey, &ValidatorKey{param.ConsensusAddress, caller})
Expand Down Expand Up @@ -985,7 +985,7 @@ func TestPerformance(t *testing.T) {
param := new(CreateValidatorParam)
param.ConsensusAddress = consensusAddr
param.SignerAddress = consensusAddr
param.ProposalAddress = caller
param.ProposalAddress = consensusAddr
param.Commission = new(big.Int).SetUint64(2000)
param.Desc = "test"
validatorsKey = append(validatorsKey, &ValidatorKey{param.ConsensusAddress, caller})
Expand Down
Loading
Loading