Skip to content

Commit

Permalink
MN-805: refactor insolard and genesis (#91)
Browse files Browse the repository at this point in the history
* MN-805: refactor insolard & genesis

* MN-805: refactoring genesis files

* MN-805: refactoring genesis files

Co-authored-by: pavel-moskovkin <[email protected]>
  • Loading branch information
pavlushev and pavel-moskovkin authored Oct 19, 2020
1 parent 580deef commit 797f7fb
Show file tree
Hide file tree
Showing 29 changed files with 941 additions and 1,227 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ $(BIN_DIR):

.PHONY: $(INSOLARD)
$(INSOLARD):
$(GOBUILD) -o $(BIN_DIR)/$(INSOLARD) ${BUILD_TAGS} -ldflags "${LDFLAGS}" cmd/insolard/*.go
$(GOBUILD) -o $(BIN_DIR)/$(INSOLARD) ${BUILD_TAGS} -ldflags "${LDFLAGS}" application/cmd/insolard/*.go

.PHONY: $(INSOLAR)
$(INSOLAR):
Expand Down
22 changes: 6 additions & 16 deletions application/appfoundation/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,34 @@ import (
"regexp"

"github.com/insolar/insolar/insolar"
"github.com/insolar/mainnet/application/genesisrefs"
"github.com/insolar/mainnet/application/genesis"
)

const AllowedVersionSmartContract = 2

// Get reference CostCenter contract.
func GetCostCenter() insolar.Reference {
return genesisrefs.ContractCostCenter
return genesis.ContractCostCenter
}

// Get reference MigrationAdminMember contract.
func GetMigrationAdminMember() insolar.Reference {
return genesisrefs.ContractMigrationAdminMember
}

// Get reference RootMember contract.
func GetRootMember() insolar.Reference {
return genesisrefs.ContractRootMember
return genesis.ContractMigrationAdminMember
}

// Get reference on MigrationAdmin contract.
func GetMigrationAdmin() insolar.Reference {
return genesisrefs.ContractMigrationAdmin
}

// Get reference on RootDomain contract.
func GetRootDomain() insolar.Reference {
return genesisrefs.ContractRootDomain
return genesis.ContractMigrationAdmin
}

// Get reference on migrationdaemon contract by migration member.
func GetMigrationDaemon(migrationMember insolar.Reference) (insolar.Reference, error) {
return genesisrefs.ContractMigrationMap[migrationMember], nil
return genesis.ContractMigrationMap[migrationMember], nil
}

// Check member is migration daemon member or not
func IsMigrationDaemonMember(member insolar.Reference) bool {
for _, mDaemonMember := range genesisrefs.ContractMigrationDaemonMembers {
for _, mDaemonMember := range genesis.ContractMigrationDaemonMembers {
if mDaemonMember.Equal(member) {
return true
}
Expand Down
4 changes: 2 additions & 2 deletions application/builtin/contract/deposit/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/insolar/mainnet/application/builtin/proxy/member"
"github.com/insolar/mainnet/application/builtin/proxy/migrationdaemon"
"github.com/insolar/mainnet/application/builtin/proxy/wallet"
"github.com/insolar/mainnet/application/genesisrefs"
"github.com/insolar/mainnet/application/genesis"
)

const (
Expand Down Expand Up @@ -204,7 +204,7 @@ func (d *Deposit) Confirm(
if err != nil {
return errors.Wrap(err, "failed to get wallet")
}
ok, maDeposit, _ := wallet.GetObject(*walletRef).FindDeposit(genesisrefs.FundsDepositName)
ok, maDeposit, _ := wallet.GetObject(*walletRef).FindDeposit(genesis.FundsDepositName)
if !ok {
return fmt.Errorf("failed to find source deposit - %s", walletRef.String())
}
Expand Down
14 changes: 7 additions & 7 deletions application/builtin/contract/member/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/insolar/mainnet/application/builtin/proxy/migrationdaemon"
"github.com/insolar/mainnet/application/builtin/proxy/rootdomain"
"github.com/insolar/mainnet/application/builtin/proxy/wallet"
"github.com/insolar/mainnet/application/genesisrefs"
"github.com/insolar/mainnet/application/genesis"
)

const (
Expand Down Expand Up @@ -475,7 +475,7 @@ func (m *Member) coinBurnCall(params map[string]interface{}) (interface{}, error

// Platform methods.
func (m *Member) registerNode(public string, role string) (interface{}, error) {
root := genesisrefs.ContractRootMember
root := genesis.ContractRootMember
if m.GetReference() != root {
return "", fmt.Errorf("only root member can register node")
}
Expand Down Expand Up @@ -540,7 +540,7 @@ func (m *Member) contractCreateMemberCall(key string) (*CreateResponse, error) {

func (m *Member) contractCreateMember(key string, migrationAddress string) (*member.Member, error) {

rootDomain := rootdomain.GetObject(appfoundation.GetRootDomain())
rootDomain := rootdomain.GetObject(genesis.GetRootDomain())

created, err := m.createMember(key, migrationAddress)
if err != nil {
Expand All @@ -560,19 +560,19 @@ func (m *Member) createMember(key string, migrationAddress string) (*member.Memb
}

aHolder := account.New(ACCOUNT_START_VALUE)
accountRef, err := aHolder.AsChild(appfoundation.GetRootDomain())
accountRef, err := aHolder.AsChild(genesis.GetRootDomain())
if err != nil {
return nil, fmt.Errorf("failed to create account for member: %s", err.Error())
}

wHolder := wallet.New(accountRef.Reference)
walletRef, err := wHolder.AsChild(appfoundation.GetRootDomain())
walletRef, err := wHolder.AsChild(genesis.GetRootDomain())
if err != nil {
return nil, fmt.Errorf("failed to create wallet for member: %s", err.Error())
}

memberHolder := member.New(key, migrationAddress, walletRef.Reference)
created, err := memberHolder.AsChild(appfoundation.GetRootDomain())
created, err := memberHolder.AsChild(genesis.GetRootDomain())
if err != nil {
return nil, fmt.Errorf("failed to save as child: %s", err.Error())
}
Expand All @@ -591,7 +591,7 @@ type GetResponse struct {
}

func (m *Member) memberGet(publicKey string) (interface{}, error) {
rootDomain := rootdomain.GetObject(appfoundation.GetRootDomain())
rootDomain := rootdomain.GetObject(genesis.GetRootDomain())
ref, err := rootDomain.GetMemberByPublicKey(publicKey)
if err != nil {
return nil, fmt.Errorf("failed to get reference by public key: %s", err.Error())
Expand Down
7 changes: 3 additions & 4 deletions application/cmd/insolar/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ package main
import (
"context"

"github.com/spf13/cobra"

basebootstrap "github.com/insolar/insolar/applicationbase/bootstrap"
"github.com/insolar/mainnet/application/bootstrap"
"github.com/insolar/mainnet/application/genesis/contracts"
"github.com/spf13/cobra"
)

func bootstrapCommand() *cobra.Command {
Expand All @@ -26,7 +25,7 @@ func bootstrapCommand() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()

contractsConfig, err := bootstrap.CreateGenesisContractsConfig(ctx, configPath)
contractsConfig, err := contracts.CreateGenesisContractsConfig(ctx, configPath)
check("failed to create genesis contracts config", err)

config, err := basebootstrap.ParseConfig(configPath)
Expand Down
4 changes: 2 additions & 2 deletions application/cmd/insolar/create_default_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
bootstrapbase "github.com/insolar/insolar/applicationbase/bootstrap"
pulsewatcher "github.com/insolar/insolar/cmd/pulsewatcher/config"
"github.com/insolar/insolar/configuration"
"github.com/insolar/mainnet/application/bootstrap"
"github.com/insolar/mainnet/application/genesis/contracts"
)

func writePulsarConfig(outputDir string) {
Expand All @@ -36,7 +36,7 @@ func writeBootstrapConfig(outputDir string) {
panic(err)
}

rawApp, err := yaml.Marshal(bootstrap.ContractsConfig{})
rawApp, err := yaml.Marshal(contracts.ContractsConfig{})
if err != nil {
panic(err)
}
Expand Down
36 changes: 36 additions & 0 deletions application/cmd/insolard/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2020 Insolar Network Ltd.
// All rights reserved.
// This material is licensed under the Insolar License version 1.0,
// available at https://github.com/insolar/insolar/blob/master/LICENSE.md.

package main

import (
"github.com/pkg/errors"

"github.com/insolar/insolar/insolard"
"github.com/insolar/insolar/log"
"github.com/insolar/insolar/logicrunner/builtin"
"github.com/insolar/mainnet/application/appfoundation"
appbuiltin "github.com/insolar/mainnet/application/builtin"
"github.com/insolar/mainnet/application/genesis"
"github.com/insolar/mainnet/application/genesis/contracts"
)

func main() {
parentDomain := genesis.GenesisNameRootDomain
scVersion := int64(appfoundation.AllowedVersionSmartContract)
builtinContracts := builtin.BuiltinContracts{
CodeRegistry: appbuiltin.InitializeContractMethods(),
CodeRefRegistry: appbuiltin.InitializeCodeRefs(),
CodeDescriptors: appbuiltin.InitializeCodeDescriptors(),
PrototypeDescriptors: appbuiltin.InitializePrototypeDescriptors(),
}
apiOptions, err := genesis.InitAPIOptions()
if err != nil {
log.Fatal(errors.Wrap(err, "failed to get API info response"))
}
initStates := contracts.InitStates

insolard.RunInsolarNode(parentDomain, scVersion, apiOptions, initStates, builtinContracts)
}
36 changes: 17 additions & 19 deletions application/functest/fund_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ import (

"github.com/insolar/insolar/applicationbase/testutils/launchnet"
"github.com/insolar/insolar/applicationbase/testutils/testrequest"

"github.com/insolar/mainnet/application"
"github.com/insolar/mainnet/application/genesisrefs"
"github.com/insolar/mainnet/application/genesis"
)

func TestFoundationMemberCreate(t *testing.T) {
for _, m := range Foundation {
err := verifyFundsMembersAndDeposits(t, m, application.FoundationDistributionAmount)
err := verifyFundsMembersAndDeposits(t, m, genesis.FoundationDistributionAmount)
if err != nil {
require.NoError(t, err)
}
Expand All @@ -33,7 +31,7 @@ func TestFoundationMemberCreate(t *testing.T) {

func TestEnterpriseMemberCreate(t *testing.T) {
for _, m := range Enterprise {
err := verifyFundsMembersExist(t, m, application.EnterpriseDistributionAmount)
err := verifyFundsMembersExist(t, m, genesis.EnterpriseDistributionAmount)
if err != nil {
require.NoError(t, err)
}
Expand All @@ -42,17 +40,17 @@ func TestEnterpriseMemberCreate(t *testing.T) {

func TestNetworkIncentivesMemberCreate(t *testing.T) {
// for speed up test check only last member
m := NetworkIncentives[application.GenesisAmountNetworkIncentivesMembers-1]
m := NetworkIncentives[genesis.GenesisAmountNetworkIncentivesMembers-1]

err := verifyFundsMembersAndDeposits(t, m, application.NetworkIncentivesDistributionAmount)
err := verifyFundsMembersAndDeposits(t, m, genesis.NetworkIncentivesDistributionAmount)
if err != nil {
require.NoError(t, err)
}
}

func TestApplicationIncentivesMemberCreate(t *testing.T) {
for _, m := range ApplicationIncentives {
err := verifyFundsMembersAndDeposits(t, m, application.AppIncentivesDistributionAmount)
err := verifyFundsMembersAndDeposits(t, m, genesis.AppIncentivesDistributionAmount)
if err != nil {
require.NoError(t, err)
}
Expand All @@ -64,7 +62,7 @@ func checkBalanceAndDepositFewTimes(t *testing.T, m *AppUser, expectedBalance st
var depositStr string
for i := 0; i < times; i++ {
balance, deposits := getBalanceAndDepositsNoErr(t, m, m.Ref)
depositStr = deposits[genesisrefs.FundsDepositName].(map[string]interface{})["balance"].(string)
depositStr = deposits[genesis.FundsDepositName].(map[string]interface{})["balance"].(string)
if balance.String() == expectedBalance && depositStr == expectedDeposit {
return
}
Expand All @@ -78,7 +76,7 @@ func checkBalanceAndDepositFewTimes(t *testing.T, m *AppUser, expectedBalance st

func TestNetworkIncentivesTransferDeposit(t *testing.T) {
// for speed up test check only last member
lastIdx := application.GenesisAmountNetworkIncentivesMembers - 1
lastIdx := genesis.GenesisAmountNetworkIncentivesMembers - 1
m := NetworkIncentives[lastIdx]

res2, err := testrequest.SignedRequest(t, launchnet.TestRPCUrlPublic, m, "member.get", nil)
Expand All @@ -88,10 +86,10 @@ func TestNetworkIncentivesTransferDeposit(t *testing.T) {
require.True(t, ok, fmt.Sprintf("failed to decode: expected map[string]interface{}, got %T", res2))

_, err = testrequest.SignedRequest(t, launchnet.TestRPCUrlPublic, m,
"deposit.transfer", map[string]interface{}{"amount": "100", "ethTxHash": genesisrefs.FundsDepositName},
"deposit.transfer", map[string]interface{}{"amount": "100", "ethTxHash": genesis.FundsDepositName},
)
require.NoError(t, err)
depositAmount, ok := new(big.Int).SetString(application.NetworkIncentivesDistributionAmount, 10)
depositAmount, ok := new(big.Int).SetString(genesis.NetworkIncentivesDistributionAmount, 10)
require.True(t, ok, "can't parse NetworkIncentivesDistributionAmount")
checkBalanceAndDepositFewTimes(t, m, "100", depositAmount.Sub(depositAmount, big.NewInt(100)).String())
}
Expand All @@ -105,10 +103,10 @@ func TestApplicationIncentivesTransferDeposit(t *testing.T) {
require.True(t, ok, fmt.Sprintf("failed to decode: expected map[string]interface{}, got %T", res2))

_, err = testrequest.SignedRequest(t, launchnet.TestRPCUrlPublic, m,
"deposit.transfer", map[string]interface{}{"amount": "100", "ethTxHash": genesisrefs.FundsDepositName},
"deposit.transfer", map[string]interface{}{"amount": "100", "ethTxHash": genesis.FundsDepositName},
)
require.NoError(t, err)
depositAmount, ok := new(big.Int).SetString(application.AppIncentivesDistributionAmount, 10)
depositAmount, ok := new(big.Int).SetString(genesis.AppIncentivesDistributionAmount, 10)
require.True(t, ok, "can't parse AppIncentivesDistributionAmount")
checkBalanceAndDepositFewTimes(t, m, "100", depositAmount.Sub(depositAmount, big.NewInt(100)).String())
}
Expand All @@ -123,10 +121,10 @@ func TestFoundationTransferDeposit(t *testing.T) {
require.True(t, ok, fmt.Sprintf("failed to decode: expected map[string]interface{}, got %T", res2))

_, err = testrequest.SignedRequest(t, launchnet.TestRPCUrlPublic, m,
"deposit.transfer", map[string]interface{}{"amount": "100", "ethTxHash": genesisrefs.FundsDepositName},
"deposit.transfer", map[string]interface{}{"amount": "100", "ethTxHash": genesis.FundsDepositName},
)
require.NoError(t, err)
depositAmount, ok := new(big.Int).SetString(application.FoundationDistributionAmount, 10)
depositAmount, ok := new(big.Int).SetString(genesis.FoundationDistributionAmount, 10)
require.True(t, ok, "can't parse FoundationDistributionAmount")
checkBalanceAndDepositFewTimes(t, m, "100", depositAmount.Sub(depositAmount, big.NewInt(100)).String())
}
Expand All @@ -142,14 +140,14 @@ func TestMigrationDaemonTransferDeposit(t *testing.T) {
m.Ref = decodedRes2["reference"].(string)

oldBalance, deposits := getBalanceAndDepositsNoErr(t, m, m.Ref)
oldDepositStr := deposits[genesisrefs.FundsDepositName].(map[string]interface{})["balance"].(string)
oldDepositStr := deposits[genesis.FundsDepositName].(map[string]interface{})["balance"].(string)

_, err = testrequest.SignedRequest(t, launchnet.TestRPCUrlPublic, m,
"deposit.transfer", map[string]interface{}{"amount": "100", "ethTxHash": genesisrefs.FundsDepositName},
"deposit.transfer", map[string]interface{}{"amount": "100", "ethTxHash": genesis.FundsDepositName},
)
require.NoError(t, err)
newBalance, newDeposits := getBalanceAndDepositsNoErr(t, m, m.Ref)
newDepositStr := newDeposits[genesisrefs.FundsDepositName].(map[string]interface{})["balance"].(string)
newDepositStr := newDeposits[genesis.FundsDepositName].(map[string]interface{})["balance"].(string)
amount := int64(100)
require.Equal(t, oldBalance.Add(oldBalance, big.NewInt(amount)).String(), newBalance.String())
oldDeposit, ok := new(big.Int).SetString(oldDepositStr, 10)
Expand Down
Loading

0 comments on commit 797f7fb

Please sign in to comment.