Skip to content

Commit 3957c79

Browse files
envestccCoderZhi
andauthored
Fix xingu (#4740)
* replace stakeview with voteview * fix test * refactor view * address comment * add unit test and fix a log error * fix build * add migrate unit test * store candidate votes in erigondb * update matchContractIndex and move registrations into erigonstore * store contract buckets in erigondb * fix wrapper base * fix system contract index * introduce xingu hardfork * fix staking * fix pack staking event error * add candidate register/update test * fix slash not apply * address comment * slash candidate by operator * update election * xingubeta hardfork * fix erigon state not exist * fix test * v4 to read candidate * fix blskey not copied * fix blskey before xingubeta * fix start ctx * slash candidate unit test * credit bucket pool without deleting bucket * address comment * unit test for nft event handler * fix test * fix viewdata panic * fix panic * fix conflict * log start * Fix candidate votes serialization order * Update Xingu and XinguBeta block heights * update ci * update go.test.sh * update ci --------- Co-authored-by: zhi <thecoderzhi@gmail.com>
1 parent 960137e commit 3957c79

File tree

116 files changed

+4951
-1028
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+4951
-1028
lines changed

.github/workflows/ci.yaml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,20 @@ env:
1616
jobs:
1717
ci:
1818
name: ci flow
19-
runs-on: self-hosted
20-
container:
21-
image: ubuntu:latest
19+
runs-on: ubuntu-latest
2220
steps:
2321
- uses: actions/checkout@v3
2422
# with:
2523
# submodules: recursive
2624

27-
- name: Install build tools
28-
run: |
29-
apt-get update
30-
apt-get install -y build-essential ca-certificates
3125
- name: Set up Go
32-
uses: actions/setup-go@v3
26+
uses: actions/setup-go@v4
3327
with:
3428
go-version: 1.23.0
3529
cache: false
36-
30+
3731
- name: Build Go
38-
run: CGO_ENABLED=1 go build ./...
32+
run: go build ./...
3933

4034
- name: Run Test
4135
id: unittest

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ recover:
256256

257257
.PHONY: ioctl
258258
ioctl:
259-
$(GOBUILD) -ldflags "$(PackageFlags)" -o ./bin/$(BUILD_TARGET_IOCTL) -v ./tools/ioctl
259+
$(GOBUILD) -tags $(BUILD_TAGS) -ldflags "$(PackageFlags)" -o ./bin/$(BUILD_TARGET_IOCTL) -v ./tools/ioctl
260260

261261
.PHONY: newioctl
262262
newioctl:

action/candidate_register.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -373,18 +373,18 @@ func PackCandidateRegisteredEvent(
373373
blsPubKey []byte,
374374
) (Topics, []byte, error) {
375375
data, err := _candidateRegisteredEvent.Inputs.NonIndexed().Pack(
376-
operatorAddress.Bytes(),
376+
common.BytesToAddress(operatorAddress.Bytes()),
377377
name,
378-
rewardAddress.Bytes(),
378+
common.BytesToAddress(rewardAddress.Bytes()),
379379
blsPubKey,
380380
)
381381
if err != nil {
382382
return nil, nil, errors.Wrap(err, "failed to pack CandidateRegisterWithBLS event")
383383
}
384384
topics := make(Topics, 3)
385385
topics[0] = hash.Hash256(_candidateRegisteredEvent.ID)
386-
topics[1] = hash.Hash256(candidate.Bytes())
387-
topics[2] = hash.Hash256(ownerAddress.Bytes())
386+
topics[1] = hash.BytesToHash256(candidate.Bytes())
387+
topics[2] = hash.BytesToHash256(ownerAddress.Bytes())
388388
return topics, data, nil
389389
}
390390

@@ -406,8 +406,8 @@ func PackStakedEvent(
406406
}
407407
topics := make(Topics, 3)
408408
topics[0] = hash.Hash256(_stakedEvent.ID)
409-
topics[1] = hash.Hash256(voter.Bytes())
410-
topics[2] = hash.Hash256(candidate.Bytes())
409+
topics[1] = hash.BytesToHash256(voter.Bytes())
410+
topics[2] = hash.BytesToHash256(candidate.Bytes())
411411
return topics, data, nil
412412
}
413413

