Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add testchain package #844

Merged
merged 16 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 20 additions & 52 deletions api/accounts/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,23 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
ABI "github.com/vechain/thor/v2/abi"
"github.com/vechain/thor/v2/api/accounts"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/chain"
"github.com/vechain/thor/v2/cmd/thor/solo"
"github.com/vechain/thor/v2/genesis"
"github.com/vechain/thor/v2/muxdb"
"github.com/vechain/thor/v2/packer"
"github.com/vechain/thor/v2/state"
"github.com/vechain/thor/v2/test/testchain"
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/thorclient"
tccommon "github.com/vechain/thor/v2/thorclient/common"
"github.com/vechain/thor/v2/tx"

ABI "github.com/vechain/thor/v2/abi"
tccommon "github.com/vechain/thor/v2/thorclient/common"
)

// pragma solidity ^0.4.18;
Expand Down Expand Up @@ -94,15 +90,14 @@ const (
)

var (
gasLimit uint64
gasLimit = math.MaxUint32
addr = thor.BytesToAddress([]byte("to"))
value = big.NewInt(10000)
storageKey = thor.Bytes32{}
genesisBlock *block.Block
contractAddr thor.Address
bytecode = common.Hex2Bytes("608060405234801561001057600080fd5b50610125806100206000396000f3006080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806324b8ba5f14604e578063bb4e3f4d14607b575b600080fd5b348015605957600080fd5b506079600480360381019080803560ff16906020019092919050505060cf565b005b348015608657600080fd5b5060b3600480360381019080803560ff169060200190929190803560ff16906020019092919050505060ec565b604051808260ff1660ff16815260200191505060405180910390f35b806000806101000a81548160ff021916908360ff16021790555050565b60008183019050929150505600a165627a7a723058201584add23e31d36c569b468097fe01033525686b59bbb263fb3ab82e9553dae50029")
runtimeBytecode = common.Hex2Bytes("6080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806324b8ba5f14604e578063bb4e3f4d14607b575b600080fd5b348015605957600080fd5b506079600480360381019080803560ff16906020019092919050505060cf565b005b348015608657600080fd5b5060b3600480360381019080803560ff169060200190929190803560ff16906020019092919050505060ec565b604051808260ff1660ff16815260200191505060405180910390f35b806000806101000a81548160ff021916908360ff16021790555050565b60008183019050929150505600a165627a7a723058201584add23e31d36c569b468097fe01033525686b59bbb263fb3ab82e9553dae50029")
acc *accounts.Accounts
ts *httptest.Server
tclient *thorclient.Client
)
Expand Down Expand Up @@ -270,22 +265,14 @@ func getStorageWithNonExistingRevision(t *testing.T) {
}

func initAccountServer(t *testing.T) {
db := muxdb.NewMem()
stater := state.NewStater(db)
gene := genesis.NewDevnet()
thorChain, err := testchain.NewIntegrationTestChain()
require.NoError(t, err)

b, _, _, err := gene.Build(stater)
if err != nil {
t.Fatal(err)
}
genesisBlock = b
repo, _ := chain.NewRepository(db, b)
genesisBlock = thorChain.GenesisBlock()
claTransfer := tx.NewClause(&addr).WithValue(value)
claDeploy := tx.NewClause(nil).WithData(bytecode)
transaction := buildTxWithClauses(repo.ChainTag(), claTransfer, claDeploy)
transaction := buildTxWithClauses(thorChain.Repo().ChainTag(), claTransfer, claDeploy)
contractAddr = thor.CreateContractAddress(transaction.ID(), 1, 0)
packTx(repo, stater, transaction, t)

method := "set"
abi, _ := ABI.New([]byte(abiJSON))
m, _ := abi.MethodByName(method)
Expand All @@ -294,13 +281,19 @@ func initAccountServer(t *testing.T) {
t.Fatal(err)
}
claCall := tx.NewClause(&contractAddr).WithData(input)
transactionCall := buildTxWithClauses(repo.ChainTag(), claCall)
packTx(repo, stater, transactionCall, t)
transactionCall := buildTxWithClauses(thorChain.Repo().ChainTag(), claCall)
require.NoError(t,
thorChain.MintTransactions(
genesis.DevAccounts()[0],
transaction,
transactionCall,
),
)

router := mux.NewRouter()
gasLimit = math.MaxUint32
acc = accounts.New(repo, stater, gasLimit, thor.NoFork, solo.NewBFTEngine(repo))
acc.Mount(router, "/accounts")
accounts.New(thorChain.Repo(), thorChain.Stater(), uint64(gasLimit), thor.NoFork, thorChain.Engine()).
Mount(router, "/accounts")

ts = httptest.NewServer(router)
}

