Skip to content

Commit

Permalink
Merge branch 'master' into dev_node_delegate_cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
huof6829 authored Jun 10, 2022
2 parents a3890e6 + 5b4a96e commit 623be41
Show file tree
Hide file tree
Showing 27 changed files with 336 additions and 243 deletions.
7 changes: 4 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ workflows:
only:
- master
jobs:
- nightly_build_docker
- nightly_bin_build_docker
- nightly_bin_build_darwin
- build_test_docker
# - nightly_build_docker
# - nightly_bin_build_docker
# - nightly_bin_build_darwin
16 changes: 0 additions & 16 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,6 @@ func AssembleSealedEnvelope(act Envelope, pk crypto.PublicKey, sig []byte) Seale
return sealed
}

// ClassifyActions classfies actions
func ClassifyActions(actions []SealedEnvelope) ([]*Transfer, []*Execution) {
tsfs := make([]*Transfer, 0)
exes := make([]*Execution, 0)
for _, elp := range actions {
act := elp.Action()
switch act := act.(type) {
case *Transfer:
tsfs = append(tsfs, act)
case *Execution:
exes = append(exes, act)
}
}
return tsfs, exes
}

// CalculateIntrinsicGas returns the intrinsic gas of an action
func CalculateIntrinsicGas(baseIntrinsicGas uint64, payloadGas uint64, payloadSize uint64) (uint64, error) {
if payloadGas == 0 && payloadSize == 0 {
Expand Down
5 changes: 5 additions & 0 deletions action/action_deserializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"encoding/hex"
"testing"

"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
Expand All @@ -34,8 +35,12 @@ func TestActionDeserializer(t *testing.T) {
r.Equal(_signByte, se.Signature())
r.Zero(se.Encoding())

// use valid signature and reset se.Hash
se.signature = _validSig
se.hash = hash.ZeroHash256
se.Hash()
se1, err := (&Deserializer{}).ActionToSealedEnvelope(se.Proto())
se1.Hash()
r.NoError(err)
r.Equal(se, se1)
}
Expand Down
18 changes: 0 additions & 18 deletions action/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,6 @@ func TestActionProtoAndVerify(t *testing.T) {
})
}

func TestActionClassifyActions(t *testing.T) {
require := require.New(t)
var (
producerAddr = identityset.Address(27).String()
producerPriKey = identityset.PrivateKey(27)
amount = big.NewInt(0)
selp0, _ = SignedTransfer(producerAddr, producerPriKey, 1, amount, nil, 100, big.NewInt(0))
selp1, _ = SignedTransfer(identityset.Address(28).String(), producerPriKey, 1, amount, nil, 100, big.NewInt(0))
selp2, _ = SignedTransfer(identityset.Address(29).String(), producerPriKey, 1, amount, nil, 100, big.NewInt(0))
selp3, _ = SignedExecution(producerAddr, producerPriKey, uint64(1), amount, uint64(100000), big.NewInt(10), []byte{})
selp4, _ = SignedExecution(producerAddr, producerPriKey, uint64(2), amount, uint64(100000), big.NewInt(10), []byte{})
)
actions := []SealedEnvelope{selp0, selp1, selp2, selp3, selp4}
tsfs, exes := ClassifyActions(actions)
require.Equal(len(tsfs), 3)
require.Equal(len(exes), 2)
}

