From d74711bc9f84b8d19bfba35143f8c9a637c39c12 Mon Sep 17 00:00:00 2001 From: Cesar Date: Fri, 29 Sep 2023 02:14:28 -0400 Subject: [PATCH] Integrating Warp Payload Types into AvalancheGo for Cross-VM Compatibility This fixes https://github.com/ava-labs/avalanchego/discussions/2050 and https://github.com/ava-labs/avalanchego/pull/2116 --- go.mod | 3 +- go.sum | 19 +++++- plugin/evm/block.go | 4 +- plugin/evm/vm_warp_test.go | 12 ++-- warp/payload/README.md | 44 ------------- warp/payload/addressed_payload.go | 60 ----------------- warp/payload/block_hash_payload.go | 57 ---------------- warp/payload/codec.go | 43 ------------- warp/payload/payload_test.go | 100 ----------------------------- x/warp/contract.go | 4 +- x/warp/contract_test.go | 14 ++-- x/warp/contract_warp_handler.go | 6 +- x/warp/predicate_test.go | 5 +- 13 files changed, 42 insertions(+), 329 deletions(-) delete mode 100644 warp/payload/README.md delete mode 100644 warp/payload/addressed_payload.go delete mode 100644 warp/payload/block_hash_payload.go delete mode 100644 warp/payload/codec.go delete mode 100644 warp/payload/payload_test.go diff --git a/go.mod b/go.mod index 1bf813f01b..366db432c9 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.20 require ( github.com/VictoriaMetrics/fastcache v1.10.0 github.com/ava-labs/avalanche-network-runner v1.7.2 - github.com/ava-labs/avalanchego v1.10.10 + github.com/ava-labs/avalanchego v1.10.12-0.20231002133535-d74b0f447351 github.com/cespare/cp v0.1.0 github.com/cockroachdb/pebble v0.0.0-20230209160836-829675f94811 github.com/davecgh/go-spew v1.1.1 @@ -87,6 +87,7 @@ require ( github.com/google/btree v1.1.2 // indirect github.com/google/go-cmp v0.5.9 // indirect github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect + github.com/google/renameio/v2 v2.0.0 // indirect github.com/gorilla/mux v1.8.0 // indirect github.com/graph-gophers/graphql-go v1.3.0 // indirect github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect diff --git a/go.sum b/go.sum index a98b5164c6..13b84c9afa 100644 --- a/go.sum +++ b/go.sum @@ -61,8 +61,12 @@ github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kd github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= github.com/ava-labs/avalanche-network-runner v1.7.2 h1:XFad/wZfYzDnqbLzPAPPRYU3a1Zc8QT8x5dtLTS3lUo= github.com/ava-labs/avalanche-network-runner v1.7.2/go.mod h1:naLveusSrP7YgTAqRykD1SyLOAUilCp9jGjk3MDxoPI= -github.com/ava-labs/avalanchego v1.10.10 h1:EYX4LVotcfdtIQ0nJSBTcoisubx/Bzk2tM1aP3yiYiw= -github.com/ava-labs/avalanchego v1.10.10/go.mod h1:6UA0nxxTvvpyuCbP2DSzx9+7uWQfQx9DPApK8JptLiE= +github.com/ava-labs/avalanchego v1.10.12-0.20230929173759-0f8b272df136 h1:rlgfS5uCP59bhkCA1g8f2xL+2wn0B4DnKCCLdqbuSMs= +github.com/ava-labs/avalanchego v1.10.12-0.20230929173759-0f8b272df136/go.mod h1:1bhtTwb+6vthA/dzImBJFpCJGKx3wDZrIxx//1Yhuzw= +github.com/ava-labs/avalanchego v1.10.12-0.20230929235304-f13427e55a10 h1:YZCEpl+HkwVLyUSDaEm+EM1eq7ELfdsrmpCdZKXzTnQ= +github.com/ava-labs/avalanchego v1.10.12-0.20230929235304-f13427e55a10/go.mod h1:1bhtTwb+6vthA/dzImBJFpCJGKx3wDZrIxx//1Yhuzw= +github.com/ava-labs/avalanchego v1.10.12-0.20231002133535-d74b0f447351 h1:YF+NBTgq/Rj8iytuIhHayD8IhdMlC58X8cfXdwSGCgc= +github.com/ava-labs/avalanchego v1.10.12-0.20231002133535-d74b0f447351/go.mod h1:1bhtTwb+6vthA/dzImBJFpCJGKx3wDZrIxx//1Yhuzw= github.com/ava-labs/coreth v0.12.5-rc.6 h1:OajGUyKkO5Q82XSuMa8T5UD6QywtCHUiZ4Tv3RFmRBU= github.com/ava-labs/coreth v0.12.5-rc.6/go.mod h1:s5wVyy+5UCCk2m0Tq3jVmy0UqOpKBDYqRE13gInCJVs= github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g= @@ -131,6 +135,7 @@ github.com/cockroachdb/pebble v0.0.0-20230209160836-829675f94811 h1:ytcWPaNPhNoG github.com/cockroachdb/pebble v0.0.0-20230209160836-829675f94811/go.mod h1:Nb5lgvnQ2+oGlE/EyZy4+2/CxRh9KfvCXnag1vtpxVM= github.com/cockroachdb/redact v1.1.3 h1:AKZds10rFSIj7qADf0g46UixK8NNLwWTNdCIGS5wfSQ= github.com/cockroachdb/redact v1.1.3/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg= +github.com/cockroachdb/sentry-go v0.6.1-cockroachdb.2/go.mod h1:8BT+cPK6xvFOcRlk0R8eg+OTkcqI6baNH4xAkpiYVvQ= github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0/go.mod h1:4Zcjuz89kmFXt9morQgcfYZAYZ5n8WHjt81YYWIwtTM= github.com/consensys/bavard v0.1.13 h1:oLhMLOFGTLdlda/kma4VOJazblc7IM5y5QPd2A/YjhQ= github.com/consensys/bavard v0.1.13/go.mod h1:9ItSMtA/dXMAiL7BG6bqW2m3NdSEObYWoH223nGHukI= @@ -220,6 +225,7 @@ github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxI github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= +github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= @@ -322,6 +328,8 @@ github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLe github.com/google/pprof v0.0.0-20230207041349-798e818bf904 h1:4/hN5RUoecvl+RmJRE2YxKWtnnQls6rQjjW5oV7qg2U= github.com/google/pprof v0.0.0-20230207041349-798e818bf904/go.mod h1:uglQLonpP8qtYCYyzA+8c/9qtqgA3qsXGYqCPKARAFg= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/google/renameio/v2 v2.0.0 h1:UifI23ZTGY8Tt29JbYFiuyIU3eX+RNFtUwefq9qAhxg= +github.com/google/renameio/v2 v2.0.0/go.mod h1:BtmJXm5YlszgC+TD4HOEEUFgkJP3nLxehU6hfe7jRt4= github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= @@ -388,13 +396,16 @@ github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7Bd github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= +github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= +github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k= github.com/kataras/golog v0.0.10/go.mod h1:yJ8YKCmyL+nWjERB90Qwn+bdyBZsaQwU3bTVFgkFIp8= github.com/kataras/iris/v12 v12.1.8/go.mod h1:LMYy4VlP67TQ3Zgriz8RE2h2kMZV2SgMYbq3UhfoFmE= @@ -425,6 +436,7 @@ github.com/labstack/echo/v4 v4.2.1/go.mod h1:AA49e0DZ8kk5jTOOCKNuPR6oTnBS0dYiM4F github.com/labstack/echo/v4 v4.5.0/go.mod h1:czIriw4a0C1dFun+ObrXp7ok03xON0N1awStJ6ArI7Y= github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k= github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c= +github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= @@ -466,9 +478,11 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/moul/http2curl v1.0.0/go.mod h1:8UbvGypXm98wA/IqH45anm5Y2Z6ep6O31QGOAZ3H0fQ= github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o= github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= +github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/nats-io/jwt v0.3.0/go.mod h1:fRYCDE99xlTsqUzISS1Bi75UBJ6ljOJQOAAu5VglpSg= github.com/nats-io/nats.go v1.9.1/go.mod h1:ZjDU1L/7fJ09jvUSRVBR2e7+RnLiiIQyqyzEE/Zbp4w= github.com/nats-io/nkeys v0.1.0/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w= @@ -576,6 +590,7 @@ github.com/status-im/keycard-go v0.2.0/go.mod h1:wlp8ZLbsmrF6g6WjugPAx+IzoLrkdf9 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= diff --git a/plugin/evm/block.go b/plugin/evm/block.go index a16bc894ed..4682b8411d 100644 --- a/plugin/evm/block.go +++ b/plugin/evm/block.go @@ -13,6 +13,7 @@ import ( "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rlp" + "github.com/ava-labs/avalanchego/vms/platformvm/warp/payload" "github.com/ava-labs/subnet-evm/core" "github.com/ava-labs/subnet-evm/core/rawdb" "github.com/ava-labs/subnet-evm/core/types" @@ -20,7 +21,6 @@ import ( "github.com/ava-labs/subnet-evm/precompile/precompileconfig" "github.com/ava-labs/subnet-evm/precompile/results" "github.com/ava-labs/subnet-evm/utils/predicate" - "github.com/ava-labs/subnet-evm/warp/payload" "github.com/ava-labs/subnet-evm/x/warp" "github.com/ava-labs/avalanchego/ids" @@ -133,7 +133,7 @@ func (b *Block) handlePrecompileAccept(rules *params.Rules, sharedMemoryWriter * // If Warp is enabled, add the block hash as an unsigned message to the warp backend. if rules.IsPrecompileEnabled(warp.ContractAddress) { - blockHashPayload, err := payload.NewBlockHashPayload(b.ethBlock.Hash()) + blockHashPayload, err := payload.NewBlockHashPayload(ids.ID(b.ethBlock.Hash().Bytes())) if err != nil { return fmt.Errorf("failed to create block hash payload: %w", err) } diff --git a/plugin/evm/vm_warp_test.go b/plugin/evm/vm_warp_test.go index c391ae733d..9c0a07c7ef 100644 --- a/plugin/evm/vm_warp_test.go +++ b/plugin/evm/vm_warp_test.go @@ -21,6 +21,7 @@ import ( "github.com/ava-labs/avalanchego/utils/set" "github.com/ava-labs/avalanchego/vms/components/chain" avalancheWarp "github.com/ava-labs/avalanchego/vms/platformvm/warp" + warpPayload "github.com/ava-labs/avalanchego/vms/platformvm/warp/payload" "github.com/ava-labs/subnet-evm/core" "github.com/ava-labs/subnet-evm/core/rawdb" "github.com/ava-labs/subnet-evm/core/types" @@ -29,7 +30,6 @@ import ( "github.com/ava-labs/subnet-evm/precompile/contract" subnetEVMUtils "github.com/ava-labs/subnet-evm/utils" predicateutils "github.com/ava-labs/subnet-evm/utils/predicate" - warpPayload "github.com/ava-labs/subnet-evm/warp/payload" "github.com/ava-labs/subnet-evm/x/warp" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" @@ -132,7 +132,7 @@ func TestValidateWarpMessage(t *testing.T) { sourceAddress := common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2") payload := []byte{1, 2, 3} addressedPayload, err := warpPayload.NewAddressedPayload( - sourceAddress, + sourceAddress.Bytes(), payload, ) require.NoError(err) @@ -158,7 +158,7 @@ func TestValidateInvalidWarpMessage(t *testing.T) { sourceAddress := common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2") payload := []byte{1, 2, 3} addressedPayload, err := warpPayload.NewAddressedPayload( - sourceAddress, + sourceAddress.Bytes(), payload, ) require.NoError(err) @@ -179,7 +179,7 @@ func TestValidateWarpBlockHash(t *testing.T) { require := require.New(t) sourceChainID := ids.GenerateTestID() blockHash := ids.GenerateTestID() - blockHashPayload, err := warpPayload.NewBlockHashPayload(common.Hash(blockHash)) + blockHashPayload, err := warpPayload.NewBlockHashPayload(blockHash) require.NoError(err) unsignedMessage, err := avalancheWarp.NewUnsignedMessage(testNetworkID, sourceChainID, blockHashPayload.Bytes()) require.NoError(err) @@ -200,7 +200,7 @@ func TestValidateInvalidWarpBlockHash(t *testing.T) { require := require.New(t) sourceChainID := ids.GenerateTestID() blockHash := ids.GenerateTestID() - blockHashPayload, err := warpPayload.NewBlockHashPayload(common.Hash(blockHash)) + blockHashPayload, err := warpPayload.NewBlockHashPayload(blockHash) require.NoError(err) unsignedMessage, err := avalancheWarp.NewUnsignedMessage(testNetworkID, sourceChainID, blockHashPayload.Bytes()) require.NoError(err) @@ -394,7 +394,7 @@ func TestReceiveWarpMessage(t *testing.T) { payload := utils.RandomBytes(100) addressedPayload, err := warpPayload.NewAddressedPayload( - testEthAddrs[0], + testEthAddrs[0].Bytes(), payload, ) require.NoError(err) diff --git a/warp/payload/README.md b/warp/payload/README.md deleted file mode 100644 index 1da3b92712..0000000000 --- a/warp/payload/README.md +++ /dev/null @@ -1,44 +0,0 @@ -# Payload - -An Avalanche Unsigned Warp Message already includes a `networkID`, `sourceChainID`, and `payload` field. The `payload` field is parsed into one of the types included in this package to be handled by the EVM. - -## AddressedPayload - -AddressedPayload: -``` -+---------------------+----------+-------------------+ -| codecID : uint16 | 2 bytes | -+---------------------+----------+-------------------+ -| typeID : uint32 | 4 bytes | -+---------------------+----------+-------------------+ -| sourceAddress : [20]byte | 20 bytes | -+---------------------+----------+-------------------+ -| payload : []byte | 4 + len(payload) | -+---------------------+----------+-------------------+ - | 30 + len(payload) | - +-------------------+ -``` - -- `codecID` is the codec version used to serialize the payload and is hardcoded to `0x0000` -- `typeID` is the payload type identifier and is `0x00000000` for `AddressedPayload` -- `sourceAddress` is the address that called `sendWarpPrecompile` on the source chain -- `payload` is an arbitrary byte array payload - -## BlockHashPayload - -BlockHashPayload: -``` -+-----------------+----------+-----------+ -| codecID : uint16 | 2 bytes | -+-----------------+----------+-----------+ -| typeID : uint32 | 4 bytes | -+-----------------+----------+-----------+ -| blockHash : [32]byte | 32 bytes | -+-----------------+----------+-----------+ - | 38 bytes | - +-----------+ -``` - -- `codecID` is the codec version used to serialize the payload and is hardcoded to `0x0000` -- `typeID` is the payload type identifier and is `0x00000001` for `BlockHashPayload` -- `blockHash` is a blockHash from the `sourceChainID`. A signed block hash payload indicates that the signer has accepted the block on the source chain. diff --git a/warp/payload/addressed_payload.go b/warp/payload/addressed_payload.go deleted file mode 100644 index a0ed6767d6..0000000000 --- a/warp/payload/addressed_payload.go +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright (C) 2023, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package payload - -import ( - "fmt" - - "github.com/ethereum/go-ethereum/common" -) - -// AddressedPayload defines the format for delivering a point to point message across VMs -// ie. (ChainA, AddressA) -> (ChainB, AddressB) -type AddressedPayload struct { - SourceAddress common.Address `serialize:"true"` - Payload []byte `serialize:"true"` - - bytes []byte -} - -// NewAddressedPayload creates a new *AddressedPayload and initializes it. -func NewAddressedPayload(sourceAddress common.Address, payload []byte) (*AddressedPayload, error) { - ap := &AddressedPayload{ - SourceAddress: sourceAddress, - Payload: payload, - } - return ap, ap.initialize() -} - -// ParseAddressedPayload converts a slice of bytes into an initialized -// AddressedPayload. -func ParseAddressedPayload(b []byte) (*AddressedPayload, error) { - var unmarshalledPayloadIntf any - if _, err := c.Unmarshal(b, &unmarshalledPayloadIntf); err != nil { - return nil, err - } - payload, ok := unmarshalledPayloadIntf.(*AddressedPayload) - if !ok { - return nil, fmt.Errorf("%w: %T", errWrongType, unmarshalledPayloadIntf) - } - payload.bytes = b - return payload, nil -} - -// initialize recalculates the result of Bytes(). -func (a *AddressedPayload) initialize() error { - payloadIntf := any(a) - bytes, err := c.Marshal(codecVersion, &payloadIntf) - if err != nil { - return fmt.Errorf("couldn't marshal warp addressed payload: %w", err) - } - a.bytes = bytes - return nil -} - -// Bytes returns the binary representation of this payload. It assumes that the -// payload is initialized from either NewAddressedPayload or ParseAddressedPayload. -func (a *AddressedPayload) Bytes() []byte { - return a.bytes -} diff --git a/warp/payload/block_hash_payload.go b/warp/payload/block_hash_payload.go deleted file mode 100644 index bd3ce6eda2..0000000000 --- a/warp/payload/block_hash_payload.go +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright (C) 2023, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package payload - -import ( - "fmt" - - "github.com/ethereum/go-ethereum/common" -) - -// BlockHashPayload includes the block hash -type BlockHashPayload struct { - BlockHash common.Hash `serialize:"true"` - - bytes []byte -} - -// NewBlockHashPayload creates a new *BlockHashPayload and initializes it. -func NewBlockHashPayload(blockHash common.Hash) (*BlockHashPayload, error) { - bhp := &BlockHashPayload{ - BlockHash: blockHash, - } - return bhp, bhp.initialize() -} - -// ParseBlockHashPayload converts a slice of bytes into an initialized -// BlockHashPayload -func ParseBlockHashPayload(b []byte) (*BlockHashPayload, error) { - var unmarshalledPayloadIntf any - if _, err := c.Unmarshal(b, &unmarshalledPayloadIntf); err != nil { - return nil, err - } - payload, ok := unmarshalledPayloadIntf.(*BlockHashPayload) - if !ok { - return nil, fmt.Errorf("%w: %T", errWrongType, unmarshalledPayloadIntf) - } - payload.bytes = b - return payload, nil -} - -// initialize recalculates the result of Bytes(). -func (b *BlockHashPayload) initialize() error { - payloadIntf := any(b) - bytes, err := c.Marshal(codecVersion, &payloadIntf) - if err != nil { - return fmt.Errorf("couldn't marshal block hash payload: %w", err) - } - b.bytes = bytes - return nil -} - -// Bytes returns the binary representation of this payload. It assumes that the -// payload is initialized from either NewBlockHashPayload or ParseBlockHashPayload. -func (b *BlockHashPayload) Bytes() []byte { - return b.bytes -} diff --git a/warp/payload/codec.go b/warp/payload/codec.go deleted file mode 100644 index 459506bf4d..0000000000 --- a/warp/payload/codec.go +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright (C) 2023, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package payload - -import ( - "errors" - - "github.com/ava-labs/avalanchego/codec" - "github.com/ava-labs/avalanchego/codec/linearcodec" - "github.com/ava-labs/avalanchego/utils/units" - "github.com/ava-labs/avalanchego/utils/wrappers" -) - -var errWrongType = errors.New("wrong payload type") - -const ( - codecVersion = 0 - - MaxMessageSize = 24 * units.KiB - - // Note: Modifying this variable can have subtle implications on memory - // usage when parsing malformed payloads. - MaxSliceLen = 24 * units.KiB -) - -// Codec does serialization and deserialization for Warp messages. -var c codec.Manager - -func init() { - c = codec.NewManager(MaxMessageSize) - lc := linearcodec.NewCustomMaxLength(MaxSliceLen) - - errs := wrappers.Errs{} - errs.Add( - lc.RegisterType(&AddressedPayload{}), - lc.RegisterType(&BlockHashPayload{}), - c.RegisterCodec(codecVersion, lc), - ) - if errs.Errored() { - panic(errs.Err) - } -} diff --git a/warp/payload/payload_test.go b/warp/payload/payload_test.go deleted file mode 100644 index 69856538cb..0000000000 --- a/warp/payload/payload_test.go +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright (C) 2023, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package payload - -import ( - "encoding/base64" - "testing" - - "github.com/ava-labs/avalanchego/ids" - "github.com/ava-labs/avalanchego/utils" - "github.com/ethereum/go-ethereum/common" - "github.com/stretchr/testify/require" -) - -func TestAddressedPayload(t *testing.T) { - require := require.New(t) - - addressedPayload, err := NewAddressedPayload( - common.Address(ids.GenerateTestShortID()), - []byte{1, 2, 3}, - ) - require.NoError(err) - - addressedPayloadBytes := addressedPayload.Bytes() - addressedPayload2, err := ParseAddressedPayload(addressedPayloadBytes) - require.NoError(err) - require.Equal(addressedPayload, addressedPayload2) -} - -func TestParseAddressedPayloadJunk(t *testing.T) { - _, err := ParseAddressedPayload(utils.RandomBytes(1024)) - require.Error(t, err) -} - -func TestParseAddressedPayload(t *testing.T) { - base64Payload := "AAAAAAAAAQIDAAAAAAAAAAAAAAAAAAAAAAAAAAADCgsM" - payload := &AddressedPayload{ - SourceAddress: common.Address{1, 2, 3}, - Payload: []byte{10, 11, 12}, - } - - require.NoError(t, payload.initialize()) - - require.Equal(t, base64Payload, base64.StdEncoding.EncodeToString(payload.Bytes())) - - parsedPayload, err := ParseAddressedPayload(payload.Bytes()) - require.NoError(t, err) - require.Equal(t, payload, parsedPayload) -} - -func TestBlockHashPayload(t *testing.T) { - require := require.New(t) - - blockHashPayload, err := NewBlockHashPayload(common.Hash(ids.GenerateTestID())) - require.NoError(err) - - blockHashPayloadBytes := blockHashPayload.Bytes() - blockHashPayload2, err := ParseBlockHashPayload(blockHashPayloadBytes) - require.NoError(err) - require.Equal(blockHashPayload, blockHashPayload2) -} - -func TestParseBlockHashPayloadJunk(t *testing.T) { - _, err := ParseBlockHashPayload(utils.RandomBytes(1024)) - require.Error(t, err) -} - -func TestParseBlockHashPayload(t *testing.T) { - base64Payload := "AAAAAAABBAUGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" - payload := &BlockHashPayload{ - BlockHash: common.Hash{4, 5, 6}, - } - - require.NoError(t, payload.initialize()) - - require.Equal(t, base64Payload, base64.StdEncoding.EncodeToString(payload.Bytes())) - - parsedPayload, err := ParseBlockHashPayload(payload.Bytes()) - require.NoError(t, err) - require.Equal(t, payload, parsedPayload) -} - -func TestParseWrongPayloadType(t *testing.T) { - require := require.New(t) - blockHashPayload, err := NewBlockHashPayload(common.Hash(ids.GenerateTestID())) - require.NoError(err) - - addressedPayload, err := NewAddressedPayload( - common.Address(ids.GenerateTestShortID()), - []byte{1, 2, 3}, - ) - require.NoError(err) - - _, err = ParseAddressedPayload(blockHashPayload.Bytes()) - require.ErrorIs(err, errWrongType) - - _, err = ParseBlockHashPayload(addressedPayload.Bytes()) - require.ErrorIs(err, errWrongType) -} diff --git a/x/warp/contract.go b/x/warp/contract.go index 29a196cae5..81b0aba950 100644 --- a/x/warp/contract.go +++ b/x/warp/contract.go @@ -8,11 +8,11 @@ import ( "fmt" "github.com/ava-labs/avalanchego/vms/platformvm/warp" + warpPayload "github.com/ava-labs/avalanchego/vms/platformvm/warp/payload" "github.com/ava-labs/subnet-evm/accounts/abi" "github.com/ava-labs/subnet-evm/params" "github.com/ava-labs/subnet-evm/precompile/contract" "github.com/ava-labs/subnet-evm/vmerrs" - warpPayload "github.com/ava-labs/subnet-evm/warp/payload" _ "embed" @@ -232,7 +232,7 @@ func sendWarpMessage(accessibleState contract.AccessibleState, caller common.Add ) addressedPayload, err := warpPayload.NewAddressedPayload( - sourceAddress, + sourceAddress.Bytes(), payload, ) if err != nil { diff --git a/x/warp/contract_test.go b/x/warp/contract_test.go index d29f6d826f..387bb19ade 100644 --- a/x/warp/contract_test.go +++ b/x/warp/contract_test.go @@ -13,12 +13,12 @@ import ( "github.com/ava-labs/avalanchego/utils" "github.com/ava-labs/avalanchego/utils/set" avalancheWarp "github.com/ava-labs/avalanchego/vms/platformvm/warp" + warpPayload "github.com/ava-labs/avalanchego/vms/platformvm/warp/payload" "github.com/ava-labs/subnet-evm/core/state" "github.com/ava-labs/subnet-evm/precompile/contract" "github.com/ava-labs/subnet-evm/precompile/testutils" predicateutils "github.com/ava-labs/subnet-evm/utils/predicate" "github.com/ava-labs/subnet-evm/vmerrs" - warpPayload "github.com/ava-labs/subnet-evm/warp/payload" "github.com/ethereum/go-ethereum/common" "github.com/stretchr/testify/require" ) @@ -140,7 +140,7 @@ func TestSendWarpMessage(t *testing.T) { addressedPayload, err := warpPayload.ParseAddressedPayload(unsignedWarpMsg.Payload) require.NoError(t, err) - require.Equal(t, addressedPayload.SourceAddress, callerAddr) + require.Equal(t, common.BytesToAddress(addressedPayload.SourceAddress), callerAddr) require.Equal(t, unsignedWarpMsg.SourceChainID, blockchainID) require.Equal(t, addressedPayload.Payload, sendWarpMessagePayload) }, @@ -157,7 +157,7 @@ func TestGetVerifiedWarpMessage(t *testing.T) { sourceChainID := ids.GenerateTestID() packagedPayloadBytes := []byte("mcsorley") addressedPayload, err := warpPayload.NewAddressedPayload( - sourceAddress, + sourceAddress.Bytes(), packagedPayloadBytes, ) require.NoError(t, err) @@ -422,7 +422,7 @@ func TestGetVerifiedWarpBlockHash(t *testing.T) { networkID := uint32(54321) callerAddr := common.HexToAddress("0x0123") sourceChainID := ids.GenerateTestID() - blockHash := common.Hash(ids.GenerateTestID()) + blockHash := ids.GenerateTestID() blockHashPayload, err := warpPayload.NewBlockHashPayload(blockHash) require.NoError(t, err) unsignedWarpMsg, err := avalancheWarp.NewUnsignedMessage(networkID, sourceChainID, blockHashPayload.Bytes()) @@ -449,7 +449,7 @@ func TestGetVerifiedWarpBlockHash(t *testing.T) { res, err := PackGetVerifiedWarpBlockHashOutput(GetVerifiedWarpBlockHashOutput{ WarpBlockHash: WarpBlockHash{ SourceChainID: common.Hash(sourceChainID), - BlockHash: blockHash, + BlockHash: common.Hash(blockHash), }, Valid: true, }) @@ -505,7 +505,7 @@ func TestGetVerifiedWarpBlockHash(t *testing.T) { res, err := PackGetVerifiedWarpBlockHashOutput(GetVerifiedWarpBlockHashOutput{ WarpBlockHash: WarpBlockHash{ SourceChainID: common.Hash(sourceChainID), - BlockHash: blockHash, + BlockHash: common.Hash(blockHash), }, Valid: true, }) @@ -546,7 +546,7 @@ func TestGetVerifiedWarpBlockHash(t *testing.T) { res, err := PackGetVerifiedWarpBlockHashOutput(GetVerifiedWarpBlockHashOutput{ WarpBlockHash: WarpBlockHash{ SourceChainID: common.Hash(sourceChainID), - BlockHash: blockHash, + BlockHash: common.Hash(blockHash), }, Valid: true, }) diff --git a/x/warp/contract_warp_handler.go b/x/warp/contract_warp_handler.go index ade22ee2fa..81adfe64c0 100644 --- a/x/warp/contract_warp_handler.go +++ b/x/warp/contract_warp_handler.go @@ -8,10 +8,10 @@ import ( "github.com/ava-labs/avalanchego/utils/set" "github.com/ava-labs/avalanchego/vms/platformvm/warp" + warpPayload "github.com/ava-labs/avalanchego/vms/platformvm/warp/payload" "github.com/ava-labs/subnet-evm/precompile/contract" predicateutils "github.com/ava-labs/subnet-evm/utils/predicate" "github.com/ava-labs/subnet-evm/vmerrs" - warpPayload "github.com/ava-labs/subnet-evm/warp/payload" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/math" ) @@ -107,7 +107,7 @@ func (addressedPayloadHandler) handleMessage(warpMessage *warp.Message) ([]byte, return PackGetVerifiedWarpMessageOutput(GetVerifiedWarpMessageOutput{ Message: WarpMessage{ SourceChainID: common.Hash(warpMessage.SourceChainID), - OriginSenderAddress: addressedPayload.SourceAddress, + OriginSenderAddress: common.BytesToAddress(addressedPayload.SourceAddress), Payload: addressedPayload.Payload, }, Valid: true, @@ -128,7 +128,7 @@ func (blockHashHandler) handleMessage(warpMessage *warp.Message) ([]byte, error) return PackGetVerifiedWarpBlockHashOutput(GetVerifiedWarpBlockHashOutput{ WarpBlockHash: WarpBlockHash{ SourceChainID: common.Hash(warpMessage.SourceChainID), - BlockHash: blockHashPayload.BlockHash, + BlockHash: common.BytesToHash(blockHashPayload.BlockHash[:]), }, Valid: true, }) diff --git a/x/warp/predicate_test.go b/x/warp/predicate_test.go index b31a19d560..9c7cbbeb11 100644 --- a/x/warp/predicate_test.go +++ b/x/warp/predicate_test.go @@ -18,12 +18,12 @@ import ( "github.com/ava-labs/avalanchego/utils/crypto/bls" "github.com/ava-labs/avalanchego/utils/set" avalancheWarp "github.com/ava-labs/avalanchego/vms/platformvm/warp" + warpPayload "github.com/ava-labs/avalanchego/vms/platformvm/warp/payload" "github.com/ava-labs/subnet-evm/params" "github.com/ava-labs/subnet-evm/precompile/precompileconfig" "github.com/ava-labs/subnet-evm/precompile/testutils" subnetEVMUtils "github.com/ava-labs/subnet-evm/utils" predicateutils "github.com/ava-labs/subnet-evm/utils/predicate" - warpPayload "github.com/ava-labs/subnet-evm/warp/payload" "github.com/ethereum/go-ethereum/common" "github.com/stretchr/testify/require" "go.uber.org/mock/gomock" @@ -81,8 +81,9 @@ func init() { } var err error + addr := ids.GenerateTestShortID() addressedPayload, err = warpPayload.NewAddressedPayload( - common.Address(ids.GenerateTestShortID()), + addr[:], []byte{1, 2, 3}, ) if err != nil {