Skip to content

Commit

Permalink
Ingest Block Batches (ethereum#8)
Browse files Browse the repository at this point in the history
Handling BlockBatches in Geth at `SendBlockBatches` endpoint (eth_sendBlockBatches)

Other:
* Adding PR template
* Adding ability to set timestamp and making blocks use configured timestamp
* Adding ability to encode original tx nonce in calldata
* Adding L1MessageSender to Contract Creation Txs
  • Loading branch information
willmeister authored Jun 23, 2020
1 parent 0a67cf8 commit 83c7d84
Show file tree
Hide file tree
Showing 25 changed files with 708 additions and 87 deletions.
125 changes: 87 additions & 38 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,89 @@
# Contributing

Thank you for considering to help out with the source code! We welcome
contributions from anyone on the internet, and are grateful for even the
smallest of fixes!

If you'd like to contribute to go-ethereum, please fork, fix, commit and send a
pull request for the maintainers to review and merge into the main code base. If
you wish to submit more complex changes though, please check up with the core
devs first on [our gitter channel](https://gitter.im/ethereum/go-ethereum) to
ensure those changes are in line with the general philosophy of the project
and/or get some early feedback which can make both your efforts much lighter as
well as our review and merge procedures quick and simple.

## Coding guidelines

Please make sure your contributions adhere to our coding guidelines:

* Code must adhere to the official Go
[formatting](https://golang.org/doc/effective_go.html#formatting) guidelines
(i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
* Code must be documented adhering to the official Go
[commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
* Pull requests need to be based on and opened against the `master` branch.
* Commit messages should be prefixed with the package(s) they modify.
* E.g. "eth, rpc: make trace configs optional"

## Can I have feature X

Before you submit a feature request, please check and make sure that it isn't
possible through some other means. The JavaScript-enabled console is a powerful
feature in the right hands. Please check our
[Wiki page](https://github.com/ethereum/go-ethereum/wiki) for more info
and help.

## Configuration, dependencies, and tests

Please see the [Developers' Guide](https://github.com/ethereum/go-ethereum/wiki/Developers'-Guide)
for more details on configuring your environment, managing project dependencies
and testing procedures.
When contributing to this repository, please first discuss the change you wish to make via issue,
email, or any other method with the owners of this repository before making a change.

Please note we have a code of conduct, please follow it in all your interactions with the project.

## Pull Request Process

1. Ensure that tests pass and code is lint free: `make all && make test && make lint`
2. Update the README.md if any changes invalidate its current content.
3. Include any tests for new functionality.
4. Reference any relevant issues in your PR comment.

## Code of Conduct

### Our Pledge

In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, gender identity and expression, level of experience,
nationality, personal appearance, race, religion, or sexual identity and
orientation.

### Our Standards

Examples of behavior that contributes to creating a positive environment
include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting

### Our Responsibilities

Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.

Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.

### Scope

This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.

### Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at contributing@optimism.io. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.

Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.

### Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at [http://contributor-covenant.org/version/1/4][version] and from the [Angular Seed Contributing Guide][angular-contrib].

[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/4/
[angular-contrib]: https://github.com/mgechev/angular-seed/blob/master/.github/CONTRIBUTING.md
17 changes: 17 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## Description

## Questions
-
-
-

## Metadata
### Fixes
- Fixes # [Link to Issue]

## Contributing Agreement
<!--
You *must* read and fully understand our Contributing Guide and Code of Conduct before submitting this pull request. Strong, healthy, and respectful communities are the best way to build great code 💖.
-->

- [ ] I have read and understood the [Optimism Contributing Guide and Code of Conduct](./CONTRIBUTING.md) and am following those guidelines in this pull request.
2 changes: 1 addition & 1 deletion accounts/abi/bind/backends/simulated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestSimulatedBackend(t *testing.T) {
// generate a transaction and confirm you can retrieve it
code := `6060604052600a8060106000396000f360606040526008565b00`
var gas uint64 = 3000000
tx := types.NewContractCreation(0, big.NewInt(0), gas, big.NewInt(1), common.FromHex(code))
tx := types.NewContractCreation(0, big.NewInt(0), gas, big.NewInt(1), common.FromHex(code), nil)
tx, _ = types.SignTx(tx, types.HomesteadSigner{}, key)

err = sim.SendTransaction(context.Background(), tx)
Expand Down
2 changes: 1 addition & 1 deletion accounts/abi/bind/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i
// Create the transaction, sign it and schedule it for execution
var rawTx *types.Transaction
if contract == nil {
rawTx = types.NewContractCreation(nonce, value, gasLimit, gasPrice, input)
rawTx = types.NewContractCreation(nonce, value, gasLimit, gasPrice, input, nil)
} else {
rawTx = types.NewTransaction(nonce, c.address, value, gasLimit, gasPrice, input, nil)
}
Expand Down
2 changes: 1 addition & 1 deletion accounts/abi/bind/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestWaitDeployed(t *testing.T) {
defer backend.Close()

// Create the transaction.
tx := types.NewContractCreation(0, big.NewInt(0), test.gas, big.NewInt(1), common.FromHex(test.code))
tx := types.NewContractCreation(0, big.NewInt(0), test.gas, big.NewInt(1), common.FromHex(test.code), nil)
tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)

// Wait for it to get mined in the background.
Expand Down
2 changes: 1 addition & 1 deletion consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *
return consensus.ErrFutureBlock
}
}
if header.Time <= parent.Time {
if header.Time < parent.Time {
return errOlderBlockTime
}
// Verify the block's difficulty based on its timestamp and parent's difficulty
Expand Down
16 changes: 16 additions & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ type BlockChain struct {

chainmu sync.RWMutex // blockchain insertion lock

currentTimestamp atomic.Value // Timestamp to be used when mining the current block.

currentBlock atomic.Value // Current head of the block chain
currentFastBlock atomic.Value // Current head of the fast-sync chain (may be above the block chain!)

Expand Down Expand Up @@ -234,6 +236,9 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
bc.currentBlock.Store(nilBlock)
bc.currentFastBlock.Store(nilBlock)

// TODO: Make default current timestamp configurable & make 0 if genesis else load from last block?
bc.SetCurrentTimestamp(int64(0))

// Initialize the chain with ancient data if it isn't empty.
if bc.empty() {
rawdb.InitDatabaseFromFreezer(bc.db)
Expand Down Expand Up @@ -495,6 +500,17 @@ func (bc *BlockChain) GasLimit() uint64 {
return bc.CurrentBlock().GasLimit()
}

// SetCurrentTimestamp sets the timestamp for blocks added to the canonical chain.
func (bc *BlockChain) SetCurrentTimestamp(timestamp int64) {
bc.currentTimestamp.Store(&timestamp)
}

// CurrentTimestamp retrieves the timestamp used for blocks added to the canonical chain.
func (bc *BlockChain) CurrentTimestamp() int64 {
// Note: Can never be nil
return *bc.currentTimestamp.Load().(*int64)
}

// CurrentBlock retrieves the current head block of the canonical chain. The
// block is retrieved from the blockchain's internal cache.
func (bc *BlockChain) CurrentBlock() *types.Block {
Expand Down
10 changes: 5 additions & 5 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ func TestLogReorgs(t *testing.T) {
blockchain.SubscribeRemovedLogsEvent(rmLogsCh)
chain, _ := GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), db, 2, func(i int, gen *BlockGen) {
if i == 1 {
tx, err := types.SignTx(types.NewContractCreation(gen.TxNonce(addr1), new(big.Int), 1000000, new(big.Int), code), signer, key1)
tx, err := types.SignTx(types.NewContractCreation(gen.TxNonce(addr1), new(big.Int), 1000000, new(big.Int), code, nil), signer, key1)
if err != nil {
t.Fatalf("failed to create tx: %v", err)
}
Expand Down Expand Up @@ -1042,7 +1042,7 @@ func TestLogRebirth(t *testing.T) {

chain, _ := GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), db, 2, func(i int, gen *BlockGen) {
if i == 1 {
tx, err := types.SignTx(types.NewContractCreation(gen.TxNonce(addr1), new(big.Int), 1000000, new(big.Int), code), signer, key1)
tx, err := types.SignTx(types.NewContractCreation(gen.TxNonce(addr1), new(big.Int), 1000000, new(big.Int), code, nil), signer, key1)
if err != nil {
t.Fatalf("failed to create tx: %v", err)
}
Expand All @@ -1062,7 +1062,7 @@ func TestLogRebirth(t *testing.T) {
// Generate long reorg chain
forkChain, _ := GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), db, 2, func(i int, gen *BlockGen) {
if i == 1 {
tx, err := types.SignTx(types.NewContractCreation(gen.TxNonce(addr1), new(big.Int), 1000000, new(big.Int), code), signer, key1)
tx, err := types.SignTx(types.NewContractCreation(gen.TxNonce(addr1), new(big.Int), 1000000, new(big.Int), code, nil), signer, key1)
if err != nil {
t.Fatalf("failed to create tx: %v", err)
}
Expand Down Expand Up @@ -1162,7 +1162,7 @@ func TestSideLogRebirth(t *testing.T) {
// Generate side chain with lower difficulty
sideChain, _ := GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), db, 2, func(i int, gen *BlockGen) {
if i == 1 {
tx, err := types.SignTx(types.NewContractCreation(gen.TxNonce(addr1), new(big.Int), 1000000, new(big.Int), code), signer, key1)
tx, err := types.SignTx(types.NewContractCreation(gen.TxNonce(addr1), new(big.Int), 1000000, new(big.Int), code, nil), signer, key1)
if err != nil {
t.Fatalf("failed to create tx: %v", err)
}
Expand Down Expand Up @@ -1207,7 +1207,7 @@ func TestReorgSideEvent(t *testing.T) {
}

replacementBlocks, _ := GenerateChain(gspec.Config, genesis, ethash.NewFaker(), db, 4, func(i int, gen *BlockGen) {
tx, err := types.SignTx(types.NewContractCreation(gen.TxNonce(addr1), new(big.Int), 1000000, new(big.Int), nil), signer, key1)
tx, err := types.SignTx(types.NewContractCreation(gen.TxNonce(addr1), new(big.Int), 1000000, new(big.Int), nil, nil), signer, key1)
if i == 2 {
gen.OffsetTime(-9)
}
Expand Down
2 changes: 1 addition & 1 deletion core/types/receipt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func encodeAsV3StoredReceiptRLP(want *Receipt) ([]byte, error) {
func TestDeriveFields(t *testing.T) {
// Create a few transactions to have receipts for
txs := Transactions{
NewContractCreation(1, big.NewInt(1), 1, big.NewInt(1), nil),
NewContractCreation(1, big.NewInt(1), 1, big.NewInt(1), nil, nil),
NewTransaction(2, common.HexToAddress("0x2"), big.NewInt(2), 2, big.NewInt(2), nil, nil),
}
// Create the corresponding receipts
Expand Down
28 changes: 26 additions & 2 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package types
import (
"container/heap"
"errors"
"fmt"
"io"
"math/big"
"sync/atomic"
Expand Down Expand Up @@ -76,8 +77,8 @@ func NewTransaction(nonce uint64, to common.Address, amount *big.Int, gasLimit u
return newTransaction(nonce, &to, amount, gasLimit, gasPrice, data, l1MessageSender)
}

func NewContractCreation(nonce uint64, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *Transaction {
return newTransaction(nonce, nil, amount, gasLimit, gasPrice, data, nil)
func NewContractCreation(nonce uint64, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, l1MessageSender *common.Address) *Transaction {
return newTransaction(nonce, nil, amount, gasLimit, gasPrice, data, l1MessageSender)
}

func newTransaction(nonce uint64, to *common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, l1MessageSender *common.Address) *Transaction {
Expand Down Expand Up @@ -106,6 +107,29 @@ func newTransaction(nonce uint64, to *common.Address, amount *big.Int, gasLimit
return &Transaction{data: d}
}

// Appends the provided 64-bit nonce to this Transaction's calldata as the last 4 bytes
func (t *Transaction) AddNonceToWrappedTransaction(nonce uint64) {
bytes := make([]byte, 8)
for i := range bytes {
bytes[i] = 0xFF & byte(nonce>>(56-i))
}
t.data.Payload = append(t.data.Payload, bytes...)
}

// Parses the encoded nonce from this Transaction's calldata and returns it as well as the calldata without the encoded nonce.
func (t *Transaction) GetNonceAndCalldataFromWrappedTransaction() (uint64, []byte, error) {
if len(t.data.Payload) < 8 {
return 0, nil, fmt.Errorf("Cannot parse encoded nonce out of calldata of less than 8 bytes in length. Calldata: %x", t.data.Payload)
}

nonceBytes := t.data.Payload[len(t.data.Payload)-8:]
nonce := uint64(0)
for i := range nonceBytes {
nonce += uint64(nonceBytes[i] << (56 - i))
}
return nonce, t.data.Payload[:len(t.data.Payload)-8], nil
}

// ChainId returns which chain id this transaction was signed for (if at all)
func (tx *Transaction) ChainId() *big.Int {
return deriveChainId(tx.data.V)
Expand Down
2 changes: 1 addition & 1 deletion core/types/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func TestTransactionJSON(t *testing.T) {
case 0:
tx = NewTransaction(i, common.Address{1}, common.Big0, 1, common.Big2, []byte("abcdef"), &sender)
case 1:
tx = NewContractCreation(i, common.Big0, 1, common.Big2, []byte("abcdef"))
tx = NewContractCreation(i, common.Big0, 1, common.Big2, []byte("abcdef"), nil)
}
transactions = append(transactions, tx)

Expand Down
8 changes: 8 additions & 0 deletions crypto/signature_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ func Sign(digestHash []byte, prv *ecdsa.PrivateKey) (sig []byte, err error) {
return secp256k1.Sign(digestHash, seckey)
}

func VerifyMessageSignature(pubKey, unhashedMessage, signature []byte) bool {
if len(signature) < 64 || len(signature) > 65 {
// signature format may be [R || S] or [R || S || V]
return false
}
return VerifySignature(pubKey, Keccak256(unhashedMessage), signature[0:64])
}

// VerifySignature checks that the given public key created signature over digest.
// The public key should be in compressed (33 bytes) or uncompressed (65 bytes) format.
// The signature should have the 64 byte [R || S] format.
Expand Down
8 changes: 8 additions & 0 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,14 @@ func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction)
return b.eth.txPool.AddLocal(signedTx)
}

func (b *EthAPIBackend) SendTxs(ctx context.Context, signedTxs []*types.Transaction) []error {
return b.eth.txPool.AddLocals(signedTxs)
}

func (b *EthAPIBackend) SetTimestamp(timestamp int64) {
b.eth.blockchain.SetCurrentTimestamp(timestamp)
}

func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) {
pending, err := b.eth.txPool.Pending()
if err != nil {
Expand Down
Loading

0 comments on commit 83c7d84

Please sign in to comment.