Skip to content

Commit

Permalink
started integrating system VM to metachain.
Browse files Browse the repository at this point in the history
  • Loading branch information
sasurobert committed Oct 14, 2019
1 parent aca677a commit 54aa2a3
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 107 deletions.
110 changes: 110 additions & 0 deletions process/transaction/baseProcess.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package transaction

import (
"bytes"

"github.com/ElrondNetwork/elrond-go/data/state"
"github.com/ElrondNetwork/elrond-go/data/transaction"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/sharding"
)

type baseTxProcessor struct {
accounts state.AccountsAdapter
shardCoordinator sharding.Coordinator
adrConv state.AddressConverter
}

func (txProc *baseTxProcessor) getAccounts(
adrSrc, adrDst state.AddressContainer,
) (*state.Account, *state.Account, error) {

var acntSrc, acntDst *state.Account

shardForCurrentNode := txProc.shardCoordinator.SelfId()
shardForSrc := txProc.shardCoordinator.ComputeId(adrSrc)
shardForDst := txProc.shardCoordinator.ComputeId(adrDst)

srcInShard := shardForSrc == shardForCurrentNode
dstInShard := shardForDst == shardForCurrentNode

if srcInShard && adrSrc == nil ||
dstInShard && adrDst == nil {
return nil, nil, process.ErrNilAddressContainer
}

if bytes.Equal(adrSrc.Bytes(), adrDst.Bytes()) {
acntWrp, err := txProc.accounts.GetAccountWithJournal(adrSrc)
if err != nil {
return nil, nil, err
}

account, ok := acntWrp.(*state.Account)
if !ok {
return nil, nil, process.ErrWrongTypeAssertion
}

return account, account, nil
}

if srcInShard {
acntSrcWrp, err := txProc.accounts.GetAccountWithJournal(adrSrc)
if err != nil {
return nil, nil, err
}

account, ok := acntSrcWrp.(*state.Account)
if !ok {
return nil, nil, process.ErrWrongTypeAssertion
}

acntSrc = account
}

if dstInShard {
acntDstWrp, err := txProc.accounts.GetAccountWithJournal(adrDst)
if err != nil {
return nil, nil, err
}

account, ok := acntDstWrp.(*state.Account)
if !ok {
return nil, nil, process.ErrWrongTypeAssertion
}

acntDst = account
}

return acntSrc, acntDst, nil
}

func (txProc *baseTxProcessor) getAccountFromAddress(adrSrc state.AddressContainer) (state.AccountHandler, error) {
shardForCurrentNode := txProc.shardCoordinator.SelfId()
shardForSrc := txProc.shardCoordinator.ComputeId(adrSrc)
if shardForCurrentNode != shardForSrc {
return nil, nil
}

acnt, err := txProc.accounts.GetAccountWithJournal(adrSrc)
if err != nil {
return nil, err
}

return acnt, nil
}

func (txProc *baseTxProcessor) getAddresses(
tx *transaction.Transaction,
) (state.AddressContainer, state.AddressContainer, error) {
adrSrc, err := txProc.adrConv.CreateAddressFromPublicKeyBytes(tx.SndAddr)
if err != nil {
return nil, nil, err
}

adrDst, err := txProc.adrConv.CreateAddressFromPublicKeyBytes(tx.RcvAddr)
if err != nil {
return nil, nil, err
}

return adrSrc, adrDst, nil
}
125 changes: 125 additions & 0 deletions process/transaction/metaProcess.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package transaction

import (
"github.com/ElrondNetwork/elrond-go/data/state"
"github.com/ElrondNetwork/elrond-go/data/transaction"
"github.com/ElrondNetwork/elrond-go/hashing"
"github.com/ElrondNetwork/elrond-go/marshal"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/sharding"
)

// txProcessor implements TransactionProcessor interface and can modify account states according to a transaction
type metaTxProcessor struct {
*baseTxProcessor
txTypeHandler process.TxTypeHandler
scProcessor process.SmartContractProcessor
}

