-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip * fix: rename file and set visibility
- Loading branch information
1 parent
00fa9ba
commit 23e8d53
Showing
4 changed files
with
140 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package tests | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"bisonai.com/orakl/node/pkg/chain/utils" | ||
errorsentinel "bisonai.com/orakl/node/pkg/error" | ||
"github.com/klaytn/klaytn/accounts/abi" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAbiCache(t *testing.T) { | ||
functionSignature := "testFunctionSignature" | ||
functionName := "testFunctionName" | ||
abiJSON := `[ | ||
{ | ||
"constant": true, | ||
"inputs": [], | ||
"name": "testFunctionName", | ||
"outputs": [{"name": "", "type": "uint256"}], | ||
"payable": false, | ||
"stateMutability": "view", | ||
"type": "function" | ||
} | ||
]` | ||
parsedAbi, err := abi.JSON(strings.NewReader(abiJSON)) | ||
assert.NoError(t, err) | ||
|
||
// Test SetAbi | ||
utils.SetAbi(functionSignature, &parsedAbi, functionName) | ||
|
||
// Test GetAbi | ||
retrievedAbi, retrievedFunctionName, err := utils.GetAbi(functionSignature) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, retrievedAbi) | ||
assert.Equal(t, functionName, retrievedFunctionName) | ||
|
||
// Ensure the retrieved ABI matches the set ABI | ||
assert.Equal(t, parsedAbi.Methods[functionName].Name, retrievedAbi.Methods[functionName].Name) | ||
|
||
// Test GetAbi for non-existent entry | ||
_, _, err = utils.GetAbi("nonExistentSignature") | ||
assert.ErrorIs(t, err, errorsentinel.ErrChainCachedAbiNotFound) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package utils | ||
|
||
import ( | ||
"sync" | ||
|
||
errorsentinel "bisonai.com/orakl/node/pkg/error" | ||
"github.com/klaytn/klaytn/accounts/abi" | ||
) | ||
|
||
type AbiWithFunctionName struct { | ||
Abi *abi.ABI | ||
FunctionName string | ||
} | ||
|
||
type AbiCache struct { | ||
AbiMap map[string]*AbiWithFunctionName | ||
mu sync.RWMutex | ||
} | ||
|
||
func newAbiCache() *AbiCache { | ||
return &AbiCache{ | ||
AbiMap: make(map[string]*AbiWithFunctionName), | ||
mu: sync.RWMutex{}, | ||
} | ||
} | ||
|
||
var AbiCacheInstance = newAbiCache() | ||
|
||
func GetAbi(functionSignature string) (*abi.ABI, string, error) { | ||
res, err := AbiCacheInstance.getAbi(functionSignature) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
return res.Abi, res.FunctionName, nil | ||
} | ||
|
||
func SetAbi(functionSignature string, abi *abi.ABI, functionName string) { | ||
AbiCacheInstance.setAbi(functionSignature, &AbiWithFunctionName{Abi: abi, FunctionName: functionName}) | ||
} | ||
|
||
func (c *AbiCache) getAbi(functionSignature string) (*AbiWithFunctionName, error) { | ||
c.mu.RLock() | ||
abi, ok := c.AbiMap[functionSignature] | ||
c.mu.RUnlock() | ||
if ok { | ||
return abi, nil | ||
} | ||
return nil, errorsentinel.ErrChainCachedAbiNotFound | ||
} | ||
|
||
func (c *AbiCache) setAbi(functionSignature string, abiWithFunctionName *AbiWithFunctionName) { | ||
c.mu.Lock() | ||
c.AbiMap[functionSignature] = abiWithFunctionName | ||
c.mu.Unlock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters