From d774634f648f064f27f24fae02e3353bf2b0eaca Mon Sep 17 00:00:00 2001 From: Ivan Kuznetsov Date: Thu, 20 Dec 2018 14:36:59 +0700 Subject: [PATCH 1/3] Implement Blob type --- types.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/types.go b/types.go index 30025f2b..2a8a87f9 100644 --- a/types.go +++ b/types.go @@ -1,6 +1,7 @@ package eos import ( + "encoding/base64" "encoding/binary" "encoding/hex" "encoding/json" @@ -797,3 +798,19 @@ func (i *Uint128) UnmarshalJSON(data []byte) error { return nil } + +// Blob + +// Blob is base64 encoded data +// https://github.com/EOSIO/fc/blob/0e74738e938c2fe0f36c5238dbc549665ddaef82/include/fc/variant.hpp#L47 +type Blob string + +// Data returns decoded base64 data +func (b Blob) Data() ([]byte, error) { + return base64.StdEncoding.DecodeString(string(b)) +} + +// String returns the blob as a string +func (b Blob) String() string { + return string(b) +} From 0296aa55ab78238a2d32367e0e9fdf53d09312e7 Mon Sep 17 00:00:00 2001 From: Ivan Kuznetsov Date: Thu, 20 Dec 2018 14:40:01 +0700 Subject: [PATCH 2/3] Add support for 'chain/get_raw_abi' API endpoint --- api.go | 5 +++ responses.go | 90 +++++++++++++++++++++++++++++----------------------- 2 files changed, 56 insertions(+), 39 deletions(-) diff --git a/api.go b/api.go index 739c3fc2..482f8414 100644 --- a/api.go +++ b/api.go @@ -508,6 +508,11 @@ func (api *API) GetTableRows(params GetTableRowsRequest) (out *GetTableRowsResp, return } +func (api *API) GetRawABI(params GetRawABIRequest) (out *GetRawABIResp, err error) { + err = api.call("chain", "get_raw_abi", params, &out) + return +} + func (api *API) GetRequiredKeys(tx *Transaction) (out *GetRequiredKeysResp, err error) { keys, err := api.Signer.AvailableKeys() if err != nil { diff --git a/responses.go b/responses.go index 0fee8e04..cd281001 100644 --- a/responses.go +++ b/responses.go @@ -36,11 +36,11 @@ type InfoResp struct { HeadBlockTime JSONTime `json:"head_block_time"` // "2018-02-02T04:19:32" HeadBlockProducer AccountName `json:"head_block_producer"` // "inita" - VirtualBlockCPULimit Int64 `json:"virtual_block_cpu_limit"` - VirtualBlockNetLimit Int64 `json:"virtual_block_net_limit"` - BlockCPULimit Int64 `json:"block_cpu_limit"` - BlockNetLimit Int64 `json:"block_net_limit"` - ServerVersionString string `json:"server_version_string"` + VirtualBlockCPULimit Int64 `json:"virtual_block_cpu_limit"` + VirtualBlockNetLimit Int64 `json:"virtual_block_net_limit"` + BlockCPULimit Int64 `json:"block_cpu_limit"` + BlockNetLimit Int64 `json:"block_net_limit"` + ServerVersionString string `json:"server_version_string"` } type BlockResp struct { @@ -68,8 +68,8 @@ type DBSizeResp struct { UsedBytes Int64 `json:"used_bytes"` Size Int64 `json:"size"` Indices []struct { - Index string `json:"index"` - RowCount Int64 `json:"row_count"` + Index string `json:"index"` + RowCount Int64 `json:"row_count"` } `json:"indices"` } @@ -166,8 +166,8 @@ type AccountResp struct { CoreLiquidBalance Asset `json:"core_liquid_balance"` RAMQuota int64 `json:"ram_quota"` RAMUsage int64 `json:"ram_usage"` - NetWeight Int64 `json:"net_weight"` - CPUWeight Int64 `json:"cpu_weight"` + NetWeight Int64 `json:"net_weight"` + CPUWeight Int64 `json:"cpu_weight"` NetLimit AccountResourceLimit `json:"net_limit"` CPULimit AccountResourceLimit `json:"cpu_limit"` Permissions []Permission `json:"permissions"` @@ -254,6 +254,18 @@ type Currency struct { Name CurrencyName } +type GetRawABIRequest struct { + AccountName string `json:"account_name"` + ABIHash Checksum256 `json:"abi_hash,omitempty"` +} + +type GetRawABIResp struct { + AccountName string `json:"account_name"` + CodeHash Checksum256 `json:"code_hash"` + ABIHash Checksum256 `json:"abi_hash"` + ABI Blob `json:"abi"` +} + type GetRequiredKeysResp struct { RequiredKeys []ecc.PublicKey `json:"required_keys"` } @@ -326,36 +338,36 @@ type NetConnectResp string type NetDisconnectResp string type Global struct { - MaxBlockNetUsage int `json:"max_block_net_usage"` - TargetBlockNetUsagePct int `json:"target_block_net_usage_pct"` - MaxTransactionNetUsage int `json:"max_transaction_net_usage"` - BasePerTransactionNetUsage int `json:"base_per_transaction_net_usage"` - NetUsageLeeway int `json:"net_usage_leeway"` - ContextFreeDiscountNetUsageNum int `json:"context_free_discount_net_usage_num"` - ContextFreeDiscountNetUsageDen int `json:"context_free_discount_net_usage_den"` - MaxBlockCPUUsage int `json:"max_block_cpu_usage"` - TargetBlockCPUUsagePct int `json:"target_block_cpu_usage_pct"` - MaxTransactionCPUUsage int `json:"max_transaction_cpu_usage"` - MinTransactionCPUUsage int `json:"min_transaction_cpu_usage"` - MaxTransactionLifetime int `json:"max_transaction_lifetime"` - DeferredTrxExpirationWindow int `json:"deferred_trx_expiration_window"` - MaxTransactionDelay int `json:"max_transaction_delay"` - MaxInlineActionSize int `json:"max_inline_action_size"` - MaxInlineActionDepth int `json:"max_inline_action_depth"` - MaxAuthorityDepth int `json:"max_authority_depth"` - MaxRAMSize string `json:"max_ram_size"` - TotalRAMBytesReserved Int64 `json:"total_ram_bytes_reserved"` - TotalRAMStake Int64 `json:"total_ram_stake"` - LastProducerScheduleUpdate string `json:"last_producer_schedule_update"` - LastPervoteBucketFill int64 `json:"last_pervote_bucket_fill,string"` - PervoteBucket int `json:"pervote_bucket"` - PerblockBucket int `json:"perblock_bucket"` - TotalUnpaidBlocks int `json:"total_unpaid_blocks"` - TotalActivatedStake float64 `json:"total_activated_stake,string"` - ThreshActivatedStakeTime int64 `json:"thresh_activated_stake_time,string"` - LastProducerScheduleSize int `json:"last_producer_schedule_size"` - TotalProducerVoteWeight float64 `json:"total_producer_vote_weight,string"` - LastNameClose string `json:"last_name_close"` + MaxBlockNetUsage int `json:"max_block_net_usage"` + TargetBlockNetUsagePct int `json:"target_block_net_usage_pct"` + MaxTransactionNetUsage int `json:"max_transaction_net_usage"` + BasePerTransactionNetUsage int `json:"base_per_transaction_net_usage"` + NetUsageLeeway int `json:"net_usage_leeway"` + ContextFreeDiscountNetUsageNum int `json:"context_free_discount_net_usage_num"` + ContextFreeDiscountNetUsageDen int `json:"context_free_discount_net_usage_den"` + MaxBlockCPUUsage int `json:"max_block_cpu_usage"` + TargetBlockCPUUsagePct int `json:"target_block_cpu_usage_pct"` + MaxTransactionCPUUsage int `json:"max_transaction_cpu_usage"` + MinTransactionCPUUsage int `json:"min_transaction_cpu_usage"` + MaxTransactionLifetime int `json:"max_transaction_lifetime"` + DeferredTrxExpirationWindow int `json:"deferred_trx_expiration_window"` + MaxTransactionDelay int `json:"max_transaction_delay"` + MaxInlineActionSize int `json:"max_inline_action_size"` + MaxInlineActionDepth int `json:"max_inline_action_depth"` + MaxAuthorityDepth int `json:"max_authority_depth"` + MaxRAMSize string `json:"max_ram_size"` + TotalRAMBytesReserved Int64 `json:"total_ram_bytes_reserved"` + TotalRAMStake Int64 `json:"total_ram_stake"` + LastProducerScheduleUpdate string `json:"last_producer_schedule_update"` + LastPervoteBucketFill int64 `json:"last_pervote_bucket_fill,string"` + PervoteBucket int `json:"pervote_bucket"` + PerblockBucket int `json:"perblock_bucket"` + TotalUnpaidBlocks int `json:"total_unpaid_blocks"` + TotalActivatedStake float64 `json:"total_activated_stake,string"` + ThreshActivatedStakeTime int64 `json:"thresh_activated_stake_time,string"` + LastProducerScheduleSize int `json:"last_producer_schedule_size"` + TotalProducerVoteWeight float64 `json:"total_producer_vote_weight,string"` + LastNameClose string `json:"last_name_close"` } type Producer struct { From b066e438399f12b66debb8135bb567d26c8bee9c Mon Sep 17 00:00:00 2001 From: Ivan Kuznetsov Date: Thu, 20 Dec 2018 15:03:45 +0700 Subject: [PATCH 3/3] Add a test for Blob --- types_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/types_test.go b/types_test.go index b2a61194..b1b8c331 100644 --- a/types_test.go +++ b/types_test.go @@ -517,3 +517,24 @@ func EqualNoDiff(t *testing.T, expected interface{}, actual interface{}, message return true } + +func TestBlob(t *testing.T) { + b := Blob("RU9TIEdv") + + t.Run("String", func(tt *testing.T) { + assert.Equal(tt, "RU9TIEdv", b.String()) + }) + + t.Run("Data", func(tt *testing.T) { + data, err := b.Data() + require.Nil(tt, err) + assert.Equal(tt, []byte("EOS Go"), data) + }) + + t.Run("malformed data", func(tt *testing.T) { + b := Blob("not base64") + data, err := b.Data() + require.Equal(tt, "illegal base64 data at input byte 3", err.Error()) + assert.Empty(tt, data) + }) +}