BunchIndex.go
package datasource
import (
"Saseul/config"
"sync"
)
type BunchIndex struct {
txIndexes map[string]map[string]interface{}
chunkIndexes map[string]map[string]string
hypothesisIndexes map[string]map[string]string
receiptIndexes map[string]map[string]string
mu sync.RWMutex
}
func NewBunchIndex() *BunchIndex {
return &BunchIndex{
txIndexes: make(map[string]map[string]interface{}),
chunkIndexes: make(map[string]map[string]string),
hypothesisIndexes: make(map[string]map[string]string),
receiptIndexes: make(map[string]map[string]string),
}
}
func (bi *BunchIndex) AddTxIndex(data map[string]interface{}) bool {
if data == nil {
return false
}
bi.mu.Lock()
defer bi.mu.Unlock()
hash := data["hash"].(string)
if _, exists := bi.txIndexes[hash]; exists {
return false
}
txCount := len(bi.txIndexes)
txSize := 0
for _, tx := range bi.txIndexes {
if size, ok := tx["size"].(int); ok {
txSize += size
}
}
countLimit := config.BlockTxCountLimit * 3
sizeLimit := config.BlockTxSizeLimit * 3
if txCount >= countLimit || txSize+data["size"].(int) > sizeLimit {
return false
}
bi.txIndexes[hash] = data
return true
}
func (bi *BunchIndex) ExistsTx(hash string) bool {
bi.mu.RLock()
defer bi.mu.RUnlock()
_, exists := bi.txIndexes[hash]
return exists
}
func (bi *BunchIndex) InfoTxs() map[string]int {
bi.mu.RLock()
defer bi.mu.RUnlock()
count := len(bi.txIndexes)
size := 0
for _, tx := range bi.txIndexes {
if txSize, ok := tx["size"].(int); ok {
size += txSize
}
}
return map[string]int{
"count": count,
"size": size,
}
}
func (bi *BunchIndex) RemoveTxs(utime int) bool {
bi.mu.Lock()
defer bi.mu.Unlock()
for hash, tx := range bi.txIndexes {
if timestamp, ok := tx["timestamp"].(int); ok && timestamp < utime {
delete(bi.txIndexes, hash)
}
}
return true
}
func (bi *BunchIndex) FlushTxs() bool {
bi.mu.Lock()
defer bi.mu.Unlock()
bi.txIndexes = make(map[string]map[string]interface{})
return true
}
func (bi *BunchIndex) AddChunkIndex(data map[string]string) bool {
if data == nil {
return false
}
bi.mu.Lock()
defer bi.mu.Unlock()
roundKey := data["round_key"]
signer := data["signer"]
hash := data["hash"]
if bi.chunkIndexes[roundKey] == nil {
bi.chunkIndexes[roundKey] = make(map[string]string)
}
bi.chunkIndexes[roundKey][signer] = hash
return true
}
func (bi *BunchIndex) CountChunks(roundKey string) int {
bi.mu.RLock()
defer bi.mu.RUnlock()
if bi.chunkIndexes[roundKey] == nil {
return 0
}
return len(bi.chunkIndexes[roundKey])
}
func (bi *BunchIndex) RemoveChunks(roundKey string) bool {
bi.mu.Lock()
defer bi.mu.Unlock()
for key := range bi.chunkIndexes {
if key != roundKey {
delete(bi.chunkIndexes, key)
}
}
return true
}
func (bi *BunchIndex) AddHypothesisIndex(data map[string]string) bool {
if data == nil {
return false
}
bi.mu.Lock()
defer bi.mu.Unlock()
roundKey := data["round_key"]
signer := data["signer"]
hash := data["hash"]
if bi.hypothesisIndexes[roundKey] == nil {
bi.hypothesisIndexes[roundKey] = make(map[string]string)
}
bi.hypothesisIndexes[roundKey][signer] = hash
return true
}
func (bi *BunchIndex) CountHypotheses(roundKey string) int {
bi.mu.RLock()
defer bi.mu.RUnlock()
if bi.hypothesisIndexes[roundKey] == nil {
return 0
}
return len(bi.hypothesisIndexes[roundKey])
}
func (bi *BunchIndex) RemoveHypotheses(roundKey string) bool {
bi.mu.Lock()
defer bi.mu.Unlock()
for key := range bi.hypothesisIndexes {
if key != roundKey {
delete(bi.hypothesisIndexes, key)
}
}
return true
}
func (bi *BunchIndex) AddReceiptIndex(data map[string]string) bool {
if data == nil {
return false
}
bi.mu.Lock()
defer bi.mu.Unlock()
roundKey := data["round_key"]
signer := data["signer"]
hash := data["hash"]
if bi.receiptIndexes[roundKey] == nil {
bi.receiptIndexes[roundKey] = make(map[string]string)
}
if _, exists := bi.receiptIndexes[roundKey][signer]; exists || len(bi.receiptIndexes) >= config.ReceiptCountLimit {
return false
}
bi.receiptIndexes[roundKey][signer] = hash
return true
}
func (bi *BunchIndex) CountReceipt(roundKey string) int {
bi.mu.RLock()
defer bi.mu.RUnlock()
if bi.receiptIndexes[roundKey] == nil {
return 0
}
return len(bi.receiptIndexes[roundKey])
}
func (bi *BunchIndex) RemoveReceipts(roundKey string) bool {
bi.mu.Lock()
defer bi.mu.Unlock()
for key := range bi.receiptIndexes {
if key != roundKey {
delete(bi.receiptIndexes, key)
}
}
return true
}BunchIndex 구조체
BunchIndex 구조체NewBunchIndex 함수
NewBunchIndex 함수트랜잭션 관련 메서드
청크 관련 메서드
가설 관련 메서드
영수증 관련 메서드
Last updated