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

Pruned resolver #299

Closed
wants to merge 11 commits into from
32 changes: 32 additions & 0 deletions boc/cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,5 +436,37 @@ func (c *Cell) GetMerkleRoot() ([32]byte, error) {
var hash [32]byte
copy(hash[:], bytes[1:])
return hash, nil
}

// TODO: move to deserializer
func (c *Cell) isValidMerkleProofCell() bool {
return c.cellType == MerkleProofCell && c.RefsSize() == 1 && c.BitSize() == 280
}

func (c *Cell) CalculateMerkleProofMeta() (int, [32]byte, error) {
if !c.isValidMerkleProofCell() {
return 0, [32]byte{}, errors.New("not valid merkle proof cell")
}
imc, err := newImmutableCell(c.Refs()[0], map[*Cell]*immutableCell{})
if err != nil {
return 0, [32]byte{}, fmt.Errorf("get immutable cell: %w", err)
}
h := imc.Hash(0)
var hash [32]byte
copy(hash[:], h)
depth := imc.Depth(0)
return depth, hash, nil
}

// TODO: or add level as optional parameter to Hash256()
func (c *Cell) Hash256WithLevel(level int) ([32]byte, error) {
// TODO: or check for pruned cell and read hash directly from cell
imc, err := newImmutableCell(c, map[*Cell]*immutableCell{})
if err != nil {
return [32]byte{}, err
}
b := imc.Hash(level)
var h [32]byte
copy(h[:], b)
return h, nil
}
143 changes: 143 additions & 0 deletions liteapi/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package liteapi

import (
"context"
"errors"
"fmt"
"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/tlb"
"github.com/tonkeeper/tongo/ton"
)

// GetAccountWithProof
// For safe operation, always use GetAccountWithProof with WithBlock(proofedBlock ton.BlockIDExt), as the proof of masterchain cashed blocks is not implemented yet!
func (c *Client) GetAccountWithProof(ctx context.Context, accountID ton.AccountID) (*tlb.ShardAccount, *tlb.ShardStateUnsplit, error) {
res, err := c.GetAccountStateRaw(ctx, accountID) // TODO: add proof check for masterHead
if err != nil {
return nil, nil, err
}
blockID := res.Id.ToBlockIdExt()
if len(res.Proof) == 0 {
return nil, nil, errors.New("empty proof")
}

var blockHash ton.Bits256
if (accountID.Workchain == -1 && blockID.Workchain == -1) || blockID == res.Shardblk.ToBlockIdExt() {
blockHash = blockID.RootHash
} else {
if len(res.ShardProof) == 0 {
return nil, nil, errors.New("empty shard proof")
}
if res.Shardblk.RootHash == [32]byte{} { // TODO: how to check for empty shard?
return nil, nil, errors.New("shard block not passed")
}
shardHash := ton.Bits256(res.Shardblk.RootHash)
if err := checkShardInMasterProof(blockID, res.ShardProof, accountID.Workchain, shardHash); err != nil {
return nil, nil, fmt.Errorf("shard proof is incorrect: %w", err)
}
blockHash = shardHash
}
cellsMap := make(map[[32]byte]*boc.Cell)
if len(res.State) > 0 {
stateCells, err := boc.DeserializeBoc(res.State)
if err != nil {
return nil, nil, fmt.Errorf("state deserialization failed: %w", err)
}
hash, err := stateCells[0].Hash256()
if err != nil {
return nil, nil, fmt.Errorf("get hash err: %w", err)
}
cellsMap[hash] = stateCells[0]
}
shardState, err := checkBlockShardStateProof(res.Proof, blockHash, cellsMap)
if err != nil {
return nil, nil, fmt.Errorf("incorrect block proof: %w", err)
}
values := shardState.ShardStateUnsplit.Accounts.Values()
keys := shardState.ShardStateUnsplit.Accounts.Keys()
for i, k := range keys {
if k == accountID.Address {
return &values[i], shardState, nil
}
}
if len(res.State) == 0 {
return &tlb.ShardAccount{Account: tlb.Account{SumType: "AccountNone"}}, shardState, nil
}
return nil, nil, errors.New("invalid account state")
}

