Skip to content
This repository has been archived by the owner on Aug 25, 2021. It is now read-only.

Commit

Permalink
Merge pull request #13 from onflow/navid/multiple-proposers
Browse files Browse the repository at this point in the history
Multiple Proposers
  • Loading branch information
nvdtf authored Aug 13, 2021
2 parents cd4c119 + 9c7f160 commit 11b0b5d
Show file tree
Hide file tree
Showing 26 changed files with 414 additions and 74 deletions.
18 changes: 17 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,29 @@ ADMIN_PRIVATE_KEY=91a22fbd87392b019fbe332c32695c14cf2ba5b6521476a8540228bdf19870
DATABASE_TYPE=psql
DATABASE_DSN=postgresql://wallet:wallet@localhost:5432/wallet

ACCESS_API_HOST=emulator:3569
# emulator
ACCESS_API_HOST=localhost:3569
CHAIN_ID=flow-emulator

# testnet
# ACCESS_API_HOST=access.testnet.nodes.onflow.org:9000
# CHAIN_ID=flow-testnet

# mainnet
# ACCESS_API_HOST=access.mainnet.nodes.onflow.org:9000
# CHAIN_ID=flow-mainnet

# When set to "local", private keys are generated by the API
# and stored as encrypted text in the database.
DEFAULT_KEY_TYPE=local

# This symmetrical key is used to encrypt private keys
# that are stored in the database.
ENCRYPTION_KEY=faae4ed1c30f4e4555ee3a71f1044a8e

ENABLED_TOKENS=FUSD:0xf8d6e0586b0a20c7:fusd,FlowToken:0x0ae53cb6e3f42a79:flowToken

# This sets the number of proposal keys to be used on the admin account.
# You can increase transaction throughput by using multiple proposal keys for
# parallel transaction execution.
ADMIN_PROPOSAL_KEY_COUNT=50
17 changes: 17 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
ADMIN_ADDRESS=0xf8d6e0586b0a20c7
ADMIN_PRIVATE_KEY=91a22fbd87392b019fbe332c32695c14cf2ba5b6521476a8540228bdf1987068

ACCESS_API_HOST=localhost:3569
CHAIN_ID=flow-emulator

# When set to "local", private keys are generated by the API
# and stored as encrypted text in the database.
DEFAULT_KEY_TYPE=local

# This symmetrical key is used to encrypt private keys
# that are stored in the database.
ENCRYPTION_KEY=faae4ed1c30f4e4555ee3a71f1044a8e

ENABLED_TOKENS=FUSD:0xf8d6e0586b0a20c7:fusd,FlowToken:0x0ae53cb6e3f42a79:flowToken

