diff --git a/core/utils/transaction_builder.go b/core/utils/transaction_builder.go index bf18dafb9..97df1d3e2 100644 --- a/core/utils/transaction_builder.go +++ b/core/utils/transaction_builder.go @@ -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" @@ -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: diff --git a/core/utils/transactoin_builder_test.go b/core/utils/transactoin_builder_test.go new file mode 100644 index 000000000..08c80f6dc --- /dev/null +++ b/core/utils/transactoin_builder_test.go @@ -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 . + */ + +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) +} diff --git a/smartcontract/service/native/testsuite/ont_suite_test.go b/smartcontract/service/native/testsuite/ont_suite_test.go index af1bed21a..9526e1ca8 100644 --- a/smartcontract/service/native/testsuite/ont_suite_test.go +++ b/smartcontract/service/native/testsuite/ont_suite_test.go @@ -157,7 +157,7 @@ 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 @@ -165,7 +165,7 @@ func ontTransfer(native *native.NativeService, from, to common.Address, value ui 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 @@ -173,7 +173,7 @@ func ontTransferV2(native *native.NativeService, from, to common.Address, value 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 @@ -181,7 +181,7 @@ func ontTransferFrom(native *native.NativeService, sender, from, to common.Addre 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 @@ -190,7 +190,7 @@ 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 } @@ -198,7 +198,7 @@ func ontApprove(native *native.NativeService, from, to common.Address, value uin 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 }