Skip to content

Commit

Permalink
Add kyb to kyc verified check, add tests, update caminogo dependency (#…
Browse files Browse the repository at this point in the history
…104)

(cherry picked from commit 9ffb0e2)
  • Loading branch information
evlekht committed Aug 2, 2024
1 parent 9b08f4f commit 382c5ef
Show file tree
Hide file tree
Showing 5 changed files with 144 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(big.NewInt(int64(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()...))
}
75 changes: 75 additions & 0 deletions eth/ethadmin/camino_ethadmin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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)
sunriseTimestampBig := big.NewInt(0).SetUint64(sunriseTimestamp)
sunriseActiveHeader := &types.Header{
Time: sunriseTimestamp,
}

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

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)
})
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ require (
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a
github.com/tyler-smith/go-bip39 v1.0.2
github.com/urfave/cli/v2 v2.10.2
go.uber.org/mock v0.2.0
golang.org/x/crypto v0.12.0
golang.org/x/sync v0.1.0
golang.org/x/sys v0.11.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,8 @@ go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA=
go.uber.org/mock v0.2.0 h1:TaP3xedm7JaAgScZO7tlvlKrqT0p7I6OsdGB5YNSMDU=
go.uber.org/mock v0.2.0/go.mod h1:J0y0rp9L3xiff1+ZBfKxlC1fz2+aO16tw0tsDOixfuM=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
Expand Down

0 comments on commit 382c5ef

Please sign in to comment.