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 LogIndex and TxIndex into logs/event response body #862

Merged
merged 25 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b6380d5
Thor client (#818)
paologalligit Oct 29, 2024
af88757
Show all issues on lint (#869)
otherview Oct 30, 2024
ad5bfbd
fix(docker): using AWS docker repo for trivy (#872)
darrenvechain Nov 4, 2024
9d5b515
Darren/feat/add subscription cache (#866)
darrenvechain Nov 5, 2024
7a89780
Add additional block tests (#863)
MakisChristou Nov 6, 2024
8be8574
enhancement(logging): leverage trace level (#873)
darrenvechain Nov 7, 2024
784604d
Add testchain package (#844)
otherview Nov 7, 2024
49d9704
chore(docs): update spec for validator nodes (#875)
darrenvechain Nov 7, 2024
ca8b38b
Darren/logdb remove leading zeros (#865)
darrenvechain Oct 29, 2024
f9173e4
feat: add new txIndex column to event meta response
paologalligit Sep 24, 2024
f3bd272
test: add convert event test
paologalligit Sep 25, 2024
c5b3c63
feat: make txLog and txIndex as optional return params
paologalligit Sep 26, 2024
76a38d9
chore: update swagger with new event optional data
paologalligit Sep 26, 2024
ad6204e
feat: save logIndex in sequence
paologalligit Oct 7, 2024
1415b40
feat: tweaked bits in sequence
paologalligit Oct 11, 2024
94f4070
refactor: rename optional log meta field
paologalligit Oct 11, 2024
464f530
refactor: comments, yaml and txIndex counts
paologalligit Nov 5, 2024
9114c0b
rebase to master
paologalligit Nov 7, 2024
a80a62a
fix: remove stale struct
paologalligit Nov 6, 2024
9217741
add txIndex to returned logdb query
paologalligit Nov 7, 2024
297ffd5
reset to 0 eventCount and transferCount each receipt and write blockI…
paologalligit Nov 7, 2024
9ff47cb
fix lint
paologalligit Nov 7, 2024
6f4c9ef
rephrase logIndex description in yaml file
paologalligit Nov 7, 2024
f81bc85
refactor: use filter.Option instead of eventFilter.Option
paologalligit Nov 7, 2024
a2a74f8
move includeIndexes to api
libotony Nov 8, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/lint-go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ jobs:
version: v1.60.3
# use the default if on main branch, otherwise use the pull request config
args: --timeout=30m --config=.golangci.yml
only-new-issues: true
only-new-issues: false
skip-cache: true
2 changes: 1 addition & 1 deletion .github/workflows/on-pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Pull Request CI
on:
pull_request:
branches:
- master
- '*'

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/publish-docker-images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ jobs:
annotations: true
severity: LOW
dockerfile: ./Dockerfile
env:
# See https://github.com/aquasecurity/trivy/discussions/7538
TRIVY_DB_REPOSITORY: public.ecr.aws/aquasecurity/trivy-db:2

- name: Internal CI
uses: peter-evans/repository-dispatch@v3
Expand Down
4 changes: 2 additions & 2 deletions api/accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (a *Accounts) handleGetCode(w http.ResponseWriter, req *http.Request) error
return err
}

return utils.WriteJSON(w, map[string]string{"code": hexutil.Encode(code)})
return utils.WriteJSON(w, &GetCodeResult{Code: hexutil.Encode(code)})
}

func (a *Accounts) getAccount(addr thor.Address, header *block.Header, state *state.State) (*Account, error) {
Expand Down Expand Up @@ -164,7 +164,7 @@ func (a *Accounts) handleGetStorage(w http.ResponseWriter, req *http.Request) er
if err != nil {
return err
}
return utils.WriteJSON(w, map[string]string{"value": storage.String()})
return utils.WriteJSON(w, &GetStorageResult{Value: storage.String()})
}

func (a *Accounts) handleCallContract(w http.ResponseWriter, req *http.Request) error {
Expand Down
303 changes: 136 additions & 167 deletions api/accounts/accounts_test.go

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions api/accounts/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ type CallData struct {
Caller *thor.Address `json:"caller"`
}

type GetCodeResult struct {
Code string `json:"code"`
}

type GetStorageResult struct {
Value string `json:"value"`
}

type CallResult struct {
Data string `json:"data"`
Events []*transactions.Event `json:"events"`
Expand Down
119 changes: 47 additions & 72 deletions api/blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,42 @@ package blocks_test

import (
"encoding/json"
"io"
"math"
"math/big"
"net/http"
"net/http/httptest"
"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"
)

var genesisBlock *block.Block
var blk *block.Block
var ts *httptest.Server
const (
invalidBytes32 = "0x000000000000000000000000000000000000000000000000000000000000000g" // invalid bytes32
)

var invalidBytes32 = "0x000000000000000000000000000000000000000000000000000000000000000g" //invlaid bytes32
var (
genesisBlock *block.Block
blk *block.Block
ts *httptest.Server
tclient *thorclient.Client
)

func TestBlock(t *testing.T) {
initBlockServer(t)
defer ts.Close()

tclient = thorclient.New(ts.URL)
for name, tt := range map[string]func(*testing.T){
"testBadQueryParams": testBadQueryParams,
"testInvalidBlockID": testInvalidBlockID,
Expand All @@ -61,14 +62,16 @@ func TestBlock(t *testing.T) {

func testBadQueryParams(t *testing.T) {
badQueryParams := "?expanded=1"
res, statusCode := httpGet(t, ts.URL+"/blocks/best"+badQueryParams)
res, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/best" + badQueryParams)
require.NoError(t, err)

assert.Equal(t, http.StatusBadRequest, statusCode)
assert.Equal(t, "expanded: should be boolean", strings.TrimSpace(string(res)))
}

func testGetBestBlock(t *testing.T) {
res, statusCode := httpGet(t, ts.URL+"/blocks/best")
res, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/best")
require.NoError(t, err)
rb := new(blocks.JSONCollapsedBlock)
if err := json.Unmarshal(res, &rb); err != nil {
t.Fatal(err)
Expand All @@ -78,7 +81,8 @@ func testGetBestBlock(t *testing.T) {
}

func testGetBlockByHeight(t *testing.T) {
res, statusCode := httpGet(t, ts.URL+"/blocks/1")
res, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/1")
require.NoError(t, err)
rb := new(blocks.JSONCollapsedBlock)
if err := json.Unmarshal(res, &rb); err != nil {
t.Fatal(err)
Expand All @@ -88,7 +92,8 @@ func testGetBlockByHeight(t *testing.T) {
}

func testGetFinalizedBlock(t *testing.T) {
res, statusCode := httpGet(t, ts.URL+"/blocks/finalized")
res, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/finalized")
require.NoError(t, err)
finalized := new(blocks.JSONCollapsedBlock)
if err := json.Unmarshal(res, &finalized); err != nil {
t.Fatal(err)
Expand All @@ -101,7 +106,8 @@ func testGetFinalizedBlock(t *testing.T) {
}

func testGetJustifiedBlock(t *testing.T) {
res, statusCode := httpGet(t, ts.URL+"/blocks/justified")
res, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/justified")
require.NoError(t, err)
justified := new(blocks.JSONCollapsedBlock)
require.NoError(t, json.Unmarshal(res, &justified))

Expand All @@ -111,7 +117,8 @@ func testGetJustifiedBlock(t *testing.T) {
}

func testGetBlockByID(t *testing.T) {
res, statusCode := httpGet(t, ts.URL+"/blocks/"+blk.Header().ID().String())
res, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/" + blk.Header().ID().String())
require.NoError(t, err)
rb := new(blocks.JSONCollapsedBlock)
if err := json.Unmarshal(res, rb); err != nil {
t.Fatal(err)
Expand All @@ -121,14 +128,17 @@ func testGetBlockByID(t *testing.T) {
}

func testGetBlockNotFound(t *testing.T) {
res, statusCode := httpGet(t, ts.URL+"/blocks/0x00000000851caf3cfdb6e899cf5958bfb1ac3413d346d43539627e6be7ec1b4a")
res, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/0x00000000851caf3cfdb6e899cf5958bfb1ac3413d346d43539627e6be7ec1b4a")
require.NoError(t, err)

assert.Equal(t, http.StatusOK, statusCode)
assert.Equal(t, "null", strings.TrimSpace(string(res)))
}

func testGetExpandedBlockByID(t *testing.T) {
res, statusCode := httpGet(t, ts.URL+"/blocks/"+blk.Header().ID().String()+"?expanded=true")
res, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/" + blk.Header().ID().String() + "?expanded=true")
require.NoError(t, err)

rb := new(blocks.JSONExpandedBlock)
if err := json.Unmarshal(res, rb); err != nil {
t.Fatal(err)
Expand All @@ -139,40 +149,35 @@ func testGetExpandedBlockByID(t *testing.T) {

func testInvalidBlockNumber(t *testing.T) {
invalidNumberRevision := "4294967296" //invalid block number
_, statusCode := httpGet(t, ts.URL+"/blocks/"+invalidNumberRevision)
_, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/" + invalidNumberRevision)
require.NoError(t, err)
assert.Equal(t, http.StatusBadRequest, statusCode)
}

func testInvalidBlockID(t *testing.T) {
_, statusCode := httpGet(t, ts.URL+"/blocks/"+invalidBytes32)
_, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/" + invalidBytes32)
require.NoError(t, err)
assert.Equal(t, http.StatusBadRequest, statusCode)
}

func testGetBlockWithRevisionNumberTooHigh(t *testing.T) {
revisionNumberTooHigh := strconv.FormatUint(math.MaxUint64, 10)
res, statusCode := httpGet(t, ts.URL+"/blocks/"+revisionNumberTooHigh)
res, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/" + revisionNumberTooHigh)
require.NoError(t, err)

assert.Equal(t, http.StatusBadRequest, statusCode)
assert.Equal(t, "revision: block number out of max uint32", strings.TrimSpace(string(res)))
}

func initBlockServer(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)
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 @@ -183,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 Expand Up @@ -248,16 +236,3 @@ func checkExpandedBlock(t *testing.T, expBl *block.Block, actBl *blocks.JSONExpa
assert.Equal(t, tx.ID(), actBl.Transactions[i].ID, "txid should be equal")
}
}

func httpGet(t *testing.T, url string) ([]byte, int) {
res, err := http.Get(url) //#nosec G107
if err != nil {
t.Fatal(err)
}
r, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
}
return r, res.StatusCode
}
Loading
Loading