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

add more governance read method #1428

Merged
merged 2 commits into from
Mar 28, 2023
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
3 changes: 2 additions & 1 deletion common/zero_copy_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
53 changes: 53 additions & 0 deletions smartcontract/service/native/governance/governance.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ const (
GET_PEER_POOL = "getPeerPool"
GET_PEER_INFO = "getPeerInfo"
GET_PEER_POOL_BY_ADDRESS = "getPeerPoolByAddress"
GET_VIEW = "getView"
GET_AUTHOR_INFO = "getAuthorizeInfo"
GET_ADDRESS_FEE = "getAddressFee"

//key prefix
GLOBAL_PARAM = "globalParam"
Expand Down Expand Up @@ -162,6 +165,10 @@ 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)
native.Register(GET_ADDRESS_FEE, GetAddressFee)
}

//Init governance contract, include vbft config, global param and ontid admin.
Expand Down Expand Up @@ -1785,6 +1792,52 @@ 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)
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)
Expand Down
1 change: 1 addition & 0 deletions smartcontract/service/native/utils/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down