From 5f011d186288a533b167c50919b595b5b4c9be5e Mon Sep 17 00:00:00 2001 From: bai Date: Tue, 6 Nov 2018 15:18:31 +0800 Subject: [PATCH] for photon usage --- accounts/abi/bind/backend.go | 4 ++++ accounts/abi/bind/base.go | 27 +++++++++++++++++++++++++-- accounts/keystore/keystore.go | 6 ++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/accounts/abi/bind/backend.go b/accounts/abi/bind/backend.go index ca60cc1b43..7bba8d8320 100644 --- a/accounts/abi/bind/backend.go +++ b/accounts/abi/bind/backend.go @@ -82,6 +82,10 @@ type ContractTransactor interface { EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) // SendTransaction injects the transaction into the pending pool for execution. SendTransaction(ctx context.Context, tx *types.Transaction) error + //NetworkID bai add to compatible with spectrum and ethereum + NetworkID(ctx context.Context) (*big.Int, error) + //HeaderByNumber bai add to get a block's header + HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) } // ContractFilterer defines the methods needed to access log events using one-off diff --git a/accounts/abi/bind/base.go b/accounts/abi/bind/base.go index 83ad1c8ae7..f1bf05a1f9 100644 --- a/accounts/abi/bind/base.go +++ b/accounts/abi/bind/base.go @@ -21,6 +21,8 @@ import ( "errors" "fmt" "math/big" + "sync" + "time" "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/accounts/abi" @@ -180,11 +182,14 @@ func (c *BoundContract) Transfer(opts *TransactOpts) (*types.Transaction, error) return c.transact(opts, &c.address, nil) } +var lock = sync.Mutex{} + // transact executes an actual transaction invocation, first deriving any missing // authorization fields, and then scheduling the transaction for execution. func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, input []byte) (*types.Transaction, error) { var err error - + lock.Lock() + defer lock.Unlock() // Ensure a valid value field and resolve the account nonce value := opts.Value if value == nil { @@ -224,6 +229,7 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i return nil, fmt.Errorf("failed to estimate gas needed: %v", err) } } + //fmt.Printf("send Transaction with nonce=%d,fromaddress=%s\n", nonce, opts.From.String()) // Create the transaction, sign it and schedule it for execution var rawTx *types.Transaction if contract == nil { @@ -234,13 +240,30 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i if opts.Signer == nil { return nil, errors.New("no signer to authorize the transaction with") } - signedTx, err := opts.Signer(types.HomesteadSigner{}, opts.From, rawTx) + networkID, err := c.transactor.NetworkID(ensureContext(opts.Context)) + if err != nil { + return nil, err + } + + // special dealing for specrum main net + var head *types.Header + head,err = c.transactor.HeaderByNumber(ensureContext(opts.Context),big.NewInt(1)) + if err!=nil{ + return nil,err + } + if head != nil && head.Hash() == common.HexToHash("0x57e682b80257aad73c4f3ad98d20435b4e1644d8762ef1ea1ff2806c27a5fa3d") { + fmt.Printf("change chainid to 20180430\n") + networkID = big.NewInt(20180430) + } + + signedTx, err := opts.Signer(types.NewEIP155Signer(networkID), opts.From, rawTx) if err != nil { return nil, err } if err := c.transactor.SendTransaction(ensureContext(opts.Context), signedTx); err != nil { return nil, err } + time.Sleep(100 * time.Millisecond) return signedTx, nil } diff --git a/accounts/keystore/keystore.go b/accounts/keystore/keystore.go index 6b04acd05f..b59f1afdfb 100644 --- a/accounts/keystore/keystore.go +++ b/accounts/keystore/keystore.go @@ -491,3 +491,9 @@ func zeroKey(k *ecdsa.PrivateKey) { b[i] = 0 } } + +//Close close accountCache +//add by bai, fix memory leak +func (ks *KeyStore) Close() { + ks.cache.close() +}