Skip to content

Commit

Permalink
Merge pull request #2 from trinhdn2/update/rlp-lib
Browse files Browse the repository at this point in the history
[Non-breaking changes] Update RLP library and RLP encoder code generation tool for structs
  • Loading branch information
tungnguyen17 committed Sep 25, 2023
2 parents d3cc557 + af788c6 commit a97da7d
Show file tree
Hide file tree
Showing 42 changed files with 5,173 additions and 913 deletions.
7 changes: 3 additions & 4 deletions accounts/abi/bind/backends/simulated.go
Expand Up @@ -20,8 +20,6 @@ import (
"context"
"errors"
"fmt"
"github.com/tomochain/tomochain/consensus"
"github.com/tomochain/tomochain/core/rawdb"
"math/big"
"sync"
"time"
Expand All @@ -30,9 +28,11 @@ import (
"github.com/tomochain/tomochain/accounts/abi/bind"
"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/common/math"
"github.com/tomochain/tomochain/consensus"
"github.com/tomochain/tomochain/consensus/ethash"
"github.com/tomochain/tomochain/core"
"github.com/tomochain/tomochain/core/bloombits"
"github.com/tomochain/tomochain/core/rawdb"
"github.com/tomochain/tomochain/core/state"
"github.com/tomochain/tomochain/core/types"
"github.com/tomochain/tomochain/core/vm"
Expand Down Expand Up @@ -202,7 +202,7 @@ func (b *SimulatedBackend) CallContract(ctx context.Context, call tomochain.Call
return rval, err
}

//FIXME: please use copyState for this function
// FIXME: please use copyState for this function
// CallContractWithState executes a contract call at the given state.
func (b *SimulatedBackend) CallContractWithState(call tomochain.CallMsg, chain consensus.ChainContext, statedb *state.StateDB) ([]byte, error) {
// Ensure message is initialized properly.
Expand Down Expand Up @@ -285,7 +285,6 @@ func (b *SimulatedBackend) EstimateGas(ctx context.Context, call tomochain.CallM

snapshot := b.pendingState.Snapshot()
_, _, failed, err := b.callContract(ctx, call, b.pendingBlock, b.pendingState)
fmt.Println("EstimateGas",err,failed)
b.pendingState.RevertToSnapshot(snapshot)

if err != nil || failed {
Expand Down
13 changes: 7 additions & 6 deletions build/ci.go
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

//go:build none
// +build none

/*
Expand All @@ -23,14 +24,13 @@ Usage: go run build/ci.go <command> <command flags/arguments>
Available commands are:
install [ -arch architecture ] [ -cc compiler ] [ packages... ] -- builds packages and executables
test [ -coverage ] [ packages... ] -- runs the tests
lint -- runs certain pre-selected linters
importkeys -- imports signing keys from env
xgo [ -alltools ] [ options ] -- cross builds according to options
install [ -arch architecture ] [ -cc compiler ] [ packages... ] -- builds packages and executables
test [ -coverage ] [ packages... ] -- runs the tests
lint -- runs certain pre-selected linters
importkeys -- imports signing keys from env
xgo [ -alltools ] [ options ] -- cross builds according to options
For all commands, -n prevents execution of external programs (dry run mode).
*/
package main

Expand Down Expand Up @@ -62,6 +62,7 @@ var (
executablePath("rlpdump"),
executablePath("swarm"),
executablePath("wnode"),
executablePath("rlp/rlpgen"),
}
)

Expand Down
8 changes: 4 additions & 4 deletions consensus/posv/posv.go
Expand Up @@ -21,9 +21,6 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/tomochain/tomochain/tomox/tradingstate"
"github.com/tomochain/tomochain/tomoxlending/lendingstate"
"gopkg.in/karalabe/cookiejar.v2/collections/prque"
"io/ioutil"
"math/big"
"math/rand"
Expand All @@ -50,6 +47,9 @@ import (
"github.com/tomochain/tomochain/params"
"github.com/tomochain/tomochain/rlp"
"github.com/tomochain/tomochain/rpc"
"github.com/tomochain/tomochain/tomox/tradingstate"
"github.com/tomochain/tomochain/tomoxlending/lendingstate"
"gopkg.in/karalabe/cookiejar.v2/collections/prque"
)

const (
Expand Down Expand Up @@ -1147,7 +1147,7 @@ func (c *Posv) CacheData(header *types.Header, txs []*types.Transaction, receipt
signTxs := []*types.Transaction{}
for _, tx := range txs {
if tx.IsSigningTransaction() {
var b uint
var b uint64
for _, r := range receipts {
if r.TxHash == tx.Hash() {
if len(r.PostState) > 0 {
Expand Down
58 changes: 58 additions & 0 deletions core/types/gen_header_rlp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions core/types/gen_receipt_json.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions core/types/log.go
Expand Up @@ -63,6 +63,9 @@ type logMarshaling struct {
Index hexutil.Uint
}

//go:generate go run ../../rlp/rlpgen -type rlpLog -out gen_log_rlp.go

// rlpLog is used to RLP-encode both the consensus and storage formats.
type rlpLog struct {
Address common.Address
Topics []common.Hash
Expand Down
6 changes: 3 additions & 3 deletions core/types/receipt.go
Expand Up @@ -36,17 +36,17 @@ var (

const (
// ReceiptStatusFailed is the status code of a transaction if execution failed.
ReceiptStatusFailed = uint(0)
ReceiptStatusFailed = uint64(0)

// ReceiptStatusSuccessful is the status code of a transaction if execution succeeded.
ReceiptStatusSuccessful = uint(1)
ReceiptStatusSuccessful = uint64(1)
)

// Receipt represents the results of a transaction.
type Receipt struct {
// Consensus fields
PostState []byte `json:"root"`
Status uint `json:"status"`
Status uint64 `json:"status"`
CumulativeGasUsed uint64 `json:"cumulativeGasUsed" gencodec:"required"`
Bloom Bloom `json:"logsBloom" gencodec:"required"`
Logs []*Log `json:"logs" gencodec:"required"`
Expand Down
111 changes: 111 additions & 0 deletions core/types/types_test.go
@@ -0,0 +1,111 @@
// Copyright 2021 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package types

import (
"math/big"
"testing"

"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/crypto"
"github.com/tomochain/tomochain/rlp"
)

type devnull struct{ len int }

func (d *devnull) Write(p []byte) (int, error) {
d.len += len(p)
return len(p), nil
}

func BenchmarkEncodeRLP(b *testing.B) {
benchRLP(b, true)
}

func BenchmarkDecodeRLP(b *testing.B) {
benchRLP(b, false)
}

func benchRLP(b *testing.B, encode bool) {
key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
to := common.HexToAddress("0x00000000000000000000000000000000deadbeef")
signer := NewEIP155Signer(big.NewInt(1337))
tx := NewTransaction(1, to, big.NewInt(1), 1000000, big.NewInt(500), nil)
signedTx, err := SignTx(tx, signer, key)
if err != nil {
b.Fatal("cannot sign transaction for benchmarking")
}
for _, tc := range []struct {
name string
obj interface{}
}{
{
"header",
&Header{
Difficulty: big.NewInt(10000000000),
Number: big.NewInt(1000),
GasLimit: 8_000_000,
GasUsed: 8_000_000,
Time: big.NewInt(555),
Extra: make([]byte, 32),
},
},
{
"receipt-for-storage",
&ReceiptForStorage{
Status: ReceiptStatusSuccessful,
CumulativeGasUsed: 0x888888888,
Logs: make([]*Log, 0),
},
},
{
"receipt-full",
&Receipt{
Status: ReceiptStatusSuccessful,
CumulativeGasUsed: 0x888888888,
Logs: make([]*Log, 0),
},
},
{
"transaction",
signedTx,
},
} {
if encode {
b.Run(tc.name, func(b *testing.B) {
b.ReportAllocs()
var null = &devnull{}
for i := 0; i < b.N; i++ {
rlp.Encode(null, tc.obj)
}
b.SetBytes(int64(null.len / b.N))
})
} else {
data, _ := rlp.EncodeToBytes(tc.obj)
// Test decoding
b.Run(tc.name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if err := rlp.DecodeBytes(data, tc.obj); err != nil {
b.Fatal(err)
}
}
b.SetBytes(int64(len(data)))
})
}
}
}

0 comments on commit a97da7d

Please sign in to comment.