Skip to content

Commit

Permalink
Merge pull request #259 from vechain/1.1-dev
Browse files Browse the repository at this point in the history
1.1 dev
  • Loading branch information
qianbin authored May 24, 2019
2 parents c33e46e + e73d173 commit 66f5f1b
Show file tree
Hide file tree
Showing 60 changed files with 1,197 additions and 455 deletions.
60 changes: 47 additions & 13 deletions api/accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,20 @@ type Accounts struct {
chain *chain.Chain
stateCreator *state.Creator
callGasLimit uint64
forkConfig thor.ForkConfig
}

func New(chain *chain.Chain, stateCreator *state.Creator, callGasLimit uint64) *Accounts {
func New(
chain *chain.Chain,
stateCreator *state.Creator,
callGasLimit uint64,
forkConfig thor.ForkConfig,
) *Accounts {
return &Accounts{
chain,
stateCreator,
callGasLimit,
forkConfig,
}
}

Expand Down Expand Up @@ -188,7 +195,7 @@ func (a *Accounts) handleCallBatchCode(w http.ResponseWriter, req *http.Request)
}

func (a *Accounts) batchCall(ctx context.Context, batchCallData *BatchCallData, header *block.Header) (results BatchCallResults, err error) {
gas, gasPrice, caller, clauses, err := a.handleBatchCallData(batchCallData)
txCtx, gas, clauses, err := a.handleBatchCallData(batchCallData)
if err != nil {
return nil, err
}
Expand All @@ -204,14 +211,13 @@ func (a *Accounts) batchCall(ctx context.Context, batchCallData *BatchCallData,
Number: header.Number(),
Time: header.Timestamp(),
GasLimit: header.GasLimit(),
TotalScore: header.TotalScore()})
TotalScore: header.TotalScore(),
},
a.forkConfig)
results = make(BatchCallResults, 0)
vmout := make(chan *runtime.Output, 1)
for i, clause := range clauses {
exec, interrupt := rt.PrepareClause(clause, uint32(i), gas, &xenv.TransactionContext{
Origin: *caller,
GasPrice: gasPrice,
ProvedWork: &big.Int{}})
exec, interrupt := rt.PrepareClause(clause, uint32(i), gas, txCtx)
go func() {
out, _ := exec()
vmout <- out
Expand All @@ -237,24 +243,52 @@ func (a *Accounts) batchCall(ctx context.Context, batchCallData *BatchCallData,
return results, nil
}

func (a *Accounts) handleBatchCallData(batchCallData *BatchCallData) (gas uint64, gasPrice *big.Int, caller *thor.Address, clauses []*tx.Clause, err error) {
func (a *Accounts) handleBatchCallData(batchCallData *BatchCallData) (txCtx *xenv.TransactionContext, gas uint64, clauses []*tx.Clause, err error) {
if batchCallData.Gas > a.callGasLimit {
return 0, nil, nil, nil, utils.Forbidden(errors.New("gas: exceeds limit"))
return nil, 0, nil, utils.Forbidden(errors.New("gas: exceeds limit"))
} else if batchCallData.Gas == 0 {
gas = a.callGasLimit
} else {
gas = batchCallData.Gas
}

txCtx = &xenv.TransactionContext{}

if batchCallData.GasPrice == nil {
gasPrice = new(big.Int)
txCtx.GasPrice = new(big.Int)
} else {
gasPrice = (*big.Int)(batchCallData.GasPrice)
txCtx.GasPrice = (*big.Int)(batchCallData.GasPrice)
}
if batchCallData.Caller == nil {
caller = &thor.Address{}
txCtx.Origin = thor.Address{}
} else {
txCtx.Origin = *batchCallData.Caller
}
if batchCallData.GasPayer == nil {
txCtx.GasPayer = thor.Address{}
} else {
txCtx.GasPayer = *batchCallData.GasPayer
}
if batchCallData.ProvedWork == nil {
txCtx.ProvedWork = new(big.Int)
} else {
caller = batchCallData.Caller
txCtx.ProvedWork = (*big.Int)(batchCallData.ProvedWork)
}
txCtx.Expiration = batchCallData.Expiration

if len(batchCallData.BlockRef) > 0 {
blockRef, err := hexutil.Decode(batchCallData.BlockRef)
if err != nil {
return nil, 0, nil, errors.WithMessage(err, "blockRef")
}
if len(blockRef) != 8 {
return nil, 0, nil, errors.New("blockRef: invalid length")
}
var blkRef tx.BlockRef
copy(blkRef[:], blockRef[:])
txCtx.BlockRef = blkRef
}

clauses = make([]*tx.Clause, len(batchCallData.Clauses))
for i, c := range batchCallData.Clauses {
var value *big.Int
Expand Down
18 changes: 16 additions & 2 deletions api/accounts/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func initAccountServer(t *testing.T) {
packTx(chain, stateC, transactionCall, t)

router := mux.NewRouter()
accounts.New(chain, stateC, math.MaxUint64).Mount(router, "/accounts")
accounts.New(chain, stateC, math.MaxUint64, thor.NoFork).Mount(router, "/accounts")
ts = httptest.NewServer(router)
}

Expand All @@ -224,7 +224,7 @@ func buildTxWithClauses(t *testing.T, chaiTag byte, clauses ...*tx.Clause) *tx.T

func packTx(chain *chain.Chain, stateC *state.Creator, transaction *tx.Transaction, t *testing.T) {
b := chain.BestBlock()
packer := packer.New(chain, stateC, genesis.DevAccounts()[0].Address, &genesis.DevAccounts()[0].Address)
packer := packer.New(chain, stateC, genesis.DevAccounts()[0].Address, &genesis.DevAccounts()[0].Address, thor.NoFork)
flow, err := packer.Schedule(b.Header(), uint64(time.Now().Unix()))
err = flow.Adopt(transaction)
if err != nil {
Expand Down Expand Up @@ -369,6 +369,20 @@ func batchCall(t *testing.T) {
assert.Equal(t, a+b, ret, "should be equal")
}
assert.Equal(t, http.StatusOK, statusCode)

big := math.HexOrDecimal256(*big.NewInt(1000))
fullBody := &accounts.BatchCallData{
Clauses: accounts.Clauses{},
Gas: 21000,
GasPrice: &big,
ProvedWork: &big,
Caller: &contractAddr,
GasPayer: &contractAddr,
Expiration: 100,
BlockRef: "0x00000000aabbccdd",
}
_, statusCode = httpPost(t, ts.URL+"/accounts/*", fullBody)
assert.Equal(t, http.StatusOK, statusCode)
}

func httpPost(t *testing.T, url string, body interface{}) ([]byte, int) {
Expand Down
12 changes: 8 additions & 4 deletions api/accounts/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,14 @@ type Clauses []Clause

//BatchCallData executes a batch of codes
type BatchCallData struct {
Clauses Clauses `json:"clauses"`
Gas uint64 `json:"gas"`
GasPrice *math.HexOrDecimal256 `json:"gasPrice"`
Caller *thor.Address `json:"caller"`
Clauses Clauses `json:"clauses"`
Gas uint64 `json:"gas"`
GasPrice *math.HexOrDecimal256 `json:"gasPrice"`
ProvedWork *math.HexOrDecimal256 `json:"provedWork"`
Caller *thor.Address `json:"caller"`
GasPayer *thor.Address `json:"gasPayer"`
Expiration uint32 `json:"expiration"`
BlockRef string `json:"blockRef"`
}

type BatchCallResults []*CallResult
9 changes: 6 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/vechain/thor/chain"
"github.com/vechain/thor/logdb"
"github.com/vechain/thor/state"
"github.com/vechain/thor/thor"
"github.com/vechain/thor/txpool"
)

Expand All @@ -41,7 +42,9 @@ func New(
backtraceLimit uint32,
callGasLimit uint64,
pprofOn bool,
skipLogs bool) (http.HandlerFunc, func()) {
skipLogs bool,
forkConfig thor.ForkConfig,
) (http.HandlerFunc, func()) {

origins := strings.Split(strings.TrimSpace(allowedOrigins), ",")
for i, o := range origins {
Expand All @@ -64,7 +67,7 @@ func New(
http.Redirect(w, req, "doc/swagger-ui/", http.StatusTemporaryRedirect)
})

accounts.New(chain, stateCreator, callGasLimit).
accounts.New(chain, stateCreator, callGasLimit, forkConfig).
Mount(router, "/accounts")

if !skipLogs {
Expand All @@ -85,7 +88,7 @@ func New(
Mount(router, "/blocks")
transactions.New(chain, txPool).
Mount(router, "/transactions")
debug.New(chain, stateCreator).
debug.New(chain, stateCreator, forkConfig).
Mount(router, "/debug")
node.New(nw).
Mount(router, "/node")
Expand Down
2 changes: 1 addition & 1 deletion api/blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func initBlockServer(t *testing.T) {
t.Fatal(err)
}
tx = tx.WithSignature(sig)
packer := packer.New(chain, stateC, genesis.DevAccounts()[0].Address, &genesis.DevAccounts()[0].Address)
packer := packer.New(chain, stateC, genesis.DevAccounts()[0].Address, &genesis.DevAccounts()[0].Address, thor.NoFork)
flow, err := packer.Schedule(b.Header(), uint64(time.Now().Unix()))
if err != nil {
t.Fatal(err)
Expand Down
2 changes: 2 additions & 0 deletions api/blocks/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Block struct {
GasUsed uint64 `json:"gasUsed"`
TotalScore uint64 `json:"totalScore"`
TxsRoot thor.Bytes32 `json:"txsRoot"`
TxsFeatures uint32 `json:"txsFeatures"`
StateRoot thor.Bytes32 `json:"stateRoot"`
ReceiptsRoot thor.Bytes32 `json:"receiptsRoot"`
Signer thor.Address `json:"signer"`
Expand Down Expand Up @@ -58,6 +59,7 @@ func convertBlock(b *block.Block, isTrunk bool) (*Block, error) {
StateRoot: header.StateRoot(),
ReceiptsRoot: header.ReceiptsRoot(),
TxsRoot: header.TxsRoot(),
TxsFeatures: uint32(header.TxsFeatures()),
IsTrunk: isTrunk,
Transactions: txIds,
}, nil
Expand Down
14 changes: 10 additions & 4 deletions api/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ import (
var devNetGenesisID = thor.MustParseBytes32("0x00000000973ceb7f343a58b08f0693d6701a5fd354ff73d7058af3fba222aea4")

type Debug struct {
chain *chain.Chain
stateC *state.Creator
chain *chain.Chain
stateC *state.Creator
forkConfig thor.ForkConfig
}

func New(chain *chain.Chain, stateC *state.Creator) *Debug {
func New(chain *chain.Chain, stateC *state.Creator, forkConfig thor.ForkConfig) *Debug {
return &Debug{
chain,
stateC,
forkConfig,
}
}

Expand All @@ -57,7 +59,11 @@ func (d *Debug) handleTxEnv(ctx context.Context, blockID thor.Bytes32, txIndex u
return nil, nil, utils.Forbidden(errors.New("clause index out of range"))
}
skipPoA := d.chain.GenesisBlock().Header().ID() == devNetGenesisID
rt, err := consensus.New(d.chain, d.stateC).NewRuntimeForReplay(block.Header(), skipPoA)
rt, err := consensus.New(
d.chain,
d.stateC,
d.forkConfig,
).NewRuntimeForReplay(block.Header(), skipPoA)
if err != nil {
return nil, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/doc/bindata.go

Large diffs are not rendered by default.

Loading

0 comments on commit 66f5f1b

Please sign in to comment.