// NewMetaTxProcessor creates a new txProcessor engine
func NewMetaTxProcessor(
accounts state.AccountsAdapter,
hasher hashing.Hasher,
addressConv state.AddressConverter,
marshalizer marshal.Marshalizer,
shardCoordinator sharding.Coordinator,
scProcessor process.SmartContractProcessor,
txFeeHandler process.TransactionFeeHandler,
txTypeHandler process.TxTypeHandler,
economicsFee process.FeeHandler,
) (*metaTxProcessor, error) {

if accounts == nil || accounts.IsInterfaceNil() {
return nil, process.ErrNilAccountsAdapter
}
if addressConv == nil || addressConv.IsInterfaceNil() {
return nil, process.ErrNilAddressConverter
}
if shardCoordinator == nil || shardCoordinator.IsInterfaceNil() {
return nil, process.ErrNilShardCoordinator
}
if scProcessor == nil || scProcessor.IsInterfaceNil() {
return nil, process.ErrNilSmartContractProcessor
}
if txTypeHandler == nil || txTypeHandler.IsInterfaceNil() {
return nil, process.ErrNilTxTypeHandler
}

baseTxProcess := &baseTxProcessor{
accounts: accounts,
shardCoordinator: shardCoordinator,
adrConv: addressConv,
}

return &metaTxProcessor{
baseTxProcessor: baseTxProcess,
scProcessor: scProcessor,
txTypeHandler: txTypeHandler,
}, nil
}

// ProcessTransaction modifies the account states in respect with the transaction data
func (txProc *metaTxProcessor) ProcessTransaction(tx *transaction.Transaction, roundIndex uint64) error {
if tx == nil || tx.IsInterfaceNil() {
return process.ErrNilTransaction
}

adrSrc, adrDst, err := txProc.getAddresses(tx)
if err != nil {
return err
}

txType, err := txProc.txTypeHandler.ComputeTransactionType(tx)
if err != nil {
return err
}

switch txType {
case process.SCDeployment:
return txProc.processSCDeployment(tx, adrSrc, roundIndex)
case process.SCInvoking:
return txProc.processSCInvoking(tx, adrSrc, adrDst, roundIndex)
}

return process.ErrWrongTransaction
}

func (txProc *metaTxProcessor) processSCDeployment(
tx *transaction.Transaction,
adrSrc state.AddressContainer,
roundIndex uint64,
) error {
// getAccounts returns acntSrc not nil if the adrSrc is in the node shard, the same, acntDst will be not nil
// if adrDst is in the node shard. If an error occurs it will be signaled in err variable.
acntSrc, err := txProc.getAccountFromAddress(adrSrc)
if err != nil {
return err
}

err = txProc.scProcessor.DeploySmartContract(tx, acntSrc, roundIndex)
return err
}

func (txProc *metaTxProcessor) processSCInvoking(
tx *transaction.Transaction,
adrSrc, adrDst state.AddressContainer,
roundIndex uint64,
) error {
// getAccounts returns acntSrc not nil if the adrSrc is in the node shard, the same, acntDst will be not nil
// if adrDst is in the node shard. If an error occurs it will be signaled in err variable.
acntSrc, acntDst, err := txProc.getAccounts(adrSrc, adrDst)
if err != nil {
return err
}

err = txProc.scProcessor.ExecuteSmartContractTransaction(tx, acntSrc, acntDst, roundIndex)
return err
}

// IsInterfaceNil returns true if there is no value under the interface
func (txProc *metaTxProcessor) IsInterfaceNil() bool {
if txProc == nil {
return true
}
return false
}
1 change: 1 addition & 0 deletions process/transaction/metaProcess_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package transaction
122 changes: 15 additions & 107 deletions process/transaction/process.go → process/transaction/shardProcess.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package transaction

