2019-08-14 14:09:24 -07:00
|
|
|
package permission
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
2019-08-23 06:49:04 -07:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2019-08-19 06:39:18 -07:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
|
2019-08-23 06:49:04 -07:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
|
|
|
"github.com/ethereum/go-ethereum/eth"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2019-08-14 14:09:24 -07: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"
|
2019-08-23 06:49:04 -07:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2019-08-14 14:09:24 -07:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2019-08-23 06:49:04 -07:00
|
|
|
"github.com/ethereum/go-ethereum/node"
|
2019-08-19 06:39:18 -07:00
|
|
|
pbind "github.com/ethereum/go-ethereum/permission/bind"
|
2019-08-14 14:09:24 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPermissionCtrl_InitializeService(t *testing.T) {
|
2019-08-19 06:39:18 -07:00
|
|
|
guardianKey, _ := crypto.GenerateKey()
|
|
|
|
guardianAddress := crypto.PubkeyToAddress(guardianKey.PublicKey)
|
|
|
|
|
|
|
|
guardianTransactor := bind.NewKeyedTransactor(guardianKey)
|
2019-08-14 14:09:24 -07:00
|
|
|
genesisAlloc := map[common.Address]core.GenesisAccount{
|
2019-08-19 06:39:18 -07:00
|
|
|
guardianAddress: {
|
2019-08-14 14:09:24 -07:00
|
|
|
Balance: big.NewInt(100000000000000),
|
|
|
|
},
|
|
|
|
}
|
2019-08-23 06:49:04 -07:00
|
|
|
// Create a networkless protocol stack and start an Ethereum service within
|
|
|
|
stack, err := node.New(&node.Config{
|
|
|
|
DataDir: "",
|
|
|
|
UseLightweightKDF: true,
|
|
|
|
P2P: p2p.Config{
|
|
|
|
PrivateKey: guardianKey,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create node: %v", err)
|
|
|
|
}
|
|
|
|
ethConf := ð.Config{
|
|
|
|
Genesis: &core.Genesis{Config: params.AllEthashProtocolChanges, GasLimit: 10000000000, Alloc: genesisAlloc},
|
|
|
|
Etherbase: guardianAddress,
|
|
|
|
Ethash: ethash.Config{
|
|
|
|
PowMode: ethash.ModeTest,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err = stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { return eth.New(ctx, ethConf) }); err != nil {
|
|
|
|
t.Fatalf("failed to register Ethereum protocol: %v", err)
|
|
|
|
}
|
|
|
|
// Start the node and assemble the JavaScript console around it
|
|
|
|
if err = stack.Start(); err != nil {
|
|
|
|
t.Fatalf("failed to start test stack: %v", err)
|
|
|
|
}
|
|
|
|
var ethereum *eth.Ethereum
|
|
|
|
if err := stack.Service(ðereum); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
sb := backends.NewSimulatedBackendFrom(ethereum)
|
2019-08-14 14:09:24 -07:00
|
|
|
|
2019-08-19 06:39:18 -07:00
|
|
|
permUpgrAddress, _, permUpgrInstance, err := pbind.DeployPermUpgr(guardianTransactor, sb, guardianAddress)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
permInterfaceAddress, _, _, err := pbind.DeployPermInterface(guardianTransactor, sb, permUpgrAddress)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
nodeManagerAddress, _, _, err := pbind.DeployNodeManager(guardianTransactor, sb, permUpgrAddress)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
roleManagerAddress, _, _, err := pbind.DeployRoleManager(guardianTransactor, sb, permUpgrAddress)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
accountManagerAddress, _, _, err := pbind.DeployAcctManager(guardianTransactor, sb, permUpgrAddress)
|
2019-08-14 14:09:24 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-08-19 06:39:18 -07:00
|
|
|
orgManagerAddress, _, _, err := pbind.DeployOrgManager(guardianTransactor, sb, permUpgrAddress)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
voterManagerAddress, _, _, err := pbind.DeployVoterManager(guardianTransactor, sb, permUpgrAddress)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
permImplAddress, _, _, err := pbind.DeployPermImpl(guardianTransactor, sb, permUpgrAddress, orgManagerAddress, roleManagerAddress, accountManagerAddress, voterManagerAddress, nodeManagerAddress)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// call init
|
|
|
|
if _, err := permUpgrInstance.Init(guardianTransactor, permInterfaceAddress, permImplAddress); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-08-23 06:49:04 -07:00
|
|
|
testObject, err := NewQuorumPermissionCtrl(stack, &types.PermissionConfig{
|
2019-08-19 06:39:18 -07:00
|
|
|
UpgrdAddress: permUpgrAddress,
|
|
|
|
InterfAddress: permInterfaceAddress,
|
|
|
|
ImplAddress: permImplAddress,
|
|
|
|
NodeAddress: nodeManagerAddress,
|
|
|
|
AccountAddress: accountManagerAddress,
|
|
|
|
RoleAddress: roleManagerAddress,
|
|
|
|
VoterAddress: voterManagerAddress,
|
|
|
|
OrgAddress: orgManagerAddress,
|
|
|
|
NwAdminOrg: "NETWORK_ADMIN",
|
|
|
|
NwAdminRole: "NETWORK_ADMIN_ROLE",
|
|
|
|
OrgAdminRole: "ORG_ADMIN_ROLE",
|
|
|
|
Accounts: []common.Address{
|
|
|
|
guardianAddress,
|
|
|
|
},
|
|
|
|
SubOrgDepth: *big.NewInt(3),
|
|
|
|
SubOrgBreadth: *big.NewInt(3),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-08-23 06:49:04 -07:00
|
|
|
testObject.ethClnt = sb
|
|
|
|
testObject.eth = ethereum
|
|
|
|
go func() {
|
|
|
|
testObject.errorChan <- nil
|
|
|
|
}()
|
2019-08-19 06:39:18 -07:00
|
|
|
|
2019-08-23 06:49:04 -07:00
|
|
|
err = testObject.AfterStart()
|
2019-08-19 06:39:18 -07:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2019-08-14 14:09:24 -07:00
|
|
|
}
|