diff --git a/application/api/api_test.go b/application/api/api_test.go index 9c8fdf3..14b7ead 100644 --- a/application/api/api_test.go +++ b/application/api/api_test.go @@ -171,8 +171,20 @@ func TestDefaultRPC_Integration_SendAndGetTransaction(t *testing.T) { localDB, ) + // Create appchain DB for AddStandardMethods + appchainDB, err := mdbx.NewMDBX(mdbxlog.New()). + Path(t.TempDir()). + Open() + require.NoError(t, err) + + defer appchainDB.Close() + rpcServer := rpc.NewStandardRPCServer(nil) - rpc.AddStandardMethods(rpcServer, nil, txPool) + rpc.AddStandardMethods[ + application.Transaction[application.Receipt], + application.Receipt, + application.Block, + ](rpcServer, appchainDB, txPool, 42) rpcAddress := "http://127.0.0.1:18545/rpc" @@ -205,7 +217,7 @@ func TestDefaultRPC_Integration_SendAndGetTransaction(t *testing.T) { require.NoError(t, err) require.Contains(t, resp, "result") - jsonReqGet := `{"jsonrpc":"2.0","method":"getTransactionByHash","params":["` + txHash + `"],"id":2}` + jsonReqGet := `{"jsonrpc":"2.0","method":"getTransaction","params":["` + txHash + `"],"id":2}` respGet, err := sendJSONRPCRequest(rpcAddress, jsonReqGet) require.NoError(t, err) require.Contains(t, respGet, "result") @@ -259,7 +271,11 @@ func TestDefaultRPC_MethodRegistration(t *testing.T) { // Test that AddStandardMethods doesn't panic (even with minimal setup) require.NotPanics(t, func() { - rpc.AddStandardMethods(rpcServer, nil, txPool) + rpc.AddStandardMethods[ + application.Transaction[application.Receipt], + application.Receipt, + application.Block, + ](rpcServer, nil, txPool, 42) }) } diff --git a/application/block.go b/application/block.go index 412b6c1..19b6175 100644 --- a/application/block.go +++ b/application/block.go @@ -1,43 +1,53 @@ package application import ( + "crypto/sha256" + "encoding/binary" + "github.com/0xAtelerix/sdk/gosdk/apptypes" ) -var _ apptypes.AppchainBlock = &Block{} +var _ apptypes.AppchainBlock = Block{} -// step 3: -// How do your block look like type Block struct { - BlockNum uint64 `json:"number"` - Root [32]byte `json:"root"` - Transactions []apptypes.ExternalTransaction `json:"transactions"` + BlockNum uint64 `json:"number"` + BlockHash [32]byte `json:"blockHash"` + ParentHash [32]byte `json:"parentHash"` + Root [32]byte `json:"root"` + Transactions []Transaction[Receipt] `json:"transactions,omitempty"` } -func (b *Block) Number() uint64 { - return b.BlockNum +func (b Block) Hash() [32]byte { + return b.BlockHash } -func (b *Block) Hash() [32]byte { +func (b Block) StateRoot() [32]byte { return b.Root } -func (b *Block) StateRoot() [32]byte { - return b.Root -} - -func (*Block) Bytes() []byte { - return []byte{} -} - func BlockConstructor( blockNumber uint64, // blockNumber stateRoot [32]byte, // stateRoot - _ [32]byte, // previousBlockHash - _ apptypes.Batch[Transaction[Receipt], Receipt], // txsBatch + previousBlockHash [32]byte, // previousBlockHash + batch apptypes.Batch[Transaction[Receipt], Receipt], // txsBatch ) *Block { + hasher := sha256.New() + + var blockNumBytes [8]byte + binary.BigEndian.PutUint64(blockNumBytes[:], blockNumber) + hasher.Write(blockNumBytes[:]) + hasher.Write(stateRoot[:]) + hasher.Write(previousBlockHash[:]) + + // Compute final hash + var blockHash [32]byte + copy(blockHash[:], hasher.Sum(nil)) + return &Block{ - BlockNum: blockNumber, - Root: stateRoot, + BlockNum: blockNumber, + BlockHash: blockHash, + Root: stateRoot, + ParentHash: previousBlockHash, + Transactions: batch.Transactions, } } diff --git a/application/transaction.go b/application/transaction.go index aef41cc..da34399 100644 --- a/application/transaction.go +++ b/application/transaction.go @@ -10,6 +10,8 @@ import ( "github.com/ledgerwatch/erigon-lib/kv" ) +var _ apptypes.AppTransaction[Receipt] = &Transaction[Receipt]{} + func AccountKey(sender string, token string) []byte { return []byte(token + sender) } diff --git a/cmd/main.go b/cmd/main.go index 553d86f..25d1e19 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -221,7 +221,7 @@ func Run(ctx context.Context, args RuntimeArgs, _ chan<- int) { select { case <-ctx.Done(): // nothing to do - case runErr <- appchainExample.Run(ctx, nil): + case runErr <- appchainExample.Run(ctx): // nothing to do } }() @@ -232,7 +232,11 @@ func Run(ctx context.Context, args RuntimeArgs, _ chan<- int) { rpcServer.AddMiddleware(api.NewExampleMiddleware(log.Logger)) // Add standard RPC methods - Refer RPC readme in sdk for details - rpc.AddStandardMethods(rpcServer, appchainDB, txPool) + rpc.AddStandardMethods[ + application.Transaction[application.Receipt], + application.Receipt, + application.Block, + ](rpcServer, appchainDB, txPool, ChainID) // Add custom RPC methods - Optional api.NewCustomRPC(rpcServer, appchainDB).AddRPCMethods() diff --git a/go.mod b/go.mod index 2d74e74..532d499 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/0xAtelerix/example go 1.25.0 require ( - github.com/0xAtelerix/sdk v0.1.3 + github.com/0xAtelerix/sdk v0.1.6 github.com/blocto/solana-go-sdk v1.30.0 github.com/ethereum/go-ethereum v1.16.3 github.com/fxamacker/cbor/v2 v2.9.0 @@ -17,8 +17,10 @@ require ( require ( filippo.io/edwards25519 v1.1.0 // indirect github.com/VictoriaMetrics/metrics v1.40.1 // indirect + github.com/bahlo/generic-list-go v0.2.0 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/bits-and-blooms/bitset v1.24.0 // indirect + github.com/buger/jsonparser v1.1.1 // indirect github.com/c2h5oh/datasize v0.0.0-20231215233829-aa82cc1e6500 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/consensys/gnark-crypto v0.19.0 // indirect @@ -32,6 +34,8 @@ require ( github.com/fsnotify/fsnotify v1.9.0 // indirect github.com/go-stack/stack v1.8.1 // indirect github.com/goccy/go-json v0.10.5 // indirect + github.com/invopop/jsonschema v0.13.0 // indirect + github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-colorable v0.1.14 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.16 // indirect @@ -46,6 +50,7 @@ require ( github.com/supranational/blst v0.3.16 // indirect github.com/valyala/fastrand v1.1.0 // indirect github.com/valyala/histogram v1.2.0 // indirect + github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect github.com/x448/float16 v0.8.4 // indirect go.yaml.in/yaml/v2 v2.4.3 // indirect golang.org/x/crypto v0.42.0 // indirect diff --git a/go.sum b/go.sum index 5ef612a..735fda3 100644 --- a/go.sum +++ b/go.sum @@ -1,19 +1,23 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= -github.com/0xAtelerix/sdk v0.1.3 h1:w6J0aGKTEjYqLueABTWx+lH7F/VD73c8bS5oSOf3nLU= -github.com/0xAtelerix/sdk v0.1.3/go.mod h1:5ecuCbFa/U6/PlZNmR+uuaBFoBPw/J5VutS1NtjiMw0= +github.com/0xAtelerix/sdk v0.1.6 h1:v1S/KJKlGmi6wyKK+jxqS2SgMe7g0TnxR+3OopZVguk= +github.com/0xAtelerix/sdk v0.1.6/go.mod h1:N6ZEyq1hmfDhFYnQnqSrKOOiLWEfxEv0xb2EG4i/YYU= github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA= github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8= github.com/VictoriaMetrics/fastcache v1.12.2 h1:N0y9ASrJ0F6h0QaC3o6uJb3NIZ9VKLjCM7NQbSmF7WI= github.com/VictoriaMetrics/fastcache v1.12.2/go.mod h1:AmC+Nzz1+3G2eCPapF6UcsnkThDcMsQicp4xDukwJYI= github.com/VictoriaMetrics/metrics v1.40.1 h1:FrF5uJRpIVj9fayWcn8xgiI+FYsKGMslzPuOXjdeyR4= github.com/VictoriaMetrics/metrics v1.40.1/go.mod h1:XE4uudAAIRaJE614Tl5HMrtoEU6+GDZO4QTnNSsZRuA= +github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk= +github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bits-and-blooms/bitset v1.24.0 h1:H4x4TuulnokZKvHLfzVRTHJfFfnHEeSYJizujEZvmAM= github.com/bits-and-blooms/bitset v1.24.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8= github.com/blocto/solana-go-sdk v1.30.0 h1:GEh4GDjYk1lMhV/hqJDCyuDeCuc5dianbN33yxL88NU= github.com/blocto/solana-go-sdk v1.30.0/go.mod h1:Xoyhhb3hrGpEQ5rJps5a3OgMwDpmEhrd9bgzFKkkwMs= +github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= +github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= github.com/c2h5oh/datasize v0.0.0-20231215233829-aa82cc1e6500 h1:6lhrsTEnloDPXyeZBvSYvQf8u86jbKehZPVDDlkgDl4= github.com/c2h5oh/datasize v0.0.0-20231215233829-aa82cc1e6500/go.mod h1:S/7n9copUssQ56c7aAgHqftWO4LTf4xY6CGWt8Bc+3M= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= @@ -74,6 +78,9 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDa github.com/grpc-ecosystem/go-grpc-middleware v1.4.0/go.mod h1:g5qyo/la0ALbONm6Vbp88Yd8NsDy6rZz+RcrMPxvld8= github.com/holiman/uint256 v1.3.2 h1:a9EgMPSC1AAaj1SZL5zIQD3WbwTuHrMGOerLjGmM/TA= github.com/holiman/uint256 v1.3.2/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E= +github.com/invopop/jsonschema v0.13.0 h1:KvpoAJWEjR3uD9Kbm2HWJmqsEaHt8lBUpd0qHcIi21E= +github.com/invopop/jsonschema v0.13.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4= @@ -90,6 +97,8 @@ github.com/ledgerwatch/erigon-lib v1.0.0 h1:2o7EfgB/6CyjXAaQ8+Dh7AmY5rWvwSKg0kGp github.com/ledgerwatch/erigon-lib v1.0.0/go.mod h1:l1i6+H9MgizD+ObQ5cXsfA9S3egYTOCnnYGjbrJMqR4= github.com/ledgerwatch/log/v3 v3.9.0 h1:iDwrXe0PVwBC68Dd94YSsHbMgQ3ufsgjzXtFNFVZFRk= github.com/ledgerwatch/log/v3 v3.9.0/go.mod h1:EiAY6upmI/6LkNhOVxb4eVsmsP11HZCnZ3PlJMjYiqE= +github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= +github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE= github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= @@ -143,6 +152,8 @@ github.com/valyala/fastrand v1.1.0 h1:f+5HkLW4rsgzdNoleUOB69hyT9IlD2ZQh9GyDMfb5G github.com/valyala/fastrand v1.1.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ= github.com/valyala/histogram v1.2.0 h1:wyYGAZZt3CpwUiIb9AU/Zbllg1llXyrtApRS815OLoQ= github.com/valyala/histogram v1.2.0/go.mod h1:Hb4kBwb4UxsaNbbbh+RRz8ZR6pdodR57tzWUS3BUzXY= +github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc= +github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw= github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=