From f54a1480ebfa299872616d0bc307a3e0b55f6b2d Mon Sep 17 00:00:00 2001 From: laizy Date: Thu, 23 Mar 2023 18:24:38 +0800 Subject: [PATCH 1/2] add more governance read method --- common/zero_copy_sink.go | 3 +- .../service/native/governance/governance.go | 35 +++++++++++++++++++ .../service/native/utils/serialization.go | 1 + 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/common/zero_copy_sink.go b/common/zero_copy_sink.go index 7626dc7e94..058676d371 100644 --- a/common/zero_copy_sink.go +++ b/common/zero_copy_sink.go @@ -106,9 +106,10 @@ func (self *ZeroCopySink) WriteUint16(data uint16) { binary.LittleEndian.PutUint16(buf, data) } -func (self *ZeroCopySink) WriteUint32(data uint32) { +func (self *ZeroCopySink) WriteUint32(data uint32) *ZeroCopySink { buf := self.NextBytes(4) binary.LittleEndian.PutUint32(buf, data) + return self } func (self *ZeroCopySink) WriteUint64(data uint64) *ZeroCopySink { diff --git a/smartcontract/service/native/governance/governance.go b/smartcontract/service/native/governance/governance.go index cabdf7e4d7..776068c2a5 100644 --- a/smartcontract/service/native/governance/governance.go +++ b/smartcontract/service/native/governance/governance.go @@ -81,6 +81,8 @@ const ( GET_PEER_POOL = "getPeerPool" GET_PEER_INFO = "getPeerInfo" GET_PEER_POOL_BY_ADDRESS = "getPeerPoolByAddress" + GET_VIEW = "getView" + GET_AUTHOR_INFO = "getAuthorizeInfo" //key prefix GLOBAL_PARAM = "globalParam" @@ -162,6 +164,9 @@ func RegisterGovernanceContract(native *native.NativeService) { native.Register(GET_PEER_POOL, GetPeerPool) native.Register(GET_PEER_INFO, GetPeerInfo) native.Register(GET_PEER_POOL_BY_ADDRESS, GetPeerPoolByAddress) + + native.Register(GET_VIEW, GetCurrView) + native.Register(GET_AUTHOR_INFO, GetAuthorizeInfo) } //Init governance contract, include vbft config, global param and ontid admin. @@ -1785,6 +1790,36 @@ func GetPeerInfo(native *native.NativeService) ([]byte, error) { return sink.Bytes(), nil } +func GetAuthorizeInfo(native *native.NativeService) ([]byte, error) { + contract := native.ContextRef.CurrentContext().ContractAddress + source := common.NewZeroCopySource(native.Input) + address, err := utils.DecodeAddress(source) + if err != nil { + return utils.BYTE_FALSE, fmt.Errorf("get authorize info, param address error: %v", err) + } + peerPubKey, err := utils.DecodeString(source) + if err != nil { + return utils.BYTE_FALSE, fmt.Errorf("get authorize info, param peer pubkey error: %v", err) + } + info, err := getAuthorizeInfo(native, contract, peerPubKey, address) + + if err != nil { + return utils.BYTE_FALSE, fmt.Errorf("get authorize info error: %v", err) + } + + return common.SerializeToBytes(info), nil +} + +func GetCurrView(native *native.NativeService) ([]byte, error) { + contract := native.ContextRef.CurrentContext().ContractAddress + //get current view + view, err := GetView(native, contract) + if err != nil { + return utils.BYTE_FALSE, fmt.Errorf("get view error: %v", err) + } + return common.NewZeroCopySink(nil).WriteUint32(view).Bytes(), nil +} + func GetPeerPoolByAddress(native *native.NativeService) ([]byte, error) { contract := native.ContextRef.CurrentContext().ContractAddress source := common.NewZeroCopySource(native.Input) diff --git a/smartcontract/service/native/utils/serialization.go b/smartcontract/service/native/utils/serialization.go index fd265e757c..94caa366c3 100644 --- a/smartcontract/service/native/utils/serialization.go +++ b/smartcontract/service/native/utils/serialization.go @@ -72,6 +72,7 @@ func DecodeAddress(source *common.ZeroCopySource) (common.Address, error) { return common.AddressParseFromBytes(from) } + func DecodeVarBytes(source *common.ZeroCopySource) ([]byte, error) { data, _, irregular, eof := source.NextVarBytes() if eof { From 97d236c8857e4e4971377d77e7ff8afcfb124081 Mon Sep 17 00:00:00 2001 From: laizy Date: Mon, 27 Mar 2023 17:20:58 +0800 Subject: [PATCH 2/2] add get address fee --- .../service/native/governance/governance.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/smartcontract/service/native/governance/governance.go b/smartcontract/service/native/governance/governance.go index 776068c2a5..bbc03ed69d 100644 --- a/smartcontract/service/native/governance/governance.go +++ b/smartcontract/service/native/governance/governance.go @@ -83,6 +83,7 @@ const ( GET_PEER_POOL_BY_ADDRESS = "getPeerPoolByAddress" GET_VIEW = "getView" GET_AUTHOR_INFO = "getAuthorizeInfo" + GET_ADDRESS_FEE = "getAddressFee" //key prefix GLOBAL_PARAM = "globalParam" @@ -167,6 +168,7 @@ func RegisterGovernanceContract(native *native.NativeService) { native.Register(GET_VIEW, GetCurrView) native.Register(GET_AUTHOR_INFO, GetAuthorizeInfo) + native.Register(GET_ADDRESS_FEE, GetAddressFee) } //Init governance contract, include vbft config, global param and ontid admin. @@ -1790,6 +1792,22 @@ func GetPeerInfo(native *native.NativeService) ([]byte, error) { return sink.Bytes(), nil } +func GetAddressFee(native *native.NativeService) ([]byte, error) { + contract := native.ContextRef.CurrentContext().ContractAddress + source := common.NewZeroCopySource(native.Input) + address, err := utils.DecodeAddress(source) + if err != nil { + return utils.BYTE_FALSE, fmt.Errorf("GetAddressFee, get address error: %s", err) + } + + splitFeeAddress, err := getSplitFeeAddress(native, contract, address) + if err != nil { + return utils.BYTE_FALSE, fmt.Errorf("GetAddressFee, getSplitFeeAddress error: %s", err) + } + + return common.NewZeroCopySink(nil).WriteUint64(splitFeeAddress.Amount).Bytes(), nil +} + func GetAuthorizeInfo(native *native.NativeService) ([]byte, error) { contract := native.ContextRef.CurrentContext().ContractAddress source := common.NewZeroCopySource(native.Input)