Remove special eth txn in block and block proposal logic. (#3496)

* Add ethereum transaction support

* fix fmt

* fix lint

* add new block body fork logic

* add back deleted code

* lower case block version const

* add eth txn into new version of block

* refactor back

* refactor more

* fix test code

* Fix build

* Fix build

* revert eth txn in block and block proposal

* fix build

* fix build

* fix build
pull/3314/head
Rongjian Lan 4 years ago committed by GitHub
parent a75a21d7c8
commit 9381b95cdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      core/rawdb/accessors_chain.go
  2. 4
      core/state_processor.go
  3. 65
      core/types/block.go
  4. 6
      core/types/bodyfieldsetter.go
  5. 22
      core/types/bodyv0.go
  6. 22
      core/types/bodyv1.go
  7. 22
      core/types/bodyv2.go
  8. 168
      core/types/bodyv3.go
  9. 8
      core/types/transaction.go
  10. 2
      core/types/transaction_test.go
  11. 2
      core/vm/contract.go
  12. 6
      core/vm/contracts_test.go
  13. 1
      internal/chain/engine.go
  14. 10
      node/node_handler_test.go
  15. 15
      node/node_newblock.go
  16. 5
      node/node_newblock_test.go
  17. 74
      node/worker/worker.go
  18. 7
      node/worker/worker_test.go
  19. 2
      rosetta/common/operations.go
  20. 2
      staking/types/delegation_test.go
  21. 17
      test/chain/main.go

@ -355,7 +355,7 @@ func ReadBlock(db DatabaseReader, hash common.Hash, number uint64) *types.Block
if body == nil {
return nil
}
return types.NewBlockWithHeader(header).WithBody(body.EthTransactions(), body.Transactions(), body.StakingTransactions(), body.Uncles(), body.IncomingReceipts())
return types.NewBlockWithHeader(header).WithBody(body.Transactions(), body.StakingTransactions(), body.Uncles(), body.IncomingReceipts())
}
// WriteBlock serializes a block into the database, header and body separately.

