package tonapiservice
import (
"fmt"
"testing"
"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/tlb"
)
func TestHashmapE(t *testing.T) {
// Hashmap的序列化有bug,数据一样的情况下,有时候会提示not enouth bits.
c := boc.NewCell()
//write function id to cell
if err := c.WriteUint(uint64(0), 32); err != nil {
panic(err)
}
type BuyInfo struct {
StartLuckNum uint16 // 开始的彩票号码,从1开始
Amount uint16 // 购买的nifi数量
}
// go map
orders := map[uint32]BuyInfo{
1: {StartLuckNum: 1, Amount: 2},
2: {StartLuckNum: 3, Amount: 4},
3: {StartLuckNum: 5, Amount: 6},
4: {StartLuckNum: 7, Amount: 8},
5: {StartLuckNum: 9, Amount: 10},
6: {StartLuckNum: 11, Amount: 12},
7: {StartLuckNum: 13, Amount: 14},
8: {StartLuckNum: 15, Amount: 16},
9: {StartLuckNum: 17, Amount: 18},
10: {StartLuckNum: 19, Amount: 20},
}
type Msg struct {
RoundId uint32
Orders tlb.Hashmap[tlb.Uint32, BuyInfo] // 订单号码:购买信息
}
// elb map
msg := Msg{
RoundId: 10000,
}
var keys []tlb.Uint32
var values []BuyInfo
for key, value := range orders {
keys = append(keys, tlb.Uint32(key))
values = append(values, value)
}
msg.Orders = tlb.NewHashmap(keys, values)
// 序列化
if err := tlb.Marshal(c, msg); err != nil {
panic(err)
}
b, _ := c.ToBoc()
fmt.Println(b)
}
这个代码有时候会提示not enouth bits,有时候又正常。
标签:toogo,err,keys,tonkeeper,tlb,Amount,StartLuckNum,序列化 From: https://www.cnblogs.com/zhanchenjin/p/18476108