@@ -422,7 +422,7 @@ func PackCandidateActivatedEvent(
422422
}
423423
topics := make(Topics, 2)
424424
topics[0] = hash.Hash256(_candidateActivatedEvent.ID)
425-
topics[1] = hash.Hash256(candidate.Bytes())
425+
topics[1] = hash.BytesToHash256(candidate.Bytes())
426426
return topics, data, nil
427427
}
428428

action/candidate_update.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func PackCandidateUpdatedEvent(
292292
blsPubKey []byte,
293293
) (Topics, []byte, error) {
294294
data, err := _candidateUpdateWithBLSEvent.Inputs.NonIndexed().Pack(
295-
rewardAddress.Bytes(),
295+
common.BytesToAddress(rewardAddress.Bytes()),
296296
name,
297297
common.BytesToAddress(operatorAddress.Bytes()),
298298
blsPubKey,
@@ -302,7 +302,7 @@ func PackCandidateUpdatedEvent(
302302
}
303303
topics := make(Topics, 3)
304304
topics[0] = hash.Hash256(_candidateUpdateWithBLSEvent.ID)
305-
topics[1] = hash.Hash256(candidate.Bytes())
306-
topics[2] = hash.Hash256(ownerAddress.Bytes())
305+
topics[1] = hash.BytesToHash256(candidate.Bytes())
306+
topics[2] = hash.BytesToHash256(ownerAddress.Bytes())
307307
return topics, data, nil
308308
}

action/candidateregister_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ import (
99
"math/big"
1010
"testing"
1111

12+
"github.com/ethereum/go-ethereum/common"
1213
"github.com/pkg/errors"
1314
"github.com/stretchr/testify/require"
1415
"google.golang.org/protobuf/proto"
1516

1617
"github.com/iotexproject/go-pkgs/crypto"
18+
"github.com/iotexproject/go-pkgs/hash"
19+
"github.com/iotexproject/iotex-address/address"
1720

1821
"github.com/iotexproject/iotex-core/v2/test/identityset"
1922
)
@@ -239,3 +242,102 @@ func TestIsValidCandidateName(t *testing.T) {
239242
require.Equal(tt.output, output)
240243
}
241244
}
245+
246+
func TestStakingEvent(t *testing.T) {
247+
require := require.New(t)
248+
cand := identityset.Address(0)
249+
owner := identityset.Address(1)
250+
operator := identityset.Address(2)
251+
reward := identityset.Address(3)
252+
voter := identityset.Address(4)
253+
blsPrivKey, err := crypto.GenerateBLS12381PrivateKey(identityset.PrivateKey(0).Bytes())
254+
require.NoError(err)
255+
blsPubKey := blsPrivKey.PublicKey().Bytes()
256+
name := "test"
257+
258+
t.Run("register", func(t *testing.T) {
259+
topics, data, err := PackCandidateRegisteredEvent(cand, operator, owner, name, reward, blsPubKey)
260+
require.NoError(err)
261+
checkCandidateRegisterEvent(require, topics, data, owner, cand, operator, reward, name, blsPubKey)
262+
})
263+
t.Run("staked", func(t *testing.T) {
264+
bktIdx := uint64(1)
265+
amount := big.NewInt(100)
266+
duration := uint32(3600)
267+
autoStake := true
268+
topics, data, err := PackStakedEvent(voter, cand, bktIdx, amount, duration, autoStake)
269+
require.NoError(err)
270+
checkStakedEvent(require, topics, data, voter, cand, bktIdx, amount, duration, autoStake)
271+
})
272+
t.Run("activate", func(t *testing.T) {
273+
bktIdx := uint64(1)
274+
topics, data, err := PackCandidateActivatedEvent(cand, bktIdx)
275+
require.NoError(err)
276+
checkActivatedEvent(require, topics, data, cand, bktIdx)
277+
})
278+
t.Run("update", func(t *testing.T) {
279+
topics, data, err := PackCandidateUpdatedEvent(cand, operator, owner, name, reward, blsPubKey)
280+
require.NoError(err)
281+
checkCandidateUpdateEvent(require, topics, data, owner, cand, operator, reward, name, blsPubKey)
282+
})
283+
}
284+
285+
func checkCandidateRegisterEvent(require *require.Assertions, topics Topics, data []byte,
286+
owner, cand, operator, reward address.Address, name string, blsPubKey []byte,
287+
) {
288+
paramsNonIndexed, err := _candidateRegisteredEvent.Inputs.Unpack(data)
289+
require.NoError(err)
290+
require.Equal(4, len(paramsNonIndexed))
291+
require.Equal(operator.Bytes(), paramsNonIndexed[0].(common.Address).Bytes())
292+
require.Equal(name, paramsNonIndexed[1].(string))
293+
require.Equal(reward.Bytes(), paramsNonIndexed[2].(common.Address).Bytes())
294+
require.Equal(blsPubKey, paramsNonIndexed[3].([]byte))
295+
require.Equal(3, len(topics))
296+
require.Equal(hash.Hash256(_candidateRegisteredEvent.ID), topics[0])
297+
require.Equal(hash.BytesToHash256(cand.Bytes()), topics[1])
298+
require.Equal(hash.BytesToHash256(owner.Bytes()), topics[2])
299+
}
300+
301+
func checkStakedEvent(require *require.Assertions, topics Topics, data []byte,
302+
voter, cand address.Address, bktIdx uint64, amount *big.Int, duration uint32, autoStake bool,
303+
) {
304+
paramsNonIndexed, err := _stakedEvent.Inputs.Unpack(data)
305+
require.NoError(err)
306+
require.Equal(4, len(paramsNonIndexed))
307+
require.Equal(bktIdx, paramsNonIndexed[0].(uint64))
308+
require.Equal(amount, paramsNonIndexed[1].(*big.Int))
309+
require.Equal(duration, paramsNonIndexed[2].(uint32))
310+
require.Equal(autoStake, paramsNonIndexed[3].(bool))
311+
require.Equal(3, len(topics))
312+
require.Equal(hash.Hash256(_stakedEvent.ID), topics[0])
313+
require.Equal(hash.BytesToHash256(voter.Bytes()), topics[1])
314+
require.Equal(hash.BytesToHash256(cand.Bytes()), topics[2])
315+
}
316+
317+
func checkActivatedEvent(require *require.Assertions, topics Topics, data []byte,
318+
cand address.Address, bktIdx uint64,
319+
) {
320+
paramsNonIndexed, err := _candidateActivatedEvent.Inputs.Unpack(data)
321+
require.NoError(err)
322+
require.Equal(1, len(paramsNonIndexed))
323+
require.Equal(bktIdx, paramsNonIndexed[0].(uint64))
324+
require.Equal(2, len(topics))
325+
require.Equal(hash.Hash256(_candidateActivatedEvent.ID), topics[0])
326+
require.Equal(hash.BytesToHash256(cand.Bytes()), topics[1])
327+
}
328+
329+
func checkCandidateUpdateEvent(require *require.Assertions, topics Topics, data []byte,
330+
owner, cand, operator, reward address.Address, name string, blsPubKey []byte,
331+
) {
332+
paramsNonIndexed, err := _candidateUpdateWithBLSEvent.Inputs.Unpack(data)
333+
require.NoError(err)
334+
require.Equal(4, len(paramsNonIndexed))
335+
require.Equal(reward.Bytes(), paramsNonIndexed[0].(common.Address).Bytes())
336+
require.Equal(name, paramsNonIndexed[1].(string))
337+
require.Equal(operator.Bytes(), paramsNonIndexed[2].(common.Address).Bytes())
338+
require.Equal(blsPubKey, paramsNonIndexed[3].([]byte))
339+
require.Equal(3, len(topics))
340+
require.Equal(hash.Hash256(_candidateUpdateWithBLSEvent.ID), topics[0])
341+
require.Equal(hash.BytesToHash256(cand.Bytes()), topics[1])
342+
require.Equal(hash.BytesToHash256(owner.Bytes()), topics[2])
343+
}

