Skip to content

Commit

Permalink
move config to ton package
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-tron committed Nov 2, 2023
1 parent fcf7296 commit 4b16041
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 43 deletions.
25 changes: 2 additions & 23 deletions liteapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ func (c *Client) GetConfigAll(ctx context.Context, mode ConfigMode) (tlb.ConfigP
if err != nil {
return tlb.ConfigParams{}, err
}
return decodeConfigParams(res.ConfigProof)
return ton.DecodeConfigParams(res.ConfigProof)
}

func (c *Client) GetConfigAllRaw(ctx context.Context, mode ConfigMode) (liteclient.LiteServerConfigInfoC, error) {
Expand Down Expand Up @@ -775,28 +775,7 @@ func (c *Client) GetConfigParams(ctx context.Context, mode ConfigMode, paramList
if err != nil {
return tlb.ConfigParams{}, err
}
return decodeConfigParams(r.ConfigProof)
}

func decodeConfigParams(b []byte) (tlb.ConfigParams, error) {
cells, err := boc.DeserializeBoc(b)
if err != nil {
return tlb.ConfigParams{}, err
}
if len(cells) != 1 {
return tlb.ConfigParams{}, boc.ErrNotSingleRoot
}
var proof struct {
Proof tlb.MerkleProof[tlb.ShardStateUnsplit]
}
err = tlb.Unmarshal(cells[0], &proof)
if err != nil {
return tlb.ConfigParams{}, err
}
if proof.Proof.VirtualRoot.ShardStateUnsplit.Custom.Exists {
return proof.Proof.VirtualRoot.ShardStateUnsplit.Custom.Value.Value.Config, nil
}
return tlb.ConfigParams{}, fmt.Errorf("empty Custom field")
return ton.DecodeConfigParams(r.ConfigProof)
}

func (c *Client) GetValidatorStats(
Expand Down
54 changes: 37 additions & 17 deletions liteapi/config.go → ton/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package liteapi
package ton

import (
"fmt"
"reflect"

"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/tlb"
"github.com/tonkeeper/tongo/ton"
)

// BlockchainConfig represents the TON blockchain configuration stored inside key blocks.
Expand Down Expand Up @@ -71,39 +70,39 @@ type BlockchainConfig struct {
ConfigParamNegative999 *boc.Cell `json:",omitempty"`
}

func (conf *BlockchainConfig) ConfigAddr() (ton.AccountID, bool) {
func (conf *BlockchainConfig) ConfigAddr() (AccountID, bool) {
if conf.ConfigParam0 != nil {
return ton.AccountID{Workchain: -1, Address: conf.ConfigParam0.ConfigAddr}, true
return AccountID{Workchain: -1, Address: conf.ConfigParam0.ConfigAddr}, true
}
return ton.AccountID{}, false
return AccountID{}, false
}

func (conf *BlockchainConfig) ElectorAddr() (ton.AccountID, bool) {
func (conf *BlockchainConfig) ElectorAddr() (AccountID, bool) {
if conf.ConfigParam1 != nil {
return ton.AccountID{Workchain: -1, Address: conf.ConfigParam1.ElectorAddr}, true
return AccountID{Workchain: -1, Address: conf.ConfigParam1.ElectorAddr}, true
}
return ton.AccountID{}, false
return AccountID{}, false
}

func (conf *BlockchainConfig) MinterAddr() (ton.AccountID, bool) {
func (conf *BlockchainConfig) MinterAddr() (AccountID, bool) {
if conf.ConfigParam2 != nil {
return ton.AccountID{Workchain: -1, Address: conf.ConfigParam2.MinterAddr}, true
return AccountID{Workchain: -1, Address: conf.ConfigParam2.MinterAddr}, true
}
return ton.AccountID{}, false
return AccountID{}, false
}

func (conf *BlockchainConfig) FeeCollectorAddr() (ton.AccountID, bool) {
func (conf *BlockchainConfig) FeeCollectorAddr() (AccountID, bool) {
if conf.ConfigParam3 != nil {
return ton.AccountID{Workchain: -1, Address: conf.ConfigParam3.FeeCollectorAddr}, true
return AccountID{Workchain: -1, Address: conf.ConfigParam3.FeeCollectorAddr}, true
}
return ton.AccountID{}, false
return AccountID{}, false
}

func (conf *BlockchainConfig) DnsRootAddr() (ton.AccountID, bool) {
func (conf *BlockchainConfig) DnsRootAddr() (AccountID, bool) {
if conf.ConfigParam4 != nil {
return ton.AccountID{Workchain: -1, Address: conf.ConfigParam4.DnsRootAddr}, true
return AccountID{Workchain: -1, Address: conf.ConfigParam4.DnsRootAddr}, true
}
return ton.AccountID{}, false
return AccountID{}, false
}

func (conf *BlockchainConfig) MandatoryParams() []int {
Expand Down Expand Up @@ -160,3 +159,24 @@ func ConvertBlockchainConfig(params tlb.ConfigParams) (*BlockchainConfig, error)
}
return conf, nil
}

func DecodeConfigParams(b []byte) (tlb.ConfigParams, error) {
cells, err := boc.DeserializeBoc(b)
if err != nil {
return tlb.ConfigParams{}, err
}
if len(cells) != 1 {
return tlb.ConfigParams{}, boc.ErrNotSingleRoot
}
var proof struct {
Proof tlb.MerkleProof[tlb.ShardStateUnsplit]
}
err = tlb.Unmarshal(cells[0], &proof)
if err != nil {
return tlb.ConfigParams{}, err
}
if proof.Proof.VirtualRoot.ShardStateUnsplit.Custom.Exists {
return proof.Proof.VirtualRoot.ShardStateUnsplit.Custom.Value.Value.Config, nil
}
return tlb.ConfigParams{}, fmt.Errorf("empty Custom field")
}
6 changes: 3 additions & 3 deletions liteapi/config_test.go → ton/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package liteapi
package ton

import (
"encoding/json"
Expand All @@ -25,9 +25,9 @@ func TestConvertBlockchainConfig(t *testing.T) {
if err != nil {
t.Fatalf("os.ReadFile() failed: %v", err)
}
params, err := decodeConfigParams(configProof)
params, err := DecodeConfigParams(configProof)
if err != nil {
t.Fatalf("decodeConfigParams() failed: %v", err)
t.Fatalf("DecodeConfigParams() failed: %v", err)
}
config, err := ConvertBlockchainConfig(params)
if err != nil {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 4b16041

Please sign in to comment.