Skip to content

Commit

Permalink
add more governance read method (#1428)
Browse files Browse the repository at this point in the history
* add more governance read method

* add get address fee
  • Loading branch information
laizy authored Mar 28, 2023
1 parent 5f9b2fe commit dde2678
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
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

0 comments on commit dde2678

Please sign in to comment.