2018-08-20 13:55:43 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
2018-08-27 08:22:21 +00:00
|
|
|
"math"
|
2018-08-20 13:55:43 +00:00
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
2020-01-02 09:10:19 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2018-08-20 13:55:43 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2018-11-14 07:03:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2018-08-20 13:55:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type VerifierTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
backend *backends.SimulatedBackend
|
|
|
|
privKey *ecdsa.PrivateKey
|
|
|
|
from common.Address
|
|
|
|
contractAddress common.Address
|
|
|
|
registry *Registry
|
|
|
|
verifier *Verifier
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVerifierTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, &VerifierTestSuite{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *VerifierTestSuite) SetupTest() {
|
|
|
|
s.setupAccount()
|
|
|
|
s.setupBackendAndContract()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
s.verifier, err = NewVerifier(s.backend, s.contractAddress)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *VerifierTestSuite) setupBackendAndContract() {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
auth := bind.NewKeyedTransactor(s.privKey)
|
|
|
|
alloc := make(core.GenesisAlloc)
|
|
|
|
alloc[auth.From] = core.GenesisAccount{Balance: big.NewInt(133700000)}
|
2018-08-27 08:22:21 +00:00
|
|
|
s.backend = backends.NewSimulatedBackend(alloc, math.MaxInt64)
|
2018-08-20 13:55:43 +00:00
|
|
|
|
|
|
|
s.contractAddress, _, s.registry, err = DeployRegistry(auth, s.backend)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.backend.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *VerifierTestSuite) setupAccount() {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
s.privKey, err = crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.from = crypto.PubkeyToAddress(s.privKey.PublicKey)
|
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
func (s *VerifierTestSuite) add(nodeID enode.ID) {
|
2018-08-20 13:55:43 +00:00
|
|
|
auth := bind.NewKeyedTransactor(s.privKey)
|
2018-11-14 07:03:58 +00:00
|
|
|
_, err := s.registry.Add(auth, nodeID.Bytes())
|
2018-08-20 13:55:43 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.backend.Commit()
|
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
func (s *VerifierTestSuite) generateNodeID() enode.ID {
|
2018-08-20 13:55:43 +00:00
|
|
|
k, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
return enode.PubkeyToIDV4(&k.PublicKey)
|
2018-08-20 13:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *VerifierTestSuite) TestVerifyNode() {
|
|
|
|
id := s.generateNodeID()
|
|
|
|
res := s.verifier.VerifyNode(context.Background(), id)
|
|
|
|
s.Require().False(res)
|
|
|
|
|
|
|
|
s.add(id)
|
|
|
|
|
|
|
|
res = s.verifier.VerifyNode(context.Background(), id)
|
|
|
|
s.Require().True(res)
|
|
|
|
}
|