func checkShardInMasterProof(master ton.BlockIDExt, shardProof []byte, workchain int32, shardRootHash ton.Bits256) error {
shardState, err := checkBlockShardStateProof(shardProof, master.RootHash, nil)
if err != nil {
return fmt.Errorf("check block proof failed: %w", err)
}
if !shardState.ShardStateUnsplit.Custom.Exists {
return fmt.Errorf("not a masterchain block")
}
stateExtra := shardState.ShardStateUnsplit.Custom.Value.Value
keys := stateExtra.ShardHashes.Keys()
values := stateExtra.ShardHashes.Values()
for i, k := range keys {
binTreeValues := values[i].Value.BinTree.Values
for _, b := range binTreeValues {
switch b.SumType {
case "Old":
if int32(k) == workchain && ton.Bits256(b.Old.RootHash) == shardRootHash {
return nil
}
case "New":
if int32(k) == workchain && ton.Bits256(b.New.RootHash) == shardRootHash {
return nil
}
}
}
}
return fmt.Errorf("required shard hash not found in proof")
}

func checkBlockShardStateProof(proof []byte, blockRootHash ton.Bits256, cellsMap map[[32]byte]*boc.Cell) (*tlb.ShardStateUnsplit, error) {
proofCells, err := boc.DeserializeBoc(proof)
if err != nil {
return nil, err
}
if len(proofCells) != 2 {
return nil, errors.New("must be two root cells")
}
newStateHash, err := checkBlockProof(proofCells[0], blockRootHash)
if err != nil {
return nil, fmt.Errorf("incorrect block proof: %w", err)
}
var stateProof struct {
Proof tlb.MerkleProof[tlb.ShardStateUnsplit]
}
decoder := tlb.NewDecoder()
if cellsMap != nil {
decoder = decoder.WithPrunedResolver(func(hash tlb.Bits256) (*boc.Cell, error) {
cell, ok := cellsMap[hash]
if ok {
return cell, nil
}
return nil, errors.New("not found")
})
}
err = decoder.Unmarshal(proofCells[1], &stateProof)
if err != nil {
return nil, err
}
if stateProof.Proof.VirtualHash != *newStateHash {
return nil, errors.New("invalid virtual hash")
}
return &stateProof.Proof.VirtualRoot, nil
}

func checkBlockProof(proof *boc.Cell, blockRootHash ton.Bits256) (*tlb.Bits256, error) {
var res tlb.MerkleProof[tlb.Block]
err := tlb.Unmarshal(proof, &res) // merkle hash and depth checks inside
if err != nil {
return nil, fmt.Errorf("failed to unmarshal block proof: %w", err)
}
if ton.Bits256(res.VirtualHash) != blockRootHash {
return nil, fmt.Errorf("invalid block root hash")
}
return &res.VirtualRoot.StateUpdate.ToHash, nil // return new_hash field of MerkleUpdate of ShardState
}
171 changes: 171 additions & 0 deletions liteapi/account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package liteapi

import (
"bytes"
"context"
"encoding/base64"
"errors"
"fmt"
"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/tlb"
"github.com/tonkeeper/tongo/ton"
"testing"
)

func TestGetAccountWithProof(t *testing.T) {
api, err := NewClient(Testnet(), FromEnvs())
if err != nil {
t.Fatal(err)
}
testCases := []struct {
name string
accountID string
}{
{
name: "account from masterchain",
accountID: "-1:34517c7bdf5187c55af4f8b61fdc321588c7ab768dee24b006df29106458d7cf",
},
{
name: "active account from basechain",
accountID: "0:e33ed33a42eb2032059f97d90c706f8400bb256d32139ca707f1564ad699c7dd",
},
{
name: "nonexisted from basechain",
accountID: "0:5f00decb7da51881764dc3959cec60609045f6ca1b89e646bde49d492705d77c",
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
accountID, err := ton.AccountIDFromRaw(tt.accountID)
if err != nil {
t.Fatal("AccountIDFromRaw() failed: %w", err)
}
acc, st, err := api.GetAccountWithProof(context.TODO(), accountID)
if err != nil {
t.Fatal(err)
}
fmt.Printf("Account status: %v\n", acc.Account.Status())
fmt.Printf("Last proof utime: %v\n", st.ShardStateUnsplit.GenUtime)
})
}
}