ADMIN_PROPOSAL_KEY_COUNT=100
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ web_modules/
# dotenv environment variables file
.env*
!.env.example
!.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache
Expand Down
7 changes: 6 additions & 1 deletion accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ func New(a *Account, k *keys.Private, ctx context.Context, fc *client.Client, km
tx := flow_templates.CreateAccount([]*flow.AccountKey{accountKey}, nil, auth.Address)
b := templates.NewBuilderFromTx(tx)

proposer, err := km.AdminProposalKey(ctx)
if err != nil {
return err
}

t := transactions.Transaction{}
if err := transactions.New(&t, id, b, transactions.General, auth, auth, nil); err != nil {
if err := transactions.New(&t, id, b, transactions.General, proposer, auth, nil); err != nil {
return err
}

Expand Down
5 changes: 3 additions & 2 deletions accounts/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (

// Config struct for account service.
type Config struct {
AdminAccountAddress string `env:"ADMIN_ADDRESS,notEmpty"`
ChainId flow.ChainID `env:"CHAIN_ID" envDefault:"flow-emulator"`
AdminAccountAddress string `env:"ADMIN_ADDRESS,notEmpty"`
ChainID flow.ChainID `env:"CHAIN_ID" envDefault:"flow-emulator"`
AdminProposalKeyCount uint16 `env:"ADMIN_PROPOSAL_KEY_COUNT" envDefault:"1"`
}

// ParseConfig parses environment variables to a valid Config.
Expand Down
40 changes: 36 additions & 4 deletions accounts/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"github.com/eqlabs/flow-wallet-api/jobs"
"github.com/eqlabs/flow-wallet-api/keys"
"github.com/eqlabs/flow-wallet-api/templates"
"github.com/eqlabs/flow-wallet-api/templates/template_strings"
"github.com/eqlabs/flow-wallet-api/transactions"
"github.com/onflow/cadence"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/client"
)
Expand Down Expand Up @@ -41,22 +43,52 @@ func NewService(
return &Service{store, km, fc, wp, txs, tes, cfg}
}

func (s *Service) InitAdminAccount() {
func (s *Service) InitAdminAccount(ctx context.Context, txService *transactions.Service) error {
a, err := s.store.Account(s.cfg.AdminAccountAddress)
if err != nil {
if !strings.Contains(err.Error(), "record not found") {
panic(err)
return err
}
// Admin account not in database
a = Account{Address: s.cfg.AdminAccountAddress}
err := s.store.InsertAccount(&a)
if err != nil {
panic(err)
return err
}
AccountAdded.Trigger(AccountAddedPayload{
Address: flow.HexToAddress(s.cfg.AdminAccountAddress),
})
}

keyCount, err := s.km.InitAdminProposalKeys(ctx)
if err != nil {
return err
}

if keyCount < s.cfg.AdminProposalKeyCount {
err = s.addAdminProposalKeys(ctx, s.cfg.AdminProposalKeyCount-keyCount, txService)
if err != nil {
return err
}

_, err = s.km.InitAdminProposalKeys(ctx)
if err != nil {
return err
}
}

return nil
}

func (s *Service) addAdminProposalKeys(ctx context.Context, count uint16, txService *transactions.Service) error {
_, _, err := txService.Create(ctx, true, s.cfg.AdminAccountAddress, templates.Raw{
Code: template_strings.AddProposalKeyTransaction,
Arguments: []templates.Argument{
cadence.NewUInt16(count),
},
}, transactions.General)

return err
}

// List returns all accounts in the datastore.
Expand Down Expand Up @@ -121,7 +153,7 @@ func (s *Service) Create(c context.Context, sync bool) (*jobs.Job, *Account, err
// Details returns a specific account.
func (s *Service) Details(address string) (Account, error) {
// Check if the input is a valid address
address, err := flow_helpers.ValidateAddress(address, s.cfg.ChainId)
address, err := flow_helpers.ValidateAddress(address, s.cfg.ChainID)
if err != nil {
return Account{}, err
}
Expand Down
43 changes: 20 additions & 23 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,39 +1,36 @@
version: "3.3"
version: "3.9"
services:
db:
image: postgres:13-alpine
environment:
- POSTGRES_DB=wallet
- POSTGRES_USER=wallet
- POSTGRES_PASSWORD=wallet

wallet:
image: gcr.io/flow-container-registry/wallet-api:v0.3.1
build:
context: .
dockerfile: ./docker/wallet/Dockerfile
ports:
- "3000:3000"
env_file:
- ./.env
restart: unless-stopped
environment:
- DATABASE_TYPE=psql
- DATABASE_DSN=postgresql://wallet:wallet@db:5432/wallet
- ACCESS_API_HOST=emulator:3569
- CHAIN_ID=flow-emulator
- ADMIN_ADDRESS=${ADMIN_ADDRESS}
- ADMIN_PRIVATE_KEY=${ADMIN_PRIVATE_KEY}
- DEFAULT_KEY_TYPE=local
- ENCRYPTION_KEY=${ENCRYPTION_KEY}
- ENABLED_TOKENS=FUSD:${ADMIN_ADDRESS}:fusd,FlowToken:0x0ae53cb6e3f42a79:flow
DATABASE_DSN: postgresql://wallet:wallet@db:5432/wallet
DATABASE_TYPE: psql
ACCESS_API_HOST: emulator:3569
CHAIN_ID: flow-emulator
depends_on:
- db
- emulator

emulator:
image: gcr.io/flow-container-registry/emulator:v0.22.0
image: gcr.io/flow-container-registry/emulator:v0.23.0
command: emulator -v
ports:
- "3569:3569"
- "8080:8080"
environment:
- FLOW_SERVICEPRIVATEKEY=${ADMIN_PRIVATE_KEY}
- FLOW_SERVICEKEYSIGALGO=ECDSA_P256
- FLOW_SERVICEKEYHASHALGO=SHA3_256
- FLOW_VERBOSE=true

db:
image: postgres:13-alpine
ports:
- "5432:5432"
environment:
- POSTGRES_DB=wallet
- POSTGRES_USER=wallet
- POSTGRES_PASSWORD=wallet
18 changes: 18 additions & 0 deletions examples/nextjs-fusd-provider/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ADMIN_ADDRESS=0xf8d6e0586b0a20c7
ADMIN_PRIVATE_KEY=91a22fbd87392b019fbe332c32695c14cf2ba5b6521476a8540228bdf1987068

DATABASE_TYPE=psql
DATABASE_DSN=postgresql://wallet:wallet@localhost:5432/wallet

ACCESS_API_HOST=localhost:3569
CHAIN_ID=flow-emulator

# When set to "local", private keys are generated by the API
# and stored as encrypted text in the database.
DEFAULT_KEY_TYPE=local

# This symmetrical key is used to encrypt private keys
# that are stored in the database.
ENCRYPTION_KEY=faae4ed1c30f4e4555ee3a71f1044a8e

ENABLED_TOKENS=FUSD:0xf8d6e0586b0a20c7:fusd,FlowToken:0x0ae53cb6e3f42a79:flowToken
8 changes: 0 additions & 8 deletions examples/nextjs-fusd-provider/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ to distribute the FUSD stablecoin.
This example uses the sample emulator configuration
from the root of this repository.

Change to the repository root:

```ls
cd ../../
```

Create a configuration file:

```sh
Expand All @@ -41,8 +35,6 @@ flow project deploy -n emulator
In this directory, install and run the Next.js app:

```bash
cd examples/nextjs-fusd-provider

npm install

npm run dev
Expand Down
39 changes: 39 additions & 0 deletions examples/nextjs-fusd-provider/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
version: "3.3"
services:
wallet:
image: gcr.io/flow-container-registry/wallet-api:v0.3.1
ports:
- "3000:3000"
environment:
- DATABASE_TYPE=psql
- DATABASE_DSN=postgresql://wallet:wallet@db:5432/wallet
- ACCESS_API_HOST=emulator:3569
- CHAIN_ID=flow-emulator
- ADMIN_ADDRESS=${ADMIN_ADDRESS}
- ADMIN_PRIVATE_KEY=${ADMIN_PRIVATE_KEY}
- DEFAULT_KEY_TYPE=local
- ENCRYPTION_KEY=${ENCRYPTION_KEY}
- ENABLED_TOKENS=FUSD:${ADMIN_ADDRESS}:fusd,FlowToken:0x0ae53cb6e3f42a79:flow
depends_on:
- db
- emulator

emulator:
image: gcr.io/flow-container-registry/emulator:v0.22.0
ports:
- "3569:3569"
- "8080:8080"
environment:
- FLOW_SERVICEPRIVATEKEY=${ADMIN_PRIVATE_KEY}
- FLOW_SERVICEKEYSIGALGO=ECDSA_P256
- FLOW_SERVICEKEYHASHALGO=SHA3_256
- FLOW_VERBOSE=true

db:
image: postgres:13-alpine
ports:
- "5432:5432"
environment:
- POSTGRES_DB=wallet
- POSTGRES_USER=wallet
- POSTGRES_PASSWORD=wallet
42 changes: 42 additions & 0 deletions examples/nextjs-fusd-provider/flow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"emulators": {
"default": {
"port": 3569,
"serviceAccount": "emulator-account"
}
},
"contracts": {
"FungibleToken": {
"source": "../../cadence/contracts/FungibleToken.cdc",
"aliases": {
"emulator": "0xee82856bf20e2aa6",
"testnet": "0x9a0766d93b6608b7"
}
},
"FUSD": {
"source": "../../cadence/contracts/FUSD.cdc",
"aliases": {
"testnet": "0xe223d8a629e49c68"
}
}
},
"deployments": {
"emulator": {
"emulator-account": [
"FUSD"
]
}
},
"networks": {
"emulator": "127.0.0.1:3569",
"mainnet": "access.mainnet.nodes.onflow.org:9000",
"testnet": "access.devnet.nodes.onflow.org:9000"
},
"accounts": {
"emulator-account": {
"address": "f8d6e0586b0a20c7",
"keys": "91a22fbd87392b019fbe332c32695c14cf2ba5b6521476a8540228bdf1987068",
"chain": "flow-emulator"
}
}
}
Loading

0 comments on commit 11b0b5d

Please sign in to comment.