github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/abi/abi.go (about)

     1  package abi
     2  
     3  import (
     4  	"sync"
     5  	"sync/atomic"
     6  
     7  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
     8  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
     9  )
    10  
    11  type SelectorSyncMap struct {
    12  	sync.Map
    13  }
    14  
    15  func (abiMap *SelectorSyncMap) GetValue(encoding string) *types.Function {
    16  	if function, ok := abiMap.Load(encoding); !ok {
    17  		return nil
    18  	} else {
    19  		return function.(*types.Function)
    20  	}
    21  }
    22  
    23  func (abiMap *SelectorSyncMap) SetValue(encoding string, function *types.Function) {
    24  	abiMap.Store(encoding, function)
    25  }
    26  
    27  func (abiMap *SelectorSyncMap) Count() int64 {
    28  	var cnt atomic.Int64
    29  	countFunc := func(k any, b any) bool {
    30  		cnt.Add(1)
    31  		return true
    32  	}
    33  	abiMap.Range(countFunc)
    34  	return cnt.Load()
    35  }
    36  
    37  func (abiMap *SelectorSyncMap) Keys() []string {
    38  	ret := make([]string, 0, abiMap.Count())
    39  	visit := func(k any, b any) bool {
    40  		name, _ := k.(string)
    41  		ret = append(ret, name)
    42  		return true
    43  	}
    44  	abiMap.Range(visit)
    45  	return ret
    46  }
    47  
    48  func (abiMap *SelectorSyncMap) Values() []types.Function {
    49  	ret := make([]types.Function, 0, abiMap.Count())
    50  	visit := func(k any, b any) bool {
    51  		function, _ := b.(*types.Function)
    52  		ret = append(ret, *function)
    53  		return true
    54  	}
    55  	abiMap.Range(visit)
    56  	return ret
    57  }
    58  
    59  type AddressSyncMap struct {
    60  	sync.Map
    61  }
    62  
    63  func (addrMap *AddressSyncMap) GetValue(addr base.Address) bool {
    64  	if set, ok := addrMap.Load(addr); !ok {
    65  		return false
    66  	} else {
    67  		return set.(bool)
    68  	}
    69  }
    70  
    71  func (addrMap *AddressSyncMap) SetValue(addr base.Address, set bool) {
    72  	addrMap.Store(addr, set)
    73  }
    74  
    75  func (addrMap *AddressSyncMap) Count() int64 {
    76  	var cnt atomic.Int64
    77  	countFunc := func(k any, b any) bool {
    78  		cnt.Add(1)
    79  		return true
    80  	}
    81  	addrMap.Range(countFunc)
    82  	return cnt.Load()
    83  }
    84  
    85  func (addrMap *AddressSyncMap) Keys() []base.Address {
    86  	ret := make([]base.Address, 0, addrMap.Count())
    87  	visit := func(k any, b any) bool {
    88  		addr, _ := k.(base.Address)
    89  		ret = append(ret, addr)
    90  		return true
    91  	}
    92  	addrMap.Range(visit)
    93  	return ret
    94  }
    95  
    96  func (addrMap *AddressSyncMap) Values() []bool {
    97  	ret := make([]bool, 0, addrMap.Count())
    98  	visit := func(k any, b any) bool {
    99  		set, _ := b.(bool)
   100  		ret = append(ret, set)
   101  		return true
   102  	}
   103  	addrMap.Range(visit)
   104  	return ret
   105  }