-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
release: prepare release for v0.0.2
- Loading branch information
Showing
24 changed files
with
464 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package attest | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/bnb-chain/greenfield-challenger/db/model" | ||
"github.com/bnb-chain/greenfield-challenger/executor" | ||
"github.com/bnb-chain/greenfield-challenger/logging" | ||
) | ||
|
||
type AttestMonitor struct { | ||
executor *executor.Executor | ||
mtx sync.RWMutex | ||
attestedChallengeIds map[uint64]bool // used to save the last attested challenge id | ||
dataProvider DataProvider | ||
} | ||
|
||
func NewAttestMonitor(executor *executor.Executor, dataProvider DataProvider) *AttestMonitor { | ||
return &AttestMonitor{ | ||
executor: executor, | ||
mtx: sync.RWMutex{}, | ||
attestedChallengeIds: make(map[uint64]bool, 0), | ||
dataProvider: dataProvider, | ||
} | ||
} | ||
|
||
// UpdateAttestedChallengeIdLoop polls the blockchain for latest attested challengeIds and updates their status | ||
func (a *AttestMonitor) UpdateAttestedChallengeIdLoop() { | ||
ticker := time.NewTicker(QueryAttestedChallengeInterval) | ||
queryCount := 0 | ||
for range ticker.C { | ||
challengeIds, err := a.executor.QueryLatestAttestedChallengeIds() | ||
// logging.Logger.Infof("latest attested challenge ids: %+v", challengeIds) | ||
if err != nil { | ||
logging.Logger.Errorf("update latest attested challenge error, err=%+v", err) | ||
continue | ||
} | ||
logging.Logger.Infof("latest attested challenge ids: %+v", challengeIds) | ||
a.mtx.Lock() | ||
a.updateAttestedCacheAndEventStatus(a.attestedChallengeIds, challengeIds) | ||
for _, id := range challengeIds { | ||
a.attestedChallengeIds[id] = true | ||
} | ||
a.mtx.Unlock() | ||
|
||
queryCount++ | ||
if queryCount > MaxQueryCount { | ||
a.attestedChallengeIds = make(map[uint64]bool, 0) | ||
} | ||
} | ||
} | ||
|
||
// updateAttestedCacheAndEventStatus only updates new entries | ||
func (a *AttestMonitor) updateAttestedCacheAndEventStatus(old map[uint64]bool, latest []uint64) { | ||
for _, challengeId := range latest { | ||
if _, ok := old[challengeId]; !ok { | ||
go a.updateEventStatus(challengeId) | ||
} | ||
} | ||
} | ||
|
||
func (a *AttestMonitor) updateEventStatus(challengeId uint64) { | ||
event, err := a.dataProvider.GetEventByChallengeId(challengeId) | ||
if err != nil || event == nil { | ||
logging.Logger.Errorf("attest monitor failed to get event by challengeId: %d, err=%+v", challengeId, err) | ||
return | ||
} | ||
if event.Status == model.SelfAttested || event.Status == model.Attested { | ||
return | ||
} | ||
var status model.EventStatus | ||
if event.Status == model.Submitted { | ||
status = model.SelfAttested | ||
} else { | ||
status = model.Attested | ||
} | ||
err = a.dataProvider.UpdateEventStatus(challengeId, status) | ||
if err != nil { | ||
logging.Logger.Errorf("update attested event status error, err=%s", err.Error()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package attest | ||
|
||
import "time" | ||
|
||
const ( | ||
QueryAttestedChallengeInterval = 5 * time.Second // query last attested challenge id | ||
MaxQueryCount = int((1 * time.Hour) / QueryAttestedChallengeInterval) // query last attested challenge id limit | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package attest | ||
|
||
import ( | ||
"github.com/bnb-chain/greenfield-challenger/db/dao" | ||
"github.com/bnb-chain/greenfield-challenger/db/model" | ||
) | ||
|
||
type DataProvider interface { | ||
GetEventByChallengeId(challengeId uint64) (*model.Event, error) | ||
UpdateEventStatus(challengeId uint64, status model.EventStatus) error | ||
} | ||
|
||
type DataHandler struct { | ||
daoManager *dao.DaoManager | ||
} | ||
|
||
func NewDataHandler(daoManager *dao.DaoManager) *DataHandler { | ||
return &DataHandler{ | ||
daoManager: daoManager, | ||
} | ||
} | ||
|
||
func (h *DataHandler) UpdateEventStatus(challengeId uint64, status model.EventStatus) error { | ||
return h.daoManager.UpdateEventStatusByChallengeId(challengeId, status) | ||
} | ||
|
||
func (h *DataHandler) GetEventByChallengeId(challengeId uint64) (*model.Event, error) { | ||
return h.daoManager.GetEventByChallengeId(challengeId) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.