diff options
author | Determinant <[email protected]> | 2019-08-15 14:50:39 -0400 |
---|---|---|
committer | Determinant <[email protected]> | 2019-08-15 14:50:39 -0400 |
commit | 669168d32a534c1054f9df659b3199f7b6da0d21 (patch) | |
tree | 5cc6fc7fa2174473d298cababb5d573640ebe977 | |
parent | 49b07487092947a8b54d39ce4fbdc94e33537993 (diff) |
show wallet state in the example
-rw-r--r-- | consensus/dummy/consensus.go | 10 | ||||
-rw-r--r-- | coreth.go | 36 | ||||
-rw-r--r-- | examples/fc/main.go | 10 | ||||
-rw-r--r-- | examples/payments/main.go | 170 |
4 files changed, 146 insertions, 80 deletions
diff --git a/consensus/dummy/consensus.go b/consensus/dummy/consensus.go index 9f3791a..7700a55 100644 --- a/consensus/dummy/consensus.go +++ b/consensus/dummy/consensus.go @@ -18,11 +18,15 @@ import ( "github.com/ethereum/go-ethereum/rpc" ) +type OnFinalizeCallbackType = func(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) +type OnFinalizeAndAssembleCallbackType = func(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) +type OnAPIsCallbackType = func(consensus.ChainReader) []rpc.API + type ConsensusCallbacks struct { OnSeal func(*types.Block) error - OnAPIs func(consensus.ChainReader) []rpc.API - OnFinalize func(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) - OnFinalizeAndAssemble func(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) + OnAPIs OnAPIsCallbackType + OnFinalize OnFinalizeCallbackType + OnFinalizeAndAssemble OnFinalizeAndAssembleCallbackType } type DummyEngine struct { @@ -9,6 +9,7 @@ import ( "github.com/ava-labs/coreth/eth" "github.com/ava-labs/coreth/node" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/event" @@ -68,10 +69,43 @@ func (self *ETHChain) AddLocalTxs(txs []*types.Transaction) []error { return self.backend.TxPool().AddLocals(txs) } -func (self *ETHChain) SetOnSeal(cb func(*types.Block)) { +func (self *ETHChain) SetOnSeal(cb func(*types.Block) error) { self.cb.OnSeal = cb } +func (self *ETHChain) SetOnAPIs(cb dummy.OnAPIsCallbackType) { + self.cb.OnAPIs = cb +} + +func (self *ETHChain) SetOnFinalize(cb dummy.OnFinalizeCallbackType) { + self.cb.OnFinalize = cb +} + +func (self *ETHChain) SetOnFinalizeAndAssemble(cb dummy.OnFinalizeAndAssembleCallbackType) { + self.cb.OnFinalizeAndAssemble = cb +} + +// Returns a new mutable state based on the current HEAD block. +func (self *ETHChain) CurrentState() (*state.StateDB, error) { + return self.backend.BlockChain().State() +} + +// Returns a new mutable state based on the given block. +func (self *ETHChain) BlockState(block *types.Block) (*state.StateDB, error) { + return self.backend.BlockChain().StateAt(block.Root()) +} + +// Retrives a block from the database by hash. +func (self *ETHChain) GetBlockByHash(hash common.Hash) *types.Block { + return self.backend.BlockChain().GetBlockByHash(hash) +} + +// SetHead sets the current head block to the one defined by the hash +// irrelevant what the chain contents were prior. +func (self *ETHChain) SetHead(hash common.Hash) error { + return self.backend.BlockChain().FastSyncCommitHead(hash) +} + type Key struct { Address common.Address PrivateKey *ecdsa.PrivateKey diff --git a/examples/fc/main.go b/examples/fc/main.go index d6b224c..f4bf65d 100644 --- a/examples/fc/main.go +++ b/examples/fc/main.go @@ -1,14 +1,16 @@ package main import ( - "os" - "github.com/ava-labs/coreth/cmd/geth" + "github.com/ava-labs/coreth/cmd/geth" + "os" ) func checkError(err error) { - if err != nil { panic(err) } + if err != nil { + panic(err) + } } func main() { - geth.App.Run(os.Args) + geth.App.Run(os.Args) } diff --git a/examples/payments/main.go b/examples/payments/main.go index ffb156c..1c0a18e 100644 --- a/examples/payments/main.go +++ b/examples/payments/main.go @@ -1,89 +1,115 @@ package main import ( - "time" - "os" - "os/signal" - "syscall" - "crypto/rand" - "math/big" - //"encoding/hex" - "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/common/hexutil" - "github.com/ethereum/go-ethereum/core" - "github.com/ava-labs/coreth/eth" - "github.com/ava-labs/coreth" - "github.com/ethereum/go-ethereum/params" - "github.com/ethereum/go-ethereum/common" + "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/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/params" ) func checkError(err error) { - if err != nil { panic(err) } + if err != nil { + panic(err) + } } func main() { - config := eth.DefaultConfig - chainConfig := ¶ms.ChainConfig { - ChainID: big.NewInt(1), - HomesteadBlock: big.NewInt(0), - DAOForkBlock: big.NewInt(0), - DAOForkSupport: true, - EIP150Block: big.NewInt(0), - EIP150Hash: common.HexToHash("0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0"), - EIP155Block: big.NewInt(0), - EIP158Block: big.NewInt(0), - ByzantiumBlock: big.NewInt(0), - ConstantinopleBlock: big.NewInt(0), - PetersburgBlock: big.NewInt(0), - IstanbulBlock: nil, - Ethash: nil, - } + // configure the chain + config := eth.DefaultConfig + chainConfig := ¶ms.ChainConfig{ + ChainID: big.NewInt(1), + HomesteadBlock: big.NewInt(0), + DAOForkBlock: big.NewInt(0), + DAOForkSupport: true, + EIP150Block: big.NewInt(0), + EIP150Hash: common.HexToHash("0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0"), + EIP155Block: big.NewInt(0), + EIP158Block: big.NewInt(0), + ByzantiumBlock: big.NewInt(0), + ConstantinopleBlock: big.NewInt(0), + PetersburgBlock: big.NewInt(0), + IstanbulBlock: nil, + Ethash: nil, + } - genBalance := big.NewInt(100000000000000000) - genKey, _ := coreth.NewKey(rand.Reader) + // configure the genesis block + genBalance := big.NewInt(100000000000000000) + genKey, _ := coreth.NewKey(rand.Reader) - config.Genesis = &core.Genesis{ - Config: chainConfig, - Nonce: 0, - Number: 0, - ExtraData: hexutil.MustDecode("0x00"), - GasLimit: 100000000, - Difficulty: big.NewInt(0), - Alloc: core.GenesisAlloc{ genKey.Address: { Balance: genBalance }}, - } + config.Genesis = &core.Genesis{ + Config: chainConfig, + Nonce: 0, + Number: 0, + ExtraData: hexutil.MustDecode("0x00"), + GasLimit: 100000000, + Difficulty: big.NewInt(0), + Alloc: core.GenesisAlloc{genKey.Address: {Balance: genBalance}}, + } - config.Miner.ManualMining = true - config.Miner.ManualUncle = true + // grab the control of block generation and disable auto uncle + config.Miner.ManualMining = true + config.Miner.ManualUncle = true - chainID := chainConfig.ChainID - nonce := uint64(0) - value := big.NewInt(1000000000000) - gasLimit := 21000 - gasPrice := big.NewInt(1000000000) - bob, err := coreth.NewKey(rand.Reader); checkError(err) + // info required to generate a transaction + chainID := chainConfig.ChainID + nonce := uint64(0) + value := big.NewInt(1000000000000) + gasLimit := 21000 + gasPrice := big.NewInt(1000000000) + bob, err := coreth.NewKey(rand.Reader) + checkError(err) - chain := coreth.NewETHChain(&config, nil) - chain.SetOnSeal(func(block *types.Block) { - go func() { - time.Sleep(1000 * time.Millisecond) - chain.GenBlock() - }() - }) - chain.Start() + blockCount := 0 + chain := coreth.NewETHChain(&config, nil) + showBalance := func() { + state, err := chain.CurrentState() + checkError(err) + log.Info(fmt.Sprintf("genesis balanche = %s", state.GetBalance(genKey.Address))) + log.Info(fmt.Sprintf("bob's balanche = %s", state.GetBalance(bob.Address))) + } + chain.SetOnSeal(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 { + showBalance() + return + } + chain.GenBlock() + }() + return nil + }) - chain.GenBlock() - for i := 0; i < 10; 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++ - } + // start the chain + chain.Start() + chain.GenBlock() + for i := 0; i < 10; 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++ + } - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt, syscall.SIGTERM) - signal.Notify(c, os.Interrupt, syscall.SIGINT) - <-c - chain.Stop() + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) + signal.Notify(c, os.Interrupt, syscall.SIGINT) + <-c + chain.Stop() } |