Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skip unexport field when emit neovm param code #1438

Merged
merged 3 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions core/utils/transaction_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"time"

"github.com/laizy/bigint"

"github.com/ontio/ontology/common"
"github.com/ontio/ontology/core/payload"
"github.com/ontio/ontology/core/types"
Expand Down Expand Up @@ -170,13 +169,15 @@ func BuildNeoVMParam(builder *vm.ParamsBuilder, smartContractParams []interface{
builder.Emit(vm.TOALTSTACK)
for i := 0; i < object.NumField(); i++ {
field := object.Field(i)
err := BuildNeoVMParam(builder, []interface{}{field.Interface()})
if err != nil {
return err
if field.CanInterface() { // skip unexported fields
err := BuildNeoVMParam(builder, []interface{}{field.Interface()})
if err != nil {
return err
}
builder.Emit(vm.DUPFROMALTSTACK)
builder.Emit(vm.SWAP)
builder.Emit(vm.APPEND)
}
builder.Emit(vm.DUPFROMALTSTACK)
builder.Emit(vm.SWAP)
builder.Emit(vm.APPEND)
}
builder.Emit(vm.FROMALTSTACK)
default:
Expand Down
51 changes: 51 additions & 0 deletions core/utils/transactoin_builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ontology is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/

package utils

import (
"testing"

"github.com/ontio/ontology/common"
"github.com/stretchr/testify/assert"
)

func TestUnexportFields(t *testing.T) {
unexport := struct {
Name string
num uint
Age int
}{
Name: "aaa",
num: 100,
Age: 123,
}
export := struct {
Name string
Age int
}{
Name: "aaa",
Age: 123,
}

unexportCode, err := BuildNeoVMInvokeCode(common.Address{}, []interface{}{unexport})
assert.Nil(t, err)
exportCode, err := BuildNeoVMInvokeCode(common.Address{}, []interface{}{export})
assert.Nil(t, err)
assert.Equal(t, unexportCode, exportCode)
}
12 changes: 6 additions & 6 deletions smartcontract/service/native/testsuite/ont_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,31 +157,31 @@ func ontTotalAllowanceV2(native *native.NativeService, addr common.Address) uint

func ontTransfer(native *native.NativeService, from, to common.Address, value uint64) error {
native.Tx.SignedAddr = append(native.Tx.SignedAddr, from)
state := ont.TransferState{from, to, value}
state := ont.TransferState{From: from, To: to, Value: value}
native.Input = common.SerializeToBytes(&ont.TransferStates{States: []ont.TransferState{state}})
_, err := ont.OntTransfer(native)
return err
}

func ontTransferV2(native *native.NativeService, from, to common.Address, value uint64) error {
native.Tx.SignedAddr = append(native.Tx.SignedAddr, from)
state := &ont.TransferStateV2{from, to, states.NativeTokenBalance{Balance: bigint.New(value)}}
state := &ont.TransferStateV2{From: from, To: to, Value: states.NativeTokenBalance{Balance: bigint.New(value)}}
native.Input = common.SerializeToBytes(&ont.TransferStatesV2{States: []*ont.TransferStateV2{state}})
_, err := ont.OntTransferV2(native)
return err
}

func ontTransferFrom(native *native.NativeService, sender, from, to common.Address, value uint64) error {
native.Tx.SignedAddr = append(native.Tx.SignedAddr, from)
state := &ont.TransferFrom{sender, ont.TransferState{from, to, value}}
state := &ont.TransferFrom{Sender: sender, TransferState: ont.TransferState{From: from, To: to, Value: value}}
native.Input = common.SerializeToBytes(state)
_, err := ont.OntTransferFrom(native)
return err
}

func ontTransferFromV2(native *native.NativeService, sender, from, to common.Address, value uint64) error {
native.Tx.SignedAddr = append(native.Tx.SignedAddr, from)
state := &ont.TransferFromStateV2{sender, ont.TransferStateV2{from, to, states.NativeTokenBalance{Balance: bigint.New(value)}}}
state := &ont.TransferFromStateV2{Sender: sender, TransferStateV2: ont.TransferStateV2{From: from, To: to, Value: states.NativeTokenBalance{Balance: bigint.New(value)}}}
native.Input = common.SerializeToBytes(state)
_, err := ont.OntTransferFromV2(native)
return err
Expand All @@ -190,15 +190,15 @@ func ontTransferFromV2(native *native.NativeService, sender, from, to common.Add
func ontApprove(native *native.NativeService, from, to common.Address, value uint64) error {
native.Tx.SignedAddr = append(native.Tx.SignedAddr, from)

native.Input = common.SerializeToBytes(&ont.TransferState{from, to, value})
native.Input = common.SerializeToBytes(&ont.TransferState{From: from, To: to, Value: value})
_, err := ont.OntApprove(native)
return err
}

func ontApproveV2(native *native.NativeService, from, to common.Address, value uint64) error {
native.Tx.SignedAddr = append(native.Tx.SignedAddr, from)

native.Input = common.SerializeToBytes((&ont.TransferStateV2{from, to, states.NativeTokenBalance{Balance: bigint.New(value)}}))
native.Input = common.SerializeToBytes(&ont.TransferStateV2{From: from, To: to, Value: states.NativeTokenBalance{Balance: bigint.New(value)}})
_, err := ont.OntApproveV2(native)
return err
}
Expand Down
Loading