Skip to content

Commit

Permalink
v1.1.6 (#436)
Browse files Browse the repository at this point in the history
* xcb: returned revert reason in traceTx #21195

* internal/xcbapi: return revert reason for xcb_call #21083

* docker image

* revert dockerfile

* fix tests

* comment out test

* merge master

Co-authored-by: Sasha Zezulinsky <[email protected]>
Co-authored-by: Rastislav <[email protected]>
  • Loading branch information
3 people authored Oct 19, 2022
1 parent 0bfcb87 commit 100749d
Show file tree
Hide file tree
Showing 57 changed files with 2,552 additions and 1,129 deletions.
22 changes: 11 additions & 11 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ jobs:
name: docker
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Version
id: version
run: echo ::set-output name=tag::$(echo ${GITHUB_REF:10})
- name: Docker
run: |
docker login ghcr.io -u ${{github.actor}} -p ${{secrets.GITHUB_TOKEN}}
docker build . -t ghcr.io/core-coin/go-core:${{steps.version.outputs.tag}} -t ghcr.io/core-coin/go-core:latest
docker push ghcr.io/core-coin/go-core:${{steps.version.outputs.tag}}
docker push ghcr.io/core-coin/go-core:latest
- name: Checkout
uses: actions/checkout@v1
- name: Version
id: version
run: echo ::set-output name=tag::$(echo ${GITHUB_REF:10})
- name: Docker
run: |
docker login ghcr.io -u ${{github.actor}} -p ${{secrets.GITHUB_TOKEN}}
docker build . -t ghcr.io/core-coin/go-core:${{steps.version.outputs.tag}} -t ghcr.io/core-coin/go-core:latest
docker push ghcr.io/core-coin/go-core:${{steps.version.outputs.tag}}
docker push ghcr.io/core-coin/go-core:latest
95 changes: 88 additions & 7 deletions accounts/abi/bind/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package bind

import (
"errors"
"github.com/core-coin/go-core/log"
"io"
"io/ioutil"
"math/big"

eddsa "github.com/core-coin/go-goldilocks"

Expand All @@ -31,9 +33,18 @@ import (
"github.com/core-coin/go-core/crypto"
)

// ErrNoNetworkID is returned whenever the user failed to specify a network id.
var ErrNoNetworkID = errors.New("no network id specified")

// ErrNotAuthorized is returned when an account is not properly unlocked.
var ErrNotAuthorized = errors.New("not authorized to sign this account")

// NewTransactor is a utility method to easily create a transaction signer from
// an encrypted json key stream and the associated passphrase.
//
// Deprecated: Use NewTransactorWithNetworkID instead.
func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) {
log.Warn("WARNING: NewTransactor has been deprecated in favour of NewTransactorWithNetworkID")
json, err := ioutil.ReadAll(keyin)
if err != nil {
return nil, err
Expand All @@ -46,13 +57,17 @@ func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) {
}

// NewKeyStoreTransactor is a utility method to easily create a transaction signer from
// an decrypted key from a keystore
// an decrypted key from a keystore.
//
// Deprecated: Use NewKeyStoreTransactorWithNetworkID instead.
func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) {
log.Warn("WARNING: NewKeyStoreTransactor has been deprecated in favour of NewTransactorWithNetworkID")
signer := types.NucleusSigner{}
return &TransactOpts{
From: account.Address,
Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
if address != account.Address {
return nil, errors.New("not authorized to sign this account")
return nil, ErrNotAuthorized
}
tx.SetNetworkID(uint(signer.NetworkID()))
signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
Expand All @@ -66,14 +81,18 @@ func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account

// NewKeyedTransactor is a utility method to easily create a transaction signer
// from a single private key.
//
// Deprecated: Use NewKeyedTransactorWithNetworkID instead.
func NewKeyedTransactor(key *eddsa.PrivateKey) *TransactOpts {
log.Warn("WARNING: NewKeyedTransactor has been deprecated in favour of NewKeyedTransactorWithNetworkID")
pub := eddsa.Ed448DerivePublicKey(*key)
keyAddr := crypto.PubkeyToAddress(pub)
signer := types.NucleusSigner{}
return &TransactOpts{
From: keyAddr,
Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
if address != keyAddr {
return nil, errors.New("not authorized to sign this account")
return nil, ErrNotAuthorized
}
tx.SetNetworkID(uint(signer.NetworkID()))
signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
Expand All @@ -85,14 +104,76 @@ func NewKeyedTransactor(key *eddsa.PrivateKey) *TransactOpts {
}
}

// NewTransactorWithNetworkID is a utility method to easily create a transaction signer from
// an encrypted json key stream and the associated passphrase.
func NewTransactorWithNetworkID(keyin io.Reader, passphrase string, networkID *big.Int) (*TransactOpts, error) {
json, err := ioutil.ReadAll(keyin)
if err != nil {
return nil, err
}
key, err := keystore.DecryptKey(json, passphrase)
if err != nil {
return nil, err
}
return NewKeyedTransactorWithNetworkID(key.PrivateKey, networkID)
}

// NewKeyStoreTransactorWithNetworkID is a utility method to easily create a transaction signer from
// an decrypted key from a keystore.
func NewKeyStoreTransactorWithNetworkID(keystore *keystore.KeyStore, account accounts.Account, networkID *big.Int) (*TransactOpts, error) {
if networkID == nil {
return nil, ErrNoNetworkID
}
signer := types.NewNucleusSigner(networkID)
return &TransactOpts{
From: account.Address,
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
if address != account.Address {
return nil, ErrNotAuthorized
}
tx.SetNetworkID(uint(signer.NetworkID()))
signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
if err != nil {
return nil, err
}
return tx.WithSignature(signer, signature)
},
}, nil
}

// NewKeyedTransactorWithNetworkID is a utility method to easily create a transaction signer
// from a single private key.
func NewKeyedTransactorWithNetworkID(key *eddsa.PrivateKey, networkID *big.Int) (*TransactOpts, error) {
pub := eddsa.Ed448DerivePublicKey(*key)
keyAddr := crypto.PubkeyToAddress(pub)
if networkID == nil {
return nil, ErrNoNetworkID
}
signer := types.NewNucleusSigner(networkID)
return &TransactOpts{
From: keyAddr,
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
if address != keyAddr {
return nil, ErrNotAuthorized
}
tx.SetNetworkID(uint(signer.NetworkID()))
signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
if err != nil {
return nil, err
}
return tx.WithSignature(signer, signature)
},
}, nil
}

// NewClefTransactor is a utility method to easily create a transaction signer
// with a clef backend.
func NewClefTransactor(clef *external.ExternalSigner, account accounts.Account) *TransactOpts {
return &TransactOpts{
From: account.Address,
Signer: func(signer types.Signer, address common.Address, transaction *types.Transaction) (*types.Transaction, error) {
Signer: func(address common.Address, transaction *types.Transaction) (*types.Transaction, error) {
if address != account.Address {
return nil, errors.New("not authorized to sign this account")
return nil, ErrNotAuthorized
}
return clef.SignTx(account, transaction, nil) // Clef enforces its own network id
},
Expand Down
64 changes: 52 additions & 12 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"fmt"
"github.com/core-coin/go-core/accounts/abi"
"github.com/core-coin/go-core/common/hexutil"
"math/big"
"sync"
"time"
Expand Down Expand Up @@ -72,8 +73,9 @@ type SimulatedBackend struct {

// NewSimulatedBackendWithDatabase creates a new binding backend based on the given database
// and uses a simulated blockchain for testing purposes.
// A simulated backend always uses chainID 1337.
func NewSimulatedBackendWithDatabase(database xcbdb.Database, alloc core.GenesisAlloc, energyLimit uint64) *SimulatedBackend {
genesis := core.Genesis{Config: params.AllCryptoreProtocolChanges, EnergyLimit: energyLimit, Alloc: alloc}
genesis := core.Genesis{Config: params.DevChainConfig, EnergyLimit: energyLimit, Alloc: alloc, Coinbase: core.DefaultCoinbaseMainnet}
genesis.MustCommit(database)
blockchain, _ := core.NewBlockChain(database, nil, genesis.Config, cryptore.NewFaker(), vm.Config{}, nil, nil)

Expand All @@ -89,6 +91,7 @@ func NewSimulatedBackendWithDatabase(database xcbdb.Database, alloc core.Genesis

// NewSimulatedBackend creates a new binding backend using a simulated blockchain
// for testing purposes.
// A simulated backend always uses networkID 1337.
func NewSimulatedBackend(alloc core.GenesisAlloc, energyLimit uint64) *SimulatedBackend {
return NewSimulatedBackendWithDatabase(rawdb.NewMemoryDatabase(), alloc, energyLimit)
}
Expand Down Expand Up @@ -120,7 +123,9 @@ func (b *SimulatedBackend) Rollback() {
}

func (b *SimulatedBackend) rollback() {
blocks, _ := core.GenerateChain(b.config, b.blockchain.CurrentBlock(), cryptore.NewFaker(), b.database, 1, func(int, *core.BlockGen) {})
blocks, _ := core.GenerateChain(b.config, b.blockchain.CurrentBlock(), cryptore.NewFaker(), b.database, 1, func(number int, block *core.BlockGen) {
block.SetCoinbase(core.DefaultCoinbaseMainnet)
})
statedb, _ := b.blockchain.State()

b.pendingBlock = blocks[0]
Expand Down Expand Up @@ -337,6 +342,36 @@ func (b *SimulatedBackend) PendingCodeAt(ctx context.Context, contract common.Ad
return b.pendingState.GetCode(contract), nil
}

func newRevertError(result *core.ExecutionResult) *revertError {
reason, errUnpack := abi.UnpackRevert(result.Revert())
err := errors.New("execution reverted")
if errUnpack == nil {
err = fmt.Errorf("execution reverted: %v", reason)
}
return &revertError{
error: err,
reason: hexutil.Encode(result.Revert()),
}
}

// revertError is an API error that encompassas an EVM revertal with JSON error
// code and a binary data blob.
type revertError struct {
error
reason string // revert reason hex encoded
}

// ErrorCode returns the JSON error code for a revertal.
// See: https://github.com/ethereum/wiki/wiki/JSON-RPC-Error-Codes-Improvement-Proposal
func (e *revertError) ErrorCode() int {
return 3
}

// ErrorData returns the hex encoded revert reason.
func (e *revertError) ErrorData() interface{} {
return e.reason
}

// CallContract executes a contract call.
func (b *SimulatedBackend) CallContract(ctx context.Context, call gocore.CallMsg, blockNumber *big.Int) ([]byte, error) {
b.mu.Lock()
Expand All @@ -353,7 +388,12 @@ func (b *SimulatedBackend) CallContract(ctx context.Context, call gocore.CallMsg
if err != nil {
return nil, err
}
return res.Return(), nil

// If the result contains a revert reason, try to unpack and return it.
if len(res.Revert()) > 0 {
return nil, newRevertError(res)
}
return res.Return(), res.Err
}

// PendingCallContract executes a contract call on the pending state.
Expand All @@ -366,7 +406,11 @@ func (b *SimulatedBackend) PendingCallContract(ctx context.Context, call gocore.
if err != nil {
return nil, err
}
return res.Return(), nil
// If the result contains a revert reason, try to unpack and return it.
if len(res.Revert()) > 0 {
return nil, newRevertError(res)
}
return res.Return(), res.Err
}

// PendingNonceAt implements PendingStateReader.PendingNonceAt, retrieving
Expand Down Expand Up @@ -444,16 +488,10 @@ func (b *SimulatedBackend) EstimateEnergy(ctx context.Context, call gocore.CallM
}
if failed {
if result != nil && result.Err != vm.ErrOutOfEnergy {
errMsg := fmt.Sprintf("always failing transaction (%v)", result.Err)
if len(result.Revert()) > 0 {
ret, err := abi.UnpackRevert(result.Revert())
if err != nil {
errMsg += fmt.Sprintf(" (%#x)", result.Revert())
} else {
errMsg += fmt.Sprintf(" (%s)", ret)
}
return 0, newRevertError(result)
}
return 0, errors.New(errMsg)
return 0, result.Err
}
// Otherwise, the specified energy cap is too low
return 0, fmt.Errorf("energy required exceeds allowance (%d)", cap)
Expand Down Expand Up @@ -506,6 +544,7 @@ func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transa
}

blocks, _ := core.GenerateChain(b.config, b.blockchain.CurrentBlock(), cryptore.NewFaker(), b.database, 1, func(number int, block *core.BlockGen) {
block.SetCoinbase(core.DefaultCoinbaseMainnet)
for _, tx := range b.pendingBlock.Transactions() {
block.AddTxWithChain(b.blockchain, tx)
}
Expand Down Expand Up @@ -619,6 +658,7 @@ func (b *SimulatedBackend) AdjustTime(adjustment time.Duration) error {
defer b.mu.Unlock()

blocks, _ := core.GenerateChain(b.config, b.blockchain.CurrentBlock(), cryptore.NewFaker(), b.database, 1, func(number int, block *core.BlockGen) {
block.SetCoinbase(core.DefaultCoinbaseMainnet)
for _, tx := range b.pendingBlock.Transactions() {
block.AddTx(tx)
}
Expand Down
Loading

0 comments on commit 100749d

Please sign in to comment.