aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/chain/main.go2
-rw-r--r--examples/counter/main.go15
-rw-r--r--examples/payments/main.go29
3 files changed, 26 insertions, 20 deletions
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++
}