@ -153,7 +153,7 @@ func (p *StateProcessor) Process(
// return true if it is valid
func getTransactionType(
config *params.ChainConfig, header *block.Header, tx types.InternalTransaction,
config *params.ChainConfig, header *block.Header, tx *types.Transaction,
) types.TransactionType {
if header.ShardID() == tx.ShardID() &&
(!config.AcceptsCrossTx(header.Epoch()) ||
@ -174,7 +174,7 @@ func getTransactionType(
// and uses the input parameters for its environment. It returns the receipt
// for the transaction, gas used and an error if the transaction failed,
// indicating the block was invalid.
func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.DB, header *block.Header, tx types.InternalTransaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, *types.CXReceipt, uint64, error) {
func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.DB, header *block.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, *types.CXReceipt, uint64, error) {
txType := getTransactionType(config, header, tx)
if txType == types.InvalidTx {
return nil, nil, 0, errors.New("Invalid Transaction Type")

@ -50,7 +50,6 @@ const (
blockV0 = "" // same as taggedrlp.LegacyTag
blockV1 = "v1"
blockV2 = "v2"
blockV3 = "v3"
)
// Constants for block.
@ -88,19 +87,12 @@ func (n *BlockNonce) UnmarshalText(input []byte) error {
// BodyInterface is a simple accessor interface for block body.
type BodyInterface interface {
// EthTransactions returns a deep copy the list of ethereum-compatible transactions in this block.
EthTransactions() []*EthTransaction
// Transactions returns a deep copy the list of transactions in this block.
Transactions() []*Transaction
// StakingTransactions returns a deep copy of staking transactions
StakingTransactions() []*staking.StakingTransaction
// EthTransactionAt returns the ethereum-compatible transaction at the given index in this block.
// It returns nil if index is out of bounds.
EthTransactionAt(index int) *EthTransaction
// TransactionAt returns the transaction at the given index in this block.
// It returns nil if index is out of bounds.
TransactionAt(index int) *Transaction
@ -113,10 +105,6 @@ type BodyInterface interface {
// It returns nil if index is out of bounds
CXReceiptAt(index int) *CXReceipt
// SetEthTransactions sets the list of ethereum-compatible transactions with a deep copy of the
// given list.
SetEthTransactions(newTransactions []*EthTransaction)
// SetTransactions sets the list of transactions with a deep copy of the
// given list.
SetTransactions(newTransactions []*Transaction)
@ -152,12 +140,9 @@ type Body struct {
//
// TODO ek – this is a stopgap, and works only while there is a N:1 mapping
// between header and body versions. Replace usage with factory.
func NewBodyForMatchingHeader(b *Block) (*Body, error) {
func NewBodyForMatchingHeader(h *block.Header) (*Body, error) {
var bi BodyInterface
if b.Version == blockV3 {
bi = new(BodyV3)
} else {
switch b.header.Header.(type) {
switch h.Header.(type) {
case *v3.Header:
bi = new(BodyV2)
case *v2.Header, *v1.Header:
@ -166,8 +151,7 @@ func NewBodyForMatchingHeader(b *Block) (*Body, error) {
bi = new(BodyV0)
default:
return nil, errors.Errorf("unsupported header type %s",
taggedrlp.TypeName(reflect.TypeOf(b.header)))
}
taggedrlp.TypeName(reflect.TypeOf(h)))
}
return &Body{bi}, nil
}
@ -176,9 +160,8 @@ func NewBodyForMatchingHeader(b *Block) (*Body, error) {
// factory. Use for unit tests.
func NewTestBody() *Body {
block := new(Block)
block.Version = blockV2
block.header = blockfactory.NewTestHeader()
body, err := NewBodyForMatchingHeader(block)
body, err := NewBodyForMatchingHeader(blockfactory.NewTestHeader())
if err != nil {
panic(err)
}
@ -220,14 +203,12 @@ func init() {
BodyRegistry.MustRegister(taggedrlp.LegacyTag, new(BodyV0))
BodyRegistry.MustRegister(blockV1, new(BodyV1))
BodyRegistry.MustRegister(blockV2, new(BodyV2))
BodyRegistry.MustRegister(blockV3, new(BodyV3))
}
// Block represents an entire block in the Harmony blockchain.
type Block struct {
header *block.Header
uncles []*block.Header
ethTransactions EthTransactions
transactions Transactions
stakingTransactions staking.StakingTransactions
incomingReceipts CXReceiptsProofs
@ -248,9 +229,6 @@ type Block struct {
// Commit Signatures/Bitmap
commitSigAndBitmap []byte
// The version of the block (v0, v1...)
Version string
}
func (b *Block) String() string {
@ -325,16 +303,6 @@ type extblockV2 struct {
IncomingReceipts CXReceiptsProofs
}
// includes eth transaction
type extblockV3 struct {
Header *block.Header
EthTxs []*EthTransaction
Txs []*Transaction
Stks []*staking.StakingTransaction
Uncles []*block.Header
IncomingReceipts CXReceiptsProofs
}
var onceBlockReg sync.Once
var extblockReg *taggedrlp.Registry
@ -344,7 +312,6 @@ func blockRegistry() *taggedrlp.Registry {
extblockReg.MustRegister(taggedrlp.LegacyTag, &extblock{})
extblockReg.MustRegister(blockV1, &extblockV1{})
extblockReg.MustRegister(blockV2, &extblockV2{})
extblockReg.MustRegister(blockV3, &extblockV3{})
})
return extblockReg
@ -438,18 +405,12 @@ func (b *Block) DecodeRLP(s *rlp.Stream) error {
return err
}
switch eb := eb.(type) {
case *extblockV3:
b.header, b.uncles, b.ethTransactions, b.transactions, b.incomingReceipts, b.stakingTransactions = eb.Header, eb.Uncles, eb.EthTxs, eb.Txs, eb.IncomingReceipts, eb.Stks
b.Version = blockV3
case *extblockV2:
b.header, b.uncles, b.transactions, b.incomingReceipts, b.stakingTransactions = eb.Header, eb.Uncles, eb.Txs, eb.IncomingReceipts, eb.Stks
b.Version = blockV2
case *extblockV1:
b.header, b.uncles, b.transactions, b.incomingReceipts = eb.Header, eb.Uncles, eb.Txs, eb.IncomingReceipts
b.Version = blockV1
case *extblock:
b.header, b.uncles, b.transactions, b.incomingReceipts = eb.Header, eb.Uncles, eb.Txs, nil
b.Version = blockV0
default:
return errors.Errorf("unknown extblock type %s", taggedrlp.TypeName(reflect.TypeOf(eb)))
}
@ -460,9 +421,7 @@ func (b *Block) DecodeRLP(s *rlp.Stream) error {
// EncodeRLP serializes b into the Ethereum RLP block format.
func (b *Block) EncodeRLP(w io.Writer) error {
var eb interface{}
if b.Version == blockV3 {
eb = extblockV3{b.header, b.ethTransactions, b.transactions, b.stakingTransactions, b.uncles, b.incomingReceipts}
} else {
switch h := b.header.Header.(type) {
case *v3.Header:
eb = extblockV2{b.header, b.transactions, b.stakingTransactions, b.uncles, b.incomingReceipts}
@ -477,7 +436,7 @@ func (b *Block) EncodeRLP(w io.Writer) error {
return errors.Errorf("unsupported block header type %s",
taggedrlp.TypeName(reflect.TypeOf(h)))
}
}
extblockReg := blockRegistry()
return extblockReg.Encode(w, eb)
}
@ -497,11 +456,6 @@ func (b *Block) Transactions() Transactions {
return b.transactions
}
// EthTransactions returns ethereum-compatible transactions.
func (b *Block) EthTransactions() EthTransactions {
return b.ethTransactions
}
// StakingTransactions returns stakingTransactions.
func (b *Block) StakingTransactions() staking.StakingTransactions {
return b.stakingTransactions
@ -565,13 +519,12 @@ func (b *Block) Header() *block.Header { return CopyHeader(b.header) }
// Body returns the non-header content of the block.
func (b *Block) Body() *Body {
body, err := NewBodyForMatchingHeader(b)
body, err := NewBodyForMatchingHeader(b.header)
if err != nil {
utils.Logger().Warn().Err(err).Msg("cannot create block Body struct")
return nil
}
return body.With().
EthTransactions(b.ethTransactions).
Transactions(b.transactions).
StakingTransactions(b.stakingTransactions).
Uncles(b.uncles).
@ -610,16 +563,14 @@ func CalcUncleHash(uncles []*block.Header) common.Hash {
}
// WithBody returns a new block with the given transaction and uncle contents.
func (b *Block) WithBody(ethTransactions []*EthTransaction, transactions []*Transaction, stakingTxns []*staking.StakingTransaction, uncles []*block.Header, incomingReceipts CXReceiptsProofs) *Block {
func (b *Block) WithBody(transactions []*Transaction, stakingTxns []*staking.StakingTransaction, uncles []*block.Header, incomingReceipts CXReceiptsProofs) *Block {
block := &Block{
header: CopyHeader(b.header),
ethTransactions: make([]*EthTransaction, len(ethTransactions)),
transactions: make([]*Transaction, len(transactions)),
stakingTransactions: make([]*staking.StakingTransaction, len(stakingTxns)),
uncles: make([]*block.Header, len(uncles)),
incomingReceipts: make([]*CXReceiptsProof, len(incomingReceipts)),
}
copy(block.ethTransactions, ethTransactions)
copy(block.transactions, transactions)
copy(block.stakingTransactions, stakingTxns)
copy(block.incomingReceipts, incomingReceipts)

@ -10,12 +10,6 @@ type BodyFieldSetter struct {
b *Body
}
// EthTransactions sets the EthTransactions field of the body.
func (bfs BodyFieldSetter) EthTransactions(newTransactions []*EthTransaction) BodyFieldSetter {
bfs.b.SetEthTransactions(newTransactions)
return bfs
}
// Transactions sets the Transactions field of the body.
func (bfs BodyFieldSetter) Transactions(newTransactions []*Transaction) BodyFieldSetter {
bfs.b.SetTransactions(newTransactions)

@ -119,25 +119,3 @@ func (b *BodyV0) EncodeRLP(w io.Writer) error {
func (b *BodyV0) DecodeRLP(s *rlp.Stream) error {
return s.Decode(&b.f)
}
// EthTransactions returns the list of transactions that's ethereum-compatible.
//
// The returned list is a deep copy; the caller may do anything with it without
// affecting the original.
func (b *BodyV0) EthTransactions() (txs []*EthTransaction) {
// not supported
return nil
}
// EthTransactionAt returns the ethereum-compatible transaction at the given index in this block.
// It returns nil if index is out of bounds.
func (b *BodyV0) EthTransactionAt(index int) *EthTransaction {
// not supported
return nil
}
// SetEthTransactions sets the list of ethereum-compatible transactions with a deep copy of the given
// list.
func (b *BodyV0) SetEthTransactions(newTransactions []*EthTransaction) {
// not supported
}

@ -125,25 +125,3 @@ func (b *BodyV1) EncodeRLP(w io.Writer) error {
func (b *BodyV1) DecodeRLP(s *rlp.Stream) error {
return s.Decode(&b.f)
}
// EthTransactions returns the list of transactions that's ethereum-compatible.
//
// The returned list is a deep copy; the caller may do anything with it without
// affecting the original.
func (b *BodyV1) EthTransactions() (txs []*EthTransaction) {
// not supported
return nil
}
// EthTransactionAt returns the ethereum-compatible transaction at the given index in this block.
// It returns nil if index is out of bounds.
func (b *BodyV1) EthTransactionAt(index int) *EthTransaction {
// not supported
return nil
}
// SetEthTransactions sets the list of ethereum-compatible transactions with a deep copy of the given
// list.
func (b *BodyV1) SetEthTransactions(newTransactions []*EthTransaction) {
// not supported
}

@ -135,25 +135,3 @@ func (b *BodyV2) EncodeRLP(w io.Writer) error {
func (b *BodyV2) DecodeRLP(s *rlp.Stream) error {
return s.Decode(&b.f)
}
// EthTransactions returns the list of transactions that's ethereum-compatible.
//
// The returned list is a deep copy; the caller may do anything with it without
// affecting the original.
func (b *BodyV2) EthTransactions() (txs []*EthTransaction) {
// not supported
return nil
}
// EthTransactionAt returns the ethereum-compatible transaction at the given index in this block.
// It returns nil if index is out of bounds.
func (b *BodyV2) EthTransactionAt(index int) *EthTransaction {
// not supported
return nil
}
// SetEthTransactions sets the list of ethereum-compatible transactions with a deep copy of the given
// list.
func (b *BodyV2) SetEthTransactions(newTransactions []*EthTransaction) {
// not supported
}

@ -1,168 +0,0 @@
package types
import (
"io"
"github.com/ethereum/go-ethereum/rlp"
"github.com/harmony-one/harmony/block"
staking "github.com/harmony-one/harmony/staking/types"
)
// BodyV3 is the V3 block body
type BodyV3 struct {
f bodyFieldsV3
}
type bodyFieldsV3 struct {
EthTransactions []*EthTransaction
Transactions []*Transaction
StakingTransactions []*staking.StakingTransaction
Uncles []*block.Header
IncomingReceipts CXReceiptsProofs
}
// Transactions returns the list of transactions.
//
// The returned list is a deep copy; the caller may do anything with it without
// affecting the original.
func (b *BodyV3) Transactions() (txs []*Transaction) {
for _, tx := range b.f.Transactions {
txs = append(txs, tx.Copy())
}
return txs
}
// StakingTransactions returns the list of staking transactions.
// The returned list is a deep copy; the caller may do anything with it without
// affecting the original.
func (b *BodyV3) StakingTransactions() (txs []*staking.StakingTransaction) {
for _, tx := range b.f.StakingTransactions {
txs = append(txs, tx.Copy())
}
return txs
}
// TransactionAt returns the transaction at the given index in this block.
// It returns nil if index is out of bounds.
func (b *BodyV3) TransactionAt(index int) *Transaction {
if index < 0 || index >= len(b.f.Transactions) {
return nil
}
return b.f.Transactions[index].Copy()
}
// StakingTransactionAt returns the staking transaction at the given index in this block.
// It returns nil if index is out of bounds.
func (b *BodyV3) StakingTransactionAt(index int) *staking.StakingTransaction {
if index < 0 || index >= len(b.f.StakingTransactions) {
return nil
}
return b.f.StakingTransactions[index].Copy()
}
// CXReceiptAt returns the CXReceipt at given index in this block
// It returns nil if index is out of bounds
func (b *BodyV3) CXReceiptAt(index int) *CXReceipt {
if index < 0 {
return nil
}
for _, cxp := range b.f.IncomingReceipts {
cxs := cxp.Receipts
if index < len(cxs) {
return cxs[index].Copy()
}
index -= len(cxs)
}
return nil
}
// SetTransactions sets the list of transactions with a deep copy of the given
// list.
func (b *BodyV3) SetTransactions(newTransactions []*Transaction) {
var txs []*Transaction
for _, tx := range newTransactions {
txs = append(txs, tx.Copy())
}
b.f.Transactions = txs
}
// SetStakingTransactions sets the list of staking transactions with a deep copy of the given
// list.
func (b *BodyV3) SetStakingTransactions(newStakingTransactions []*staking.StakingTransaction) {
var txs []*staking.StakingTransaction
for _, tx := range newStakingTransactions {
txs = append(txs, tx.Copy())
}
b.f.StakingTransactions = txs
}
// Uncles returns a deep copy of the list of uncle headers of this block.
func (b *BodyV3) Uncles() (uncles []*block.Header) {
for _, uncle := range b.f.Uncles {
uncles = append(uncles, CopyHeader(uncle))
}
return uncles
}
// SetUncles sets the list of uncle headers with a deep copy of the given list.
func (b *BodyV3) SetUncles(newUncle []*block.Header) {
var uncles []*block.Header
for _, uncle := range newUncle {
uncles = append(uncles, CopyHeader(uncle))
}
b.f.Uncles = uncles
}
// IncomingReceipts returns a deep copy of the list of incoming cross-shard
// transaction receipts of this block.
func (b *BodyV3) IncomingReceipts() (incomingReceipts CXReceiptsProofs) {
return b.f.IncomingReceipts.Copy()
}
// SetIncomingReceipts sets the list of incoming cross-shard transaction
// receipts of this block with a dep copy of the given list.
func (b *BodyV3) SetIncomingReceipts(newIncomingReceipts CXReceiptsProofs) {
b.f.IncomingReceipts = newIncomingReceipts.Copy()
}
// EncodeRLP RLP-encodes the block body into the given writer.
func (b *BodyV3) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, &b.f)
}
// DecodeRLP RLP-decodes a block body from the given RLP stream into the
// receiver.
func (b *BodyV3) DecodeRLP(s *rlp.Stream) error {
return s.Decode(&b.f)
}
// EthTransactions returns the list of transactions that's ethereum-compatible.
//
// The returned list is a deep copy; the caller may do anything with it without
// affecting the original.
func (b *BodyV3) EthTransactions() (txs []*EthTransaction) {
for _, tx := range b.f.EthTransactions {
txs = append(txs, tx.Copy())
}
return txs
}
// EthTransactionAt returns the ethereum-compatible transaction at the given index in this block.
// It returns nil if index is out of bounds.
func (b *BodyV3) EthTransactionAt(index int) *EthTransaction {
if index < 0 || index >= len(b.f.EthTransactions) {
return nil
}
return b.f.EthTransactions[index].Copy()
}
// SetEthTransactions sets the list of ethereum-compatible transactions with a deep copy of the given
// list.
func (b *BodyV3) SetEthTransactions(newTransactions []*EthTransaction) {
var txs []*EthTransaction
for _, tx := range newTransactions {
txs = append(txs, tx.Copy())
}
b.f.EthTransactions = txs
}

@ -483,7 +483,7 @@ func (tx *Transaction) SenderAddress() (common.Address, error) {
// TxByNonce implements the sort interface to allow sorting a list of transactions
// by their nonces. This is usually only useful for sorting transactions from a
// single account, otherwise a nonce comparison doesn't make much sense.
type TxByNonce InternalTransactions
type TxByNonce Transactions
func (s TxByNonce) Len() int { return len(s) }
func (s TxByNonce) Less(i, j int) bool { return s[i].Nonce() < s[j].Nonce() }
@ -491,7 +491,7 @@ func (s TxByNonce) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// TxByPrice implements both the sort and the heap interface, making it useful
// for all at once sorting as well as individually adding and removing elements.
type TxByPrice InternalTransactions
type TxByPrice Transactions
func (s TxByPrice) Len() int { return len(s) }
func (s TxByPrice) Less(i, j int) bool { return s[i].GasPrice().Cmp(s[j].GasPrice()) > 0 }
@ -515,7 +515,7 @@ func (s *TxByPrice) Pop() interface{} {
// transactions in a profit-maximizing sorted order, while supporting removing
// entire batches of transactions for non-executable accounts.
type TransactionsByPriceAndNonce struct {
txs map[common.Address]InternalTransactions // Per account nonce-sorted list of transactions
txs map[common.Address]Transactions // Per account nonce-sorted list of transactions
heads TxByPrice // Next transaction for each unique account (price heap)
signer Signer // Signer for the set of transactions
}
@ -525,7 +525,7 @@ type TransactionsByPriceAndNonce struct {
//
// Note, the input map is reowned so the caller should not interact any more with
// if after providing it to the constructor.
func NewTransactionsByPriceAndNonce(signer Signer, txs map[common.Address]InternalTransactions) *TransactionsByPriceAndNonce {
func NewTransactionsByPriceAndNonce(signer Signer, txs map[common.Address]Transactions) *TransactionsByPriceAndNonce {
// Initialize a price based heap with the head transactions
heads := make(TxByPrice, 0, len(txs))
for from, accTxs := range txs {

@ -44,7 +44,7 @@ func TestTransactionPriceNonceSort(t *testing.T) {
signer := HomesteadSigner{}
// Generate a batch of transactions with overlapping values, but shifted nonces
groups := map[common.Address]InternalTransactions{}
groups := map[common.Address]Transactions{}
for start, key := range keys {
addr := crypto.PubkeyToAddress(key.PublicKey)
for i := 0; i < 25; i++ {

@ -72,7 +72,7 @@ func NewContract(caller ContractRef, object ContractRef, value *big.Int, gas uin
c.jumpdests = make(map[common.Hash]bitvec)
}
// GasLimit should be a pointer so it can safely be reduced through the run
// Gas should be a pointer so it can safely be reduced through the run
// This pointer will be off the state transition
c.Gas = gas
// ensures a value is set

@ -403,7 +403,7 @@ func testPrecompiled(addr string, test precompiledTest, t *testing.T) {
in := common.Hex2Bytes(test.input)
contract := NewContract(AccountRef(common.HexToAddress("1337")),
nil, new(big.Int), p.RequiredGas(in))
t.Run(fmt.Sprintf("%s-GasLimit=%d", test.name, contract.Gas), func(t *testing.T) {
t.Run(fmt.Sprintf("%s-Gas=%d", test.name, contract.Gas), func(t *testing.T) {
if res, err := RunPrecompiledContract(p, in, contract); err != nil {
t.Error(err)
} else if common.Bytes2Hex(res) != test.expected {
@ -422,7 +422,7 @@ func testPrecompiledOOG(addr string, test precompiledTest, t *testing.T) {
in := common.Hex2Bytes(test.input)
contract := NewContract(AccountRef(common.HexToAddress("1337")),
nil, new(big.Int), p.RequiredGas(in)-1)
t.Run(fmt.Sprintf("%s-GasLimit=%d", test.name, contract.Gas), func(t *testing.T) {
t.Run(fmt.Sprintf("%s-Gas=%d", test.name, contract.Gas), func(t *testing.T) {
_, err := RunPrecompiledContract(p, in, contract)
if err.Error() != "out of gas" {
t.Errorf("Expected error [out of gas], got [%v]", err)
@ -470,7 +470,7 @@ func benchmarkPrecompiled(addr string, test precompiledTest, bench *testing.B) {
data = make([]byte, len(in))
)
bench.Run(fmt.Sprintf("%s-GasLimit=%d", test.name, contract.Gas), func(bench *testing.B) {
bench.Run(fmt.Sprintf("%s-Gas=%d", test.name, contract.Gas), func(bench *testing.B) {
bench.ResetTimer()
for i := 0; i < bench.N; i++ {
contract.Gas = reqGas

@ -269,7 +269,6 @@ func (e *engineImpl) Finalize(
// Finalize the state root
header.SetRoot(state.IntermediateRoot(chain.Config().IsS3(header.Epoch())))
// TODO: put block version and eth transaction in new block
return types.NewBlock(header, txs, receipts, outcxs, incxs, stks), payout, nil
}

@ -37,11 +37,10 @@ func TestAddNewBlock(t *testing.T) {
nodeconfig.SetNetworkType(nodeconfig.Devnet)
node := New(host, consensus, testDBFactory, nil, nil)
ethTxs := make(map[common.Address]types.InternalTransactions)
txs := make(map[common.Address]types.InternalTransactions)
txs := make(map[common.Address]types.Transactions)
stks := staking.StakingTransactions{}
node.Worker.CommitTransactions(
ethTxs, txs, stks, common.Address{},
txs, stks, common.Address{},
)
commitSigs := make(chan []byte)
go func() {
@ -84,11 +83,10 @@ func TestVerifyNewBlock(t *testing.T) {
archiveMode[1] = false
node := New(host, consensus, testDBFactory, nil, archiveMode)
ethTxs := make(map[common.Address]types.InternalTransactions)
txs := make(map[common.Address]types.InternalTransactions)
txs := make(map[common.Address]types.Transactions)
stks := staking.StakingTransactions{}
node.Worker.CommitTransactions(
ethTxs, txs, stks, common.Address{},
txs, stks, common.Address{},
)
commitSigs := make(chan []byte)
go func() {

@ -168,16 +168,12 @@ func (node *Node) ProposeNewBlock(commitSigs chan []byte) (*types.Block, error)
utils.Logger().Err(err).Msg("Failed to fetch pending transactions")
return nil, err
}
pendingEthTxs := map[common.Address]types.InternalTransactions{}
pendingPlainTxs := map[common.Address]types.InternalTransactions{}
pendingPlainTxs := map[common.Address]types.Transactions{}
pendingStakingTxs := staking.StakingTransactions{}
for addr, poolTxs := range pendingPoolTxs {
plainEthTxsPerAcc := types.InternalTransactions{}
plainTxsPerAcc := types.InternalTransactions{}
plainTxsPerAcc := types.Transactions{}
for _, tx := range poolTxs {
if ethTx, ok := tx.(*types.EthTransaction); ok {
plainEthTxsPerAcc = append(plainEthTxsPerAcc, ethTx)
} else if plainTx, ok := tx.(*types.Transaction); ok {
if plainTx, ok := tx.(*types.Transaction); ok {
plainTxsPerAcc = append(plainTxsPerAcc, plainTx)
} else if stakingTx, ok := tx.(*staking.StakingTransaction); ok {
// Only process staking transactions after pre-staking epoch happened.
@ -190,9 +186,6 @@ func (node *Node) ProposeNewBlock(commitSigs chan []byte) (*types.Block, error)
return nil, types.ErrUnknownPoolTxType
}
}
if plainEthTxsPerAcc.Len() > 0 {
pendingEthTxs[addr] = plainEthTxsPerAcc
}
if plainTxsPerAcc.Len() > 0 {
pendingPlainTxs[addr] = plainTxsPerAcc
}
@ -202,7 +195,7 @@ func (node *Node) ProposeNewBlock(commitSigs chan []byte) (*types.Block, error)
// Try commit normal and staking transactions based on the current state
// The successfully committed transactions will be put in the proposed block
if err := node.Worker.CommitTransactions(
pendingEthTxs, pendingPlainTxs, pendingStakingTxs, beneficiary,
pendingPlainTxs, pendingStakingTxs, beneficiary,
); err != nil {
utils.Logger().Error().Err(err).Msg("cannot commit transactions")
return nil, err

@ -41,11 +41,10 @@ func TestFinalizeNewBlockAsync(t *testing.T) {
node.Worker.UpdateCurrent()
ethTxs := make(map[common.Address]types.InternalTransactions)
txs := make(map[common.Address]types.InternalTransactions)
txs := make(map[common.Address]types.Transactions)
stks := staking.StakingTransactions{}
node.Worker.CommitTransactions(
ethTxs, txs, stks, common.Address{},
txs, stks, common.Address{},
)
commitSigs := make(chan []byte)
go func() {

@ -37,7 +37,6 @@ type environment struct {
state *state.DB // apply state changes here
gasPool *core.GasPool // available gas used to pack transactions
header *block.Header
ethTxs []*types.EthTransaction
txs []*types.Transaction
stakingTxs []*staking.StakingTransaction
receipts []*types.Receipt
@ -91,20 +90,9 @@ func (w *Worker) CommitSortedTransactions(
continue
}
var err error
// Start executing the transaction
switch tx.(type) {
case *types.EthTransaction:
w.current.state.Prepare(tx.Hash(), common.Hash{}, len(w.current.ethTxs))
_, err = w.commitEthTransaction(tx.(*types.EthTransaction), coinbase)
case *types.Transaction:
w.current.state.Prepare(tx.Hash(), common.Hash{}, len(w.current.ethTxs)+len(w.current.txs))
_, err = w.commitHmyTransaction(tx.(*types.Transaction), coinbase)
default:
txs.Shift()
continue
}
w.current.state.Prepare(tx.Hash(), common.Hash{}, len(w.current.txs))
_, err := w.commitTransaction(tx.(*types.Transaction), coinbase)
sender, _ := common2.AddressToBech32(from)
switch err {
@ -138,18 +126,13 @@ func (w *Worker) CommitSortedTransactions(
// CommitTransactions commits transactions for new block.
func (w *Worker) CommitTransactions(
pendingEth map[common.Address]types.InternalTransactions,
pendingNormal map[common.Address]types.InternalTransactions,
pendingNormal map[common.Address]types.Transactions,
pendingStaking staking.StakingTransactions, coinbase common.Address,
) error {
if w.current.gasPool == nil {
w.current.gasPool = new(core.GasPool).AddGas(w.current.header.GasLimit())
}
// ETHEREUM COMPATIBLE TXNS
ethTxns := types.NewTransactionsByPriceAndNonce(w.current.signer, pendingEth)
w.CommitSortedTransactions(ethTxns, coinbase)
// HARMONY TXNS
normalTxns := types.NewTransactionsByPriceAndNonce(w.current.signer, pendingNormal)
w.CommitSortedTransactions(normalTxns, coinbase)
@ -225,45 +208,9 @@ var (
errNilReceipt = errors.New("nil receipt")
)
func (w *Worker) commitEthTransaction(
tx *types.EthTransaction, coinbase common.Address,
) ([]*types.Log, error) {
receipt, cx, err := w.commitTransaction(tx, coinbase)
if err == nil {
w.current.ethTxs = append(w.current.ethTxs, tx)
w.current.receipts = append(w.current.receipts, receipt)
if cx != nil {
w.current.outcxs = append(w.current.outcxs, cx)
}
} else {
return nil, err
}
return receipt.Logs, nil
}
func (w *Worker) commitHmyTransaction(
func (w *Worker) commitTransaction(
tx *types.Transaction, coinbase common.Address,
) ([]*types.Log, error) {
receipt, cx, err := w.commitTransaction(tx, coinbase)
if err == nil {
w.current.txs = append(w.current.txs, tx)
w.current.receipts = append(w.current.receipts, receipt)
if cx != nil {
w.current.outcxs = append(w.current.outcxs, cx)
}
} else {
return nil, err
}
return receipt.Logs, nil
}
func (w *Worker) commitTransaction(
tx types.InternalTransaction, coinbase common.Address,
) (*types.Receipt, *types.CXReceipt, error) {
snap := w.current.state.Snapshot()
gasUsed := w.current.header.GasUsed()
receipt, cx, _, err := core.ApplyTransaction(
@ -283,13 +230,20 @@ func (w *Worker) commitTransaction(
utils.Logger().Error().
Err(err).Interface("txn", tx).
Msg("Transaction failed commitment")
return nil, nil, errNilReceipt
return nil, errNilReceipt
}
if receipt == nil {
utils.Logger().Warn().Interface("tx", tx).Interface("cx", cx).Msg("Receipt is Nil!")
return nil, nil, errNilReceipt
return nil, errNilReceipt
}
return receipt, cx, nil
w.current.txs = append(w.current.txs, tx)
w.current.receipts = append(w.current.receipts, receipt)
if cx != nil {
w.current.outcxs = append(w.current.outcxs, cx)
}
return receipt.Logs, nil
}
// CommitReceipts commits a list of already verified incoming cross shard receipts

@ -79,11 +79,10 @@ func TestCommitTransactions(t *testing.T) {
tx, _ := types.SignTx(types.NewTransaction(baseNonce, testBankAddress, uint32(0), big.NewInt(int64(denominations.One*randAmount)), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey)
// Commit the tx to the worker
txs := make(map[common.Address]types.InternalTransactions)
txs[testBankAddress] = types.InternalTransactions{tx}
ethTxs := make(map[common.Address]types.InternalTransactions)
txs := make(map[common.Address]types.Transactions)
txs[testBankAddress] = types.Transactions{tx}
err := worker.CommitTransactions(
ethTxs, txs, nil, testBankAddress,
txs, nil, testBankAddress,
)
if err != nil {
t.Error(err)

@ -12,7 +12,7 @@ import (
const (
// ExpendGasOperation is an operation that only affects the native currency.
ExpendGasOperation = "GasLimit"
ExpendGasOperation = "Gas"
// ContractCreationOperation is an operation that only affects the native currency.
ContractCreationOperation = "ContractCreation"

@ -21,7 +21,7 @@ func TestUndelegate(t *testing.T) {
amount1 := big.NewInt(1000)
delegation.Undelegate(epoch1, amount1)
// check the undelegation's Value
// check the undelegation's Amount
if delegation.Undelegations[0].Amount.Cmp(amount1) != 0 {
t.Errorf("undelegate failed, amount does not match")
}

@ -38,7 +38,7 @@ var (
chainConfig = params.TestChainConfig
blockFactory = blockfactory.ForTest
// Test transactions
pendingTxs []types.InternalTransaction
pendingTxs []types.PoolTransaction
database = rawdb.NewMemoryDatabase()
gspec = core.Genesis{
Config: chainConfig,
@ -46,7 +46,7 @@ var (
Alloc: core.GenesisAlloc{FaucetAddress: {Balance: FaucetInitFunds}},
ShardID: 0,
}
txs []types.InternalTransaction
txs []*types.Transaction
contractworker *pkgworker.Worker
nonce uint64
dataEnc []byte
@ -119,12 +119,10 @@ func fundFaucetContract(chain *core.BlockChain) {
tx, _ := types.SignTx(types.NewTransaction(nonce+uint64(4), randomUserAddress, 0, big.NewInt(int64(amount)), params.TxGas, nil, nil), types.HomesteadSigner{}, FaucetPriKey)
txs = append(txs, tx)
txmap := make(map[common.Address]types.InternalTransactions)
txmap := make(map[common.Address]types.Transactions)
txmap[FaucetAddress] = txs
ethTxmap := make(map[common.Address]types.InternalTransactions)
err := contractworker.CommitTransactions(
ethTxmap, txmap, nil, testUserAddress,
txmap, nil, testUserAddress,
)
if err != nil {
fmt.Println(err)
@ -166,12 +164,11 @@ func callFaucetContractToFundAnAddress(chain *core.BlockChain) {
callEnc = append(callEnc, paddedAddress...)
callfaucettx, _ := types.SignTx(types.NewTransaction(nonce+uint64(5), faucetContractAddress, 0, big.NewInt(0), params.TxGasContractCreation*10, nil, callEnc), types.HomesteadSigner{}, FaucetPriKey)
txmap := make(map[common.Address]types.InternalTransactions)
txmap[FaucetAddress] = types.InternalTransactions{callfaucettx}
txmap := make(map[common.Address]types.Transactions)
txmap[FaucetAddress] = types.Transactions{callfaucettx}
ethTxmap := make(map[common.Address]types.InternalTransactions)
err = contractworker.CommitTransactions(
ethTxmap, txmap, nil, testUserAddress,
txmap, nil, testUserAddress,
)
if err != nil {
fmt.Println(err)

Loading…
Cancel
Save