Skip to content

Commit

Permalink
[core] fix: block multiple cx xfers per tx
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxMustermann2 committed May 31, 2022
1 parent 96e075b commit c9768c9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
26 changes: 16 additions & 10 deletions core/vm/contracts_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,22 @@ func (c *crossShardXferPrecompile) RunWriteCapable(
// now do the actual transfer
// step 1 -> remove funds from the precompile address
evm.Transfer(evm.StateDB, contract.Address(), toAddress, value, types.SubtractionOnly)
// step 2 -> make a cross link
// note that the transaction hash is added by state_processor.go to this receipt
// and that the receiving shard does not care about the `From` but we use the original
// instead of the precompile address for consistency
evm.CXReceipt = &types.CXReceipt{
From: fromAddress,
To: &toAddress,
ShardID: fromShardID,
ToShardID: toShardID,
Amount: value,
// make sure that cxreceipt is already nil to prevent multiple calls to the precompile
// in the same transaction
if evm.CXReceipt == nil {
// step 2 -> make a cross link
// note that the transaction hash is added by state_processor.go to this receipt
// and that the receiving shard does not care about the `From` but we use the original
// instead of the precompile address for consistency
evm.CXReceipt = &types.CXReceipt{
From: fromAddress,
To: &toAddress,
ShardID: fromShardID,
ToShardID: toShardID,
Amount: value,
}
} else {
return nil, errors.New("cannot call cross shard precompile again in same tx")
}
return nil, nil
}
Expand Down
3 changes: 1 addition & 2 deletions core/vm/contracts_write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package vm
import (
"bytes"
"errors"
"fmt"
"math/big"
"testing"

Expand Down Expand Up @@ -66,7 +65,7 @@ func CalculateMigrationGasFn() CalculateMigrationGasFunc {
}

func testWriteCapablePrecompile(test writeCapablePrecompileTest, t *testing.T, env *EVM, p WriteCapablePrecompiledContract) {
t.Run(fmt.Sprintf("%s", test.name), func(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
contract := NewContract(AccountRef(common.HexToAddress("1337")), AccountRef(common.HexToAddress("1338")), test.value, 0)
gas, err := p.RequiredGas(env, contract, test.input)
if err != nil {
Expand Down

0 comments on commit c9768c9

Please sign in to comment.