-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync malfeasance proofs continuously (#5718)
## Motivation Need to sync malfeasance proofs continuously to facilitate distributed verification. See #5306
- Loading branch information
Showing
23 changed files
with
1,461 additions
and
257 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
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
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,45 @@ | ||
package malsync | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/spacemeshos/go-spacemesh/sql" | ||
) | ||
|
||
func GetSyncState(db sql.Executor) (time.Time, error) { | ||
var timestamp time.Time | ||
rows, err := db.Exec("select timestamp from malfeasance_sync_state", | ||
nil, func(stmt *sql.Statement) bool { | ||
v := stmt.ColumnInt64(0) | ||
if v > 0 { | ||
timestamp = time.Unix(v, 0) | ||
} | ||
return true | ||
}) | ||
if err != nil { | ||
return time.Time{}, fmt.Errorf("error getting malfeasance sync state: %w", err) | ||
} else if rows != 1 { | ||
return time.Time{}, fmt.Errorf("expected malfeasance_sync_state to have 1 row but got %d rows", rows) | ||
} | ||
return timestamp, nil | ||
} | ||
|
||
func updateSyncState(db sql.Executor, ts int64) error { | ||
_, err := db.Exec("update malfeasance_sync_state set timestamp = ?1", | ||
func(stmt *sql.Statement) { | ||
stmt.BindInt64(1, ts) | ||
}, nil) | ||
if err != nil { | ||
return fmt.Errorf("error updating malfeasance sync state: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func UpdateSyncState(db sql.Executor, timestamp time.Time) error { | ||
return updateSyncState(db, timestamp.Unix()) | ||
} | ||
|
||
func Clear(db sql.Executor) error { | ||
return updateSyncState(db, 0) | ||
} |
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 malsync | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/spacemeshos/go-spacemesh/sql/localsql" | ||
) | ||
|
||
func TestMalfeasanceSyncState(t *testing.T) { | ||
db := localsql.InMemory() | ||
timestamp, err := GetSyncState(db) | ||
require.NoError(t, err) | ||
require.Equal(t, time.Time{}, timestamp) | ||
ts := time.Now() | ||
for i := 0; i < 3; i++ { | ||
require.NoError(t, UpdateSyncState(db, ts)) | ||
timestamp, err = GetSyncState(db) | ||
require.NoError(t, err) | ||
require.Equal(t, ts.Truncate(time.Second), timestamp) | ||
ts = ts.Add(3 * time.Minute) | ||
} | ||
require.NoError(t, Clear(db)) | ||
timestamp, err = GetSyncState(db) | ||
require.NoError(t, err) | ||
require.Equal(t, time.Time{}, timestamp) | ||
} |
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,6 @@ | ||
CREATE TABLE malfeasance_sync_state | ||
( | ||
timestamp INT NOT NULL | ||
); | ||
|
||
INSERT INTO malfeasance_sync_state (timestamp) VALUES (0); |
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.