Skip to content

Commit

Permalink
v0.1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
Dharitri-org committed Mar 22, 2024
1 parent 734a9fe commit 32594ea
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 161 deletions.
45 changes: 39 additions & 6 deletions builtInFunctions/changeOwnerAddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"sync"

"github.com/Dharitri-org/drtg-core/core"
"github.com/Dharitri-org/drtg-core/core/check"
vmcommon "github.com/Dharitri-org/drtg-vm-common"
)
Expand All @@ -13,11 +14,20 @@ type changeOwnerAddress struct {
baseAlwaysActiveHandler
gasCost uint64
mutExecution sync.RWMutex

enableEpochsHandler vmcommon.EnableEpochsHandler
}

// NewChangeOwnerAddressFunc create a new change owner built in function
func NewChangeOwnerAddressFunc(gasCost uint64) *changeOwnerAddress {
return &changeOwnerAddress{gasCost: gasCost}
// NewChangeOwnerAddressFunc create a new change owner built-in function
func NewChangeOwnerAddressFunc(gasCost uint64, enableEpochsHandler vmcommon.EnableEpochsHandler) (*changeOwnerAddress, error) {
if check.IfNil(enableEpochsHandler) {
return nil, ErrNilEnableEpochsHandler
}

return &changeOwnerAddress{
gasCost: gasCost,
enableEpochsHandler: enableEpochsHandler,
}, nil
}

// SetNewGasConfig is called whenever gas cost is changed
Expand Down Expand Up @@ -55,9 +65,12 @@ func (c *changeOwnerAddress) ProcessBuiltinFunction(
return nil, ErrNotEnoughGas
}
gasRemaining := computeGasRemaining(acntSnd, vmInput.GasProvided, c.gasCost)

vmOutput := &vmcommon.VMOutput{ReturnCode: vmcommon.Ok, GasRemaining: gasRemaining}

if check.IfNil(acntDst) {
// cross-shard call, in sender shard only the gas is taken out
return &vmcommon.VMOutput{ReturnCode: vmcommon.Ok, GasRemaining: gasRemaining}, nil
c.addOutputTransferToVmOutputForCallThroughSC(acntDst, vmInput, vmOutput)
return vmOutput, nil
}

if !bytes.Equal(vmInput.CallerAddr, acntDst.GetOwnerAddress()) {
Expand All @@ -69,7 +82,6 @@ func (c *changeOwnerAddress) ProcessBuiltinFunction(
return nil, err
}

vmOutput := &vmcommon.VMOutput{GasRemaining: gasRemaining, ReturnCode: vmcommon.Ok}
logEntry := &vmcommon.LogEntry{
Identifier: []byte(vmInput.Function),
Address: vmInput.RecipientAddr,
Expand All @@ -81,6 +93,27 @@ func (c *changeOwnerAddress) ProcessBuiltinFunction(
return vmOutput, nil
}

func (c *changeOwnerAddress) addOutputTransferToVmOutputForCallThroughSC(acntDst vmcommon.UserAccountHandler, vmInput *vmcommon.ContractCallInput, vmOutput *vmcommon.VMOutput) {
if !c.enableEpochsHandler.IsChangeOwnerAddressCrossShardThroughSCEnabled() {
return
}

isCrossShardCallThroughASmartContract := check.IfNil(acntDst) && vmcommon.IsSmartContractAddress(vmInput.CallerAddr)
if !isCrossShardCallThroughASmartContract {
return
}

addOutputTransferToVMOutput(
1,
vmInput.CallerAddr,
core.BuiltInFunctionChangeOwnerAddress,
vmInput.Arguments,
vmInput.RecipientAddr,
vmInput.GasLocked,
vmInput.CallType,
vmOutput)
}

func computeGasRemaining(snd vmcommon.UserAccountHandler, gasProvided uint64, gasToUse uint64) uint64 {
if gasProvided < gasToUse {
return 0
Expand Down
54 changes: 51 additions & 3 deletions builtInFunctions/changeOwnerAddress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,27 @@ import (

"github.com/Dharitri-org/drtg-core/core"
"github.com/Dharitri-org/drtg-core/core/check"
"github.com/Dharitri-org/drtg-core/data/vm"
vmcommon "github.com/Dharitri-org/drtg-vm-common"
"github.com/Dharitri-org/drtg-vm-common/mock"
"github.com/stretchr/testify/require"
)

func TestNewChangeOwnerNilEnableEpochsHandler(t *testing.T) {
t.Parallel()

gasCost := uint64(100)
coa, err := NewChangeOwnerAddressFunc(gasCost, nil)
require.Nil(t, coa)
require.Equal(t, ErrNilEnableEpochsHandler, err)
}

func TestNewChangeOwnerAddressFunc(t *testing.T) {
t.Parallel()

gasCost := uint64(100)
coa := NewChangeOwnerAddressFunc(gasCost)
coa, err := NewChangeOwnerAddressFunc(gasCost, &mock.EnableEpochsHandlerStub{})
require.Nil(t, err)
require.False(t, check.IfNil(coa))
require.Equal(t, gasCost, coa.gasCost)
require.True(t, coa.IsActive())
Expand All @@ -24,7 +35,7 @@ func TestNewChangeOwnerAddressFunc(t *testing.T) {
func TestChangeOwnerAddress_SetNewGasConfig(t *testing.T) {
t.Parallel()

coa := NewChangeOwnerAddressFunc(100)
coa, _ := NewChangeOwnerAddressFunc(100, &mock.EnableEpochsHandlerStub{})

newCost := uint64(37)
expectedGasConfig := &vmcommon.GasCost{BuiltInCost: vmcommon.BuiltInCost{ChangeOwnerAddress: newCost}}
Expand All @@ -36,7 +47,9 @@ func TestChangeOwnerAddress_SetNewGasConfig(t *testing.T) {
func TestChangeOwnerAddress_ProcessBuiltinFunction(t *testing.T) {
t.Parallel()

coa := changeOwnerAddress{}
coa := changeOwnerAddress{
enableEpochsHandler: &mock.EnableEpochsHandlerStub{},
}

owner := []byte("send")
addr := []byte("addr")
Expand Down Expand Up @@ -80,3 +93,38 @@ func TestChangeOwnerAddress_ProcessBuiltinFunction(t *testing.T) {
Topics: [][]byte{newAddr},
}, vmOutput.Logs[0])
}

func TestProcessBuiltInFunctionCallThroughSC(t *testing.T) {
t.Parallel()

coa := changeOwnerAddress{
enableEpochsHandler: &mock.EnableEpochsHandlerStub{
IsChangeOwnerAddressCrossShardThroughSCEnabledField: true,
},
}

owner := []byte("00000000000")
addr := []byte("addraddradd")
rcvAddr := []byte("contract")

acc := mock.NewUserAccount(addr)
acc.OwnerAddress = owner
vmInput := &vmcommon.ContractCallInput{
Function: core.BuiltInFunctionChangeOwnerAddress,
RecipientAddr: rcvAddr,
VMInput: vmcommon.VMInput{
CallerAddr: make([]byte, 11),
CallValue: big.NewInt(0),
Arguments: [][]byte{[]byte("00000000000")},
},
}

vmOutput, err := coa.ProcessBuiltinFunction(acc, nil, vmInput)
require.Nil(t, err)
require.NotNil(t, vmOutput)
require.Equal(t, 1, len(vmOutput.OutputAccounts))

outputTransfer := vmOutput.OutputAccounts[string(rcvAddr)].OutputTransfers[0]
require.Equal(t, []byte("ChangeOwnerAddress@3030303030303030303030"), outputTransfer.Data)
require.Equal(t, vm.DirectCall, outputTransfer.CallType)
}
6 changes: 5 additions & 1 deletion builtInFunctions/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ func (b *builtInFuncCreator) CreateBuiltInFunctionContainer() error {
return err
}

newFunc = NewChangeOwnerAddressFunc(b.gasConfig.BuiltInCost.ChangeOwnerAddress)
newFunc, err = NewChangeOwnerAddressFunc(b.gasConfig.BuiltInCost.ChangeOwnerAddress, b.enableEpochsHandler)
if err != nil {
return err
}

err = b.builtInFunctions.Add(core.BuiltInFunctionChangeOwnerAddress, newFunc)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 32594ea

Please sign in to comment.