diff --git a/cmd/geth/config.go b/cmd/geth/config.go index 07712ecca..eab7f699c 100644 --- a/cmd/geth/config.go +++ b/cmd/geth/config.go @@ -24,6 +24,9 @@ import ( "reflect" "unicode" + "github.com/ethereum/go-ethereum/eth/downloader" + "github.com/ethereum/go-ethereum/statediff" + cli "gopkg.in/urfave/cli.v1" "github.com/ethereum/go-ethereum/cmd/utils" @@ -165,38 +168,58 @@ func checkWhisper(ctx *cli.Context) { func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) { stack, cfg := makeConfigNode(ctx) + if cfg.Eth.SyncMode == downloader.LightSync { + return makeLightNode(ctx, stack, cfg) + } + backend := utils.RegisterEthService(stack, &cfg.Eth) checkWhisper(ctx) if ctx.GlobalBool(utils.StateDiffFlag.Name) { - var dbParams *[3]string + var dbParams *statediff.DBParams if ctx.GlobalIsSet(utils.StateDiffDBFlag.Name) { - dbParams = new([3]string) - dbParams[0] = ctx.GlobalString(utils.StateDiffDBFlag.Name) + dbParams = new(statediff.DBParams) + dbParams.ConnectionURL = ctx.GlobalString(utils.StateDiffDBFlag.Name) if ctx.GlobalIsSet(utils.StateDiffDBNodeIDFlag.Name) { - dbParams[1] = ctx.GlobalString(utils.StateDiffDBNodeIDFlag.Name) + dbParams.ID = ctx.GlobalString(utils.StateDiffDBNodeIDFlag.Name) } else { utils.Fatalf("Must specify node ID for statediff DB output") } if ctx.GlobalIsSet(utils.StateDiffDBClientNameFlag.Name) { - dbParams[2] = ctx.GlobalString(utils.StateDiffDBClientNameFlag.Name) + dbParams.ClientName = ctx.GlobalString(utils.StateDiffDBClientNameFlag.Name) } else { utils.Fatalf("Must specify client name for statediff DB output") } } - utils.RegisterStateDiffService(stack, dbParams, ctx.GlobalBool(utils.StateDiffWritingFlag.Name)) + utils.RegisterStateDiffService(stack, backend, dbParams, ctx.GlobalBool(utils.StateDiffWritingFlag.Name)) } // Configure GraphQL if requested if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) { - utils.RegisterGraphQLService(stack, backend, cfg.Node) + utils.RegisterGraphQLService(stack, backend.APIBackend, cfg.Node) } // Add the Ethereum Stats daemon if requested. if cfg.Ethstats.URL != "" { - utils.RegisterEthStatsService(stack, backend, cfg.Ethstats.URL) + utils.RegisterEthStatsService(stack, backend.APIBackend, cfg.Ethstats.URL) } - return stack, backend + return stack, backend.APIBackend +} + +func makeLightNode(ctx *cli.Context, stack *node.Node, cfg gethConfig) (*node.Node, ethapi.Backend) { + backend := utils.RegisterLesEthService(stack, &cfg.Eth) + + checkWhisper(ctx) + + // Configure GraphQL if requested + if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) { + utils.RegisterGraphQLService(stack, backend.ApiBackend, cfg.Node) + } + // Add the Ethereum Stats daemon if requested. + if cfg.Ethstats.URL != "" { + utils.RegisterEthStatsService(stack, backend.ApiBackend, cfg.Ethstats.URL) + } + return stack, backend.ApiBackend } // dumpConfig is the dumpconfig command. diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index eba453f71..56c57b59f 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -1705,26 +1705,27 @@ func SetDNSDiscoveryDefaults(cfg *eth.Config, genesis common.Hash) { } // RegisterEthService adds an Ethereum client to the stack. -func RegisterEthService(stack *node.Node, cfg *eth.Config) ethapi.Backend { - if cfg.SyncMode == downloader.LightSync { - backend, err := les.New(stack, cfg) - if err != nil { - Fatalf("Failed to register the Ethereum service: %v", err) - } - return backend.ApiBackend - } else { - backend, err := eth.New(stack, cfg) - if err != nil { - Fatalf("Failed to register the Ethereum service: %v", err) - } - if cfg.LightServ > 0 { - _, err := les.NewLesServer(stack, backend, cfg) - if err != nil { - Fatalf("Failed to create the LES server: %v", err) - } - } - return backend.APIBackend +func RegisterEthService(stack *node.Node, cfg *eth.Config) *eth.Ethereum { + backend, err := eth.New(stack, cfg) + if err != nil { + Fatalf("Failed to register the Ethereum service: %v", err) } + if cfg.LightServ > 0 { + _, err := les.NewLesServer(stack, backend, cfg) + if err != nil { + Fatalf("Failed to create the LES server: %v", err) + } + } + return backend +} + +// RegisterLesEthService adds an Ethereum les client to the stack. +func RegisterLesEthService(stack *node.Node, cfg *eth.Config) *les.LightEthereum { + backend, err := les.New(stack, cfg) + if err != nil { + Fatalf("Failed to register the Ethereum service: %v", err) + } + return backend } // RegisterEthStatsService configures the Ethereum Stats daemon and adds it to @@ -1744,16 +1745,9 @@ func RegisterGraphQLService(stack *node.Node, backend ethapi.Backend, cfg node.C // RegisterStateDiffService configures and registers a service to stream state diff data over RPC // dbParams are: Postgres connection URI, Node ID, client name -func RegisterStateDiffService(stack *node.Node, dbParams *[3]string, startWriteLoop bool) { - if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { - var ethServ *eth.Ethereum - err := ctx.Service(ðServ) - if err != nil { - return nil, err - } - return statediff.NewStateDiffService(ethServ, dbParams, startWriteLoop) - }); err != nil { - Fatalf("Failed to register State Diff Service", err) +func RegisterStateDiffService(stack *node.Node, ethServ *eth.Ethereum, dbParams *statediff.DBParams, startWriteLoop bool) { + if err := statediff.New(stack, ethServ, dbParams, startWriteLoop); err != nil { + Fatalf("Failed to register the Statediff service: %v", err) } } diff --git a/go.mod b/go.mod old mode 100755 new mode 100644 index dcc3ffb07..1516ce996 --- a/go.mod +++ b/go.mod @@ -52,8 +52,8 @@ require ( github.com/naoina/go-stringutil v0.1.0 // indirect github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416 github.com/olekukonko/tablewriter v0.0.2-0.20190409134802-7e037d187b0c - github.com/onsi/ginkgo v1.7.0 - github.com/onsi/gomega v1.4.3 + github.com/onsi/ginkgo v1.14.0 + github.com/onsi/gomega v1.10.1 github.com/pborman/uuid v0.0.0-20170112150404-1b00554d8222 github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7 github.com/prometheus/client_golang v0.9.3 diff --git a/go.sum b/go.sum old mode 100755 new mode 100644 index 473e2b4c7..fbfd503f2 --- a/go.sum +++ b/go.sum @@ -125,6 +125,7 @@ github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= @@ -145,12 +146,16 @@ github.com/google/gofuzz v1.1.0 h1:Hsa8mG0dQ46ij8Sl2AYJDUv1oA9/d6Vk+3LG99Oe02g= github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.1.1-0.20200604201612-c04b05f3adfa h1:Q75Upo5UN4JbPFURXZ8nLKYUvF85dyFRop/vQ0Rv+64= github.com/google/gofuzz v1.1.1-0.20200604201612-c04b05f3adfa/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/websocket v1.4.1-0.20190629185528-ae1634f6a989 h1:giknQ4mEuDFmmHSrGcbargOuLHQGtywqo4mheITex54= github.com/gorilla/websocket v1.4.1-0.20190629185528-ae1634f6a989/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/graph-gophers/graphql-go v0.0.0-20191115155744-f33e81362277 h1:E0whKxgp2ojts0FDgUA8dl62bmH0LxKanMoBr6MDTDM= github.com/graph-gophers/graphql-go v0.0.0-20191115155744-f33e81362277/go.mod h1:9CQHMSxwO4MprSdzoIEobiHpoLtHm77vfxsvsIN5Vuc= +github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfmKFJ6tItnhQ67kU= +github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48= github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/holiman/uint256 v1.1.1 h1:4JywC80b+/hSfljFlEBLHrrh+CIONLDz9NuFl0af4Mw= @@ -201,6 +206,7 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1 github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.1.1-0.20170430222011-975b5c4c7c21 h1:F/iKcka0K2LgnKy/fgSBf235AETtm1n1TvBzqu40LE0= github.com/julienschmidt/httprouter v1.1.1-0.20170430222011-975b5c4c7c21/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/julienschmidt/httprouter v1.2.0 h1:TDTW5Yz1mjftljbcKqRcrYhd4XeOoI98t+9HbQbYf7g= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/karalabe/usb v0.0.0-20190919080040-51dc0efba356 h1:I/yrLt2WilKxlQKCM52clh5rGzTKpVctGT1lH4Dc8Jw= github.com/karalabe/usb v0.0.0-20190919080040-51dc0efba356/go.mod h1:Od972xHfMJowv7NGVDiWVxk2zxnWgjLlJzE+F4F7AGU= @@ -285,12 +291,15 @@ github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXW github.com/olekukonko/tablewriter v0.0.2-0.20190409134802-7e037d187b0c h1:1RHs3tNxjXGHeul8z2t6H2N2TlAqpKe5yryJztRx4Jk= github.com/olekukonko/tablewriter v0.0.2-0.20190409134802-7e037d187b0c/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= github.com/onsi/ginkgo v1.14.0 h1:2mOpI4JVVPBN+WQRa0WKH2eXR+Ey+uK4n7Zj0aYpIQA= github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY= +github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= +github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= @@ -341,6 +350,8 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1 github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= +github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4 h1:Gb2Tyox57NRNuZ2d3rmvB3pcmbu7O1RS3m8WRx7ilrg= github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4/go.mod h1:RZLeN1LMWmRsyYjvAu+I6Dm9QmlDaIIt+Y+4Kd7Tp+Q= github.com/steakknife/bloomfilter v0.0.0-20180922174646-6819c0d2a570 h1:gIlAHnH1vJb5vwEjIp5kBj/eu99p/bl0Ay2goiPe5xE= @@ -362,14 +373,20 @@ github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc h1:9lDbC6 github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc/go.mod h1:bopw91TMyo8J3tvftk8xmU2kPmlrt4nScJQZU2hE5EM= github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208 h1:1cngl9mPEoITZG8s8cVcUy5CeIBYhEESkOB7m6Gmkrk= github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208/go.mod h1:IotVbo4F+mw0EzQ08zFqg7pK3FebNXpaMsRy2RT+Ees= +go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk= +go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= golang.org/x/mobile v0.0.0-20200801112145-973feb4309de h1:OVJ6QQUBAesB8CZijKDSsXX7xYVtUhrkY0gwMfbi4p4= golang.org/x/mobile v0.0.0-20200801112145-973feb4309de/go.mod h1:skQtrUTUwhdJvXM/2KKJzY8pDgNr9I/FOMqDVRPBUS4= @@ -379,6 +396,8 @@ golang.org/x/mod v0.1.1-0.20191209134235-331c550502dd h1:ePuNC7PZ6O5BzgPn9bZayER golang.org/x/mod v0.1.1-0.20191209134235-331c550502dd/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181011144130-49bb7cea24b1/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190227160552-c95aed5357e7/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -389,12 +408,16 @@ golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81R golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f h1:Bl/8QSvNqXvPGPGXa2z5xUTmV7VDcZyvRZ+QQXkXTZQ= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190219092855-153ac476189d/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -415,7 +438,9 @@ golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 h1:SvFZT6jyqRaOeXpc5h/JSfZe golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200117012304-6edc0a871e69 h1:yBHHx+XZqXJBm6Exke3N7V9gnlsyXxoCPEb1yVenjfk= golang.org/x/tools v0.0.0-20200117012304-6edc0a871e69/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= @@ -450,6 +475,7 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkep gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/urfave/cli.v1 v1.20.0 h1:NdAVW6RYxDif9DhDHaAortIu956m2c0v+09AZBPTbE0= gopkg.in/urfave/cli.v1 v1.20.0/go.mod h1:vuBzUtMdQeixQj8LVd+/98pzhxNGQoyuPBlsXHOQNO0= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/statediff/indexer/helpers.go b/statediff/indexer/helpers.go index 4865cafa1..bb62fd079 100644 --- a/statediff/indexer/helpers.go +++ b/statediff/indexer/helpers.go @@ -44,7 +44,7 @@ func ChainConfig(chainID uint64) (*params.ChainConfig, error) { case 1: return params.MainnetChainConfig, nil case 3: - return params.TestnetChainConfig, nil // Ropsten + return params.RopstenChainConfig, nil case 4: return params.RinkebyChainConfig, nil case 5: diff --git a/statediff/indexer/mocks/test_data.go b/statediff/indexer/mocks/test_data.go index 6dcb14b5a..e2a2af54b 100644 --- a/statediff/indexer/mocks/test_data.go +++ b/statediff/indexer/mocks/test_data.go @@ -22,6 +22,8 @@ import ( "crypto/rand" "math/big" + "github.com/ethereum/go-ethereum/trie" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" @@ -29,12 +31,9 @@ import ( "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" - "github.com/ipfs/go-block-format" "github.com/multiformats/go-multihash" "github.com/ethereum/go-ethereum/statediff/indexer/ipfs/ipld" - "github.com/ethereum/go-ethereum/statediff/indexer/models" - "github.com/ethereum/go-ethereum/statediff/indexer/shared" "github.com/ethereum/go-ethereum/statediff/testhelpers" sdtypes "github.com/ethereum/go-ethereum/statediff/types" ) @@ -54,8 +53,7 @@ var ( } MockTransactions, MockReceipts, SenderAddr = createTransactionsAndReceipts() ReceiptsRlp, _ = rlp.EncodeToBytes(MockReceipts) - MockBlock = types.NewBlock(&MockHeader, MockTransactions, nil, MockReceipts) - MockBlockRlp, _ = rlp.EncodeToBytes(MockBlock) + MockBlock = types.NewBlock(&MockHeader, MockTransactions, nil, MockReceipts, new(trie.Trie)) MockHeaderRlp, _ = rlp.EncodeToBytes(MockBlock.Header()) Address = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476592") AnotherAddress = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476593") @@ -77,167 +75,15 @@ var ( Data: []byte{}, } HeaderCID, _ = ipld.RawdataToCid(ipld.MEthHeader, MockHeaderRlp, multihash.KECCAK_256) - HeaderMhKey = shared.MultihashKeyFromCID(HeaderCID) Trx1CID, _ = ipld.RawdataToCid(ipld.MEthTx, MockTransactions.GetRlp(0), multihash.KECCAK_256) - Trx1MhKey = shared.MultihashKeyFromCID(Trx1CID) Trx2CID, _ = ipld.RawdataToCid(ipld.MEthTx, MockTransactions.GetRlp(1), multihash.KECCAK_256) - Trx2MhKey = shared.MultihashKeyFromCID(Trx2CID) Trx3CID, _ = ipld.RawdataToCid(ipld.MEthTx, MockTransactions.GetRlp(2), multihash.KECCAK_256) - Trx3MhKey = shared.MultihashKeyFromCID(Trx3CID) Rct1CID, _ = ipld.RawdataToCid(ipld.MEthTxReceipt, MockReceipts.GetRlp(0), multihash.KECCAK_256) - Rct1MhKey = shared.MultihashKeyFromCID(Rct1CID) Rct2CID, _ = ipld.RawdataToCid(ipld.MEthTxReceipt, MockReceipts.GetRlp(1), multihash.KECCAK_256) - Rct2MhKey = shared.MultihashKeyFromCID(Rct2CID) Rct3CID, _ = ipld.RawdataToCid(ipld.MEthTxReceipt, MockReceipts.GetRlp(2), multihash.KECCAK_256) - Rct3MhKey = shared.MultihashKeyFromCID(Rct3CID) State1CID, _ = ipld.RawdataToCid(ipld.MEthStateTrie, ContractLeafNode, multihash.KECCAK_256) - State1MhKey = shared.MultihashKeyFromCID(State1CID) State2CID, _ = ipld.RawdataToCid(ipld.MEthStateTrie, AccountLeafNode, multihash.KECCAK_256) - State2MhKey = shared.MultihashKeyFromCID(State2CID) StorageCID, _ = ipld.RawdataToCid(ipld.MEthStorageTrie, StorageLeafNode, multihash.KECCAK_256) - StorageMhKey = shared.MultihashKeyFromCID(StorageCID) - MockTrxMeta = []models.TxModel{ - { - CID: "", // This is empty until we go to publish to ipfs - MhKey: "", - Src: SenderAddr.Hex(), - Dst: Address.String(), - Index: 0, - TxHash: MockTransactions[0].Hash().String(), - Data: []byte{}, - Deployment: false, - }, - { - CID: "", - MhKey: "", - Src: SenderAddr.Hex(), - Dst: AnotherAddress.String(), - Index: 1, - TxHash: MockTransactions[1].Hash().String(), - Data: []byte{}, - Deployment: false, - }, - { - CID: "", - MhKey: "", - Src: SenderAddr.Hex(), - Dst: "", - Index: 2, - TxHash: MockTransactions[2].Hash().String(), - Data: MockContractByteCode, - Deployment: true, - }, - } - MockTrxMetaPostPublsh = []models.TxModel{ - { - CID: Trx1CID.String(), // This is empty until we go to publish to ipfs - MhKey: Trx1MhKey, - Src: SenderAddr.Hex(), - Dst: Address.String(), - Index: 0, - TxHash: MockTransactions[0].Hash().String(), - Data: []byte{}, - Deployment: false, - }, - { - CID: Trx2CID.String(), - MhKey: Trx2MhKey, - Src: SenderAddr.Hex(), - Dst: AnotherAddress.String(), - Index: 1, - TxHash: MockTransactions[1].Hash().String(), - Data: []byte{}, - Deployment: false, - }, - { - CID: Trx3CID.String(), - MhKey: Trx3MhKey, - Src: SenderAddr.Hex(), - Dst: "", - Index: 2, - TxHash: MockTransactions[2].Hash().String(), - Data: MockContractByteCode, - Deployment: true, - }, - } - MockRctMeta = []models.ReceiptModel{ - { - CID: "", - MhKey: "", - Topic0s: []string{ - mockTopic11.String(), - }, - Topic1s: []string{ - mockTopic12.String(), - }, - Contract: "", - ContractHash: "", - LogContracts: []string{ - Address.String(), - }, - }, - { - CID: "", - MhKey: "", - Topic0s: []string{ - mockTopic21.String(), - }, - Topic1s: []string{ - mockTopic22.String(), - }, - Contract: "", - ContractHash: "", - LogContracts: []string{ - AnotherAddress.String(), - }, - }, - { - CID: "", - MhKey: "", - Contract: ContractAddress.String(), - ContractHash: ContractHash, - LogContracts: []string{}, - }, - } - MockRctMetaPostPublish = []models.ReceiptModel{ - { - CID: Rct1CID.String(), - MhKey: Rct1MhKey, - Topic0s: []string{ - mockTopic11.String(), - }, - Topic1s: []string{ - mockTopic12.String(), - }, - Contract: "", - ContractHash: "", - LogContracts: []string{ - Address.String(), - }, - }, - { - CID: Rct2CID.String(), - MhKey: Rct2MhKey, - Topic0s: []string{ - mockTopic21.String(), - }, - Topic1s: []string{ - mockTopic22.String(), - }, - Contract: "", - ContractHash: "", - LogContracts: []string{ - AnotherAddress.String(), - }, - }, - { - CID: Rct3CID.String(), - MhKey: Rct3MhKey, - Contract: ContractAddress.String(), - ContractHash: ContractHash, - LogContracts: []string{}, - }, - } // statediff data storageLocation = common.HexToHash("0") @@ -252,7 +98,6 @@ var ( nonce1 = uint64(1) ContractRoot = "0x821e2556a290c86405f8160a2d662042a431ba456b9db265c79bb837c04be5f0" ContractCodeHash = common.HexToHash("0x753f98a8d4328b15636e46f66f2cb4bc860100aa17967cc145fcd17d1d4710ea") - contractPath = common.Bytes2Hex([]byte{'\x06'}) ContractLeafKey = testhelpers.AddressToLeafKey(ContractAddress) ContractAccount, _ = rlp.EncodeToBytes(state.Account{ Nonce: nonce1, @@ -269,8 +114,6 @@ var ( nonce0 = uint64(0) AccountRoot = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" AccountCodeHash = common.HexToHash("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470") - accountPath = common.Bytes2Hex([]byte{'\x0c'}) - AccountAddresss = common.HexToAddress("0x0D3ab14BBaD3D99F4203bd7a11aCB94882050E7e") AccountLeafKey = testhelpers.Account2LeafKey Account, _ = rlp.EncodeToBytes(state.Account{ Nonce: nonce0, @@ -307,110 +150,6 @@ var ( StorageNodes: []sdtypes.StorageNode{}, }, } - - MockStateNodes = []shared.TrieNode{ - { - LeafKey: common.BytesToHash(ContractLeafKey), - Path: []byte{'\x06'}, - Value: ContractLeafNode, - Type: sdtypes.Leaf, - }, - { - LeafKey: common.BytesToHash(AccountLeafKey), - Path: []byte{'\x0c'}, - Value: AccountLeafNode, - Type: sdtypes.Leaf, - }, - } - MockStateMetaPostPublish = []models.StateNodeModel{ - { - CID: State1CID.String(), - MhKey: State1MhKey, - Path: []byte{'\x06'}, - NodeType: 2, - StateKey: common.BytesToHash(ContractLeafKey).Hex(), - }, - { - CID: State2CID.String(), - MhKey: State2MhKey, - Path: []byte{'\x0c'}, - NodeType: 2, - StateKey: common.BytesToHash(AccountLeafKey).Hex(), - }, - } - MockStorageNodes = map[string][]shared.TrieNode{ - contractPath: { - { - LeafKey: common.BytesToHash(StorageLeafKey), - Value: StorageLeafNode, - Type: sdtypes.Leaf, - Path: []byte{}, - }, - }, - } - - // aggregate payloads - MockCIDPayload = shared.CIDPayload{ - HeaderCID: models.HeaderModel{ - BlockHash: MockBlock.Hash().String(), - BlockNumber: MockBlock.Number().String(), - CID: HeaderCID.String(), - MhKey: HeaderMhKey, - ParentHash: MockBlock.ParentHash().String(), - TotalDifficulty: MockBlock.Difficulty().String(), - Reward: "5000000000000000000", - StateRoot: MockBlock.Root().String(), - RctRoot: MockBlock.ReceiptHash().String(), - TxRoot: MockBlock.TxHash().String(), - UncleRoot: MockBlock.UncleHash().String(), - Bloom: MockBlock.Bloom().Bytes(), - Timestamp: MockBlock.Time(), - }, - UncleCIDs: []models.UncleModel{}, - TransactionCIDs: MockTrxMetaPostPublsh, - ReceiptCIDs: map[common.Hash]models.ReceiptModel{ - MockTransactions[0].Hash(): MockRctMetaPostPublish[0], - MockTransactions[1].Hash(): MockRctMetaPostPublish[1], - MockTransactions[2].Hash(): MockRctMetaPostPublish[2], - }, - StateNodeCIDs: MockStateMetaPostPublish, - StorageNodeCIDs: map[string][]models.StorageNodeModel{ - contractPath: { - { - CID: StorageCID.String(), - MhKey: StorageMhKey, - Path: []byte{}, - StorageKey: common.BytesToHash(StorageLeafKey).Hex(), - NodeType: 2, - }, - }, - }, - StateAccounts: map[string]models.StateAccountModel{ - contractPath: { - Balance: big.NewInt(0).String(), - Nonce: nonce1, - CodeHash: ContractCodeHash.Bytes(), - StorageRoot: common.HexToHash(ContractRoot).String(), - }, - accountPath: { - Balance: big.NewInt(1000).String(), - Nonce: nonce0, - CodeHash: AccountCodeHash.Bytes(), - StorageRoot: common.HexToHash(AccountRoot).String(), - }, - }, - } - - HeaderIPLD, _ = blocks.NewBlockWithCid(MockHeaderRlp, HeaderCID) - Trx1IPLD, _ = blocks.NewBlockWithCid(MockTransactions.GetRlp(0), Trx1CID) - Trx2IPLD, _ = blocks.NewBlockWithCid(MockTransactions.GetRlp(1), Trx2CID) - Trx3IPLD, _ = blocks.NewBlockWithCid(MockTransactions.GetRlp(2), Trx3CID) - Rct1IPLD, _ = blocks.NewBlockWithCid(MockReceipts.GetRlp(0), Rct1CID) - Rct2IPLD, _ = blocks.NewBlockWithCid(MockReceipts.GetRlp(1), Rct2CID) - Rct3IPLD, _ = blocks.NewBlockWithCid(MockReceipts.GetRlp(2), Rct3CID) - State1IPLD, _ = blocks.NewBlockWithCid(ContractLeafNode, State1CID) - State2IPLD, _ = blocks.NewBlockWithCid(AccountLeafNode, State2CID) - StorageIPLD, _ = blocks.NewBlockWithCid(StorageLeafNode, StorageCID) ) // createTransactionsAndReceipts is a helper function to generate signed mock transactions and mock receipts with mock logs diff --git a/statediff/indexer/shared/test_helpers.go b/statediff/indexer/shared/test_helpers.go index 17ccda02a..b2e13f832 100644 --- a/statediff/indexer/shared/test_helpers.go +++ b/statediff/indexer/shared/test_helpers.go @@ -36,8 +36,8 @@ func ExpectEqual(t *testing.T, got interface{}, want interface{}) { // SetupDB is use to setup a db for watcher tests func SetupDB() (*postgres.DB, error) { uri := postgres.DbConnectionString(postgres.ConnectionParams{ - User: "vulcanize", - Password: "libertad", + User: "postgres", + Password: "", Hostname: "localhost", Name: "vulcanize_testing", Port: 5432, @@ -66,9 +66,3 @@ func TestCID(b []byte) cid.Cid { c, _ := pref.Sum(b) return c } - -// PublishMockIPLD writes a mhkey-data pair to the public.blocks table so that test data can FK reference the mhkey -func PublishMockIPLD(db *postgres.DB, mhKey string, mockData []byte) error { - _, err := db.Exec(`INSERT INTO public.blocks (key, data) VALUES ($1, $2) ON CONFLICT (key) DO NOTHING`, mhKey, mockData) - return err -} diff --git a/statediff/indexer/test_helpers.go b/statediff/indexer/test_helpers.go index b1ee173db..024bb58f0 100644 --- a/statediff/indexer/test_helpers.go +++ b/statediff/indexer/test_helpers.go @@ -19,7 +19,6 @@ package indexer import ( "testing" - "github.com/ethereum/go-ethereum/statediff/indexer/models" "github.com/ethereum/go-ethereum/statediff/indexer/postgres" ) @@ -59,23 +58,3 @@ func TearDownDB(t *testing.T, db *postgres.DB) { t.Fatal(err) } } - -// TxModelsContainsCID used to check if a list of TxModels contains a specific cid string -func TxModelsContainsCID(txs []models.TxModel, cid string) bool { - for _, tx := range txs { - if tx.CID == cid { - return true - } - } - return false -} - -// ListContainsBytes used to check if a list of byte arrays contains a particular byte array -func ReceiptModelsContainsCID(rcts []models.ReceiptModel, cid string) bool { - for _, rct := range rcts { - if rct.CID == cid { - return true - } - } - return false -} diff --git a/statediff/service.go b/statediff/service.go index 6a6d92fb4..718d9c797 100644 --- a/statediff/service.go +++ b/statediff/service.go @@ -18,7 +18,6 @@ package statediff import ( "bytes" - "fmt" "math/big" "strconv" "sync" @@ -69,7 +68,7 @@ type blockChain interface { // IService is the state-diffing service interface type IService interface { // APIs(), Protocols(), Start() and Stop() - node.Service + node.Lifecycle // Main event loop for processing state diffs Loop(chainEventCh chan core.ChainEvent) // Method to subscribe to receive state diff processing output @@ -118,30 +117,28 @@ type lastBlockCache struct { block *types.Block } -// NewStateDiffService creates a new statediff.Service -func NewStateDiffService(ethServ *eth.Ethereum, dbParams *[3]string, enableWriteLoop bool) (*Service, error) { +// New creates a new statediff.Service +func New(stack *node.Node, ethServ *eth.Ethereum, dbParams *DBParams, enableWriteLoop bool) error { blockChain := ethServ.BlockChain() var indexer ind.Indexer if dbParams != nil { info := nodeinfo.Info{ GenesisBlock: blockChain.Genesis().Hash().Hex(), NetworkID: strconv.FormatUint(ethServ.NetVersion(), 10), - // ChainID: blockChain.Config().ChainID.String(), - ChainID: blockChain.Config().ChainID.Uint64(), - ID: dbParams[1], - ClientName: dbParams[2], + ChainID: blockChain.Config().ChainID.Uint64(), + ID: dbParams.ID, + ClientName: dbParams.ClientName, } // TODO: pass max idle, open, lifetime? - db, err := postgres.NewDB(dbParams[0], postgres.ConnectionConfig{}, info) + db, err := postgres.NewDB(dbParams.ConnectionURL, postgres.ConnectionConfig{}, info) if err != nil { - return nil, err + return err } indexer = ind.NewStateDiffIndexer(blockChain.Config(), db) } prom.Init() - - return &Service{ + serv := &Service{ Mutex: sync.Mutex{}, BlockChain: blockChain, Builder: NewBuilder(blockChain.StateCache()), @@ -150,7 +147,9 @@ func NewStateDiffService(ethServ *eth.Ethereum, dbParams *[3]string, enableWrite SubscriptionTypes: make(map[common.Hash]Params), indexer: indexer, enableWriteLoop: enableWriteLoop, - }, nil + } + stack.RegisterLifecycle(serv) + return nil } // Protocols exports the services p2p protocols, this service has none @@ -196,12 +195,12 @@ func (sds *Service) WriteLoop(chainEventCh chan core.ChainEvent) { currentBlock := chainEvent.Block parentBlock := sds.lastBlock.replace(currentBlock, sds.BlockChain) if parentBlock == nil { - log.Error(fmt.Sprintf("Parent block is nil, skipping this block (%d)", currentBlock.Number())) + log.Error("Parent block is nil, skipping this block", "block height", currentBlock.Number()) continue } err := sds.writeStateDiff(currentBlock, parentBlock.Root(), writeLoopParams) if err != nil { - log.Error(fmt.Sprintf("statediff (DB write) processing error at blockheight %d: err: %s", currentBlock.Number().Uint64(), err.Error())) + log.Error("statediff (DB write) processing error", "block height", currentBlock.Number().Uint64(), "error", err.Error()) continue } case err := <-errCh: @@ -234,7 +233,7 @@ func (sds *Service) Loop(chainEventCh chan core.ChainEvent) { currentBlock := chainEvent.Block parentBlock := sds.lastBlock.replace(currentBlock, sds.BlockChain) if parentBlock == nil { - log.Error(fmt.Sprintf("Parent block is nil, skipping this block (%d)", currentBlock.Number())) + log.Error("Parent block is nil, skipping this block", "number", currentBlock.Number()) continue } sds.streamStateDiff(currentBlock, parentBlock.Root()) @@ -256,22 +255,22 @@ func (sds *Service) streamStateDiff(currentBlock *types.Block, parentRoot common for ty, subs := range sds.Subscriptions { params, ok := sds.SubscriptionTypes[ty] if !ok { - log.Error(fmt.Sprintf("subscriptions type %s do not have a parameter set associated with them", ty.Hex())) + log.Error("no parameter set associated with this subscription", "subscription type", ty.Hex()) sds.closeType(ty) continue } // create payload for this subscription type payload, err := sds.processStateDiff(currentBlock, parentRoot, params) if err != nil { - log.Error(fmt.Sprintf("statediff processing error a blockheight %d for subscriptions with parameters: %+v err: %s", currentBlock.Number().Uint64(), params, err.Error())) + log.Error("statediff processing error", "block height", currentBlock.Number().Uint64(), "parameters", params, "error", err.Error()) continue } for id, sub := range subs { select { case sub.PayloadChan <- *payload: - log.Debug(fmt.Sprintf("sending statediff payload at head height %d to subscription %s", currentBlock.Number(), id)) + log.Debug("sending statediff payload at head", "height", currentBlock.Number(), "subscription id", id) default: - log.Info(fmt.Sprintf("unable to send statediff payload to subscription %s; channel has no receiver", id)) + log.Info("unable to send statediff payload; channel has no receiver", "subscription id", id) } } } @@ -282,7 +281,7 @@ func (sds *Service) streamStateDiff(currentBlock *types.Block, parentRoot common // This operation cannot be performed back past the point of db pruning; it requires an archival node for historical data func (sds *Service) StateDiffAt(blockNumber uint64, params Params) (*Payload, error) { currentBlock := sds.BlockChain.GetBlockByNumber(blockNumber) - log.Info(fmt.Sprintf("sending state diff at block %d", blockNumber)) + log.Info("sending state diff", "block height", blockNumber) if blockNumber == 0 { return sds.processStateDiff(currentBlock, common.Hash{}, params) } @@ -307,7 +306,7 @@ func (sds *Service) processStateDiff(currentBlock *types.Block, parentRoot commo if err != nil { return nil, err } - log.Info(fmt.Sprintf("state diff object at block %d is %d bytes in length", currentBlock.Number().Uint64(), len(stateDiffRlp))) + log.Info("state diff size", "at block height", currentBlock.Number().Uint64(), "rlp byte size", len(stateDiffRlp)) return sds.newPayload(stateDiffRlp, currentBlock, params) } @@ -340,7 +339,7 @@ func (sds *Service) newPayload(stateObject []byte, block *types.Block, params Pa // This operation cannot be performed back past the point of db pruning; it requires an archival node for historical data func (sds *Service) StateTrieAt(blockNumber uint64, params Params) (*Payload, error) { currentBlock := sds.BlockChain.GetBlockByNumber(blockNumber) - log.Info(fmt.Sprintf("sending state trie at block %d", blockNumber)) + log.Info("sending state trie", "block height", blockNumber) return sds.processStateTrie(currentBlock, params) } @@ -353,7 +352,7 @@ func (sds *Service) processStateTrie(block *types.Block, params Params) (*Payloa if err != nil { return nil, err } - log.Info(fmt.Sprintf("state trie object at block %d is %d bytes in length", block.Number().Uint64(), len(stateTrieRlp))) + log.Info("state trie size", "at block height", block.Number().Uint64(), "rlp byte size", len(stateTrieRlp)) return sds.newPayload(stateTrieRlp, block, params) } @@ -385,7 +384,7 @@ func (sds *Service) Subscribe(id rpc.ID, sub chan<- Payload, quitChan chan<- boo // Unsubscribe is used to unsubscribe from the service loop func (sds *Service) Unsubscribe(id rpc.ID) error { - log.Info(fmt.Sprintf("Unsubscribing subscription %s from the statediff service", id)) + log.Info("Unsubscribing from the statediff service", "subscription id", id) sds.Lock() for ty := range sds.Subscriptions { delete(sds.Subscriptions[ty], id) @@ -405,14 +404,14 @@ func (sds *Service) Unsubscribe(id rpc.ID) error { } // Start is used to begin the service -func (sds *Service) Start(*p2p.Server) error { +func (sds *Service) Start() error { log.Info("Starting statediff service") chainEventCh := make(chan core.ChainEvent, chainEventChanSize) go sds.Loop(chainEventCh) if sds.enableWriteLoop { - log.Info("Starting statediff DB write loop", writeLoopParams) + log.Info("Starting statediff DB write loop", "params", writeLoopParams) go sds.WriteLoop(make(chan core.ChainEvent, chainEventChanSize)) } @@ -433,9 +432,9 @@ func (sds *Service) close() { for id, sub := range subs { select { case sub.QuitChan <- true: - log.Info(fmt.Sprintf("closing subscription %s", id)) + log.Info("closing subscription", "id", id) default: - log.Info(fmt.Sprintf("unable to close subscription %s; channel has no receiver", id)) + log.Info("unable to close subscription; channel has no receiver", "subscription id", id) } delete(sds.Subscriptions[ty], id) } @@ -459,16 +458,16 @@ func (sds *Service) closeType(subType common.Hash) { func sendNonBlockingQuit(id rpc.ID, sub Subscription) { select { case sub.QuitChan <- true: - log.Info(fmt.Sprintf("closing subscription %s", id)) + log.Info("closing subscription", "id", id) default: - log.Info("unable to close subscription %s; channel has no receiver", id) + log.Info("unable to close subscription; channel has no receiver", "subscription id", id) } } // StreamCodeAndCodeHash subscription method for extracting all the codehash=>code mappings that exist in the trie at the provided height func (sds *Service) StreamCodeAndCodeHash(blockNumber uint64, outChan chan<- CodeAndCodeHash, quitChan chan<- bool) { current := sds.BlockChain.GetBlockByNumber(blockNumber) - log.Info(fmt.Sprintf("sending code and codehash at block %d", blockNumber)) + log.Info("sending code and codehash", "block height", blockNumber) currentTrie, err := sds.BlockChain.StateCache().OpenTrie(current.Root()) if err != nil { log.Error("error creating trie for block", "number", current.Number(), "err", err) @@ -508,7 +507,7 @@ func (sds *Service) StreamCodeAndCodeHash(blockNumber uint64, outChan chan<- Cod // This operation cannot be performed back past the point of db pruning; it requires an archival node // for historical data func (sds *Service) WriteStateDiffAt(blockNumber uint64, params Params) error { - log.Info(fmt.Sprintf("writing state diff at block %d", blockNumber)) + log.Info("writing state diff", "block height", blockNumber) currentBlock := sds.BlockChain.GetBlockByNumber(blockNumber) parentRoot := common.Hash{} if blockNumber != 0 { diff --git a/statediff/service_test.go b/statediff/service_test.go index 0f161757a..ef3c1bb2c 100644 --- a/statediff/service_test.go +++ b/statediff/service_test.go @@ -24,6 +24,8 @@ import ( "sync" "testing" + "github.com/ethereum/go-ethereum/trie" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" @@ -46,8 +48,8 @@ var ( parentHeader1 = types.Header{Number: big.NewInt(rand.Int63()), Root: parentRoot1} parentHeader2 = types.Header{Number: big.NewInt(rand.Int63()), Root: parentRoot2} - parentBlock1 = types.NewBlock(&parentHeader1, nil, nil, nil) - parentBlock2 = types.NewBlock(&parentHeader2, nil, nil, nil) + parentBlock1 = types.NewBlock(&parentHeader1, nil, nil, nil, new(trie.Trie)) + parentBlock2 = types.NewBlock(&parentHeader2, nil, nil, nil, new(trie.Trie)) parentHash1 = parentBlock1.Hash() parentHash2 = parentBlock2.Hash() @@ -59,9 +61,9 @@ var ( header2 = types.Header{ParentHash: parentHash2, Root: testRoot2, Number: big.NewInt(2)} header3 = types.Header{ParentHash: common.HexToHash("parent hash"), Root: testRoot3, Number: big.NewInt(3)} - testBlock1 = types.NewBlock(&header1, nil, nil, nil) - testBlock2 = types.NewBlock(&header2, nil, nil, nil) - testBlock3 = types.NewBlock(&header3, nil, nil, nil) + testBlock1 = types.NewBlock(&header1, nil, nil, nil, new(trie.Trie)) + testBlock2 = types.NewBlock(&header2, nil, nil, nil, new(trie.Trie)) + testBlock3 = types.NewBlock(&header3, nil, nil, nil, new(trie.Trie)) receiptRoot1 = common.HexToHash("0x05") receiptRoot2 = common.HexToHash("0x06") diff --git a/statediff/testhelpers/helpers.go b/statediff/testhelpers/helpers.go index ab141c900..7fd320b25 100644 --- a/statediff/testhelpers/helpers.go +++ b/statediff/testhelpers/helpers.go @@ -33,7 +33,7 @@ import ( func MakeChain(n int, parent *types.Block, chainGen func(int, *core.BlockGen)) ([]*types.Block, *core.BlockChain) { config := params.TestChainConfig blocks, _ := core.GenerateChain(config, parent, ethash.NewFaker(), Testdb, n, chainGen) - chain, _ := core.NewBlockChain(Testdb, nil, params.TestChainConfig, ethash.NewFaker(), vm.Config{}, nil) + chain, _ := core.NewBlockChain(Testdb, nil, params.TestChainConfig, ethash.NewFaker(), vm.Config{}, nil, nil) return blocks, chain } diff --git a/statediff/testhelpers/mocks/service.go b/statediff/testhelpers/mocks/service.go index 27c36f2f4..64e48a325 100644 --- a/statediff/testhelpers/mocks/service.go +++ b/statediff/testhelpers/mocks/service.go @@ -279,7 +279,7 @@ func (sds *MockStateDiffService) close() { } // Start mock method -func (sds *MockStateDiffService) Start(server *p2p.Server) error { +func (sds *MockStateDiffService) Start() error { log.Info("Starting mock statediff service") if sds.ParentBlockChan == nil || sds.BlockChan == nil { return errors.New("MockStateDiffingService needs to be configured with a MockParentBlockChan and MockBlockChan") diff --git a/statediff/testhelpers/mocks/service_test.go b/statediff/testhelpers/mocks/service_test.go index dd14a2356..4b8d89e41 100644 --- a/statediff/testhelpers/mocks/service_test.go +++ b/statediff/testhelpers/mocks/service_test.go @@ -136,7 +136,7 @@ func testSubscriptionAPI(t *testing.T) { Subscriptions: make(map[common.Hash]map[rpc.ID]statediff.Subscription), SubscriptionTypes: make(map[common.Hash]statediff.Params), } - mockService.Start(nil) + mockService.Start() id := rpc.NewID() payloadChan := make(chan statediff.Payload) quitChan := make(chan bool) diff --git a/statediff/types.go b/statediff/types.go index 5c798a6f9..148567dd7 100644 --- a/statediff/types.go +++ b/statediff/types.go @@ -34,6 +34,13 @@ type Subscription struct { QuitChan chan<- bool } +// DBParams holds params for Postgres db connection +type DBParams struct { + ConnectionURL string + ID string + ClientName string +} + // Params is used to carry in parameters from subscribing/requesting clients configuration type Params struct { IntermediateStateNodes bool