import (
"bytes"
"math/big"
"sync"

Expand All @@ -18,14 +17,13 @@ var log = logger.DefaultLogger()

// txProcessor implements TransactionProcessor interface and can modify account states according to a transaction
type txProcessor struct {
accounts state.AccountsAdapter
adrConv state.AddressConverter
*baseTxProcessor
hasher hashing.Hasher
scProcessor process.SmartContractProcessor
marshalizer marshal.Marshalizer
txFeeHandler process.TransactionFeeHandler
shardCoordinator sharding.Coordinator
txTypeHandler process.TxTypeHandler
shardCoordinator sharding.Coordinator
economicsFee process.FeeHandler
mutTxFee sync.RWMutex
}
Expand Down Expand Up @@ -71,17 +69,21 @@ func NewTxProcessor(
return nil, process.ErrNilEconomicsFeeHandler
}

return &txProcessor{
baseTxProcess := &baseTxProcessor{
accounts: accounts,
hasher: hasher,
adrConv: addressConv,
marshalizer: marshalizer,
shardCoordinator: shardCoordinator,
scProcessor: scProcessor,
txFeeHandler: txFeeHandler,
txTypeHandler: txTypeHandler,
economicsFee: economicsFee,
mutTxFee: sync.RWMutex{},
adrConv: addressConv,
}

return &txProcessor{
baseTxProcessor: baseTxProcess,
hasher: hasher,
marshalizer: marshalizer,
scProcessor: scProcessor,
txFeeHandler: txFeeHandler,
txTypeHandler: txTypeHandler,
economicsFee: economicsFee,
mutTxFee: sync.RWMutex{},
}, nil
}

Expand Down Expand Up @@ -227,100 +229,6 @@ func (txProc *txProcessor) processSCInvoking(
return err
}

func (txProc *txProcessor) getAddresses(
tx *transaction.Transaction,
) (state.AddressContainer, state.AddressContainer, error) {
adrSrc, err := txProc.adrConv.CreateAddressFromPublicKeyBytes(tx.SndAddr)
if err != nil {
return nil, nil, err
}

adrDst, err := txProc.adrConv.CreateAddressFromPublicKeyBytes(tx.RcvAddr)
if err != nil {
return nil, nil, err
}

return adrSrc, adrDst, nil
}

func (txProc *txProcessor) getAccounts(
adrSrc, adrDst state.AddressContainer,
) (*state.Account, *state.Account, error) {

var acntSrc, acntDst *state.Account

shardForCurrentNode := txProc.shardCoordinator.SelfId()
shardForSrc := txProc.shardCoordinator.ComputeId(adrSrc)
shardForDst := txProc.shardCoordinator.ComputeId(adrDst)

srcInShard := shardForSrc == shardForCurrentNode
dstInShard := shardForDst == shardForCurrentNode

if srcInShard && adrSrc == nil ||
dstInShard && adrDst == nil {
return nil, nil, process.ErrNilAddressContainer
}

if bytes.Equal(adrSrc.Bytes(), adrDst.Bytes()) {
acntWrp, err := txProc.accounts.GetAccountWithJournal(adrSrc)
if err != nil {
return nil, nil, err
}

account, ok := acntWrp.(*state.Account)
if !ok {
return nil, nil, process.ErrWrongTypeAssertion
}

return account, account, nil
}

if srcInShard {
acntSrcWrp, err := txProc.accounts.GetAccountWithJournal(adrSrc)
if err != nil {
return nil, nil, err
}

account, ok := acntSrcWrp.(*state.Account)
if !ok {
return nil, nil, process.ErrWrongTypeAssertion
}

acntSrc = account
}

if dstInShard {
acntDstWrp, err := txProc.accounts.GetAccountWithJournal(adrDst)
if err != nil {
return nil, nil, err
}

account, ok := acntDstWrp.(*state.Account)
if !ok {
return nil, nil, process.ErrWrongTypeAssertion
}

acntDst = account
}

return acntSrc, acntDst, nil
}

func (txProc *txProcessor) getAccountFromAddress(adrSrc state.AddressContainer) (state.AccountHandler, error) {
shardForCurrentNode := txProc.shardCoordinator.SelfId()
shardForSrc := txProc.shardCoordinator.ComputeId(adrSrc)
if shardForCurrentNode != shardForSrc {
return nil, nil
}

acnt, err := txProc.accounts.GetAccountWithJournal(adrSrc)
if err != nil {
return nil, err
}

return acnt, nil
}

func (txProc *txProcessor) checkTxValues(tx *transaction.Transaction, acntSnd state.AccountHandler) error {
if acntSnd == nil || acntSnd.IsInterfaceNil() {
// transaction was already done at sender shard
Expand Down
File renamed without changes.

0 comments on commit 54aa2a3

Please sign in to comment.