-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Blockchain updates for L2 extension #1430
Open
esuwu
wants to merge
60
commits into
master
Choose a base branch
from
node-updates-plugin-l2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 36 commits
Commits
Show all changes
60 commits
Select commit
Hold shift + click to select a range
63b159c
Initial draft
esuwu 69e9dbd
Changed a comment
esuwu b35bea1
moved a file
esuwu 27494ad
rename a folder
esuwu 983babc
Sent the updates channel into block appplier
esuwu 5272899
Added some design comments
esuwu 4fef6e3
Context in the updates function
esuwu dcd18f3
Merge branch 'master' into node-updates-plugin-l2
esuwu 3cc00d5
Added protobuf
esuwu 6752f8a
Moved proto, added conversion
esuwu bdc8854
Added state comparison
esuwu 19cc302
Added a draft for retrieving history entries
esuwu b6fb33c
Renamed a function
esuwu dca31ee
Fixed most of the linter issues
esuwu f524fe9
Merge branch 'master' into node-updates-plugin-l2
esuwu 18eab8c
Added paging
esuwu e2da71e
Added changes detector
esuwu da48829
Fixed a bug
esuwu b9318c2
Renamed functions
esuwu 1e91dac
Merged from main
esuwu 4dd8e5a
Merged from master
esuwu 59f75f3
Started writing the filter
esuwu 3a0ce55
Merged from master
esuwu 7b77774
Commented an unused function
esuwu 4d9419f
Added a basic filter
esuwu 6338578
Fixed filtering
esuwu 3d2bf74
Added tests
esuwu 9fe8ed9
Added tests and parameters for nats subscriber
esuwu e057d2a
Fixed tests and linter errors
esuwu b2a461c
Merged from master
esuwu 860b830
fixed gosec issues
esuwu ec15106
fixed one more gosec issue
esuwu 8f70624
Mod clean.
nickeskov c0652df
Merge branch 'master' into node-updates-plugin-l2
nickeskov 02f82ef
Fix lint for 'runNode' function.
nickeskov bb3bd64
Fix gosec issues.
nickeskov 47320cf
Deleted stale go protobuf generated code.
nickeskov fb56f6b
Make some types and functions package private.
nickeskov 89ffaf2
Update 'go.mod' go version.
nickeskov 28086dc
Change location of 'blockchaininfo_test.go'.
nickeskov 2f8c3c0
Add new method 'PartialBlockHeader' for 'ProtobufConverter'.
nickeskov 5f1010c
Refactor 'BUpdatesInfoFromProto' function with unused functions removal.
nickeskov 7dc0748
Refactored a bit 'L2ContractDataEntriesFromProto'.
nickeskov 42bf0ff
Merge branch 'master' into node-updates-plugin-l2
nickeskov 3f9bb83
Added a parse function
esuwu 5d37195
Merge branch 'master' into node-updates-plugin-l2
nickeskov cf23bdf
Set 'NoSigs' option for nats sever to true.
nickeskov d7dcd5e
Fix 'runPublisher' in case when updates channel is closed.
nickeskov d13b940
Add nats sever shutdown.
nickeskov 4dc94d5
Fix deadlock and refactoring.
nickeskov 214bd46
Merge branch 'master' into node-updates-plugin-l2
nickeskov 8fbe2b4
Fixed err not found handling
esuwu bb23dbc
Fixed a toolchain not found issue (#1545)
esuwu b65c6d9
Bump github.com/stretchr/testify from 1.9.0 to 1.10.0 (#1546)
dependabot[bot] 68db8b3
Handle blockchain import errors properly (#1548)
nickeskov d66423a
Bump github.com/elliotchance/orderedmap/v2 from 2.4.0 to 2.5.0 (#1549)
dependabot[bot] c598d67
Check on leasing state added. (#1550)
alexeykiselev bd889f5
Prepare timers for go1.23. (#1552)
nickeskov c24cb57
Peer address parsing refactoring (#1551)
nickeskov 6814098
Merge branch 'master' into node-updates-plugin-l2
esuwu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"flag" | ||
"log" | ||
"os" | ||
"os/signal" | ||
"strconv" | ||
"strings" | ||
"syscall" | ||
|
||
"go.uber.org/zap" | ||
|
||
"github.com/nats-io/nats.go" | ||
"github.com/pkg/errors" | ||
"github.com/wavesplatform/gowaves/pkg/blockchaininfo" | ||
g "github.com/wavesplatform/gowaves/pkg/grpc/l2/blockchain_info" | ||
"github.com/wavesplatform/gowaves/pkg/proto" | ||
) | ||
|
||
func printBlockInfo(blockInfoProto *g.BlockInfo) error { | ||
blockInfo, err := blockchaininfo.BUpdatesInfoFromProto(blockInfoProto) | ||
if err != nil { | ||
return err | ||
} | ||
blockInfoJSON, err := json.Marshal(blockInfo) | ||
if err != nil { | ||
return err | ||
} | ||
log.Println(string(blockInfoJSON)) | ||
return nil | ||
} | ||
|
||
func printContractInfo(contractInfoProto *g.L2ContractDataEntries, scheme proto.Scheme, path string) error { | ||
contractInfo, err := blockchaininfo.L2ContractDataEntriesFromProto(contractInfoProto, scheme) | ||
if err != nil { | ||
return err | ||
} | ||
prettyJSON, err := json.MarshalIndent(contractInfo, "", " ") | ||
if err != nil { | ||
log.Println("Error converting to pretty JSON:", err) | ||
return err | ||
} | ||
heightStr := strconv.FormatUint(contractInfoProto.Height, 10) | ||
// Write the pretty JSON to a file | ||
err = os.WriteFile(path+heightStr+".json", prettyJSON, 0600) | ||
if err != nil { | ||
log.Println("Error writing to file:", err) | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func receiveBlockUpdates(msg *nats.Msg) { | ||
blockUpdatesInfo := new(g.BlockInfo) | ||
unmrshlErr := blockUpdatesInfo.UnmarshalVT(msg.Data) | ||
if unmrshlErr != nil { | ||
log.Printf("failed to unmarshal block updates, %v", unmrshlErr) | ||
return | ||
} | ||
|
||
err := printBlockInfo(blockUpdatesInfo) | ||
if err != nil { | ||
return | ||
} | ||
log.Printf("Received on %s:\n", msg.Subject) | ||
} | ||
|
||
func receiveContractUpdates(msg *nats.Msg, contractMsg []byte, scheme proto.Scheme, path string) []byte { | ||
zap.S().Infof("Received on %s:\n", msg.Subject) | ||
|
||
switch msg.Data[0] { | ||
case blockchaininfo.NoPaging: | ||
contractMsg = msg.Data[1:] | ||
contractUpdatesInfo := new(g.L2ContractDataEntries) | ||
if err := contractUpdatesInfo.UnmarshalVT(contractMsg); err != nil { | ||
log.Printf("Failed to unmarshal contract updates: %v", err) | ||
return contractMsg | ||
} | ||
if err := printContractInfo(contractUpdatesInfo, scheme, path); err != nil { | ||
log.Printf("Failed to print contract info: %v", err) | ||
return contractMsg | ||
} | ||
contractMsg = nil | ||
|
||
case blockchaininfo.StartPaging: | ||
contractMsg = append(contractMsg, msg.Data[1:]...) | ||
|
||
case blockchaininfo.EndPaging: | ||
if contractMsg != nil { | ||
contractMsg = append(contractMsg, msg.Data[1:]...) | ||
contractUpdatesInfo := new(g.L2ContractDataEntries) | ||
if err := contractUpdatesInfo.UnmarshalVT(contractMsg); err != nil { | ||
log.Printf("Failed to unmarshal contract updates: %v", err) | ||
return contractMsg | ||
} | ||
|
||
go func() { | ||
if err := printContractInfo(contractUpdatesInfo, scheme, path); err != nil { | ||
log.Printf("Failed to print contract info updates: %v", err) | ||
} | ||
}() | ||
contractMsg = nil | ||
} | ||
} | ||
|
||
return contractMsg | ||
} | ||
|
||
func main() { | ||
var ( | ||
blockchainType string | ||
updatesPath string | ||
natsURL string | ||
) | ||
// Initialize the zap logger | ||
l, err := zap.NewProduction() | ||
if err != nil { | ||
log.Fatalf("failed to initialize zap logger: %v", err) | ||
} | ||
defer func(l *zap.Logger) { | ||
syncErr := l.Sync() | ||
if syncErr != nil { | ||
log.Fatalf("failed to sync zap logger %v", syncErr) | ||
} | ||
}(l) | ||
|
||
flag.StringVar(&blockchainType, "blockchain-type", "testnet", "Blockchain scheme (e.g., stagenet, testnet, mainnet)") | ||
flag.StringVar(&updatesPath, "updates-path", "", "File path to store contract updates") | ||
flag.StringVar(&natsURL, "nats-url", nats.DefaultURL, "URL for the NATS server") | ||
|
||
scheme, err := schemeFromString(blockchainType) | ||
if err != nil { | ||
zap.S().Fatalf("Failed to parse the blockchain type: %v", err) | ||
} | ||
ctx, done := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) | ||
defer done() | ||
// Connect to a NATS server | ||
nc, err := nats.Connect(natsURL) | ||
if err != nil { | ||
zap.S().Fatalf("Failed to connect to nats server: %v", err) | ||
return | ||
} | ||
defer nc.Close() | ||
|
||
_, err = nc.Subscribe(blockchaininfo.BlockUpdates, func(msg *nats.Msg) { | ||
receiveBlockUpdates(msg) | ||
}) | ||
if err != nil { | ||
zap.S().Fatalf("Failed to subscribe to block updates: %v", err) | ||
return | ||
} | ||
|
||
var contractMsg []byte | ||
_, err = nc.Subscribe(blockchaininfo.ContractUpdates, func(msg *nats.Msg) { | ||
contractMsg = receiveContractUpdates(msg, contractMsg, scheme, updatesPath) | ||
}) | ||
if err != nil { | ||
zap.S().Fatalf("Failed to subscribe to contract updates: %v", err) | ||
return | ||
} | ||
<-ctx.Done() | ||
zap.S().Info("NATS subscriber finished...") | ||
} | ||
|
||
func schemeFromString(networkType string) (proto.Scheme, error) { | ||
switch strings.ToLower(networkType) { | ||
case "mainnet": | ||
return proto.MainNetScheme, nil | ||
case "testnet": | ||
return proto.TestNetScheme, nil | ||
case "stagenet": | ||
return proto.StageNetScheme, nil | ||
default: | ||
return 0, errors.New("invalid blockchain type string") | ||
} | ||
} |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This parameter must be configurable.