From 35f930791821a966e61acc5499841cc6c3c14835 Mon Sep 17 00:00:00 2001 From: Alexey Kiselev Date: Wed, 25 Dec 2024 22:33:26 +0400 Subject: [PATCH 1/2] Fix ride alias retrieval (#1571) * Use function NewestRecipientToAddress in state function IsStateUntouched. Comment added. * Test added. --- pkg/state/common_test.go | 5 ++ pkg/state/state.go | 4 +- pkg/state/state_test.go | 119 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 1 deletion(-) diff --git a/pkg/state/common_test.go b/pkg/state/common_test.go index 0d42414c2..e1dc4efda 100644 --- a/pkg/state/common_test.go +++ b/pkg/state/common_test.go @@ -543,6 +543,11 @@ func (s *testStorageObjects) transferWaves( s.setWavesBalance(t, to, toBalance, blockID) } +func (s *testStorageObjects) createAlias(t testing.TB, addr proto.WavesAddress, alias string, blockID proto.BlockID) { + err := s.entities.aliases.createAlias(alias, addr, blockID) + assert.NoError(t, err, "createAlias() failed") +} + func storeScriptByAddress( stor *blockchainEntitiesStorage, scheme proto.Scheme, diff --git a/pkg/state/state.go b/pkg/state/state.go index e6fcf5a19..87f56afbc 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -2367,8 +2367,10 @@ func (s *stateManager) RetrieveEntries(account proto.Recipient) ([]proto.DataEnt return entries, nil } +// IsStateUntouched returns true if the account has no data entries. +// ATTENTION: Despite the name, this function operates on the newest state. The name is kept for compatibility. func (s *stateManager) IsStateUntouched(account proto.Recipient) (bool, error) { - addr, err := s.recipientToAddress(account) + addr, err := s.NewestRecipientToAddress(account) if err != nil { return false, wrapErr(RetrievalError, err) } diff --git a/pkg/state/state_test.go b/pkg/state/state_test.go index abc54b066..da749b96c 100644 --- a/pkg/state/state_test.go +++ b/pkg/state/state_test.go @@ -900,3 +900,122 @@ func TestGeneratingBalanceValuesInRide(t *testing.T) { }) }) } + +func TestIsStateUntouched(t *testing.T) { + createTestScript := func(t *testing.T, libV ast.LibraryVersion) *ast.Tree { + const scriptTemplate = ` + {-# STDLIB_VERSION %d #-} + {-# CONTENT_TYPE DAPP #-} + {-# SCRIPT_TYPE ACCOUNT #-} + @Callable(i) + func checkStorageUntouchedByAlias(accountAlias: String) = { + let alias = Alias(accountAlias) + let res = if isDataStorageUntouched(alias) then { + unit + } else { + throw("Data storage is not untouched by alias") + } + ([], res) + } + ` + scriptSrc := fmt.Sprintf(scriptTemplate, libV) + tree, errs := ridec.CompileToTree(scriptSrc) + require.NoError(t, stderrs.Join(errs...), "ride.CompileToTree() failed") + return tree + } + doTest := func(t *testing.T, state *stateManager, testObj *testStorageObjects, libV ast.LibraryVersion) { + // create test accounts + dApp, err := proto.NewKeyPair(binary.BigEndian.AppendUint32(nil, 999)) + require.NoError(t, err, "NewKeyPair() failed") + anotherAccount, err := proto.NewKeyPair(binary.BigEndian.AppendUint32(nil, 1)) + require.NoError(t, err, "NewKeyPair() failed") + // create test addresses + bs, bsErr := state.BlockchainSettings() + require.NoError(t, bsErr, "BlockchainSettings() failed") + caller, aErr := anotherAccount.Addr(bs.AddressSchemeCharacter) + require.NoError(t, aErr, "Addr() failed") + dAppAddr, aErr := dApp.Addr(bs.AddressSchemeCharacter) + require.NoError(t, aErr, "Addr() failed") + // create test script + tree := createTestScript(t, libV) + // create assertion function for the current state + assertDataStorageByAlias := func(t *testing.T, alias string) { + fc := proto.NewFunctionCall("checkStorageUntouchedByAlias", + proto.Arguments{proto.NewStringArgument(alias)}) + env := createNewRideEnv(t, state, dAppAddr, caller, libV) + _, err = ride.CallFunction(env, tree, fc) + require.NoError(t, err, "ride.CallFunction() failed") + } + assertHeight := func(t *testing.T, expectedHeight int) { + nh, hErr := state.NewestHeight() + require.NoError(t, hErr, "NewestHeight() failed") + require.Equal(t, proto.Height(expectedHeight), nh) + } + assertHeight(t, 1) // check that height is 1 + // set initial balance for dApp and another account + const ( + initialDAppBalance = 100 * proto.PriceConstant + initialAnotherAccountBalance = 500 * proto.PriceConstant + ) + testObj.setWavesBalance(t, dAppAddr, balanceProfile{initialDAppBalance, 0, 0}, blockID0) // height 1 + testObj.setWavesBalance(t, caller, balanceProfile{initialAnotherAccountBalance, 0, 0}, blockID0) // height 1 + + // Alias "alice" created and checked in different blocks, should always pass. + testObj.addBlockAndDo(t, blockID1, func(_ proto.BlockID) { // height 2 - create alias "alice". + testObj.createAlias(t, dAppAddr, "alice", blockID1) + }) + assertHeight(t, 2) + + testObj.addBlockAndDo(t, blockID2, func(_ proto.BlockID) { // height 3 - check data storage by alias "alice". + assertDataStorageByAlias(t, "alice") + }) + assertHeight(t, 3) + // Bob alias is created and checked in the same block. + testObj.addBlockAndDo(t, blockID3, func(_ proto.BlockID) { // height 4 - create alias "bob" and check the storage. + testObj.createAlias(t, dAppAddr, "bob", blockID3) + assertDataStorageByAlias(t, "bob") + }) + assertHeight(t, 4) + } + t.Run("The data storage can be checked by alias created in the same block", func(t *testing.T) { + generateFeaturesList := func(targetFeature settings.Feature) []settings.Feature { + var feats []settings.Feature + for f := settings.SmallerMinimalGeneratingBalance; f <= targetFeature; f++ { + feats = append(feats, f) + } + return feats + } + activateFeatures := func(t *testing.T, testObj *testStorageObjects, feats []settings.Feature, id proto.BlockID) { + for _, f := range feats { + testObj.activateFeatureWithBlock(t, int16(f), id) + } + } + createMockState := func(t *testing.T, targetFeature settings.Feature) (*stateManager, *testStorageObjects) { + sets := settings.MustDefaultCustomSettings() + sets.LightNodeBlockFieldsAbsenceInterval = 0 // disable absence interval for Light Node + sets.GenerationBalanceDepthFrom50To1000AfterHeight = 1 // set from the first height + state, testObj := createMockStateManager(t, sets) + featuresList := generateFeaturesList(targetFeature) + testObj.addBlock(t, blockID0) // add "genesis" block, height 1 + activateFeatures(t, testObj, featuresList, blockID0) // activate features at height 1 + testObj.flush(t) // write changes to the storage + return state, testObj + } + t.Run("RideV5, STDLIB_VERSION 5", func(t *testing.T) { + state, testObj := createMockState(t, settings.RideV5) + doTest(t, state, testObj, ast.LibV5) + }) + t.Run("RideV6, STDLIB_VERSION 6", func(t *testing.T) { + state, testObj := createMockState(t, settings.RideV6) + doTest(t, state, testObj, ast.LibV6) + }) + t.Run("BlockRewardDistribution, STDLIB_VERSION 7", func(t *testing.T) { + state, testObj := createMockState(t, settings.BlockRewardDistribution) + doTest(t, state, testObj, ast.LibV7) + }) + t.Run("LightNode, STDLIB_VERSION 8", func(t *testing.T) { + state, testObj := createMockState(t, settings.LightNode) + doTest(t, state, testObj, ast.LibV8) + }) + }) +} From 4559244299c4e2d0a9f8924c01add806990309c6 Mon Sep 17 00:00:00 2001 From: Alexey Kiselev Date: Wed, 25 Dec 2024 23:39:46 +0400 Subject: [PATCH 2/2] Targets to build native executables added where it was missed. (#1570) Target "build" added to build all native executables. Co-authored-by: Nikolay Eskov --- Makefile | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Makefile b/Makefile index 2b57054a6..55dd7feb7 100644 --- a/Makefile +++ b/Makefile @@ -74,6 +74,8 @@ vetcheck: strict-vet-check: golangci-lint run -c .golangci-strict.yml +build-chaincmp-native: + @go build -o build/bin/native/chaincmp -ldflags="-X main.version=$(VERSION)" ./cmd/chaincmp build-chaincmp-linux: @CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/bin/linux-amd64/chaincmp -ldflags="-X main.version=$(VERSION)" ./cmd/chaincmp build-chaincmp-darwin: @@ -89,6 +91,8 @@ dist-chaincmp: release-chaincmp @cd ./build/bin/linux-amd64/; tar pzcvf ../../dist/chaincmp_$(VERSION)_Linux-amd64.tar.gz ./chaincmp* @cd ./build/bin/darwin-amd64/; tar pzcvf ../../dist/chaincmp_$(VERSION)_macOS-amd64.tar.gz ./chaincmp* +build-blockcmp-native: + @go build -o build/bin/native/blockcmp -ldflags="-X main.version=$(VERSION)" ./cmd/blockcmp build-blockcmp-linux: @CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/bin/linux-amd64/blockcmp -ldflags="-X main.version=$(VERSION)" ./cmd/blockcmp build-blockcmp-darwin: @@ -147,6 +151,8 @@ dist-importer: release-importer @cd ./build/bin/linux-amd64/; tar pzcvf ../../dist/importer_$(VERSION)_Linux-amd64.tar.gz ./importer* @cd ./build/bin/darwin-amd64/; tar pzcvf ../../dist/importer_$(VERSION)_macOS-amd64.tar.gz ./importer* +build-wallet-native: + @go build -o build/bin/native/wallet ./cmd/wallet build-wallet-linux: @CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/bin/linux-amd64/wallet ./cmd/wallet build-wallet-darwin: @@ -162,6 +168,8 @@ dist-wallet: release-wallet @cd ./build/bin/linux-amd64/; tar pzcvf ../../dist/wallet_$(VERSION)_Linux-amd64.tar.gz ./wallet* @cd ./build/bin/darwin-amd64/; tar pzcvf ../../dist/wallet_$(VERSION)_macOS-amd64.tar.gz ./wallet* +build-rollback-native: + @go build -o build/bin/native/rollback -ldflags="-X 'github.com/wavesplatform/gowaves/pkg/versioning.Version=$(VERSION)'" ./cmd/rollback build-rollback-linux: @CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/bin/linux-amd64/rollback -ldflags="-X 'github.com/wavesplatform/gowaves/pkg/versioning.Version=$(VERSION)'" ./cmd/rollback build-rollback-darwin: @@ -171,6 +179,8 @@ build-rollback-windows: release-rollback: ver build-rollback-linux build-rollback-darwin build-rollback-windows +build-compiler-native: + @go build -o build/bin/native/compiler ./cmd/compiler build-compiler-linux: @CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/bin/linux-amd64/compiler ./cmd/compiler build-compiler-darwin: @@ -186,6 +196,8 @@ dist-compiler: release-compiler @cd ./build/bin/linux-amd64/; tar pzcvf ../../dist/compiler_$(VERSION)_Linux-amd64.tar.gz ./compiler* @cd ./build/bin/darwin-amd64/; tar pzcvf ../../dist/compiler_$(VERSION)_macOS-amd64.tar.gz ./compiler* +build-statehash-native: + @go build -o build/bin/native/statehash ./cmd/statehash build-statehash-linux: @CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/bin/linux-amd64/statehash ./cmd/statehash build-statehash-darwin: @@ -195,6 +207,8 @@ build-statehash-windows: release-statehash: ver build-statehash-linux build-statehash-darwin build-statehash-windows +build-convert-native: + @go build -o build/bin/native/convert ./cmd/convert build-convert-linux: @CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/bin/linux-amd64/convert ./cmd/convert build-convert-darwin: @@ -212,6 +226,8 @@ dist-convert: release-convert dist: clean dist-chaincmp dist-importer dist-node dist-wallet dist-compiler +build: vendor ver build-chaincmp-native build-blockcmp-native build-node-native build-importer-native build-wallet-native build-rollback-native build-compiler-native build-statehash-native build-convert-native + mock: mockgen -source pkg/miner/utxpool/cleaner.go -destination pkg/miner/utxpool/mock.go -package utxpool stateWrapper mockgen -source pkg/node/peers/peer_manager.go -destination pkg/mock/peer_manager.go -package mock PeerManager