func TestUnmarshallingProofWithPrunedResolver(t *testing.T) {
testCases := []struct {
name string
accountID string
state string
proof string
}{
{
name: "account from masterchain",
accountID: "-1:34517c7bdf5187c55af4f8b61fdc321588c7ab768dee24b006df29106458d7cf",
state: "te6ccgEBAQEANwAAac/zRRfHvfUYfFWvT4th/cMhWIx6t2je4ksAbfKRBkWNfPICV8MiQ7WQAAAnwcjbgQjD0JAE",
proof: "te6ccgECMQIABnwBAAlGAzm5ngf8wRtgCPSbEv1KYCOfL3YI9/HjNbeRsayNPbNBAcQCCUYDNjyZxQ6TS+uioSqhEmArXFMzcJ0iBgOO8gRScN1HDdQAFykkW5Ajr+L////9AP////8AAAAAAAAAAAFZWXUAAAAAZtWqnAAAFybEzYeEAVlZcmADBAUGKEgBAaHRVuPHjzLUmEYd44x/vzWcCD0Yz14taK8l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EY9W7RvcSWANvlIgyLGvngMPQkBwpuh9/a8Q8liD7J/k6cDctwlpd7/wLfSj2ZkV3vQyWYAABPg5G3AgwmKEgBAdYv7OF3BCOejG/QrCN6d0U1gfBqoWUQPs3lCtkW0bqjAAgoSAEBfNH4EBmmQQpGeuMDQrdiJrcg1/foaCtvYt8A2eULqDYAAChIAQGyDjajs2pM3uYBEGxkLpBxiwpY2vIAdT27MYn5VrSUtgABKEgBAfXuY7WxrmX661SjWg783A5GU3G2ZUuk96jxo07FszvmAcAkEBHvVar////9KissLQGgm8ephwAAAAAEAQFZWXUAAAAAAP////8AAAAAAAAAAGbVqpwAABcmxM2HgAAAFybEzYeEjIe6qAAErqgBWVlyAVlQJsQAAAAIAAAAAAAAAe4uKEgBASkmYQFN/IwKZw+6jDvG7Hla0bypRJASLgCSLllgZYYxAAMqigQUxb9ElcbqpVZzQQGVtjJWkzZu/gqQV6cRwEqnJT7ljzm5ngf8wRtgCPSbEv1KYCOfL3YI9/HjNbeRsayNPbNBAcQBxC8wKEgBAfZANQckARh74l3KoHg6MIoIlCtXCklSokH5oFnYkvWaAAcAmAAAFybEvkVEAVlZdIw41mHg1tiTlZWmUEC5Zs1iJSaJiU/PG7sL/HsqfBj+zYXmULmtzn4TRGwnVVC5tKAhaIUDbFZrLZ+xVZ8cOhpojAEDFMW/RJXG6qVWc0EBlbYyVpM2bv4KkFenEcBKpyU+5Y+0LNwg0RHTx+GvVrTHWlXSAsJOr1Re1+VF1o0FxmRgmwHEABVojAEDObmeB/zBG2AI9JsS/UpgI58vdgj38eM1t5GxrI09s0EncQaO3Qwlxbnasj2PyljXoXXcs0VfOqaRU3MLD/XjOwHEABU=",
},
{
name: "active account from basechain",
accountID: "0:e33ed33a42eb2032059f97d90c706f8400bb256d32139ca707f1564ad699c7dd",
state: "te6ccgECRgEACUQAAnPADjPtM6QusgMgWfl9kMcG+EALslbTITnKcH8VZK1pnH3SjJBKAzalNagAAFyBA05ODYC7pLkMcNNAAQIBFP8A9KQ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kF3wIJUiI7PTg32Ejju+cdCEhP3rVfdUykAsuzXJC+XeAhoCGj0+KEgBAdzATRK2nM22UX18oy9xQvXwCJ9Zf54Cg36vVlziEuIcAAgAmAAAFybGtc+EAVlZkEB8nWe27+renBlk3N5pP74pZf4e/YaTiin6gyymFVdlCBlRGKzjN1o9rQaXqNId79Q/aFWYvsjym94bNWRyCj8AmAAAFybGtc+CAXI8dnR1t2tl9ygPFKytIrYccschqEVLVJKRzfGoXydZLkF/V+9JqqYAgWOFo1SWBohYySfyS4Jzv7iZCQya5q+vgNJojAEDkI7m7wcheM73k/3eti1uVM5fcAFaPvaMxueUOKGa/5Au1SPeq+s/fkBEbdDR9O8KVspDwcDI3pfnn1mShOrlfAIaABpojAEDXfAglSIjs9ODfYSOO75x0ISE/etV91TKQCy7NckL5d4d2zEQnvPwYNp0OmphoUWBv1hhDLQJv0uX98Ed7i21wgIaABs=",
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
accountID, err := ton.AccountIDFromRaw(tt.accountID)
if err != nil {
t.Fatal("AccountIDFromRaw() failed: %w", err)
}
state, err := base64.StdEncoding.DecodeString(tt.state)
if err != nil {
t.Fatal("base64 decoding failed: %w", err)
}
proof, err := base64.StdEncoding.DecodeString(tt.proof)
if err != nil {
t.Fatal("base64 decoding failed: %w", err)
}
stateCells, err := boc.DeserializeBoc(state)
if err != nil {
t.Fatal("DeserializeBoc() failed: %w", err)
}
proofCells, err := boc.DeserializeBoc(proof)
if err != nil {
t.Fatal("DeserializeBoc() failed: %w", err)
}
hash, err := stateCells[0].Hash256()
if err != nil {
t.Fatal("Get hash failed: %w", err)
}
cellsMap := map[[32]byte]*boc.Cell{hash: stateCells[0]}
if err != nil {
t.Fatal("Get NonPrunedCells() failed: %w", err)
}
decoder := tlb.NewDecoder().WithDebug().WithPrunedResolver(func(hash tlb.Bits256) (*boc.Cell, error) {
cell, ok := cellsMap[hash]
if ok {
return cell, nil
}
return nil, errors.New("not found")
})
var stateProof struct {
Proof tlb.MerkleProof[tlb.ShardStateUnsplit]
}
err = decoder.Unmarshal(proofCells[1], &stateProof)
if err != nil {
t.Fatal("proof unmarshalling failed: %w", err)
}
values := stateProof.Proof.VirtualRoot.ShardStateUnsplit.Accounts.Values()
keys := stateProof.Proof.VirtualRoot.ShardStateUnsplit.Accounts.Keys()
for i, k := range keys {
if bytes.Equal(k[:], accountID.Address[:]) {
fmt.Printf("Account status: %v\n", values[i].Account.Status())
}
}
})
}
}

