Skip to content

Commit

Permalink
Refactor thor node
Browse files Browse the repository at this point in the history
  • Loading branch information
otherview committed Sep 11, 2024
1 parent a9357cc commit c41489d
Show file tree
Hide file tree
Showing 29 changed files with 775 additions and 389 deletions.
8 changes: 7 additions & 1 deletion api/accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"github.com/vechain/thor/v2/xenv"
)

const MountPath = "/accounts"

type Accounts struct {
repo *chain.Repository
stater *state.Stater
Expand All @@ -40,7 +42,7 @@ func New(
callGasLimit uint64,
forkConfig thor.ForkConfig,
bft bft.Committer,
) *Accounts {
) utils.APIServer {
return &Accounts{
repo,
stater,
Expand Down Expand Up @@ -382,3 +384,7 @@ func (a *Accounts) Mount(root *mux.Router, pathPrefix string) {
Name("accounts_call_contract_address").
HandlerFunc(utils.WrapHandlerFunc(a.handleCallContract))
}

func (a *Accounts) MountDefaultPath(root *mux.Router) {
a.Mount(root, MountPath)
}
76 changes: 22 additions & 54 deletions api/accounts/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,19 @@ 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/ethereum/go-ethereum/crypto"
"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/node"
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/tx"
)
Expand Down Expand Up @@ -89,7 +85,7 @@ var addr = thor.BytesToAddress([]byte("to"))
var value = big.NewInt(10000)
var storageKey = thor.Bytes32{}
var storageValue = byte(1)
var gasLimit uint64
var gasLimit = math.MaxUint32
var genesisBlock *block.Block

var contractAddr thor.Address
Expand All @@ -101,8 +97,6 @@ var runtimeBytecode = common.Hex2Bytes("6080604052600436106049576000357c01000000
var invalidAddr = "abc" //invlaid address
var invalidBytes32 = "0x000000000000000000000000000000000000000000000000000000000000000g" //invlaid bytes32
var invalidNumberRevision = "4294967296" //invalid block number

var acc *accounts.Accounts
var ts *httptest.Server

func TestAccount(t *testing.T) {
Expand Down Expand Up @@ -250,22 +244,14 @@ func getStorageWithNonExisitingRevision(t *testing.T) {
}

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

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

contractAddr = thor.CreateContractAddress(transaction.ID(), 1, 0)
method := "set"
abi, _ := ABI.New([]byte(abiJSON))
m, _ := abi.MethodByName(method)
Expand All @@ -274,14 +260,21 @@ func initAccountServer(t *testing.T) {
t.Fatal(err)
}
claCall := tx.NewClause(&contractAddr).WithData(input)
transactionCall := buildTxWithClauses(t, repo.ChainTag(), claCall)
packTx(repo, stater, transactionCall, t)

router := mux.NewRouter()
gasLimit = math.MaxUint32
acc = accounts.New(repo, stater, gasLimit, thor.NoFork, solo.NewBFTEngine(repo))
acc.Mount(router, "/accounts")
ts = httptest.NewServer(router)
transactionCall := buildTxWithClauses(t, thorChain.Repo().ChainTag(), claCall)

require.NoError(t, thorChain.MintTransactions(transaction, transactionCall))

thorNode, err := new(node.Builder).
WithChain(thorChain).
WithAPIs(
accounts.New(thorChain.Repo(), thorChain.Stater(), uint64(gasLimit), thor.NoFork, solo.NewBFTEngine(thorChain.Repo())),
).
Build()
require.NoError(t, err)

genesisBlock = thorChain.GenesisBlock()

ts = httptest.NewServer(thorNode.Router())
}

func buildTxWithClauses(t *testing.T, chaiTag byte, clauses ...*tx.Clause) *tx.Transaction {
Expand All @@ -301,31 +294,6 @@ func buildTxWithClauses(t *testing.T, chaiTag byte, clauses ...*tx.Clause) *tx.T
return transaction.WithSignature(sig)
}

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
5 changes: 3 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func New(
})

accounts.New(repo, stater, callGasLimit, forkConfig, bft).
Mount(router, "/accounts")
Mount(router, accounts.MountPath)

if !skipLogs {
events.New(repo, logDB, logsLimit).
Expand Down Expand Up @@ -114,5 +114,6 @@ func New(
handler = RequestLoggerHandler(handler, logger)
}

return handler.ServeHTTP, subs.Close // subscriptions handles hijacked conns, which need to be closed
castedSub := subs.(*subscriptions.Subscriptions)
return handler.ServeHTTP, castedSub.Close // subscriptions handles hijacked conns, which need to be closed
}
8 changes: 7 additions & 1 deletion api/blocks/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ import (
"github.com/vechain/thor/v2/thor"
)

const MountPath = "/blocks"

type Blocks struct {
repo *chain.Repository
bft bft.Committer
}

func New(repo *chain.Repository, bft bft.Committer) *Blocks {
func New(repo *chain.Repository, bft bft.Committer) utils.APIServer {
return &Blocks{
repo,
bft,
Expand Down Expand Up @@ -98,3 +100,7 @@ func (b *Blocks) Mount(root *mux.Router, pathPrefix string) {
Name("blocks_get_block").
HandlerFunc(utils.WrapHandlerFunc(b.handleGetBlock))
}

func (b *Blocks) MountDefaultPath(router *mux.Router) {
b.Mount(router, MountPath)
}
65 changes: 20 additions & 45 deletions api/blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,15 @@ import (
"strconv"
"strings"
"testing"
"time"

"github.com/ethereum/go-ethereum/crypto"
"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/node"
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/tx"
)
Expand Down Expand Up @@ -158,21 +153,13 @@ func testGetBlockWithRevisionNumberTooHigh(t *testing.T) {
}

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

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

repo, _ := chain.NewRepository(db, b)
addr := thor.BytesToAddress([]byte("to"))
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
tx := new(tx.Builder).
ChainTag(repo.ChainTag()).
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Expand All @@ -186,34 +173,22 @@ func initBlockServer(t *testing.T) {
t.Fatal(err)
}
tx = tx.WithSignature(sig)
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(tx)
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)
}
router := mux.NewRouter()
bftEngine := solo.NewBFTEngine(repo)
blocks.New(repo, bftEngine).Mount(router, "/blocks")
ts = httptest.NewServer(router)
blk = block
require.NoError(t, thorChain.MintTransactions(tx))

thorNode, err := new(node.Builder).
WithChain(thorChain).
WithAPIs(
blocks.New(thorChain.Repo(), solo.NewBFTEngine(thorChain.Repo())),
).
Build()
require.NoError(t, err)

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

genesisBlock = allBlocks[0]
blk = allBlocks[1]
ts = httptest.NewServer(thorNode.Router())
}

func checkCollapsedBlock(t *testing.T, expBl *block.Block, actBl *blocks.JSONCollapsedBlock) {
Expand Down
14 changes: 11 additions & 3 deletions api/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ import (
"github.com/vechain/thor/v2/xenv"
)

const defaultMaxStorageResult = 1000
const (
defaultMaxStorageResult = 1000

MountPath = "/debug"
)

type Debug struct {
repo *chain.Repository
Expand All @@ -56,8 +60,8 @@ func New(
allowCustomTracer bool,
bft bft.Committer,
allowedTracers []string,
soloMode bool) *Debug {

soloMode bool,
) utils.APIServer {
allowedMap := make(map[string]struct{})
for _, t := range allowedTracers {
allowedMap[t] = struct{}{}
Expand Down Expand Up @@ -477,3 +481,7 @@ func (d *Debug) Mount(root *mux.Router, pathPrefix string) {
Name("debug_trace_storage").
HandlerFunc(utils.WrapHandlerFunc(d.handleDebugStorage))
}

func (d *Debug) MountDefaultPath(router *mux.Router) {
d.Mount(router, MountPath)
}
Loading

0 comments on commit c41489d

Please sign in to comment.