Skip to content

Commit

Permalink
Merge pull request #5468 from multiversx/extend-sc-deploy-and-upgrade…
Browse files Browse the repository at this point in the history
…-log

codeHash in scdeploy and upgrade log
  • Loading branch information
miiu96 authored Aug 21, 2023
2 parents f7045ec + d864dde commit 8606916
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 34 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ require (
github.com/multiversx/mx-chain-communication-go v1.0.6
github.com/multiversx/mx-chain-core-go v1.2.13
github.com/multiversx/mx-chain-crypto-go v1.2.8
github.com/multiversx/mx-chain-es-indexer-go v1.4.9
github.com/multiversx/mx-chain-es-indexer-go v1.4.10
github.com/multiversx/mx-chain-logger-go v1.0.13
github.com/multiversx/mx-chain-scenario-go v1.2.1
github.com/multiversx/mx-chain-storage-go v1.0.12
github.com/multiversx/mx-chain-vm-common-go v1.5.3
github.com/multiversx/mx-chain-vm-common-go v1.5.4
github.com/multiversx/mx-chain-vm-go v1.5.6
github.com/multiversx/mx-chain-vm-v1_2-go v1.2.60
github.com/multiversx/mx-chain-vm-v1_3-go v1.3.61
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -382,16 +382,16 @@ github.com/multiversx/mx-chain-core-go v1.2.13 h1:4Svi23hdsoibStFXv0i7lbBWus3kDJ
github.com/multiversx/mx-chain-core-go v1.2.13/go.mod h1:BILOGHUOIG5dNNX8cgkzCNfDaVtoYrJRYcPnpxRMH84=
github.com/multiversx/mx-chain-crypto-go v1.2.8 h1:wOgVlUaO5X4L8iEbFjcQcL8SZvv6WZ7LqH73BiRPhxU=
github.com/multiversx/mx-chain-crypto-go v1.2.8/go.mod h1:fkaWKp1rbQN9wPKya5jeoRyC+c/SyN/NfggreyeBw+8=
github.com/multiversx/mx-chain-es-indexer-go v1.4.9 h1:rL+8P+X279/9xY8a6t1ynWhhOzJya0LmBAm5ZNCosik=
github.com/multiversx/mx-chain-es-indexer-go v1.4.9/go.mod h1:rgsFY2RwaH5Slud0i6zJbxidvf1y8LE+YOXnnny0O/c=
github.com/multiversx/mx-chain-es-indexer-go v1.4.10 h1:qwYWQ92PuaKhrhZEHm+blEeYSvrxtT1rG8sAL4OEzqc=
github.com/multiversx/mx-chain-es-indexer-go v1.4.10/go.mod h1:rgsFY2RwaH5Slud0i6zJbxidvf1y8LE+YOXnnny0O/c=
github.com/multiversx/mx-chain-logger-go v1.0.13 h1:eru/TETo0MkO4ZTnXsQDKf4PBRpAXmqjT02klNT/JnY=
github.com/multiversx/mx-chain-logger-go v1.0.13/go.mod h1:MZJhTAtZTJxT+yK2EHc4ZW3YOHUc1UdjCD0iahRNBZk=
github.com/multiversx/mx-chain-scenario-go v1.2.1 h1:9eC6VcOEAKRRKZ7EbSWPLzCdNIMWwuNBtAZlgR4cSMA=
github.com/multiversx/mx-chain-scenario-go v1.2.1/go.mod h1:EuZY7DpNFHVNSxJR8dKE1z2I8gBYfEFFPSwNUOXptqE=
github.com/multiversx/mx-chain-storage-go v1.0.12 h1:FrkgHPV38BO8cwdK0GUYHxVkUyOhyBBIhPz0P1e72NA=
github.com/multiversx/mx-chain-storage-go v1.0.12/go.mod h1:/8VrMbO9CbIi0Ym3F1QPY6EENGcUq5DpEBPDt4evn9Q=
github.com/multiversx/mx-chain-vm-common-go v1.5.3 h1:u4ttQc+1bj92SoGeRk3EYc8JvOMDPIGyrQuQGM+YYpU=
github.com/multiversx/mx-chain-vm-common-go v1.5.3/go.mod h1:sqkKMCnwkWl8DURdb9q7pctK8IANghdHY1KJLE0ox2c=
github.com/multiversx/mx-chain-vm-common-go v1.5.4 h1:IWrRQxrNuE/jX12kUz0qaKL5mqA7O+ZcpisiCc9MAr4=
github.com/multiversx/mx-chain-vm-common-go v1.5.4/go.mod h1:sqkKMCnwkWl8DURdb9q7pctK8IANghdHY1KJLE0ox2c=
github.com/multiversx/mx-chain-vm-go v1.5.6 h1:IOwq/L4dBDBjXTIAR9hHCS/D4fn2xMkkINimHWsYiUQ=
github.com/multiversx/mx-chain-vm-go v1.5.6/go.mod h1:WCYCljlHi2UQWHQAlSw1LzDsiMlckoMzvHL2fA5VKU4=
github.com/multiversx/mx-chain-vm-v1_2-go v1.2.60 h1:YYElal1woAT3gwcVGe2JUEqBlzMXt963BSdQHuJpTMk=
Expand Down
40 changes: 28 additions & 12 deletions process/smartContract/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -2563,7 +2563,7 @@ func (sc *scProcessor) processSCOutputAccounts(
log.Trace("storeUpdate", "acc", outAcc.Address, "key", storeUpdate.Offset, "data", storeUpdate.Data)
}

err = sc.updateSmartContractCode(vmOutput, acc, outAcc)
logEntry, err := sc.updateSmartContractCode(vmOutput, acc, outAcc)
if err != nil {
return false, nil, err
}
Expand All @@ -2584,6 +2584,7 @@ func (sc *scProcessor) processSCOutputAccounts(
if err != nil {
return false, nil, err
}
addCodeHashInLogEntryTopics(logEntry, acc.GetCodeHash())

continue
}
Expand All @@ -2599,6 +2600,8 @@ func (sc *scProcessor) processSCOutputAccounts(
if err != nil {
return false, nil, err
}

addCodeHashInLogEntryTopics(logEntry, acc.GetCodeHash())
}

if sumOfAllDiff.Cmp(zero) != 0 {
Expand All @@ -2608,36 +2611,49 @@ func (sc *scProcessor) processSCOutputAccounts(
return createdAsyncCallback, scResults, nil
}

func addCodeHashInLogEntryTopics(entry *vmcommon.LogEntry, codeHash []byte) {
if entry == nil {
return
}
isExpectedIdentifier := string(entry.Identifier) == core.SCUpgradeIdentifier || string(entry.Identifier) == core.SCDeployIdentifier
if !isExpectedIdentifier {
return
}

entry.Topics = append(entry.Topics, codeHash)
}

// updateSmartContractCode upgrades code for "direct" deployments & upgrades and for "indirect" deployments & upgrades
// It receives:
// (1) the account as found in the State
//
// (1) the account as found in the State
// (2) the account as returned in VM Output
// (3) the transaction that, upon execution, produced the VM Output
// (3) the transaction that, upon execution, produced the VM Output
func (sc *scProcessor) updateSmartContractCode(
vmOutput *vmcommon.VMOutput,
stateAccount state.UserAccountHandler,
outputAccount *vmcommon.OutputAccount,
) error {
) (*vmcommon.LogEntry, error) {
if len(outputAccount.Code) == 0 {
return nil
return nil, nil
}
if len(outputAccount.CodeMetadata) == 0 {
return nil
return nil, nil
}
if !core.IsSmartContractAddress(outputAccount.Address) {
return nil
return nil, nil
}

outputAccountCodeMetadataBytes, err := sc.blockChainHook.FilterCodeMetadataForUpgrade(outputAccount.CodeMetadata)
if err != nil {
return err
return nil, err
}

// This check is desirable (not required though) since currently both Wasm VM and IELE send the code in the output account even for "regular" execution
sameCode := bytes.Equal(outputAccount.Code, sc.accounts.GetCode(stateAccount.GetCodeHash()))
sameCodeMetadata := bytes.Equal(outputAccountCodeMetadataBytes, stateAccount.GetCodeMetadata())
if sameCode && sameCodeMetadata {
return nil
return nil, nil
}

currentOwner := stateAccount.GetOwnerAddress()
Expand Down Expand Up @@ -2670,7 +2686,7 @@ func (sc *scProcessor) updateSmartContractCode(

entry.Identifier = []byte(core.SCDeployIdentifier)
vmOutput.Logs = append(vmOutput.Logs, entry)
return nil
return entry, nil
}

if isUpgrade {
Expand All @@ -2680,10 +2696,10 @@ func (sc *scProcessor) updateSmartContractCode(

entry.Identifier = []byte(core.SCUpgradeIdentifier)
vmOutput.Logs = append(vmOutput.Logs, entry)
return nil
return entry, nil
}

return nil
return entry, nil
}

// delete accounts - only suicide by current SC or another SC called by current SC - protected by VM
Expand Down
30 changes: 18 additions & 12 deletions process/smartContract/processorV2/vmOutputAccountsProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (oap *VMOutputAccountsProcessor) Run() (bool, []data.TransactionHandler, er
return false, nil, err
}

err = oap.updateSmartContractCodeStep(oap.vmOutput, acc, outAcc)
logEntry, err := oap.updateSmartContractCodeStep(oap.vmOutput, acc, outAcc)
if err != nil {
return false, nil, err
}
Expand All @@ -82,6 +82,11 @@ func (oap *VMOutputAccountsProcessor) Run() (bool, []data.TransactionHandler, er
if err != nil {
return false, nil, err
}

if logEntry != nil {
// add the last topic only after the account is updated
logEntry.Topics = append(logEntry.Topics, acc.GetCodeHash())
}
}

if sumOfAllDiff.Cmp(zero) != 0 {
Expand Down Expand Up @@ -155,34 +160,35 @@ func (oap *VMOutputAccountsProcessor) processStorageUpdatesStep(

// updateSmartContractCode upgrades code for "direct" deployments & upgrades and for "indirect" deployments & upgrades
// It receives:
// (1) the account as found in the State
//
// (1) the account as found in the State
// (2) the account as returned in VM Output
// (3) the transaction that, upon execution, produced the VM Output
// (3) the transaction that, upon execution, produced the VM Output
func (oap *VMOutputAccountsProcessor) updateSmartContractCodeStep(
vmOutput *vmcommon.VMOutput,
stateAccount state.UserAccountHandler,
outputAccount *vmcommon.OutputAccount,
) error {
) (*vmcommon.LogEntry, error) {
if len(outputAccount.Code) == 0 {
return nil
return nil, nil
}
if len(outputAccount.CodeMetadata) == 0 {
return nil
return nil, nil
}
if !core.IsSmartContractAddress(outputAccount.Address) {
return nil
return nil, nil
}

outputAccountCodeMetadataBytes, err := oap.sc.blockChainHook.FilterCodeMetadataForUpgrade(outputAccount.CodeMetadata)
if err != nil {
return err
return nil, err
}

// This check is desirable (not required though) since currently both Arwen and IELE send the code in the output account even for "regular" execution
sameCode := bytes.Equal(outputAccount.Code, oap.sc.accounts.GetCode(stateAccount.GetCodeHash()))
sameCodeMetadata := bytes.Equal(outputAccountCodeMetadataBytes, stateAccount.GetCodeMetadata())
if sameCode && sameCodeMetadata {
return nil
return nil, nil
}

currentOwner := stateAccount.GetOwnerAddress()
Expand Down Expand Up @@ -215,7 +221,7 @@ func (oap *VMOutputAccountsProcessor) updateSmartContractCodeStep(

entry.Identifier = []byte(core.SCDeployIdentifier)
vmOutput.Logs = append(vmOutput.Logs, entry)
return nil
return entry, nil
}

if isUpgrade {
Expand All @@ -227,10 +233,10 @@ func (oap *VMOutputAccountsProcessor) updateSmartContractCodeStep(

entry.Identifier = []byte(core.SCUpgradeIdentifier)
vmOutput.Logs = append(vmOutput.Logs, entry)
return nil
return entry, nil
}

return nil
return entry, nil
}

func (oap *VMOutputAccountsProcessor) updateAccountNonceIfThereIsAChange(
Expand Down
16 changes: 12 additions & 4 deletions vm/systemSmartContracts/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4845,14 +4845,18 @@ func TestDelegationSystemSC_ExecuteChangeOwnerWithoutAccountUpdate(t *testing.T)
returnCode = d.Execute(vmInput)
assert.Equal(t, returnCode, vmcommon.UserError)

assert.Len(t, eei.logs, 2)
assert.Len(t, eei.logs, 3)
assert.Equal(t, []byte("delegate"), eei.logs[0].Identifier)
assert.Equal(t, []byte("second123"), eei.logs[0].Address)
assert.Len(t, eei.logs, 2)

assert.Equal(t, []byte(withdraw), eei.logs[1].Identifier)
assert.Equal(t, []byte("ownerAddr"), eei.logs[1].Address)
assert.Equal(t, boolToSlice(true), eei.logs[1].Topics[4])

assert.Equal(t, []byte(core.BuiltInFunctionChangeOwnerAddress), eei.logs[2].Identifier)
assert.Equal(t, []byte("addr"), eei.logs[2].Address)
assert.Equal(t, []byte("second123"), eei.logs[2].Topics[0])

eei.logs = nil
vmInput.CallerAddr = []byte("second123")
vmInput.Arguments[0] = []byte("ownerAddr")
Expand All @@ -4863,13 +4867,17 @@ func TestDelegationSystemSC_ExecuteChangeOwnerWithoutAccountUpdate(t *testing.T)
assert.Equal(t, eei.storageUpdate[string(d.delegationMgrSCAddress)]["ownerAddr"], vmInput.RecipientAddr)
assert.Equal(t, eei.storageUpdate[string(d.delegationMgrSCAddress)]["second123"], []byte{})

assert.Len(t, eei.logs, 2)
assert.Len(t, eei.logs, 3)
assert.Equal(t, []byte("delegate"), eei.logs[0].Identifier)
assert.Equal(t, []byte("ownerAddr"), eei.logs[0].Address)
assert.Len(t, eei.logs, 2)

assert.Equal(t, []byte(withdraw), eei.logs[1].Identifier)
assert.Equal(t, []byte("second123"), eei.logs[1].Address)
assert.Equal(t, boolToSlice(true), eei.logs[1].Topics[4])

assert.Equal(t, []byte(core.BuiltInFunctionChangeOwnerAddress), eei.logs[2].Identifier)
assert.Equal(t, []byte("addr"), eei.logs[2].Address)
assert.Equal(t, []byte("ownerAddr"), eei.logs[2].Topics[0])
}

func TestDelegationSystemSC_ExecuteChangeOwnerWithAccountUpdate(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions vm/systemSmartContracts/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ func (d *delegation) createLogEventsForChangeOwner(

d.createAndAddLogEntryForDelegate(args, big.NewInt(0), globalFund, ownerDelegatorData, dStatus, false)
d.createAndAddLogEntryForWithdraw(withdraw, args.CallerAddr, big.NewInt(0), globalFund, ownerDelegatorData, d.numUsers(), true, nil)

entry := &vmcommon.LogEntry{
Identifier: []byte(core.BuiltInFunctionChangeOwnerAddress),
Address: args.RecipientAddr,
Topics: [][]byte{args.Arguments[0]},
}
d.eei.AddLogEntry(entry)
}

func boolToSlice(b bool) []byte {
Expand Down

0 comments on commit 8606916

Please sign in to comment.