func TestGetAccountWithProofForBlock(t *testing.T) {
api, err := NewClient(Testnet(), FromEnvs())
if err != nil {
t.Fatal(err)
}
testCases := []struct {
name string
accountID string
block string
}{
{
name: "active account from basechain",
accountID: "0:e33ed33a42eb2032059f97d90c706f8400bb256d32139ca707f1564ad699c7dd",
block: "(0,e000000000000000,24681072)",
},
{
name: "account from masterchain",
accountID: "-1:34517c7bdf5187c55af4f8b61fdc321588c7ab768dee24b006df29106458d7cf",
block: "(-1,8000000000000000,23040403)",
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
accountID, err := ton.AccountIDFromRaw(tt.accountID)
if err != nil {
t.Fatal("AccountIDFromRaw() failed: %w", err)
}
b, err := ton.ParseBlockID(tt.block)
if err != nil {
t.Fatal("ParseBlockID() failed: %w", err)
}
block, _, err := api.LookupBlock(context.TODO(), b, 1, nil, nil)
if err != nil {
t.Fatal("LookupBlock() failed: %w", err)
}
acc, st, err := api.WithBlock(block).GetAccountWithProof(context.TODO(), accountID)
if err != nil {
t.Fatal(err)
}
fmt.Printf("Account status: %v\n", acc.Account.Status())
fmt.Printf("Last proof utime: %v\n", st.ShardStateUnsplit.GenUtime)
})
}
}
3 changes: 3 additions & 0 deletions liteapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,9 @@ func (c *Client) GetAccountStateRaw(ctx context.Context, accountID ton.AccountID
if err != nil {
return liteclient.LiteServerAccountStateC{}, err
}
if res.Id.ToBlockIdExt() != blockID {
return liteclient.LiteServerAccountStateC{}, errors.New("invalid block ID")
}
return res, nil
}

Expand Down
7 changes: 7 additions & 0 deletions tlb/bintree.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ func (b *BinTree[T]) UnmarshalTLB(c *boc.Cell, decoder *Decoder) error {
}
b.Values = make([]T, 0, len(dec))
for _, i := range dec {
if i.CellType() == boc.PrunedBranchCell {
cell := resolvePrunedCell(c, decoder.resolvePruned)
if cell == nil {
continue
}
i = cell
}
var t T
err := decoder.Unmarshal(i, &t)
if err != nil {
Expand Down
Loading
Loading