func TestActionFakeSeal(t *testing.T) {
require := require.New(t)
priKey := identityset.PrivateKey(27)
Expand Down
2 changes: 1 addition & 1 deletion action/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func getRecipientAddr(addr *common.Address) string {
// BuildExecution loads executino action into envelope
func (b *EnvelopeBuilder) BuildExecution(tx *types.Transaction) (Envelope, error) {
b.setEnvelopeCommonFields(tx)
exec, err := NewExecution(getRecipientAddr(tx.To()), tx.Nonce(), tx.Value(), tx.Gas(), tx.GasPrice(), tx.Data())
exec, err := NewExecutionWithAccessList(getRecipientAddr(tx.To()), tx.Nonce(), tx.Value(), tx.Gas(), tx.GasPrice(), tx.Data(), tx.AccessList())
if err != nil {
return nil, err
}
Expand Down
26 changes: 25 additions & 1 deletion action/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Execution struct {
accessList types.AccessList
}

// NewExecution returns a Execution instance
// NewExecution returns an Execution instance (w/o access list)
func NewExecution(
contractAddress string,
nonce uint64,
Expand All @@ -64,6 +64,30 @@ func NewExecution(
}, nil
}

// NewExecutionWithAccessList returns an Execution instance with access list
func NewExecutionWithAccessList(
contractAddress string,
nonce uint64,
amount *big.Int,
gasLimit uint64,
gasPrice *big.Int,
data []byte,
list types.AccessList,
) (*Execution, error) {
return &Execution{
AbstractAction: AbstractAction{
version: version.ProtocolVersion,
nonce: nonce,
gasLimit: gasLimit,
gasPrice: gasPrice,
},
contract: contractAddress,
amount: amount,
data: data,
accessList: list,
}, nil
}

// Contract returns a contract address
func (ex *Execution) Contract() string { return ex.contract }

Expand Down
20 changes: 10 additions & 10 deletions action/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,6 @@ var (

func TestExecutionAccessList(t *testing.T) {
require := require.New(t)
ex, err := NewExecution(
identityset.Address(29).String(),
1,
big.NewInt(20),
uint64(100),
big.NewInt(1000000),
[]byte("test"),
)
require.NoError(err)

ex1 := &Execution{}
for _, v := range []struct {
Expand All @@ -114,7 +105,16 @@ func TestExecutionAccessList(t *testing.T) {
}, 30900,
},
} {
ex.accessList = v.list
ex, err := NewExecutionWithAccessList(
identityset.Address(29).String(),
1,
big.NewInt(20),
uint64(100),
big.NewInt(1000000),
[]byte("test"),
v.list,
)
require.NoError(err)
require.NoError(ex1.LoadProto(ex.Proto()))
ex1.AbstractAction = ex.AbstractAction
require.Equal(ex, ex1)
Expand Down
4 changes: 3 additions & 1 deletion action/protocol/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ type (
BlockchainCtx struct {
// Tip is the information of tip block
Tip TipInfo
//ChainID of the node
//ChainID is the native chain ID
ChainID uint32
// EvmNetworkID is the EVM network ID
EvmNetworkID uint32
}

// BlockCtx provides block auxiliary information.
Expand Down
12 changes: 10 additions & 2 deletions action/protocol/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,19 @@ func TestWithBlockchainCtx(t *testing.T) {

func TestGetBlockchainCtx(t *testing.T) {
require := require.New(t)
bcCtx := BlockchainCtx{}
bcCtx := BlockchainCtx{
Tip: TipInfo{
Height: 1024,
Timestamp: time.Now(),
},
ChainID: 1,
EvmNetworkID: 100,
}
ctx := WithBlockchainCtx(context.Background(), bcCtx)
require.NotNil(ctx)
_, ok := GetBlockchainCtx(ctx)
bcCtx1, ok := GetBlockchainCtx(ctx)
require.True(ok)
require.Equal(bcCtx, bcCtx1)
}

func TestMustGetBlockchainCtx(t *testing.T) {
Expand Down
9 changes: 5 additions & 4 deletions action/protocol/execution/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/config"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/tracer"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
Expand Down Expand Up @@ -74,6 +73,7 @@ type (
context vm.BlockContext
txCtx vm.TxContext
nonce uint64
evmNetworkID uint32
executorRawAddress string
amount *big.Int
contract *common.Address
Expand Down Expand Up @@ -165,6 +165,7 @@ func newParams(
GasPrice: execution.GasPrice(),
},
execution.Nonce(),
protocol.MustGetBlockchainCtx(ctx).EvmNetworkID,
actionCtx.Caller.String(),
execution.Amount(),
contractAddrPointer,
Expand Down Expand Up @@ -321,7 +322,7 @@ func prepareStateDB(ctx context.Context, sm protocol.StateManager) *StateDBAdapt
)
}

func getChainConfig(g genesis.Blockchain, height uint64) *params.ChainConfig {
func getChainConfig(g genesis.Blockchain, height uint64, id uint32) *params.ChainConfig {
var chainConfig params.ChainConfig
chainConfig.ConstantinopleBlock = new(big.Int).SetUint64(0) // Constantinople switch block (nil = no fork, 0 = already activated)
chainConfig.BeringBlock = new(big.Int).SetUint64(g.BeringBlockHeight)
Expand All @@ -331,7 +332,7 @@ func getChainConfig(g genesis.Blockchain, height uint64) *params.ChainConfig {
chainConfig.IstanbulBlock = new(big.Int).SetUint64(g.IcelandBlockHeight)
chainConfig.MuirGlacierBlock = new(big.Int).SetUint64(g.IcelandBlockHeight)
if g.IsIceland(height) {
chainConfig.ChainID = new(big.Int).SetUint64(uint64(config.EVMNetworkID()))
chainConfig.ChainID = new(big.Int).SetUint64(uint64(id))
}
// enable Berlin and London
chainConfig.BerlinBlock = new(big.Int).SetUint64(g.ToBeEnabledBlockHeight)
Expand All @@ -353,7 +354,7 @@ func executeInEVM(ctx context.Context, evmParams *Params, stateDB *StateDBAdapte
if vmCfg, ok := protocol.GetVMConfigCtx(ctx); ok {
config = vmCfg
}
chainConfig := getChainConfig(g, blockHeight)
chainConfig := getChainConfig(g, blockHeight, evmParams.evmNetworkID)
evm := vm.NewEVM(evmParams.context, evmParams.txCtx, stateDB, chainConfig, config)
if g.IsToBeEnabled(blockHeight) {
accessList = evmParams.accessList
Expand Down
24 changes: 15 additions & 9 deletions action/protocol/execution/evm/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/config"
"github.com/iotexproject/iotex-core/state"
"github.com/iotexproject/iotex-core/test/identityset"
"github.com/iotexproject/iotex-core/test/mock/mock_chainmanager"
Expand Down Expand Up @@ -57,7 +56,10 @@ func TestExecuteContractFailure(t *testing.T) {
})
ctx = genesis.WithGenesisContext(ctx, genesis.Default)

ctx = protocol.WithFeatureCtx(ctx)
ctx = protocol.WithBlockchainCtx(protocol.WithFeatureCtx(ctx), protocol.BlockchainCtx{
ChainID: 1,
EvmNetworkID: 100,
})
retval, receipt, err := ExecuteContract(ctx, sm, e,
func(uint64) (hash.Hash256, error) {
return hash.ZeroHash256, nil
Expand All @@ -80,8 +82,12 @@ func TestConstantinople(t *testing.T) {
Caller: identityset.Address(27),
})

evmNetworkID := uint32(100)
g := genesis.Default
ctx = genesis.WithGenesisContext(ctx, g)
ctx = protocol.WithBlockchainCtx(genesis.WithGenesisContext(ctx, g), protocol.BlockchainCtx{
ChainID: 1,
EvmNetworkID: evmNetworkID,
})

execHeights := []struct {
contract string
Expand Down Expand Up @@ -210,19 +216,19 @@ func TestConstantinople(t *testing.T) {
}
stateDB := NewStateDBAdapter(sm, e.height, hash.ZeroHash256, opt...)

ctx = protocol.WithBlockCtx(ctx, protocol.BlockCtx{
fCtx := protocol.WithBlockCtx(ctx, protocol.BlockCtx{
Producer: identityset.Address(27),
GasLimit: testutil.TestGasLimit,
BlockHeight: e.height,
})
ctx = protocol.WithFeatureCtx(ctx)
ps, err := newParams(ctx, ex, stateDB, func(uint64) (hash.Hash256, error) {
fCtx = protocol.WithFeatureCtx(fCtx)
ps, err := newParams(fCtx, ex, stateDB, func(uint64) (hash.Hash256, error) {
return hash.ZeroHash256, nil
})
require.NoError(err)

var evmConfig vm.Config
chainConfig := getChainConfig(g.Blockchain, e.height)
chainConfig := getChainConfig(g.Blockchain, e.height, ps.evmNetworkID)
evm := vm.NewEVM(ps.context, ps.txCtx, stateDB, chainConfig, evmConfig)

evmChainConfig := evm.ChainConfig()
Expand Down Expand Up @@ -253,7 +259,7 @@ func TestConstantinople(t *testing.T) {
// iceland = support chainID + enable Istanbul and Muir Glacier
isIceland := g.IsIceland(e.height)
if isIceland {
require.EqualValues(config.EVMNetworkID(), evmChainConfig.ChainID.Uint64())
require.EqualValues(evmNetworkID, evmChainConfig.ChainID.Uint64())
} else {
require.Nil(evmChainConfig.ChainID)
}
Expand Down Expand Up @@ -342,7 +348,7 @@ func TestGasEstimate(t *testing.T) {
for _, v := range []struct {
gas, consume, refund, size uint64
}{
{config.Default.Genesis.BlockGasLimit, 8200300, 1000000, 20000},
{genesis.Default.BlockGasLimit, 8200300, 1000000, 20000},
{1000000, 245600, 100000, 5600},
{500000, 21000, 10000, 36},
} {
Expand Down
2 changes: 0 additions & 2 deletions action/protocol/execution/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -941,8 +941,6 @@ func TestMaxTime(t *testing.T) {
}

func TestIstanbulEVM(t *testing.T) {
cfg := config.Default
config.SetEVMNetworkID(cfg.Chain.EVMNetworkID)
t.Run("ArrayReturn", func(t *testing.T) {
NewSmartContractTest(t, "testdata-istanbul/array-return.json")
})
Expand Down
24 changes: 24 additions & 0 deletions action/sealedenvelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
"go.uber.org/zap"
Expand All @@ -22,6 +23,8 @@ type SealedEnvelope struct {
evmNetworkID uint32
srcPubkey crypto.PublicKey
signature []byte
srcAddress address.Address
hash hash.Hash256
}

// envelopeHash returns the raw hash of embedded Envelope (this is the hash to be signed)
Expand All @@ -48,6 +51,17 @@ func (sealed *SealedEnvelope) envelopeHash() (hash.Hash256, error) {
// Hash returns the hash value of SealedEnvelope.
// an all-0 return value means the transaction is invalid
func (sealed *SealedEnvelope) Hash() (hash.Hash256, error) {
if sealed.hash == hash.ZeroHash256 {
hashVal, hashErr := sealed.calcHash()
if hashErr == nil {
sealed.hash = hashVal
}
return sealed.hash, hashErr
}
return sealed.hash, nil
}

func (sealed *SealedEnvelope) calcHash() (hash.Hash256, error) {
switch sealed.encoding {
case iotextypes.Encoding_ETHEREUM_RLP:
act, ok := sealed.Action().(EthCompatibleAction)
Expand All @@ -69,6 +83,14 @@ func (sealed *SealedEnvelope) Hash() (hash.Hash256, error) {
// SrcPubkey returns the source public key
func (sealed *SealedEnvelope) SrcPubkey() crypto.PublicKey { return sealed.srcPubkey }

// SenderAddress returns address of the source public key
func (sealed *SealedEnvelope) SenderAddress() address.Address {
if sealed.srcAddress == nil {
sealed.srcAddress = sealed.srcPubkey.Address()
}
return sealed.srcAddress
}

// Signature returns signature bytes
func (sealed *SealedEnvelope) Signature() []byte {
sig := make([]byte, len(sealed.signature))
Expand Down Expand Up @@ -141,6 +163,8 @@ func (sealed *SealedEnvelope) LoadProto(pbAct *iotextypes.Action) error {
sealed.signature = make([]byte, sigSize)
copy(sealed.signature, pbAct.GetSignature())
sealed.encoding = encoding
sealed.hash = hash.ZeroHash256
sealed.srcAddress = nil
return nil
}

Expand Down
Loading

0 comments on commit 623be41

Please sign in to comment.