Skip to content

Commit

Permalink
skip unexport field when emit neovm param code (#1438)
Browse files Browse the repository at this point in the history
  • Loading branch information
laizy authored Apr 26, 2024
1 parent 86738c4 commit 91450e8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 13 deletions.
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

0 comments on commit 91450e8

Please sign in to comment.