Skip to content

Commit

Permalink
[persistence/contract] do some rename (#538)
Browse files Browse the repository at this point in the history
  • Loading branch information
huangzhiran authored Jun 6, 2024
1 parent 9abd102 commit 6843e0b
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 45 deletions.
2 changes: 1 addition & 1 deletion cmd/stresstest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func updateProjectRequiredProver(contractPersistence *contract.Contract, project
project := projects[index]
expectProvers := rand.Intn(len(provers)) + 1

tx, err := projectInstance.SetAttributes(opts, new(big.Int).SetUint64(project.ID), [][32]byte{contract.RequiredProverAmountHash}, [][]byte{[]byte(strconv.Itoa(expectProvers))})
tx, err := projectInstance.SetAttributes(opts, new(big.Int).SetUint64(project.ID), [][32]byte{contract.RequiredProverAmount}, [][]byte{[]byte(strconv.Itoa(expectProvers))})
if err != nil {
slog.Error("failed to set project attributes", "error", err)
return
Expand Down
22 changes: 11 additions & 11 deletions persistence/contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ const (
)

var (
allTopicHash = []common.Hash{
attributeSetTopicHash,
projectPausedTopicHash,
projectResumedTopicHash,
projectConfigUpdatedTopicHash,
allTopic = []common.Hash{
attributeSetTopic,
projectPausedTopic,
projectResumedTopic,
projectConfigUpdatedTopic,

operatorSetTopicHash,
nodeTypeUpdatedTopicHash,
proverPausedTopicHash,
proverResumedTopicHash,
operatorSetTopic,
nodeTypeUpdatedTopic,
proverPausedTopic,
proverResumedTopic,
}
)

Expand Down Expand Up @@ -375,7 +375,7 @@ func (c *Contract) list() (uint64, error) {
}
query := ethereum.FilterQuery{
Addresses: []common.Address{c.proverContractAddr, c.projectContractAddr},
Topics: [][]common.Hash{allTopicHash},
Topics: [][]common.Hash{allTopic},
}
from := head
to := from
Expand Down Expand Up @@ -410,7 +410,7 @@ func (c *Contract) watch(listedBlockNumber uint64) {
queriedBlockNumber := listedBlockNumber
query := ethereum.FilterQuery{
Addresses: []common.Address{c.proverContractAddr, c.projectContractAddr},
Topics: [][]common.Hash{allTopicHash},
Topics: [][]common.Hash{allTopic},
}
ticker := time.NewTicker(c.watchInterval)

Expand Down
26 changes: 12 additions & 14 deletions persistence/contract/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ import (
)

var (
RequiredProverAmountHash = crypto.Keccak256Hash([]byte("RequiredProverAmount"))
VmTypeHash = crypto.Keccak256Hash([]byte("VmType"))
ClientManagementContractAddressHash = crypto.Keccak256Hash([]byte("ClientManagementContractAddress"))

attributeSetTopicHash = crypto.Keccak256Hash([]byte("AttributeSet(uint256,bytes32,bytes)"))
projectPausedTopicHash = crypto.Keccak256Hash([]byte("ProjectPaused(uint256)"))
projectResumedTopicHash = crypto.Keccak256Hash([]byte("ProjectResumed(uint256)"))
projectConfigUpdatedTopicHash = crypto.Keccak256Hash([]byte("ProjectConfigUpdated(uint256,string,bytes32)"))

emptyHash = common.Hash{}
RequiredProverAmount = crypto.Keccak256Hash([]byte("RequiredProverAmount"))
VmType = crypto.Keccak256Hash([]byte("VmType"))
ClientManagementContractAddr = crypto.Keccak256Hash([]byte("ClientManagementContractAddress"))

attributeSetTopic = crypto.Keccak256Hash([]byte("AttributeSet(uint256,bytes32,bytes)"))
projectPausedTopic = crypto.Keccak256Hash([]byte("ProjectPaused(uint256)"))
projectResumedTopic = crypto.Keccak256Hash([]byte("ProjectResumed(uint256)"))
projectConfigUpdatedTopic = crypto.Keccak256Hash([]byte("ProjectConfigUpdated(uint256,string,bytes32)"))
)

type Project struct {
Expand Down Expand Up @@ -97,7 +95,7 @@ func (c *Contract) processProjectLogs(logs []types.Log) (map[uint64]*blockProjec
}
}
switch l.Topics[0] {
case attributeSetTopicHash:
case attributeSetTopic:
e, err := c.projectInstance.ParseAttributeSet(l)
if err != nil {
return nil, errors.Wrap(err, "failed to parse project attribute set event")
Expand All @@ -113,7 +111,7 @@ func (c *Contract) processProjectLogs(logs []types.Log) (map[uint64]*blockProjec
p.attributes[e.Key] = e.Value
ps.diffs[e.ProjectId.Uint64()] = p

case projectPausedTopicHash:
case projectPausedTopic:
e, err := c.projectInstance.ParseProjectPaused(l)
if err != nil {
return nil, errors.Wrap(err, "failed to parse project paused event")
Expand All @@ -130,7 +128,7 @@ func (c *Contract) processProjectLogs(logs []types.Log) (map[uint64]*blockProjec
p.paused = &paused
ps.diffs[e.ProjectId.Uint64()] = p

case projectResumedTopicHash:
case projectResumedTopic:
e, err := c.projectInstance.ParseProjectResumed(l)
if err != nil {
return nil, errors.Wrap(err, "failed to parse project resumed event")
Expand All @@ -147,7 +145,7 @@ func (c *Contract) processProjectLogs(logs []types.Log) (map[uint64]*blockProjec
p.paused = &paused
ps.diffs[e.ProjectId.Uint64()] = p

case projectConfigUpdatedTopicHash:
case projectConfigUpdatedTopic:
e, err := c.projectInstance.ParseProjectConfigUpdated(l)
if err != nil {
return nil, errors.Wrap(err, "failed to parse project config updated event")
Expand Down
18 changes: 8 additions & 10 deletions persistence/contract/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import (
)

var (
operatorSetTopicHash = crypto.Keccak256Hash([]byte("OperatorSet(uint256,address)"))
nodeTypeUpdatedTopicHash = crypto.Keccak256Hash([]byte("NodeTypeUpdated(uint256,uint256)"))
proverPausedTopicHash = crypto.Keccak256Hash([]byte("ProverPaused(uint256)"))
proverResumedTopicHash = crypto.Keccak256Hash([]byte("ProverResumed(uint256)"))

emptyAddress = common.Address{}
operatorSetTopic = crypto.Keccak256Hash([]byte("OperatorSet(uint256,address)"))
nodeTypeUpdatedTopic = crypto.Keccak256Hash([]byte("NodeTypeUpdated(uint256,uint256)"))
proverPausedTopic = crypto.Keccak256Hash([]byte("ProverPaused(uint256)"))
proverResumedTopic = crypto.Keccak256Hash([]byte("ProverResumed(uint256)"))
)

type Prover struct {
Expand Down Expand Up @@ -87,7 +85,7 @@ func (c *Contract) processProverLogs(logs []types.Log) (map[uint64]*blockProverD
}
}
switch l.Topics[0] {
case operatorSetTopicHash:
case operatorSetTopic:
e, err := c.proverInstance.ParseOperatorSet(l)
if err != nil {
return nil, errors.Wrap(err, "failed to parse prover operator set event")
Expand All @@ -100,7 +98,7 @@ func (c *Contract) processProverLogs(logs []types.Log) (map[uint64]*blockProverD
p.operatorAddress = &e.Operator
ps.diffs[e.Id.Uint64()] = p

case nodeTypeUpdatedTopicHash:
case nodeTypeUpdatedTopic:
e, err := c.proverInstance.ParseNodeTypeUpdated(l)
if err != nil {
return nil, errors.Wrap(err, "failed to parse prover node type updated event")
Expand All @@ -114,7 +112,7 @@ func (c *Contract) processProverLogs(logs []types.Log) (map[uint64]*blockProverD
p.nodeTypes = &nt
ps.diffs[e.Id.Uint64()] = p

case proverPausedTopicHash:
case proverPausedTopic:
e, err := c.proverInstance.ParseProverPaused(l)
if err != nil {
return nil, errors.Wrap(err, "failed to parse prover paused event")
Expand All @@ -128,7 +126,7 @@ func (c *Contract) processProverLogs(logs []types.Log) (map[uint64]*blockProverD
p.paused = &paused
ps.diffs[e.Id.Uint64()] = p

case proverResumedTopicHash:
case proverResumedTopic:
e, err := c.proverInstance.ParseProverResumed(l)
if err != nil {
return nil, errors.Wrap(err, "failed to parse prover resumed event")
Expand Down
2 changes: 1 addition & 1 deletion scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (s *scheduler) schedule() {
}

amount := uint64(1)
if v, ok := cp.Attributes[contract.RequiredProverAmountHash]; ok {
if v, ok := cp.Attributes[contract.RequiredProverAmount]; ok {
n, err := strconv.ParseUint(string(v), 10, 64)
if err != nil {
slog.Error("failed to parse project required prover amount", "project_id", projectID)
Expand Down
4 changes: 2 additions & 2 deletions scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestScheduler_schedule(t *testing.T) {
p.ApplyMethodReturn(pes, "Projects", []*ScheduledProject{{1, 0}})
p.ApplyMethodReturn(pm, "Provers", []*contract.Prover{{ID: 1, Paused: paused}})
p.ApplyMethodReturn(pm, "Project", &contract.Project{
Attributes: map[common.Hash][]byte{contract.RequiredProverAmountHash: []byte("err")},
Attributes: map[common.Hash][]byte{contract.RequiredProverAmount: []byte("err")},
})

chainHead := make(chan uint64, 10)
Expand Down Expand Up @@ -130,7 +130,7 @@ func TestScheduler_schedule(t *testing.T) {
p.ApplyMethodReturn(pes, "Projects", []*ScheduledProject{{1, 0}})
p.ApplyMethodReturn(pm, "Provers", []*contract.Prover{{ID: 1, Paused: paused}})
p.ApplyMethodReturn(pm, "Project", &contract.Project{
Attributes: map[common.Hash][]byte{contract.RequiredProverAmountHash: []byte("10")},
Attributes: map[common.Hash][]byte{contract.RequiredProverAmount: []byte("10")},
})
p.ApplyMethodReturn(&p2p.PubSubs{}, "Delete")

Expand Down
2 changes: 1 addition & 1 deletion task/dispatcher/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (d *Dispatcher) setRequiredProverAmount(head uint64) {
}

proverAmount := uint64(1)
if v, ok := cp.Attributes[contract.RequiredProverAmountHash]; ok {
if v, ok := cp.Attributes[contract.RequiredProverAmount]; ok {
n, err := strconv.ParseUint(string(v), 10, 64)
if err != nil {
slog.Error("failed to parse project required prover amount when set window size", "project_id", p.ID)
Expand Down
4 changes: 2 additions & 2 deletions task/dispatcher/dispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestDispatcher_setRequiredProverAmount(t *testing.T) {
p.ApplyMethodReturn(po, "Projects", []*scheduler.ScheduledProject{{ID: 1, ScheduledBlockNumber: 0}})
p.ApplyMethodReturn(c, "Project", &contract.Project{
ID: 1,
Attributes: map[common.Hash][]byte{contract.RequiredProverAmountHash: []byte("err")},
Attributes: map[common.Hash][]byte{contract.RequiredProverAmount: []byte("err")},
})
d.projectDispatchers.Store(uint64(1), &projectDispatcher{})
d.setRequiredProverAmount(1)
Expand All @@ -116,7 +116,7 @@ func TestDispatcher_setRequiredProverAmount(t *testing.T) {
p.ApplyMethodReturn(po, "Projects", []*scheduler.ScheduledProject{{ID: 1, ScheduledBlockNumber: 0}})
p.ApplyMethodReturn(c, "Project", &contract.Project{
ID: 1,
Attributes: map[common.Hash][]byte{contract.RequiredProverAmountHash: []byte("2")},
Attributes: map[common.Hash][]byte{contract.RequiredProverAmount: []byte("2")},
})
size := atomic.Uint64{}
d.projectDispatchers.Store(uint64(1), &projectDispatcher{
Expand Down
2 changes: 1 addition & 1 deletion task/dispatcher/project_dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func newProjectDispatcher(persistence Persistence, datasourceURI string, newData

proverAmount := &atomic.Uint64{}
proverAmount.Store(1)
if v, ok := p.Attributes[contract.RequiredProverAmountHash]; ok {
if v, ok := p.Attributes[contract.RequiredProverAmount]; ok {
n, err := strconv.ParseUint(string(v), 10, 64)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse project required prover amount, project_id %v", p.ID)
Expand Down
4 changes: 2 additions & 2 deletions task/dispatcher/project_dispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func TestNewProjectDispatcher(t *testing.T) {
nd := func(string) (datasource.Datasource, error) { return nil, nil }

_, err := newProjectDispatcher(ps, "", nd, &contract.Project{
Attributes: map[common.Hash][]byte{contract.RequiredProverAmountHash: []byte("err")},
Attributes: map[common.Hash][]byte{contract.RequiredProverAmount: []byte("err")},
}, nil, nil, nil)
r.ErrorContains(err, "failed to parse project required prover amount")
})
Expand All @@ -181,7 +181,7 @@ func TestNewProjectDispatcher(t *testing.T) {

paused := true
_, err := newProjectDispatcher(ps, "", nd, &contract.Project{
Attributes: map[common.Hash][]byte{contract.RequiredProverAmountHash: []byte("1")},
Attributes: map[common.Hash][]byte{contract.RequiredProverAmount: []byte("1")},
Paused: paused,
}, nil, nil, nil)
time.Sleep(10 * time.Millisecond)
Expand Down

0 comments on commit 6843e0b

Please sign in to comment.