diff options
Diffstat (limited to 'examples/chain/main.go')
-rw-r--r-- | examples/chain/main.go | 115 |
1 files changed, 66 insertions, 49 deletions
diff --git a/examples/chain/main.go b/examples/chain/main.go index eddebd5..92350d3 100644 --- a/examples/chain/main.go +++ b/examples/chain/main.go @@ -13,8 +13,8 @@ import ( "github.com/ava-labs/go-ethereum/params" "github.com/ava-labs/go-ethereum/rlp" "math/big" - //mrand "math/rand" - "time" + "sync" + //"time" ) func checkError(err error) { @@ -30,6 +30,7 @@ type TestChain struct { chain *coreth.ETHChain parentBlock common.Hash outBlockCh chan<- []byte + blockWait sync.WaitGroup } func (tc *TestChain) insertBlock(block *types.Block) { @@ -50,7 +51,16 @@ func NewTestChain(name string, config *eth.Config, outBlockCh: outBlockCh, } tc.insertBlock(tc.chain.GetGenesisBlock()) - tc.chain.SetOnSealMiner(func(block *types.Block) error { + tc.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...) + //fmt.Printf("%s\n", hexutil.Encode(header.Extra)) + }) + tc.chain.SetOnSealFinish(func(block *types.Block) error { blkID := tc.blkCount tc.blkCount++ if len(block.Uncles()) != 0 { @@ -71,6 +81,7 @@ func NewTestChain(name string, config *eth.Config, <-inAckCh log.Info(fmt.Sprintf("%s: got ack", name)) } + tc.blockWait.Done() return nil }) go func() { @@ -104,72 +115,37 @@ func (tc *TestChain) GenRandomTree(n int, max int) { for i := 0; i < n; i++ { nblocks := len(tc.blocks) m := max - if m < 0 { + if m < 0 || nblocks < m { m = nblocks } pb, _ := rand.Int(rand.Reader, big.NewInt((int64)(m))) pn := pb.Int64() tc.parentBlock = tc.blocks[nblocks-1-(int)(pn)] tc.chain.SetTail(tc.parentBlock) + tc.blockWait.Add(1) tc.chain.GenBlock() - time.Sleep(time.Second) + tc.blockWait.Wait() } } -func main() { - // 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, - } - - // 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}}, - } - - // grab the control of block generation and disable auto uncle - config.Miner.ManualMining = true - config.Miner.DisableUncle = true - +func run(config *eth.Config, a1, a2, b1, b2 int) { aliceBlk := make(chan []byte) bobBlk := make(chan []byte) aliceAck := make(chan struct{}) bobAck := make(chan struct{}) - alice := NewTestChain("alice", &config, bobBlk, aliceBlk, bobAck, aliceAck) - bob := NewTestChain("bob", &config, aliceBlk, bobBlk, aliceAck, bobAck) + alice := NewTestChain("alice", config, bobBlk, aliceBlk, bobAck, aliceAck) + bob := NewTestChain("bob", config, aliceBlk, bobBlk, aliceAck, bobAck) alice.Start() bob.Start() - alice.GenRandomTree(60, -1) + alice.GenRandomTree(a1, a2) log.Info("alice finished generating the tree") - time.Sleep(2 * time.Second) + //time.Sleep(1 * time.Second) bob.outBlockCh = nil - bob.GenRandomTree(60, 2) + bob.GenRandomTree(b1, b2) //mrand.Shuffle(len(bob.blocks), // func(i, j int) { bob.blocks[i], bob.blocks[j] = bob.blocks[j], bob.blocks[i] }) log.Info("bob finished generating the tree") - time.Sleep(2 * time.Second) + //time.Sleep(1 * time.Second) log.Info("bob sends out all its blocks") for i := range bob.blocks { serialized, err := rlp.EncodeToBytes(bob.chain.GetBlockByHash(bob.blocks[i])) @@ -181,7 +157,7 @@ func main() { log.Info(fmt.Sprintf("bob: got ack")) } log.Info("bob finished generating the tree") - time.Sleep(2 * time.Second) + //time.Sleep(1 * time.Second) log.Info("comparing two trees") if len(alice.blocks) != len(bob.blocks) { panic(fmt.Sprintf("mismatching tree size %d != %d", len(alice.blocks), len(bob.blocks))) @@ -211,3 +187,44 @@ func main() { alice.Stop() bob.Stop() } + +func main() { + // 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, + } + + // 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}}, + } + + // grab the control of block generation and disable auto uncle + config.Miner.ManualMining = true + config.Miner.DisableUncle = true + + run(&config, 60, 1, 60, 1) + run(&config, 500, 10, 500, 5) +} |