Skip to content

Commit

Permalink
Fixed HW3 state init single transfer, fixed proof chain gaps, fixed d…
Browse files Browse the repository at this point in the history
…ns get wallet method ref
  • Loading branch information
xssnick committed Jun 10, 2024
1 parent 41837f9 commit 9b2eadd
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 15 deletions.
2 changes: 1 addition & 1 deletion example/dns/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {
api := ton.NewAPIClient(client).WithRetry()

// get root dns address from network config
root, err := dns.RootContractAddr(api)
root, err := dns.RootContractAddrV2(context.Background(), api)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion example/site-request/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func getDNSResolver() *dns.Client {
api := ton.NewAPIClient(client)

// get root dns address from network config
root, err := dns.RootContractAddr(api)
root, err := dns.RootContractAddrV2(context.Background(), api)
if err != nil {
panic(err)
}
Expand Down
18 changes: 13 additions & 5 deletions ton/dns/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,18 @@ var randomizer = func() uint64 {
return binary.LittleEndian.Uint64(buf)
}

// Deprecated: use RootContractAddrV2
func RootContractAddr(api TonApi) (*address.Address, error) {
b, err := api.CurrentMasterchainInfo(context.Background())
return RootContractAddrV2(context.Background(), api)
}

func RootContractAddrV2(ctx context.Context, api TonApi) (*address.Address, error) {
b, err := api.CurrentMasterchainInfo(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get masterchain info: %w", err)
}

cfg, err := api.GetBlockchainConfig(context.Background(), b, 4)
cfg, err := api.GetBlockchainConfig(ctx, b, 4)
if err != nil {
return nil, fmt.Errorf("failed to get root address from network config: %w", err)
}
Expand Down Expand Up @@ -174,7 +179,11 @@ func (d *Domain) GetWalletRecord() *address.Address {
if rec == nil {
return nil
}
p := rec.BeginParse()

p, err := rec.BeginParse().LoadRef()
if err != nil {
return nil
}

category, err := p.LoadUInt(16)
if err != nil {
Expand All @@ -198,9 +207,8 @@ func (d *Domain) GetSiteRecord() (_ []byte, inStorage bool) {
if rec == nil {
return nil, false
}
p := rec.BeginParse()

p, err := p.LoadRef()
p, err := rec.BeginParse().LoadRef()
if err != nil {
return nil, false
}
Expand Down
7 changes: 6 additions & 1 deletion ton/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,10 @@ func (c *APIClient) VerifyProofChain(ctx context.Context, from, to *BlockIDExt)
}

if isForward {
if len(part.Steps) > 3 {
//part.Steps = append(part.Steps[:2], part.Steps[len(part.Steps)-1])
}

for _, step := range part.Steps {
fwd, ok := step.(BlockLinkForward)
if !ok {
Expand Down Expand Up @@ -600,10 +604,11 @@ func (c *APIClient) VerifyProofChain(ctx context.Context, from, to *BlockIDExt)
return fmt.Errorf("config proof boc parse err: %w", err)
}

err = CheckForwardBlockProof(fwd.From, fwd.To, fwd.ToKeyBlock, configProof, destProof, fwd.SignatureSet)
err = CheckForwardBlockProof(from, fwd.To, fwd.ToKeyBlock, configProof, destProof, fwd.SignatureSet)
if err != nil {
return fmt.Errorf("invalid forward block from %d to %d proof: %w", fwd.From.SeqNo, fwd.To.SeqNo, err)
}
from = fwd.To
}
} else {
for _, step := range part.Steps {
Expand Down
16 changes: 9 additions & 7 deletions ton/wallet/highloadv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ func (s *SpecHighloadV3) BuildMessage(ctx context.Context, messages []*Message)

if len(messages) > 254*254 {
return nil, errors.New("for this type of wallet max 254*254 messages can be sent in the same time")
} else if len(messages) > 1 {
} else if len(messages) == 1 && messages[0].InternalMessage.StateInit == nil { // messages with state init must be packed because of external msg validation in contract
msg = messages[0]
} else if len(messages) > 0 {
msg, err = s.packActions(uint64(queryID), messages)
if err != nil {
return nil, fmt.Errorf("failed to pack messages to cell: %w", err)
}
} else if len(messages) == 1 {
msg = messages[0]
} else {
return nil, errors.New("should have at least one message")
}
Expand All @@ -88,13 +88,15 @@ func (s *SpecHighloadV3) BuildMessage(ctx context.Context, messages []*Message)
MustStoreRef(payload).EndCell(), nil
}

func (s *SpecHighloadV3) packActions(queryId uint64, messages []*Message) (*Message, error) {
if len(messages) > 253 {
rest, err := s.packActions(queryId, messages[253:])
func (s *SpecHighloadV3) packActions(queryId uint64, messages []*Message) (_ *Message, err error) {
const messagesPerPack = 253

if len(messages) > messagesPerPack {
rest, err := s.packActions(queryId, messages[messagesPerPack:])
if err != nil {
return nil, err
}
messages = append(messages[:253], rest)
messages = append(messages[:messagesPerPack], rest)
}

var amt = big.NewInt(0)
Expand Down
46 changes: 46 additions & 0 deletions ton/wallet/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,52 @@ func TestWallet_DeployContract(t *testing.T) {
}
}

func TestWallet_DeployContractUsingHW3(t *testing.T) {
seed := strings.Split(_seed, " ")
ctx := api.Client().StickyContext(context.Background())

// init wallet
w, err := FromSeed(api, seed, ConfigHighloadV3{
MessageTTL: 120,
MessageBuilder: func(ctx context.Context, subWalletId uint32) (id uint32, createdAt int64, err error) {
tm := time.Now().Unix() - 30
return uint32(10000 + tm%(1<<23)), tm, nil
},
})
if err != nil {
t.Fatal("FromSeed err:", err.Error())
}
t.Logf("wallet address: %s", w.Address().String())

codeBytes, _ := hex.DecodeString("b5ee9c72410104010020000114ff00f4a413f4bcf2c80b010203844003020009a1b63c43510007a0000061d2421bb1")
code, _ := cell.FromBOC(codeBytes)

buf := make([]byte, 8)
_, _ = rand.Read(buf)
rnd := binary.LittleEndian.Uint64(buf)

addr, _, block, err := w.DeployContractWaitTransaction(ctx, tlb.MustFromTON("0.005"), cell.BeginCell().EndCell(), code, cell.BeginCell().MustStoreUInt(rnd, 64).EndCell())
if err != nil {
t.Fatal("deploy err:", err)
}
t.Logf("contract address: %s", addr.String())

// wait next block to be sure everything updated
block, err = api.WaitForBlock(block.SeqNo + 5).GetMasterchainInfo(ctx)
if err != nil {
t.Fatal("wait master err:", err.Error())
}

res, err := api.WaitForBlock(block.SeqNo).RunGetMethod(ctx, block, addr, "dappka", 5, 10)
if err != nil {
t.Fatal("run err:", err)
}

if res.MustInt(0).Uint64() != 5 || res.MustInt(1).Uint64() != 50 {
t.Fatal("result err:", res.MustInt(0).Uint64(), res.MustInt(1).Uint64())
}
}

func TestWallet_TransferEncrypted(t *testing.T) {
seed := strings.Split(_seed, " ")
ctx := api.Client().StickyContext(context.Background())
Expand Down

0 comments on commit 9b2eadd

Please sign in to comment.