Expand All @@ -318,31 +311,6 @@ func buildTxWithClauses(chaiTag byte, clauses ...*tx.Clause) *tx.Transaction {
return tx.MustSign(trx, genesis.DevAccounts()[0].PrivateKey)
}

func packTx(repo *chain.Repository, stater *state.Stater, transaction *tx.Transaction, t *testing.T) {
packer := packer.New(repo, stater, genesis.DevAccounts()[0].Address, &genesis.DevAccounts()[0].Address, thor.NoFork)
flow, err := packer.Schedule(repo.BestBlockSummary(), uint64(time.Now().Unix()))
if err != nil {
t.Fatal(err)
}
err = flow.Adopt(transaction)
if err != nil {
t.Fatal(err)
}
b, stage, receipts, err := flow.Pack(genesis.DevAccounts()[0].PrivateKey, 0, false)
if err != nil {
t.Fatal(err)
}
if _, err := stage.Commit(); err != nil {
t.Fatal(err)
}
if err := repo.AddBlock(b, receipts, 0); err != nil {
t.Fatal(err)
}
if err := repo.SetBestBlockID(b.Header().ID()); err != nil {
t.Fatal(err)
}
}

func deployContractWithCall(t *testing.T) {
badBody := &accounts.CallData{
Gas: 10000000,
Expand Down
56 changes: 13 additions & 43 deletions api/blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@ import (
"strconv"
"strings"
"testing"
"time"

"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vechain/thor/v2/api/blocks"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/chain"
"github.com/vechain/thor/v2/cmd/thor/solo"
"github.com/vechain/thor/v2/genesis"
"github.com/vechain/thor/v2/muxdb"
"github.com/vechain/thor/v2/packer"
"github.com/vechain/thor/v2/state"
"github.com/vechain/thor/v2/test/testchain"
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/thorclient"
"github.com/vechain/thor/v2/tx"
Expand Down Expand Up @@ -175,22 +170,14 @@ func testGetBlockWithRevisionNumberTooHigh(t *testing.T) {
}

func initBlockServer(t *testing.T) {
db := muxdb.NewMem()
stater := state.NewStater(db)
gene := genesis.NewDevnet()

b, _, _, err := gene.Build(stater)
if err != nil {
t.Fatal(err)
}
genesisBlock = b
thorChain, err := testchain.NewIntegrationTestChain()
require.NoError(t, err)

repo, _ := chain.NewRepository(db, b)
addr := thor.BytesToAddress([]byte("to"))
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
trx := tx.MustSign(
new(tx.Builder).
ChainTag(repo.ChainTag()).
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Expand All @@ -201,34 +188,17 @@ func initBlockServer(t *testing.T) {
genesis.DevAccounts()[0].PrivateKey,
)

packer := packer.New(repo, stater, genesis.DevAccounts()[0].Address, &genesis.DevAccounts()[0].Address, thor.NoFork)
sum, _ := repo.GetBlockSummary(b.Header().ID())
flow, err := packer.Schedule(sum, uint64(time.Now().Unix()))
if err != nil {
t.Fatal(err)
}
err = flow.Adopt(trx)
if err != nil {
t.Fatal(err)
}
block, stage, receipts, err := flow.Pack(genesis.DevAccounts()[0].PrivateKey, 0, false)
if err != nil {
t.Fatal(err)
}
if _, err := stage.Commit(); err != nil {
t.Fatal(err)
}
if err := repo.AddBlock(block, receipts, 0); err != nil {
t.Fatal(err)
}
if err := repo.SetBestBlockID(block.Header().ID()); err != nil {
t.Fatal(err)
}
require.NoError(t, thorChain.MintTransactions(genesis.DevAccounts()[0], trx))

allBlocks, err := thorChain.GetAllBlocks()
require.NoError(t, err)

genesisBlock = allBlocks[0]
blk = allBlocks[1]

router := mux.NewRouter()
bftEngine := solo.NewBFTEngine(repo)
blocks.New(repo, bftEngine).Mount(router, "/blocks")
blocks.New(thorChain.Repo(), thorChain.Engine()).Mount(router, "/blocks")
ts = httptest.NewServer(router)
blk = block
}

func checkCollapsedBlock(t *testing.T, expBl *block.Block, actBl *blocks.JSONCollapsedBlock) {
Expand Down
58 changes: 13 additions & 45 deletions api/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
Expand All @@ -22,13 +21,11 @@ import (
"github.com/stretchr/testify/require"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/builtin"
"github.com/vechain/thor/v2/chain"
"github.com/vechain/thor/v2/cmd/thor/solo"
"github.com/vechain/thor/v2/genesis"
"github.com/vechain/thor/v2/muxdb"
"github.com/vechain/thor/v2/packer"
"github.com/vechain/thor/v2/state"
"github.com/vechain/thor/v2/test/datagen"
"github.com/vechain/thor/v2/test/testchain"
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/thorclient"
"github.com/vechain/thor/v2/tracers/logger"
Expand Down Expand Up @@ -515,22 +512,15 @@ func testStorageRangeDefaultOption(t *testing.T) {
}

func initDebugServer(t *testing.T) {
db := muxdb.NewMem()
stater := state.NewStater(db)
gene := genesis.NewDevnet()

b, _, _, err := gene.Build(stater)
if err != nil {
t.Fatal(err)
}
repo, _ := chain.NewRepository(db, b)
thorChain, err := testchain.NewIntegrationTestChain()
require.NoError(t, err)

addr := thor.BytesToAddress([]byte("to"))

// Adding an empty clause transaction to the block to cover the case of
// scanning multiple txs before getting the right one
noClausesTx := new(tx.Builder).
ChainTag(repo.ChainTag()).
ChainTag(thorChain.Repo().ChainTag()).
Expiration(10).
Gas(21000).
Build()
Expand All @@ -539,7 +529,7 @@ func initDebugServer(t *testing.T) {
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
cla2 := tx.NewClause(&addr).WithValue(big.NewInt(10000))
transaction = new(tx.Builder).
ChainTag(repo.ChainTag()).
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(37000).
Expand All @@ -550,38 +540,16 @@ func initDebugServer(t *testing.T) {
Build()
transaction = tx.MustSign(transaction, genesis.DevAccounts()[0].PrivateKey)

packer := packer.New(repo, stater, genesis.DevAccounts()[0].Address, &genesis.DevAccounts()[0].Address, thor.NoFork)
sum, _ := repo.GetBlockSummary(b.Header().ID())
flow, err := packer.Schedule(sum, uint64(time.Now().Unix()))
if err != nil {
t.Fatal(err)
}
err = flow.Adopt(noClausesTx)
if err != nil {
t.Fatal(err)
}
err = flow.Adopt(transaction)
if err != nil {
t.Fatal(err)
}
b, stage, receipts, err := flow.Pack(genesis.DevAccounts()[0].PrivateKey, 0, false)
blk = b
if err != nil {
t.Fatal(err)
}
if _, err := stage.Commit(); err != nil {
t.Fatal(err)
}
if err := repo.AddBlock(b, receipts, 0); err != nil {
t.Fatal(err)
}
if err := repo.SetBestBlockID(b.Header().ID()); err != nil {
t.Fatal(err)
}
require.NoError(t, thorChain.MintTransactions(genesis.DevAccounts()[0], transaction, noClausesTx))
require.NoError(t, thorChain.MintTransactions(genesis.DevAccounts()[0]))

allBlocks, err := thorChain.GetAllBlocks()
require.NoError(t, err)
blk = allBlocks[1]

forkConfig := thor.GetForkConfig(b.Header().ID())
forkConfig := thor.GetForkConfig(blk.Header().ID())
router := mux.NewRouter()
debug = New(repo, stater, forkConfig, 21000, true, solo.NewBFTEngine(repo), []string{"all"}, false)
debug = New(thorChain.Repo(), thorChain.Stater(), forkConfig, 21000, true, thorChain.Engine(), []string{"all"}, false)
debug.Mount(router, "/debug")
ts = httptest.NewServer(router)
}
Expand Down
Loading