Skip to content

Commit

Permalink
fixing code review
Browse files Browse the repository at this point in the history
  • Loading branch information
laudiacay committed Nov 24, 2021
1 parent a31e6b9 commit b1c4090
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 21 deletions.
2 changes: 1 addition & 1 deletion chain/vm/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func (rt *Runtime) DeleteActor(beneficiary address.Address) {
}

// Transfer the executing actor's balance to the beneficiary
if err := rt.vm.transfer(rt.Receiver(), beneficiary, act.Balance, rt.vm.ntwkVersion(rt.ctx, rt.vm.blockHeight)); err != nil {
if err := rt.vm.transfer(rt.Receiver(), beneficiary, act.Balance, rt.NetworkVersion()); err != nil {
panic(aerrors.Fatalf("failed to transfer balance to beneficiary actor: %s", err))
}
}
Expand Down
18 changes: 12 additions & 6 deletions chain/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,14 @@ func (vm *VM) transfer(from, to address.Address, amt types.BigInt, networkVersio
var fromID, toID address.Address
var err error
// switching the order around so that transactions for more than the balance sent to self fail
fmt.Printf("network version! %s\n", networkVersion)
if networkVersion >= network.Version15 {
fmt.Println("network version high")

if amt.LessThan(types.NewInt(0)) {
return aerrors.Newf(exitcode.SysErrForbidden, "attempted to transfer negative value: %s", amt)
}

fromID, err = vm.cstate.LookupID(from)
if err != nil {
return aerrors.Fatalf("transfer failed when resolving sender address: %s", err)
Expand All @@ -890,6 +897,7 @@ func (vm *VM) transfer(from, to address.Address, amt types.BigInt, networkVersio
}

if from == to {
log.Infow("sending to same address: noop", "from/to addr", from)
return nil
}

Expand All @@ -899,13 +907,11 @@ func (vm *VM) transfer(from, to address.Address, amt types.BigInt, networkVersio
}

if fromID == toID {
log.Infow("sending to same actor ID: noop", "from/to actor", fromID)
return nil
}

if amt.LessThan(types.NewInt(0)) {
return aerrors.Newf(exitcode.SysErrForbidden, "attempted to transfer negative value: %s", amt)
}
} else {
fmt.Println("network version low")
if from == to {
return nil
}
Expand Down Expand Up @@ -945,11 +951,11 @@ func (vm *VM) transfer(from, to address.Address, amt types.BigInt, networkVersio
depositFunds(t, amt)

if err = vm.cstate.SetActor(fromID, f); err != nil {
return aerrors.Fatalf("transfer failed when setting receiver actor: %s", err)
return aerrors.Fatalf("transfer failed when setting sender actor: %s", err)
}

if err = vm.cstate.SetActor(toID, t); err != nil {
return aerrors.Fatalf("transfer failed when setting sender actor: %s", err)
return aerrors.Fatalf("transfer failed when setting receiver actor: %s", err)
}

return nil
Expand Down
68 changes: 54 additions & 14 deletions itests/self_sent_txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,84 @@ import (
"github.com/stretchr/testify/require"
)

func TestSelfSentTxn(t *testing.T) {
func TestSelfSentTxnV15(t *testing.T) {
ctx := context.Background()

kit.QuietMiningLogs()

client, _, ens := kit.EnsembleMinimal(t, kit.MockProofs(), kit.GenesisNetworkVersion(network.Version15))
client15, _, ens := kit.EnsembleMinimal(t, kit.MockProofs(), kit.GenesisNetworkVersion(network.Version15))
ens.InterconnectAll().BeginMining(10 * time.Millisecond)

bal, err := client.WalletBalance(ctx, client.DefaultKey.Address)
bal, err := client15.WalletBalance(ctx, client15.DefaultKey.Address)
require.NoError(t, err)

// send self half of account balance
msgExactlyBal := &types.Message{
From: client.DefaultKey.Address,
To: client.DefaultKey.Address,
msgHalfBal := &types.Message{
From: client15.DefaultKey.Address,
To: client15.DefaultKey.Address,
Value: big.Div(bal, big.NewInt(2)),
}
smExactlyBal, err := client.MpoolPushMessage(ctx, msgExactlyBal, nil)
smHalfBal, err := client15.MpoolPushMessage(ctx, msgHalfBal, nil)
require.NoError(t, err)
mLookup, err := client.StateWaitMsg(ctx, smExactlyBal.Cid(), 3, api.LookbackNoLimit, true)
mLookup, err := client15.StateWaitMsg(ctx, smHalfBal.Cid(), 3, api.LookbackNoLimit, true)
require.NoError(t, err)
require.Equal(t, exitcode.Ok, mLookup.Receipt.ExitCode)

msgOverBal := &types.Message{
From: client.DefaultKey.Address,
To: client.DefaultKey.Address,
From: client15.DefaultKey.Address,
To: client15.DefaultKey.Address,
Value: big.Mul(big.NewInt(2), bal),
GasLimit: 10000000000,
GasPremium: big.NewInt(10000000000),
GasFeeCap: big.NewInt(100000000000),
Nonce: 1,
}
smOverBal, err := client.WalletSignMessage(ctx, client.DefaultKey.Address, msgOverBal)
smOverBal, err := client15.WalletSignMessage(ctx, client15.DefaultKey.Address, msgOverBal)
require.NoError(t, err)
smcid, err := client.MpoolPush(ctx, smOverBal)
smcid, err := client15.MpoolPush(ctx, smOverBal)
require.NoError(t, err)

mLookup, err = client.StateWaitMsg(ctx, smcid, 3, api.LookbackNoLimit, true)
mLookup, err = client15.StateWaitMsg(ctx, smcid, 3, api.LookbackNoLimit, true)
require.NoError(t, err)
require.Equal(t, exitcode.SysErrInsufficientFunds, mLookup.Receipt.ExitCode)
}

func TestSelfSentTxnV14(t *testing.T) {
ctx := context.Background()

kit.QuietMiningLogs()

client14, _, ens := kit.EnsembleMinimal(t, kit.MockProofs(), kit.GenesisNetworkVersion(network.Version14))
ens.InterconnectAll().BeginMining(10 * time.Millisecond)

bal, err := client14.WalletBalance(ctx, client14.DefaultKey.Address)
require.NoError(t, err)

// send self half of account balance
msgHalfBal := &types.Message{
From: client14.DefaultKey.Address,
To: client14.DefaultKey.Address,
Value: big.Div(bal, big.NewInt(2)),
}
smHalfBal, err := client14.MpoolPushMessage(ctx, msgHalfBal, nil)
require.NoError(t, err)
mLookup, err := client14.StateWaitMsg(ctx, smHalfBal.Cid(), 3, api.LookbackNoLimit, true)
require.NoError(t, err)
require.Equal(t, exitcode.Ok, mLookup.Receipt.ExitCode)

msgOverBal := &types.Message{
From: client14.DefaultKey.Address,
To: client14.DefaultKey.Address,
Value: big.Mul(big.NewInt(2), bal),
GasLimit: 10000000000,
GasPremium: big.NewInt(10000000000),
GasFeeCap: big.NewInt(100000000000),
Nonce: 1,
}
smOverBal, err := client14.WalletSignMessage(ctx, client14.DefaultKey.Address, msgOverBal)
require.NoError(t, err)
smcid, err := client14.MpoolPush(ctx, smOverBal)
require.NoError(t, err)
mLookup, err = client14.StateWaitMsg(ctx, smcid, 3, api.LookbackNoLimit, true)
require.NoError(t, err)
require.Equal(t, exitcode.Ok, mLookup.Receipt.ExitCode)
}

0 comments on commit b1c4090

Please sign in to comment.