action/protocol/context.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ type (
163163
CandidateBLSPublicKey bool
164164
NotUseMinSelfStakeToBeActive bool
165165
StoreVoteOfNFTBucketIntoView bool
166+
CandidateSlashByOwner bool
167+
CandidateBLSPublicKeyNotCopied bool
166168
}
167169

168170
// FeatureWithHeightCtx provides feature check functions.
@@ -325,10 +327,12 @@ func WithFeatureCtx(ctx context.Context) context.Context {
325327
TimestampedStakingContract: g.IsWake(height),
326328
PreStateSystemAction: !g.IsWake(height),
327329
CreatePostActionStates: g.IsWake(height),
328-
NotSlashUnproductiveDelegates: !g.IsToBeEnabled(height),
329-
CandidateBLSPublicKey: g.IsToBeEnabled(height),
330-
NotUseMinSelfStakeToBeActive: !g.IsToBeEnabled(height),
331-
StoreVoteOfNFTBucketIntoView: !g.IsToBeEnabled(height),
330+
NotSlashUnproductiveDelegates: !g.IsXingu(height),
331+
CandidateBLSPublicKey: g.IsXingu(height),
332+
NotUseMinSelfStakeToBeActive: !g.IsXingu(height),
333+
StoreVoteOfNFTBucketIntoView: !g.IsXingu(height),
334+
CandidateSlashByOwner: !g.IsXinguBeta(height),
335+
CandidateBLSPublicKeyNotCopied: !g.IsXinguBeta(height),
332336
},
333337
)
334338
}
@@ -351,7 +355,7 @@ func GetFeatureCtx(ctx context.Context) (FeatureCtx, bool) {
351355
func MustGetFeatureCtx(ctx context.Context) FeatureCtx {
352356
fc, ok := ctx.Value(featureContextKey{}).(FeatureCtx)
353357
if !ok {
354-
log.S().Panic("Miss feature context")
358+
log.L().Panic("Miss feature context")
355359
}
356360
return fc
357361
}

action/protocol/execution/evm/evmstatedbadapter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ func (stateDB *StateDBAdapter) CommitContracts() error {
10921092
sort.Slice(contractAddrs, func(i, j int) bool { return bytes.Compare(contractAddrs[i][:], contractAddrs[j][:]) < 0 })
10931093

10941094
for _, addr := range contractAddrs {
1095-
_, err := stateDB.sm.DelState(protocol.KeyOption(addr[:]))
1095+
_, err := stateDB.sm.DelState(protocol.KeyOption(addr[:]), protocol.ObjectOption(&state.Account{}))
10961096
if stateDB.assertError(err, "failed to delete SelfDestruct account/contract", zap.Error(err), zap.String("address", addr.Hex())) {
10971097
return errors.Wrapf(err, "failed to delete SelfDestruct account/contract %x", addr[:])
10981098
}

action/protocol/managers.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,22 @@ func ObjectOption(obj any) StateOption {
6262
}
6363
}
6464

65+
// ErigonStoreOnlyOption sets the option to only read/write from/to erigon store
66+
func ErigonStoreOnlyOption() StateOption {
67+
return func(cfg *StateConfig) error {
68+
cfg.ErigonStoreOnly = true
69+
return nil
70+
}
71+
}
72+
6573
type (
6674
// StateConfig is the config for accessing stateDB
6775
StateConfig struct {
68-
Namespace string // namespace used by state's storage
69-
Key []byte
70-
Keys [][]byte
71-
Object any // object used by state's storage
76+
Namespace string // namespace used by state's storage
77+
Key []byte
78+
Keys [][]byte
79+
Object any // object used by state's storage
80+
ErigonStoreOnly bool // whether only read/write from/to erigon store
7281
}
7382

7483
// StateOption sets parameter for access state

action/protocol/mock_protocol.go

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

action/protocol/poll/util.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ func validate(ctx context.Context, sr protocol.StateReader, p Protocol, act acti
9696
}
9797
for i, d := range ds {
9898
if !proposedDelegates[i].Equal(d) {
99-
msg := fmt.Sprintf(", %v vs %v (expected)",
100-
proposedDelegates,
101-
ds)
99+
msg := fmt.Sprintf(", %+v vs %+v (expected)",
100+
proposedDelegates[i],
101+
d)
102102
return errors.Wrap(ErrDelegatesNotAsExpected, msg)
103103
}
104104
}

0 commit comments

Comments
 (0)