From 841b2b7225a9318718c3c856a9debdf01bc4f061 Mon Sep 17 00:00:00 2001 From: Determinant Date: Thu, 26 Sep 2019 22:34:43 -0400 Subject: update examples --- cmd/utils/flags.go | 40 ++++++++++++++++++++-------------------- examples/chain/main.go | 2 -- examples/counter/main.go | 15 ++++++++++----- examples/payments/main.go | 29 ++++++++++++++++------------- 4 files changed, 46 insertions(+), 40 deletions(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 683c490..ea3ca1d 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -29,12 +29,16 @@ import ( "strings" "time" + "github.com/ava-labs/coreth/consensus/dummy" + "github.com/ava-labs/coreth/eth" + "github.com/ava-labs/coreth/ethstats" + "github.com/ava-labs/coreth/miner" + "github.com/ava-labs/coreth/node" "github.com/ava-labs/go-ethereum/accounts" "github.com/ava-labs/go-ethereum/accounts/keystore" "github.com/ava-labs/go-ethereum/common" "github.com/ava-labs/go-ethereum/common/fdlimit" "github.com/ava-labs/go-ethereum/consensus" - "github.com/ava-labs/coreth/consensus/dummy" "github.com/ava-labs/go-ethereum/consensus/clique" "github.com/ava-labs/go-ethereum/consensus/ethash" "github.com/ava-labs/go-ethereum/core" @@ -43,15 +47,11 @@ import ( "github.com/ava-labs/go-ethereum/dashboard" "github.com/ava-labs/go-ethereum/eth/downloader" "github.com/ava-labs/go-ethereum/eth/gasprice" - "github.com/ava-labs/coreth/eth" "github.com/ava-labs/go-ethereum/ethdb" - "github.com/ava-labs/coreth/ethstats" "github.com/ava-labs/go-ethereum/graphql" "github.com/ava-labs/go-ethereum/log" "github.com/ava-labs/go-ethereum/metrics" "github.com/ava-labs/go-ethereum/metrics/influxdb" - "github.com/ava-labs/coreth/miner" - "github.com/ava-labs/coreth/node" "github.com/ava-labs/go-ethereum/p2p" "github.com/ava-labs/go-ethereum/p2p/discv5" "github.com/ava-labs/go-ethereum/p2p/enode" @@ -1477,21 +1477,21 @@ func SetDashboardConfig(ctx *cli.Context, cfg *dashboard.Config) { // RegisterEthService adds an Ethereum client to the stack. func RegisterEthService(stack *node.Node, cfg *eth.Config) { - var err error - if cfg.SyncMode == downloader.LightSync { - panic("not supported") - } else { - err = stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { - fullNode, err := eth.New(ctx, cfg, &dummy.ConsensusCallbacks{}) - if fullNode != nil && cfg.LightServ > 0 { - panic("not supported") - } - return fullNode, err - }) - } - if err != nil { - Fatalf("Failed to register the Ethereum service: %v", err) - } + var err error + if cfg.SyncMode == downloader.LightSync { + panic("not supported") + } else { + err = stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { + fullNode, err := eth.New(ctx, cfg, &dummy.ConsensusCallbacks{}, &miner.MinerCallbacks{}, nil) + if fullNode != nil && cfg.LightServ > 0 { + panic("not supported") + } + return fullNode, err + }) + } + if err != nil { + Fatalf("Failed to register the Ethereum service: %v", err) + } } // RegisterDashboardService adds a dashboard to the stack. diff --git a/examples/chain/main.go b/examples/chain/main.go index d129acb..b2f5629 100644 --- a/examples/chain/main.go +++ b/examples/chain/main.go @@ -14,7 +14,6 @@ import ( "github.com/ava-labs/go-ethereum/rlp" "math/big" "sync" - //"time" ) func checkError(err error) { @@ -58,7 +57,6 @@ func NewTestChain(name string, config *eth.Config, panic("cannot generate hid") } header.Extra = append(header.Extra, hid...) - //fmt.Printf("%s\n", hexutil.Encode(header.Extra)) }) tc.chain.SetOnSealFinish(func(block *types.Block) error { blkID := tc.blkCount diff --git a/examples/counter/main.go b/examples/counter/main.go index 8dfb6bf..db6e1de 100644 --- a/examples/counter/main.go +++ b/examples/counter/main.go @@ -92,7 +92,7 @@ func main() { gasPrice := big.NewInt(1000000000) blockCount := 0 - chain := coreth.NewETHChain(&config, nil, nil) + chain := coreth.NewETHChain(&config, nil, nil, nil) log.Info(chain.GetGenesisBlock().Hash().Hex()) firstBlock := false var contractAddr common.Address @@ -125,17 +125,22 @@ func main() { signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), genKey.PrivateKey) checkError(err) chain.AddRemoteTxs([]*types.Transaction{signedTx}) - time.Sleep(1000 * time.Millisecond) nonce++ } }() } return false } - chain.SetOnSeal(func(block *types.Block) error { + chain.SetOnHeaderNew(func(header *types.Header) { + hid := make([]byte, 32) + _, err := rand.Read(hid) + if err != nil { + panic("cannot generate hid") + } + header.Extra = append(header.Extra, hid...) + }) + chain.SetOnSealFinish(func(block *types.Block) error { go func() { - // the minimum time gap is 1s - time.Sleep(1000 * time.Millisecond) // generate 15 blocks blockCount++ if postGen(block) { diff --git a/examples/payments/main.go b/examples/payments/main.go index 4a49813..fbacacd 100644 --- a/examples/payments/main.go +++ b/examples/payments/main.go @@ -3,12 +3,6 @@ package main import ( "crypto/rand" "fmt" - "math/big" - "os" - "os/signal" - "syscall" - "time" - //"encoding/hex" "github.com/ava-labs/coreth" "github.com/ava-labs/coreth/eth" "github.com/ava-labs/go-ethereum/common" @@ -17,6 +11,10 @@ import ( "github.com/ava-labs/go-ethereum/core/types" "github.com/ava-labs/go-ethereum/log" "github.com/ava-labs/go-ethereum/params" + "math/big" + "os" + "os/signal" + "syscall" ) func checkError(err error) { @@ -72,20 +70,26 @@ func main() { checkError(err) blockCount := 0 - chain := coreth.NewETHChain(&config, nil, nil) + chain := coreth.NewETHChain(&config, nil, nil, nil) showBalance := func() { state, err := chain.CurrentState() checkError(err) log.Info(fmt.Sprintf("genesis balance = %s", state.GetBalance(genKey.Address))) log.Info(fmt.Sprintf("bob's balance = %s", state.GetBalance(bob.Address))) } - chain.SetOnSeal(func(block *types.Block) error { + chain.SetOnHeaderNew(func(header *types.Header) { + hid := make([]byte, 32) + _, err := rand.Read(hid) + if err != nil { + panic("cannot generate hid") + } + header.Extra = append(header.Extra, hid...) + }) + chain.SetOnSealFinish(func(block *types.Block) error { go func() { - // the minimum time gap is 1s - time.Sleep(1000 * time.Millisecond) // generate 15 blocks blockCount++ - if blockCount == 15 { + if blockCount == 43 { showBalance() return } @@ -97,13 +101,12 @@ func main() { // start the chain chain.Start() chain.GenBlock() - for i := 0; i < 10; i++ { + for i := 0; i < 42; i++ { tx := types.NewTransaction(nonce, bob.Address, value, uint64(gasLimit), gasPrice, nil) signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), genKey.PrivateKey) checkError(err) _ = signedTx chain.AddRemoteTxs([]*types.Transaction{signedTx}) - time.Sleep(1000 * time.Millisecond) nonce++ } -- cgit v1.2.3