mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-28 07:36:44 +00:00
wip
This commit is contained in:
parent
200472dfdc
commit
2526aa8530
4 changed files with 91 additions and 8 deletions
|
|
@ -221,7 +221,7 @@ type Bor struct {
|
|||
lock sync.RWMutex // Protects the signer fields
|
||||
|
||||
ethAPI *ethapi.PublicBlockChainAPI
|
||||
genesisContractsClient *GenesisContractsClient
|
||||
GenesisContractsClient *GenesisContractsClient
|
||||
validatorSetABI abi.ABI
|
||||
stateReceiverABI abi.ABI
|
||||
HeimdallClient IHeimdallClient
|
||||
|
|
@ -263,7 +263,7 @@ func New(
|
|||
signatures: signatures,
|
||||
validatorSetABI: vABI,
|
||||
stateReceiverABI: sABI,
|
||||
genesisContractsClient: genesisContractsClient,
|
||||
GenesisContractsClient: genesisContractsClient,
|
||||
HeimdallClient: heimdallClient,
|
||||
}
|
||||
|
||||
|
|
@ -1076,11 +1076,11 @@ func (c *Bor) CommitStates(
|
|||
chain chainContext,
|
||||
) error {
|
||||
number := header.Number.Uint64()
|
||||
lastSync, err := c.genesisContractsClient.LastStateSyncTime(number - 1)
|
||||
lastSync, err := c.GenesisContractsClient.LastStateSyncTime(number - 1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_lastStateID, err := c.genesisContractsClient.LastStateId(number - 1)
|
||||
_lastStateID, err := c.GenesisContractsClient.LastStateId(number - 1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -1088,6 +1088,7 @@ func (c *Bor) CommitStates(
|
|||
from := lastSync
|
||||
to := time.Unix(int64(chain.Chain.GetHeaderByNumber(number-c.config.Sprint).Time), 0)
|
||||
lastStateID := _lastStateID.Uint64()
|
||||
fmt.Println("to Uint64", lastStateID)
|
||||
if !from.Before(to) {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -1123,12 +1124,19 @@ func (c *Bor) CommitStates(
|
|||
return eventRecords[i].ID < eventRecords[j].ID
|
||||
})
|
||||
|
||||
for _, e := range eventRecords {
|
||||
fmt.Println("here", e.ID)
|
||||
}
|
||||
|
||||
chainID := c.chainConfig.ChainID.String()
|
||||
for _, eventRecord := range eventRecords {
|
||||
fmt.Println(eventRecord.ID, lastStateID)
|
||||
if eventRecord.ID <= lastStateID {
|
||||
fmt.Println("continuiing", lastStateID)
|
||||
continue
|
||||
}
|
||||
if err := validateEventRecord(eventRecord, number, from, to, lastStateID, chainID); err != nil {
|
||||
fmt.Println("breaking", err.Error())
|
||||
log.Error(err.Error())
|
||||
break
|
||||
}
|
||||
|
|
@ -1143,11 +1151,13 @@ func (c *Bor) CommitStates(
|
|||
c.stateDataFeed.Send(core.NewStateChangeEvent{StateData: &stateData})
|
||||
}()
|
||||
|
||||
if err := c.genesisContractsClient.CommitState(eventRecord, state, header, chain); err != nil {
|
||||
if err := c.GenesisContractsClient.CommitState(eventRecord, state, header, chain); err != nil {
|
||||
fmt.Println("erred", err)
|
||||
return err
|
||||
}
|
||||
lastStateID++
|
||||
}
|
||||
lastStateID++
|
||||
fmt.Println("lastStateID is", lastStateID)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -115,6 +115,72 @@ func TestFetchStateSyncEvents(t *testing.T) {
|
|||
assert.True(t, h.AssertCalled(t, "FetchWithRetry", clerkPath, query2Params))
|
||||
}
|
||||
|
||||
func TestFetchStateSyncEvents_2(t *testing.T) {
|
||||
init := buildEthereumInstance(t, rawdb.NewMemoryDatabase())
|
||||
chain := init.ethereum.BlockChain()
|
||||
engine := init.ethereum.Engine()
|
||||
_bor := engine.(*bor.Bor)
|
||||
|
||||
// A. Insert blocks for 0th sprint
|
||||
db := init.ethereum.ChainDb()
|
||||
block := init.genesis.ToBlock(db)
|
||||
// Insert sprintSize # of blocks so that span is fetched at the start of a new sprint
|
||||
for i := uint64(1); i < sprintSize; i++ {
|
||||
block = buildNextBlock(t, _bor, chain, block, nil, init.genesis.Config.Bor)
|
||||
insertNewBlock(t, chain, block)
|
||||
}
|
||||
|
||||
// B. Before inserting 1st block of the next sprint, mock heimdall deps
|
||||
// B.1 Mock /bor/span/1
|
||||
res, _ := loadSpanFromFile(t)
|
||||
h := &mocks.IHeimdallClient{}
|
||||
h.On("FetchWithRetry", spanPath, "").Return(res, nil)
|
||||
|
||||
// B.2 Mock State Sync events
|
||||
// read heimdall api response from file
|
||||
res = stateSyncEventsPayload(t)
|
||||
var _eventRecords []*bor.EventRecordWithTime
|
||||
if err := json.Unmarshal(res.Result, &_eventRecords); err != nil {
|
||||
t.Fatalf("%s", err)
|
||||
}
|
||||
sample := _eventRecords[0]
|
||||
|
||||
// First query will be from [0, (block-sprint).Time]
|
||||
// Insert 4 events are in this time range
|
||||
eventRecords := []*bor.EventRecordWithTime{
|
||||
buildStateEvent(sample, 2, 3), // id = 2, time = 3
|
||||
buildStateEvent(sample, 3, 2), // id = 3, time = 2
|
||||
buildStateEvent(sample, 1, 1), // id = 1, time = 1
|
||||
// event with id 4 is missing
|
||||
buildStateEvent(sample, 5, 4), // id = 5, time = 4
|
||||
}
|
||||
_res, _ := json.Marshal(eventRecords)
|
||||
response := bor.ResponseWithHeight{Height: "0"}
|
||||
if err := json.Unmarshal(_res, &response.Result); err != nil {
|
||||
t.Fatalf("%s", err)
|
||||
}
|
||||
|
||||
from := 0
|
||||
to := int64(chain.GetHeaderByNumber(0).Time)
|
||||
page := 1
|
||||
queryParams := fmt.Sprintf(clerkQueryParams, from, to, page)
|
||||
h.On("FetchWithRetry", clerkPath, queryParams).Return(&response, nil)
|
||||
|
||||
page = 2
|
||||
query2Params := fmt.Sprintf(clerkQueryParams, from, to, page)
|
||||
h.On("FetchWithRetry", clerkPath, query2Params).Return(&bor.ResponseWithHeight{}, nil)
|
||||
_bor.SetHeimdallClient(h)
|
||||
|
||||
block = buildNextBlock(t, _bor, chain, block, nil, init.genesis.Config.Bor)
|
||||
insertNewBlock(t, chain, block)
|
||||
|
||||
assert.True(t, h.AssertCalled(t, "FetchWithRetry", spanPath, ""))
|
||||
assert.True(t, h.AssertCalled(t, "FetchWithRetry", clerkPath, queryParams))
|
||||
lastStateID, _ := _bor.GenesisContractsClient.LastStateId(sprintSize)
|
||||
assert.Equal(t, uint64(3), lastStateID.Uint64())
|
||||
fmt.Println("lastStateId after", lastStateID)
|
||||
}
|
||||
|
||||
func TestOutOfTurnSigning(t *testing.T) {
|
||||
init := buildEthereumInstance(t, rawdb.NewMemoryDatabase())
|
||||
chain := init.ethereum.BlockChain()
|
||||
|
|
@ -210,3 +276,10 @@ func generateFakeStateSyncEvents(sample *bor.EventRecordWithTime, count int) []*
|
|||
}
|
||||
return events
|
||||
}
|
||||
|
||||
func buildStateEvent(sample *bor.EventRecordWithTime, id uint64, timeStamp int64) *bor.EventRecordWithTime {
|
||||
event := *sample
|
||||
event.ID = id
|
||||
event.Time = time.Unix(timeStamp, 0)
|
||||
return &event
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -135,7 +135,7 @@ type InvalidStateReceivedError struct {
|
|||
|
||||
func (e *InvalidStateReceivedError) Error() string {
|
||||
return fmt.Sprintf(
|
||||
"Received invalid event %s at block %d. Requested events from %s to %s. lastStateID was %d.\n",
|
||||
"Received invalid event %s at block %d. Requested events from %s to %s. lastStateID was %d.",
|
||||
e.Event,
|
||||
e.Number,
|
||||
e.From.Format(time.RFC3339),
|
||||
|
|
|
|||
Loading…
Reference in a new issue