Skip to content

Commit

Permalink
Add kyb to kyc verified check, add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
evlekht committed Jul 25, 2024
1 parent 6ddf0fe commit a8dadea
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 6 deletions.
54 changes: 54 additions & 0 deletions core/admin/camino_mock_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions eth/ethadmin/camino_ethadmin.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ var (
)

const (
KYC_VERIFIED = 1
KYC_EXPIRED = 2
NOT_VERIFIED = 0b0000 // 0
KYC_VERIFIED = 0b0001 // 1, bit 0
KYC_EXPIRED = 0b0010 // 2, bit 1
KYB_VERIFIED = 0b1000 // 8, bit 3

VERIFIED = KYC_VERIFIED | KYB_VERIFIED
)

type AdminControllerBackend interface {
Expand Down Expand Up @@ -139,16 +143,18 @@ func (a *AdminController) GetFixedBaseFee(head *types.Header, state admin.StateD

func (a *AdminController) KycVerified(head *types.Header, state admin.StateDB, addr common.Address) bool {
if a.cfg.IsSunrisePhase0(head.Time) {
// Calculate storage position
storagePos := crypto.Keccak256Hash(append(addr.Hash().Bytes(), common.HexToHash("0x2").Bytes()...))
// Get the KYC states
kycStates := new(big.Int).SetBytes(state.GetState(contractAddr, storagePos).Bytes()).Uint64()
kycStates := new(big.Int).SetBytes(state.GetState(contractAddr, kycStoragePosition(addr)).Bytes()).Uint64()
// Return true if KYC flag is set
return (kycStates & KYC_VERIFIED) != 0
return (kycStates & VERIFIED) != 0
}
return true
}

func (a *AdminController) inScanRange(head *types.Header) bool {
return head.Number.Cmp(&a.lastChangeHeight) >= 0 && head.Number.Cmp(&a.scanHeight) <= 0
}

func kycStoragePosition(addr common.Address) common.Hash {
return crypto.Keccak256Hash(append(addr.Hash().Bytes(), common.HexToHash("0x2").Bytes()...))
}
74 changes: 74 additions & 0 deletions eth/ethadmin/camino_ethadmin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package ethadmin

import (
"math/big"
"testing"

"github.com/ava-labs/coreth/core/admin"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/params"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

func TestKycVerified(t *testing.T) {
address := common.Address{1}
sunriseTimestamp := uint64(1000)
sunriseActiveHeader := &types.Header{
Time: sunriseTimestamp,
}

adminCtrl := NewController(nil, &params.ChainConfig{
SunrisePhase0BlockTimestamp: &sunriseTimestamp,
})

tests := map[string]struct {
stateDB func(c *gomock.Controller) *admin.MockStateDB
header *types.Header
address common.Address
expectedResult bool
}{
"Not verified": {
stateDB: func(c *gomock.Controller) *admin.MockStateDB {
stateDB := admin.NewMockStateDB(c)
stateDB.EXPECT().GetState(contractAddr, kycStoragePosition(address)).
Return(common.BigToHash(big.NewInt(NOT_VERIFIED)))
return stateDB
},
header: sunriseActiveHeader,
address: address,
expectedResult: false,
},
"KYC verified": {
stateDB: func(c *gomock.Controller) *admin.MockStateDB {
stateDB := admin.NewMockStateDB(c)
stateDB.EXPECT().GetState(contractAddr, kycStoragePosition(address)).
Return(common.BigToHash(big.NewInt(KYC_VERIFIED)))
return stateDB
},
header: sunriseActiveHeader,
address: address,
expectedResult: true,
},
"KYB verified": {
stateDB: func(c *gomock.Controller) *admin.MockStateDB {
stateDB := admin.NewMockStateDB(c)
stateDB.EXPECT().GetState(contractAddr, kycStoragePosition(address)).
Return(common.BigToHash(big.NewInt(KYB_VERIFIED)))
return stateDB
},
header: sunriseActiveHeader,
address: address,
expectedResult: true,
},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
c := gomock.NewController(t)
result := adminCtrl.KycVerified(tt.header, tt.stateDB(c), tt.address)
require.Equal(t, tt.expectedResult, result)
})
}
}

0 comments on commit a8dadea

Please sign in to comment.