This commit is contained in:
thloyi
2026-01-08 11:57:57 +08:00
parent d9aea3e8d7
commit 4c0abc5c34
4 changed files with 85 additions and 30 deletions

View File

@@ -18,8 +18,9 @@ type TableInfo struct {
}
type AddressTables struct {
showTableLoaded bool
rpcClient *rpc.Client
mux sync.RWMutex
loadMux sync.Mutex
tables *lru.Cache[solana.PublicKey, *TableInfo]
loading map[solana.PublicKey]struct{}
@@ -27,7 +28,7 @@ type AddressTables struct {
pool *ants.Pool
}
func NewAddressTables(rpcClient *rpc.Client) *AddressTables {
func NewAddressTables(rpcClient *rpc.Client, showTableLoaded bool) *AddressTables {
pool, _ := ants.NewPool(5, ants.WithPreAlloc(true), ants.WithNonblocking(true))
cache, _ := lru.New[solana.PublicKey, *TableInfo](10000)
return &AddressTables{
@@ -35,6 +36,8 @@ func NewAddressTables(rpcClient *rpc.Client) *AddressTables {
tables: cache,
loading: make(map[solana.PublicKey]struct{}),
pool: pool,
showTableLoaded: showTableLoaded,
}
}
@@ -83,25 +86,43 @@ func (at *AddressTables) load(tablePubkey solana.PublicKey) {
delete(at.loading, tablePubkey)
at.loadMux.Unlock()
at.mux.Lock()
at.tables.Add(tablePubkey, &TableInfo{
addresses: table,
})
total := at.tables.Len()
at.mux.Unlock()
logger.Info("loadAddressTable", "table", tablePubkey.String(), "table count:", total)
if at.showTableLoaded {
total := at.tables.Len()
logger.Info("loadAddressTable", "table", tablePubkey.String(), "table count:", total)
}
})
}
func (at *AddressTables) GetAddressTable(tablePubkey solana.PublicKey, idx []uint8) []solana.PublicKey {
at.mux.RLock()
func (at *AddressTables) FillToTx(tx *versionedTransaction, tablePubkey solana.PublicKey, idx []uint8) bool {
addresses, ok := at.tables.Get(tablePubkey)
if !ok {
at.load(tablePubkey)
return false
}
for _, i := range idx {
if int(i) >= len(addresses.addresses) {
logger.Error("over loadAddressTable failed", "idx", i, "table", tablePubkey)
addresses.overErrCount++
if addresses.overErrCount > 10 {
at.load(tablePubkey)
}
return false
}
tx.Message.StaticAccountKeys = append(tx.Message.StaticAccountKeys, addresses.addresses[i])
}
return true
}
func (at *AddressTables) GetAddressTable(tablePubkey solana.PublicKey, idx []uint8) []solana.PublicKey {
addresses, ok := at.tables.Get(tablePubkey)
if !ok {
at.mux.RUnlock()
at.load(tablePubkey)
return nil
}
at.mux.RUnlock()
var result solana.PublicKeySlice = make([]solana.PublicKey, 0, len(idx))
for _, i := range idx {