forked from multiversx/mx-chain-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
started integrating system VM to metachain.
- Loading branch information
1 parent
aca677a
commit 54aa2a3
Showing
5 changed files
with
251 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package transaction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.